Target description
This tutorial helps to:
- Use the X-NUCLEO-SRC1M1 shield that includes a TCPP02-M18 protection circuit and provides a USB Type-C® connector
- Create a USB-PD source application with the NUCLEO-G071RB board and the X-NUCLEO-SRC1M1 shield by using STM32CubeIDE software
Prerequisites
- Computer with Windows 7 (or higher)
- Computer with Windows 7 (or higher)
Hardware
- NUCLEO-G071RB (tested on rev C) [1]
- X-NUCLEO-SRC1M1 shield [2]
- A USB-PD sink device to test our USB-PD device (it can be the sink created in this wiki article, or a PD-capable 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
- UM2973 X-NUCLEO-SRC1M1 User Manual
How to build an USBPD Source application using the X-Cube-TCPP software pack
Create a USB-PD Source 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 under your project's name, and click Save.
When prompted for initializing peripherals with their default mode, click No.
3. Configuring the system
At this point, your project is created. The next steps focus on the configuration of 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
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 source 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 CMSIS_V1 interface. Under the Config Parameters tab, change "TOTAL_HEAP_SIZE" to 7000 bytes.
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 0x0001912C (corresponds to a simple 5V / 3A source)
The following table is extracted from USB Power Delivery Specification, Table 6-9 Fixed Supply PDO - Source. 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 | 0b | No |
B28 | USB Suspend Supported | 0b | No |
B27 | Unconstrained Power | 0b | No |
B26 | USB Communications Capable | 0b | No |
B25 | Dual-Role Data | 0b | No |
B24..22 | Reserved - Shall de set to zero | 000b | 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 |
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.
Since the X-NUCLEO-SRC1M1 BSP is used, the ADC configuration does not need to be done in CubeMX.
As the ADC HAL drivers are required for it to work properly, the ADC needs to be configured in CubeMX for it to include the driver files, but the actual configuration and init function are not called in this project.
In the Analog section, enable ADC1 peripheral channel 0. Leave the configuration as default, as the software pack reconfigures it.
3.7. Configure I2C peripheral
As the X-NUCLEO-SRC1M1 shield includes a TCPP02-M18 that communicates through I2C, the I2C peripheral needs to be enabled in this project.
In the Connectivity section, enable I2C2 peripheral in I2C mode. Leave the configuration as default, as the software pack reconfigures it.
Note: Enable the I2C2 peripheral in the CubeMX view for code generation to include the I2C drivers as done 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-SRC1M1' board support.
3.9. Configure Clocks
Under the Clock Configuration main tab, change system clock mux to PLLCLK. It sets 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 ST-LINK is the LPUART1.
In the Connectivity section, enable LPUART1 in asynchronous mode, and baudrate 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. In the Utilities section, enable GUI_INTERFACE, then enter free text to describe the board.
4. Configure the 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 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 USBPD 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 ADC and I2C initialization functions are not needed (handled by the BSP drivers), uncheck Generate Code for the MX_I2C_Init and MX_ADC1_Init functions.
5. Generate the 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.
STM32CubeIDE starts and imports the project.
This project contains the following folders:
- The USBPD folder contains the source files that are needed 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-SRC1M1 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
Place the jumpers on the X-NUCLEO-SRC1M1 shield as shown in the picture.
Next, plug an external 5V source into the green "source" connector.
With this configuration, the board is powered by the ST-Link of the Nucleo board.
To power your system from the external power supply connected to the "source" terminal, and not from the ST-Link, add the JP1 jumpers between 1-2 and 3-4.
7. Compile and run the application
The compilation must be performed without error 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. 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-SRC1M1 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 source (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).
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.
You can find other applicative examples on GitHub: x-cube-tcpp
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 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 allows building an USBPD 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 chapter 2: Create the project
- As in chapter 3.1: Clear the pinout
- As in chapter 3.2: Select the X-CUBE-TCPP software pack
Do not select the board support for X-NUCLEO-SRC1M1 as your application is based on a custom board.
- As in chapter 3.3: Configure UCPD Peripheral
- As in chapter 3.4: Configure FreeRTOS Middleware
- As in 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 its 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 resources to the application requirements.
- As in chapter 4: Configure the project
However, in the advanced settings, keep ADC and I2C initialization code generation.
- As in chapter 5: Generate the code
- As in chapter 7: Compile and run the application
11. References