dm00663511 How To Build A Simple Usbpd Sink Application With Stm32cubemx Stmicroelectronics
dm00663511 How To Build A Simple Usbpd Sink Application With Stm32cubemx Stmicroelectronics
Application note
Introduction
This application note is a guideline to build a very simple USB power delivery sink example, starting from STM32CubeMX. This
document applies to STM32 MCUs embedding the UCPD peripheral.
The principal hardware used in the different screenshots is based on the STM32G0 Series microcontroller with its associated
firmware included in the STM32CubeG0 MCU Package, but some notes are added to this document so that the STM32G4
Series microcontroller with its associated firmware in STM32CubeG4 can also be used. The associated firmware of the
STM32L5 Series microcontroller is in STM32CubeL5.
To ease connections with USB Type-C® signals, the STM32G0 and STM32G4 Nucleo-64 boards are equipped with the
X-NUCLEO-USBPDM1 shield. The STM32L5 Nucleo-144, Discovery, and Evaluation boards are USB-PD ready as they embed
the TCPP01-M12 chip.
1 General information
The simple USB-PD sink application runs on STM32G0 Series, STM32G4 Series, and STM32L5 Series 32-bit
microcontrollers based on the Arm® Cortex®-M processor.
Note: Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
2 Acronyms
Acronym Definition
USB Type-C® power delivery. A new peripheral in some STM32 series, like STM32G0 Series, STM32G4 Series
UCPD
or STM32L5 Series, which manage power delivery protocol communication with two lines.
3 Reference documents
4 Getting started
The very first goal is to configure the UCPD peripheral with USB-PD stack and to check that a first contract
is reached, which means the sink finds a matching power source. So any wall charger or any power delivery
certified source is also needed.
To reach this goal, the following steps are necessary:
1. Set up the UCPD peripheral to expose Rd resistors on the CC lines and detect the Rp from the source,
using STM32CubeMX.
2. Read the VBUS from the attached source. The initialization part is done by STM32CubeMX, but the
measurement start must be added manually in the application.
3. Finally, send a power delivery request message to the source and reach an explicit contract. This can only
be done manually, by editing the application files, after they are generated by STM32CubeMX.
Optional steps are described in this document, to help the user to debug:
4. Addition of a trace utility that uses the ST-LINK Virtual COM port to get some debug information from the
board
5. Addition of an embedded tool to communicate with STM32CubeMonitor-UCPD, a Java application (GUI) to
help to build the application
The sequence described in the following subsections based on the NUCLEO-G071RB STM32G0 Nucleo-64
board with the TCPP01 shield is adaptable to other configurations.
Create a new project File/New Project or click on ACCESS TO MCU SELECTOR, and check STM32G0 and
LQFP64 package, to filter available MCUs, double click on STM32G071RB.
Now the DMA for TX and RX paths must be added, and the interrupts enabled.
Our device requires DMA initialization for the PD communication through the UCPD peripheral, so the user must
configure a DMA channel for TX and RX. For example, set RX on DMA1 Channel 1, and TX on DMA1 Channel 2.
Two DMA handlers are enabled but they are not directly used by the firmware. All the UCPD treatments are done
through UCPD handlers.
Check that port 0 Sink PDO1 is set to 0x02019096, even if it is not important, because it is unused in the current
simple example. 0x02019096 means 5000 mV, 1500 mA, dual-role data, without Fast Role Swap. For further
information, see table 6-14 in document Universal Serial Bus Power Delivery Specification, Revision 3.0, Version
2.0, August 28, 2019, replicated below.
Figure 9. Specification detail (table 6-14 in Universal Serial Bus Power Delivery Specification)
For more details on the PDO definition, have a look at the POWER_IF section in the user manual Managing USB
power delivery systems with STM32 microcontrollers (UM2552).
In the Stack Port0 Parameters tab, the user selects what he wants to support, such as the PD3.0 specification
revision, among other parameters. These parameters are all power delivery settings. There is no need to change
them for a first application. For more information, have a look inside the Power Delivery specification (Universal
Serial Bus Power Delivery Specification, Revision 3.0, Version 2.0, August 28, 2019).
Looking at the X-NUCLEO-USBPDM1 shield schematics, VBUS is on the ST morpho connector pin 34,
corresponding to PB1
Figure 13. STM32G0 Nucleo-64 board (left) and TCPP01 shield (right) schematics
Then in the GPIO settings tab, add the User Label VSENSE for this signal:
Last edition for ADC: A user constant VDDA_APPLI with 3300 value is created, representing the ADC voltage
reference of 3.3 V. This variable is called by the generated code.
Note: On the STM32G4 Nucleo-64 board, the ADC IN15 signal must be used instead of IN9, because of ADC
mapping difference with STM32G0, and the rank sampling time can be set to 247.5 Cycles.
The mandatory settings for the simple USB-PD sink application are finished. The following part is highly
recommended for debugging.
Figure 20. LPUART1 activation and GPIO pin (TX and RX) update
Then the DMA requests are activated for the TX path only: DMA1 channel 3.
Back to the USB-PD middleware configuration, the trace evacuation is activated: check the tracer source for
TRACER_EMB.
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 Code Generator tab, STMicroelectronics recommends checking the Add necessary library files as
reference tab.
Then it is recommended to initialize Git to experiment with some code and be able to roll back to previous
versions, as in classic software development.
…
/* USER CODE BEGIN ADC1_Init 2 */
HAL_ADCEx_Calibration_Start(&hadc1);
HAL_ADC_Start(&hadc1);
/* USER CODE END ADC1_Init 2 */
…
Note: For STM32G4, ADC calibration API is different, the calibration line must be replaced by:
HAL_ADCEx_Calibration_Start(&hadc1, sConfig.SingleDiff);
Note: This simple example is not optimized from a power point of view, as the ADC is always running.
…
/* USER CODE BEGIN SysTick_IRQn 0 */
#if defined( _GUI_INTERFACE)
GUI_TimerCounter(); /* needed with GUI_Interface */
#endif
/* USER CODE END SysTick_IRQn 0 */
…
case USBPD_CORE_DATATYPE_SNK_PDO: /*!< Handling of port Sink PDO, requested by get sink
capa*/
USBPD_PWR_IF_GetPortPDOs(PortNum, DataId, Ptr, Size);
*Size *= 4;
break;
…
/* USER CODE BEGIN USBPD_DPM_SNK_EvaluateCapabilities */
USBPD_SNKRDO_TypeDef rdo;
/* Initialize RDO */
rdo.d32 = 0;
/* Prepare the requested pdo */
rdo.FixedVariableRDO.ObjectPosition = 1;
rdo.FixedVariableRDO.OperatingCurrentIn10mAunits = 50;
rdo.FixedVariableRDO.MaxOperatingCurrent10mAunits = 50;
rdo.FixedVariableRDO.CapabilityMismatch = 0;
*PtrPowerObjectType = USBPD_CORE_PDO_TYPE_FIXED;
*PtrRequestData = rdo.d32;
/* USER CODE END USBPD_DPM_SNK_EvaluateCapabilities */
…
Note: ADVICE keyword is used to indicate to the user that he may need to add his code to get a functional application.
…
/* USER CODE BEGIN include */
#include "main.h"
/* USER CODE END include */
…
/* USER CODE BEGIN BSP_USBPD_PWR_VBUSGetVoltage */
The calculation of the val variable depends on the voltage divider shown in Figure 12. On the X-NUCLEO-
USBPDM1 shield, Value is multiplied by 5.97 (Divider R6/R7 40.2 kΩ/200 kΩ) for VSENSE.
Fitted jumpers
Fitted jumpers
Verify that the two JP1 jumpers located on the right to select the STM32G0 and STM32G4 configuration are
inserted.
Then select the pins that are controlled by the MCU, using the left JP2 jumpers:
• Fault detection and hard reset are not managed in this demonstration.
• The power consumption is also not optimized. This is the reason why the TCPP01 VCC is set to the fixed
3.3 V, instead of taking an MCU GPIO, so the JP2 jumper VCC-3.3V [7-8] is ON
• In the first step demonstration, the dead battery from the TCPP01 is not used, so the JP2 jumper DB-3.3V
[11-12] is ON.
This concludes the settings.
Now is the time to check the first contract.
Compile the application, flash the board, start the STM32G0 program, keep the USB cable plugged, as the Virtual
COM port is mandatory, and launch the UCPD monitor.
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. Then it is possible to plug a power
delivery source cable into the USB Type-C® receptacle of the X-NUCLEO-USBPDM1 shield. The screen may look
like Figure 35:
Note: The SRC PDO part may look different. It depends on the capabilities of the power source.
Figure 35 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 (OUT orange message).
3. The ACCEPT and the PS_RDY are sent by the source (IN green 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.
• Add livewatch on CAD_HW_Handles. This variable can be used to check the Type-C attachment or
detachment.
See usbpd_cad_hw_if.c:
If the CC lines level changes are invisible, check that the TCPP is powered on and the active low _DB pin is
not set at 3.3 V. It may come from the JP2 jumper or some GPIO settings, like pull-up resistors.
Note: In the current STM32CubeMX for STM32G4, there is an issue with the default GPIO mode for CC2. In
usbpd_cad_hw_if.c there must be:
• For further debugging, the VBUS measured value can be printed in the trace, using the user button:
Add in src/main.c:
/**
* @brief EXTI line detection callbacks
* @param GPIO_Pin Specifies the pins connected EXTI line
AN5418 - Rev 3 * @retval None page 35/43
*/
AN5418
Next steps
7 Next steps
TCPP01 can be better controlled by driving its dead battery pin and its power.
If these pins need to be controlled by the application, the jumper positions must be adapted. The potential faults
must also be read by setting the JP2 jumper in [5-6] position.
Figure 42. TCPP01 shield jumpers position when application manages FLT, DB, and the TCPP01 VCC
The user application also needs to be done to react upon the TCPP01 fault detection (Over-temperature, over-
voltage).
8 Conclusion
Revision history
Contents
1 General information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Acronyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
3 Reference documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
5 STM32CubeMX step-by-step sequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.1 Mandatory parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.1.1 Start STM32CubeMX and select the MCU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
List of tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
List of figures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42
List of tables
Table 1. Acronym definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Table 2. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
List of figures
Figure 1. STM32G0 Nucleo-64 board equipped with X-NUCLEO-USBPDM1 shield . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Figure 2. Start STM32CubeMX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Figure 3. Select the STM32G0 MCU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Figure 4. UCPD peripheral basic configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 5. UCPD peripheral DMA configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Figure 6. UCPD peripheral IT activation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 7. FreeRTOS™ configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 8. USB-PD middleware configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 9. Specification detail (table 6-14 in Universal Serial Bus Power Delivery Specification) . . . . . . . . . . . . . . . . . . . 11
Figure 10. Detailed PDO decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Figure 11. Detailed stack configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Figure 12. TCPP01 shield voltage divider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Figure 13. STM32G0 Nucleo-64 board (left) and TCPP01 shield (right) schematics . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Figure 14. ADC configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Figure 15. ADC GPIO settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Figure 16. ADC parameters settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Figure 17. ADC user constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Figure 18. Clock configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Figure 19. STM32G0 Nucleo-64 board STLK connection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Figure 20. LPUART1 activation and GPIO pin (TX and RX) update. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Figure 21. DMA activation for LPUART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Figure 22. DMA activation for LPUART1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Figure 23. Activation of TRACER_EMB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Figure 24. Selection of USB-PD middleware TRACER_EMB source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Figure 25. Activation of GUI_INTERFACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Figure 26. Project manager settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Figure 27. Code generator settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Figure 28. Project advanced setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Figure 29. Generation warning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Figure 30. First compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Figure 31. TCPP01 shield picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Figure 32. TCPP01 jumper settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Figure 33. Board selection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Figure 34. Port selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Figure 35. Explicit contract visible in UCPD monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Figure 36. Example of CAD debug information visible in the trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Figure 37. cstate=1: detached . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Figure 38. cstate=3: attached . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Figure 39. User button on STM32G0 Nucleo-64 board schematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Figure 40. Add the user button in STM32CubeMX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Figure 41. Modify TCPP01 controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Figure 42. TCPP01 shield jumpers position when application manages FLT, DB, and the TCPP01 VCC . . . . . . . . . . . . . 36