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]
- 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.)
- 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
Literature
- UM2324 NUCLEO-G071RB User Manual
- UM2891 X-NUCLEO-DRP1M1 User Manual
How to build an USBPD Dual-Role application using the X-CUBE-TCPP software pack
Create a USB PD dual-role device
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
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
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.
3.2. Select the X-CUBE-TCPP software pack
Click on 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.
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.
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.
3.5. Configure USB PD 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 Sink tab, verify the following configuration:
- Number of sink PDOs for port 0: 1
- Port 0 sink PDO 1 5V
- Number of source PDOs for port 0: 1
- Port 0 source PDO 1 5V 100mA
In the stack port 0 parameters section, enable the CAD role toggle: CAD role toggle: Supported
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 reconfigures it.
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 reconfigures it.
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 sets the HCLK clock to 64 MHz.
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 STLINK is the LPUART1.
In the connectivity section, enable LPUART1 in asynchronous mode, and baud rate 921600 bauds. Leave the rest as default.
In the pinout view, left-click PA2 and PA3 to remap them to LPUART1_TX and LPUART1_RX.
Under the DMA configuration tab, add a request for LPUART1_TX. Use DMA1 channel 3.
Finally, under the NVIC settings tab, enable LPUART1 global interrupts.
3.10.2. Configure embedded tracer
In the utilities section, select TRACER_EMB and use LPUART1 as the trace source.
Then, go back to the USBPD middleware configuration and check the tracer source checkbox.
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[7]. In the utilities section, enable GUI_INTERFACE, then enter free text to describe the board.
4. Configure project
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.
For the STM32G0 or G4 MCU, uncheck “use default firmware location” and instead, select the software pack “c:|\user\ … \STM32Cube\Repositoryctronics/Packs\STMicroelectronics\X-CUBE-TCPP\V4.1.0\” as firmware location to be sure to use the latest USB PD lib releases as the standard evolution is very fast.
Under the advanced settings tab, change the LPUART driver to LL to save a bit of memory heap size. As we do not need the 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
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.
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 button (or select project/build project).
Run the application by clicking on the button (or select run/run).
8. Establish the first explicit contract
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").
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".
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:
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:
- The capabilities are sent by the STM32G0 drp (OUT orange message).
- The request is sent by the sink (IN green message).
- The ACCEPT and the PS_RDY are sent by the STM32G0 source (OUT orange message).
- The contract negotiation ends by the POWER_EXPLICIT_CONTRACT notification (blue message).
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:
- The capabilities are sent by the source (IN green message).
- The request is sent by the STM32G0 drp (OUT orange message).
- The ACCEPT and the PS_RDY are sent by the source (IN green message).
- 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.
9. Information focus: Code inserted by the software pack
By enabling the software pack in section 3.8, the code below has been added automatically in the following files:
Expandusbpd_dpm_user_h |
Expandusbpd_dpm_user_c |
Expandusbpd_pdo_defs.h |
Expandusbpd_pwr_if.c |
10. Project with a custom board
This chapter describes how to build a USB PD source application using a custom board with an STM32 MCU from series G0, G4, H5, L5, or U5 that includes the UCPD peripheral.
- 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 the UCPD peripheral.
- As in the chapter 3.4, configure the FreeRTOS™ middleware.
- As in the chapter 3.5, configure the USB PD middleware.
- Configure the 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 the 12-bits resolution. Enable the continuous conversion mode. Set a medium cycle sampling time.
- Configure the I2C peripheral.
Enable the I2C connected to the TCPP02 I2C bus. Set its speed in fast mode.
- Configure the 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 resources to the application requirements.
- As in the chapter 4, configure the project.
But in the advanced settings keep ADC and I2C initialization code generation.
- As in the chapter 5, generate the code.
- As in the chapter 7, compile and run the application.
11. References