Getting started with USB-Power Delivery Dual Role

Revision as of 07:54, 6 May 2024 by Registered User (1st Draft DRP with software pack)

Target description

This tutorial aims to help you to:

  • use the X-NUCLEO-DRP1M1 shield that includes a TCPP03-M20 protection circuit and provides a USB Type-C® connector.
  • create a USB-PD Dual Role application with the NUCLEO-G071RB board and the X-NUCLEO-DRP1M1 shield by using STM32CubeIDE software

Prerequisites

  • Computer with Windows 7 (or higher)

Hardware

  • NUCLEO-G071RB (tested on rev C) [1]
  • X-NUCLEO-DRP1M1 shield [2]
  • A USB-PD sink device to test our USB Source device (it can be the sink created in this wiki article or a USB Type-C® mobile phone or device)
  • A USB-PD source device to test our USB Source device (it can be the sink created in this wiki article or a USB Type-C® mobile phone or device)
  • USB cable Type-A to Micro-B
  • USB Type-C® to Type-C® cable

Software

  • STM32CubeMX (tested with V6.11.0 - minimal release 6.11.0) [3]
  • STM32CubeIDE (tested with V1.14.0) [4]
  • STM32CubeMonitor-UCPD (tested with V1.3.0) [5]
  • X-CUBE-TCPP MCU Firmware Package (BSP) [6]

Literature

  • UM2324 NUCLEO-G071RB User Manual
  • UM2891 X-NUCLEO-DRP1M1 User Manual

Create a USB-PD Dual-Role Device

Clock.png Total 60min

1. Software pack installation

Open STM32CubeMX, in the software pack area, click on the install/remove button


Then select the STMicroelectronics tab, scroll down to the X-Cube-TCPP software pack, and click on the install button if it is not already installed.


2. Creating the project

Clock.png 5min

IN STM32CubeMX, create a New STM32 Project. As a target selection, choose the NUCLEO-G071RB from the Board Selector Tab


Click "Start Project", then in the file menu, Create a new folder at your project's name, and click "Save".


3. Configuring the system

Clock.png 15min

At this point, your project is created and in the next steps, we will configure the peripherals and options needed for the project.

3.1. Clear the pinout

To start from a blank configuration, click on the Pinout menu and select Clear Pinouts. This resets the pinouts in the pinout view.

USBPD 0-pinoutConf.png


3.2. Select the X-Cube-TCPP software pack

From the software pack menu,


Select the X-Cube-TCPP Software pack and enable its Source application, the tcpp0203 Board part and the X-NUCLEO-SRC1M1 Board support.



3.3. Configure UCPD peripheral

In the Connectivity tab, select the UCPD1 peripheral and enable it in Dual-Role mode. Under the NVIC Settings tab, enable UCPD global interrupts.

USBPD DRP 0-USBPDConf.png


Under the DMA Settings tab, add UCPD1_RX and UCPD1_TX DMA requests. Select DMA1 channel 4 for RX and DMA1 channel 2 for TX.

USBPD 1-UCPD1Conf.png


Info white.png Information
You can use any DMA channel you want except for DMA1_Channel1 which would be used later by the BSP drivers.

3.4. Configure FreeRTOS Middleware

In the Middleware section, enable FreeRTOS with the CMSIS_V1 interface. Under the Config Parameters tab, change "TOTAL_HEAP_SIZE" to 7000 bytes.

USBPD 0-FreeRTOSConf.png



Info white.png Information

If an STM32G4 is used of a G0, LIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY needs to be set to 3 instead of CubeMX's default value 5. In some cases, with STM32G4, leaving it to 5 will get the code execution stuck in the vPortValidateInterruptPriority function.

3.5. Configure USBPD Middleware

In the Middleware section, enable USBPD with the following configuration:

  • Port Configuration: Port 0: UCPD1
  • Stack Configuration: PD3 Full Stack
  • Timer service Source: TIM1

Under the PDO General Definitions tab, verify the following configuration:

  • Number of Sink PDOs for port 0: 1
  • Port 0 Sink PDO 1 0x2201912C (correspond to a simple 5V / 1.5A source)
  • Number of Sink PDOs for port 0: 1
  • Port 0 Source PDO 1 0x26019096 (correspond to a simple 5V / 3A source)
USBPD DRP 1-USBPDConf.png


The following table is extracted from USB Power Delivery Specification, Table 6-9 Fixed Supply PDO - Source [7]. Used values and associated decoding for this project have been added to the table.

Bit(s) Description Used value Decoding
B31..30 Fixed supply 00b Fixed
B29 Dual-Role Power 1b Yes
B28 USB Suspend Supported 0b No
B27 Unconstrained Power 0b No
B26 USB Communications Capable 0b No
B25 Dual-Role Data 1b Yes
B24 Unchunked Extended Messages Supported 0b No
B23 EPR Mode Capable 0b No
B22 Reserved - Shall de set to zero 0b No
B21..20 Peak Current 00b Peak Equals
B19..10 Voltage in 50mV units 0001100100b 5V
B9..0 Maximum current in 10mA units 0100101100b 3A

The following table is extracted from USB Power Delivery Specification, Table 6-16 Fixed Supply PDO - Sink [8]. Used values and associated decoding for this project have been added to the table.

Bit(s) Description Used value Decoding
B31..30 Fixed supply 00b Fixed
B29 Dual-Role Power 1b Yes
B28 Higher capability 0b No
B27 Unconstrained Power 0b No
B26 USB Communications Capable 0b No
B25 Dual-Role Data 1b Yes
B24..23 Fast Role Swap required USB Type-C Current 00b

00b = Fast Swap not supported (default)
01b = Default USB Power
10b = 1.5A @ 5V
11b = 3.0A @ 5V

B22..20 Reserved - Shall be set to zero 0b No
B19..10 Voltage in 50mV units 0001100100b 5V
B9..0 Maximum current in 10mA units 0010010110b 1.5A


In the Stack Port 0 parameters section, enable the CAD role toggle: CAD role toggle: Supported

11 USBPD DRP 3-USBPDConf.png


3.6. Configure ADC peripheral

For the Power Delivery stack to work, VBUS needs to be monitored. To do it, an ADC needs to be configured to measure the VBUS voltage and current.
As we are going to use the X-NUCLEO-DRP1M1 BSP, the ADC configuration does not need to be done in CubeMX.
As we need the ADC HAL drivers for it to work properly, we still need to configure the ADC in CubeMX for it to include the driver files, but the actual configuration and init function will not be called in our project.

In the Analog section, enable ADC1 peripheral channel 0. Leave the configuration as default, as the X-NUCLEO-DRP1M1 BSP will reconfigure it.

USBPD 0-ADC1Conf.png


3.7. Configure I2C peripheral

As the X-NUCLEO-DRP1M1 shield includes a TCPP03-M20 that communicates via I2C, we need to enable the I2C peripheral in our project.

In the Connectivity section, enable I2C1 peripheral, in I2C mode. Leave the configuration as default, as the X-NUCLEO-DRP1M1 BSP will reconfigure it.

USBPD DRP 0-I2C1Conf1.png

Note: We need to enable the I2C1 peripheral in the CubeMX view for code generation to include the I2C drivers as we do for the ADC.

3.8. Enable the software pack

IN the middleware and software pack category, select the X-Cube-TCPP software pack. Enable the 'Source' Application, the 'tcpp0203' Board Part and the 'X-NUCLEO-DRP1M1' Board support.


3.9. Configure Clocks

Under the Clock Configuration main tab, change the system clock mux to PLLCLK. It will set the HCLK clock to 64 MHz.

USBPD 0-Clock.png


Info white.png Information
The mandatory settings for the simple USB-PD sink application are finished.

The following part is highly recommended for debugging

3.10. [OPTIONAL] Configure Tracer for debug

3.10.1. Configure LPUART

On the STM32G0 Nucleo-64 board, the Virtual COM port connected to the ST-LINK is the LPUART1.

Warning white.png Warning
The default STM32CubeMX pins used by LPUART1 must be changed to match the STM32G0 Nucleo-64 hardware:
  • PA2 for TX
  • PA3 for RX.

In the Connectivity section, enable LPUART1 in Asynchronous mode, and baudrate 921600 bauds. Leave the rest as default.

USBPD 0-LPUARTConf.png


In the pinout view, left-click PA2 and PA3 to remap them to LPUART1_TX and LPUART1_RX.

16 USBPD DRP 0-LPUARTConf2.png

Under the DMA Configuration tab, add a request for LPUART1_TX. Use DMA1 channel 3.

USBPD 1-LPUARTConf.png


Finally, under the NVIC Settings tab, enable LPUART1 global interrupts.

USBPD 2-LPUARTConf.png


3.10.2. Configure embedded tracer

In the Utilities section, select TRACER_EMB and use LPUART1 as the trace source.

USBPD 0-tracerConf.png


Then, go back to the USBPD middleware configuration and check the Tracer Source checkbox.

USBPD 1-tracerConf.png


3.10.3. Configure UCPD monitor firmware responder for debug

The firmware interactive stack responder can be activated if interaction with the USB-PD stack is needed, using the UCPD monitor tool STM32CubeMonUCPD[9]. In the Utilities section, enable GUI_INTERFACE, then enter free text to describe the board.

USBPD DRP 0-GUIConf.png


4. Configure project

Clock.png 5min

Under the Project Manager main tab, configure the minimum stack size to 0xC00 under the Project tab. This is a first value, which can be tuned later, depending on application needs.


Under the Advanced Settings tab, change the LPUART driver to LL to save a bit of memory heap size. As we do not need ADC and I2C initialization functions (handled by the BSP drivers), uncheck Generate Code for the MX_I2C2_Init and MX_ADC1_Init functions.



5. Generate code

Clock.png 5min

Save your file with Ctrl+s and select generate code.


A warning appears, informing that a proper HAL timebase is not defined. It is safer to use a dedicated timer as a HAL timebase source.
For this demonstration, the below warning can be ignored by clicking Yes.

USBPD 1-projGen.png


Info white.png Information
This becomes the recommended standard way of working in the forthcoming firmware package deliveries, especially when using CMSIS OS V2, which defines Systick as FreeRTOS™ timebase.

For this demonstration, the warning can be ignored by clicking Yes.


In this project, different folders can be found:

  • The USBPD folder contains the source files that we need to edit to enrich the Power Delivery application.
  • The Core folder contains the source files for the core of the project.
  • The Drivers folder contains the HAL drivers for the STM32, and the BSP for the Nucleo board and X-NUCLEO-DRP1M1 shield.
  • The Middleware folder contains the source files and the libraries for FreeRTOS™ and USB-PD.
  • The Utilities folder contains the GUI (UCPD monitor) and tracer embedded source files part.

The Drivers folder in the Explorer view of the project must contain the BSP folders added earlier.



6. Configure the shield's jumpers

No jumper is needed on the X-NUCLEO-DRP1M1 shield.

7. Compile and run the application

The compilation must be performed without errors or warnings.
Build the application by clicking on the Built Button.png button (or select Project/Build Project).
Run the application by clicking on the DownloadRun Button.png button (or select Run/Run)

8. Establish the first explicit contract

Clock.png 5min

With your application running on the board, launch the STM32CubeMonitor-UCPD application. The user's board must appear in the list when clicking "Refresh list of connected boards", so double-click on the corresponding line (or click "NEXT").

USBPD DRP 0-cubeMon.png


Note: The ComPort may be different. It depends on the number of boards installed on the computer. Then double-click on the desired UCPD port, here Port 0, or select it and click "NEXT".

USBPD DRP 1-cubeMon.png


Click on the TRACES button in the bottom right corner to get protocol traces. You can then plug a power delivery sink into the USB Type-C® receptacle of the X-NUCLEO-DRP1M1 shield. The screen may look like this:

USBPD DRP 2-cubeMon.png


The figure above shows the communication between the STM32G0 and the power delivery sink on the right panel. It is possible to verify the correct sequence to reach an explicit contract:

  1. The capabilities are sent by the STM32G0 drp (OUT orange message).
  2. The request is sent by the sink (IN green message).
  3. The ACCEPT and the PS_RDY are sent by the STM32G0 source (OUT orange message).
  4. The contract negotiation ends by the POWER_EXPLICIT_CONTRACT notification (blue message).
USBPD DRP 3-cubeMon.png


The figure above shows the communication between the STM32G0 and the power delivery source on the right panel. It is possible to verify the correct sequence to reach an explicit contract:

  1. The capabilities are sent by the source (IN green message).
  2. The request is sent by the STM32G0 drp (OUT orange message).
  3. The ACCEPT and the PS_RDY are sent by the source (IN green message).
  4. The contract negotiation ends by the POWER_EXPLICIT_CONTRACT notification (blue message).

For more details on how to use this tool, refer to UM2468. And for more details on the protocol, refer to UM2552. Note that this trace is very helpful for debugging and application development.

You can also use the Measurement window in STM32CubeMonitor-UCPD to display a graph of the measured VBUS voltage and delivered current. Set the sampling period and click start.

USBPD 3-cubeMon.png




Info white.png Information
You can find other applicative examples on GitHub: x-cube-tcpp[6]

9. For information, code inserted by the software pack

Following code has been automatically inserted by the software pack in:


10. Project with a custom board

This chapter allows to build an USBPD Source application using a custom board with a STM32 MCU from series G0, G4, H5, L5 or U5 that includes the UCPD peripheral.

Info white.png Information
Selected resources: ADC1-IN0, I2C1, PC5 and PC8, are for the example, replace by the custom board resources affectation.
  • As in the chapter 2, create the project
  • As in the chapter 3.1, clear the pinout
  • As per chapter 3.2, Select the X-Cube-TCPP software pack

But do not select the board support for X-NUCLEO-SRC1M1 as your application is based on a custom board


  • As in the chapter 3.3, Configure UCPD Peripheral
  • As in the chapter 3.4, Configure FreeRTOS Middleware
  • As in the chapter 3.5, Configure USBPD Middleware
  • Configure ADC Peripheral

Select and configure the ADC and its channel on which Vbus is connected for monitoring Select the ADC and it Channel, Adjust the clock prescaler, Keep 12 Bits resolution, Enable the continuous conversion mode, And set a medium cycle sampling time


  • Configure I2C Peripheral

Enable the I2C connected to the TCPP02 I2C Bus Set its speed in fast Mode


  • Configure GPIO

Enable and configure the External Interrupt input where the TCPP02 FLG signal is connected Select it in the pinout view In the GPIO category, set it as External Interrupt Mode with Falling edge trigger detection Enable its Pull-up


In the Pinout view, select the GPIO Output for TCPP02 Enable


  • Enable the software Pack

In the middleware category, select the X-Cube-TCPP Software pack and enable its application and Board Part


  • Assign ressources to the application requirements


  • As in the chapter 4,Configure Project

But in the Advanced settings keep ADC and I2C initialization code generation.

  • As in the chapter 5, Generate code
  • As in the chapter 7, Compile and run the application

11. References