0% found this document useful (0 votes)
153 views43 pages

dm00663511 How To Build A Simple Usbpd Sink Application With Stm32cubemx Stmicroelectronics

Uploaded by

Big Boy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views43 pages

dm00663511 How To Build A Simple Usbpd Sink Application With Stm32cubemx Stmicroelectronics

Uploaded by

Big Boy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

AN5418

Application note

How to build a simple USB-PD sink application with STM32CubeMX

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.

Figure 1. STM32G0 Nucleo-64 board equipped with X-NUCLEO-USBPDM1 shield

AN5418 - Rev 3 - November 2020 www.st.com


For further information contact your local STMicroelectronics sales office.
AN5418
General information

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.

AN5418 - Rev 3 page 2/43


AN5418
Acronyms

2 Acronyms

Table 1. Acronym definitions

Acronym Definition

AMS Atomic message sequence


APDO Augmented power delivery object. Specific PDO to handle PPS
BSP Board support package
CAD Cable detection module responsible for attaching or detaching detection
CC Communication channel
DPM Device policy manager. In the power delivery context, this part corresponds to the user application.
DRP Dual role power. The ability for a product to either source or sink power.
GUI Graphical user interface. It applies here to STM32CubeMonitor-UCPD (STM32CubeMonUCPD).
HAL Hardware abstraction layer
HW Hardware
LL Low layer
OVP Over-voltage protection
PDO Power delivery object
PPS Programmable power supply
PE Policy engine
SNK Power sink. Ability to request power
SRC Power source. Ability to provide power

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.

AN5418 - Rev 3 page 3/43


AN5418
Reference documents

3 Reference documents

STMicroelectronics ecosystem material:


• STM32CubeMX: STM32Cube initialization code generator
• User manual Managing USB power delivery systems with STM32 microcontrollers (UM2552)
• User manual STM32CubeMonitor-UCPD software tool for USB Type-C™ Power Delivery port management
(UM2468)
• Datasheet USB-C overvoltage protection for VBUS and CC lines (DS12900)
• Application note USB Type-C protection and filtering (AN4871)
• Databrief STM32CubeMonitor-UCPD software tool for USB Type-C™ Power Delivery port management
(DB3747)
• Technical article Overview of USB Type-C and Power Delivery technologies (TA0357)
• Application note USB Type-C Power Delivery using STM32 MCUs and MPUs (AN5225)
• Youtube video STM32G0: Create a USB Power delivery sink application in less than 10 minutes
• X-NUCLEO-USBPDM1 user manual (UM2668)
• USBPD overview wiki
USB specification documents:
• USB2.0 Universal Serial Bus Revision 2.0 Specification
• USB3.1 Universal Serial Bus Revision 3.2 Specification
• USB BC Battery Charging Specification Revision 1.2
• Universal Serial Bus Type-C Cable and Connector Specification 2.0, August 2019.
• Universal Serial Bus Power Delivery Specification, Revision 3.0, Version 2.0, August 28, 2019

AN5418 - Rev 3 page 4/43


AN5418
Getting started

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

AN5418 - Rev 3 page 5/43


AN5418
STM32CubeMX step-by-step sequence

5 STM32CubeMX step-by-step sequence

5.1 Mandatory parts


The below steps can be done because of the presence of the UCPD peripheral in some STM32 devices, such as
STM32G071xx and STM32G431xx. This peripheral manages power delivery communication over the CC lines.
The X-NUCLEO-USBPDM1 shield, embedding the TCPP01-M12 single-chip solution, adds the microcontroller
protection on the CC lines, such as over-voltage and over-temperature, and it also gives access to a USB
Type-C® receptacle.
TCPP01-M12 is already embedded in the STM32L5 Nucleo-144, Discovery, and Evaluation boards.
Before starting, check first that both latest versions of STM32CubeMX and STM32CubeG0, STM32CubeG4, or
STM32CubeL5 MCU Packages are used.

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.

5.1.1 Start STM32CubeMX and select the MCU

Figure 2. Start STM32CubeMX

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.

AN5418 - Rev 3 page 6/43


AN5418
Mandatory parts

Figure 3. Select the STM32G0 MCU

5.1.2 UCPD peripheral configuration


Now it is possible to activate the UCPD peripheral inside the microcontroller.
This peripheral manages the power delivery detection and its communication over the CC lines. For more details,
refer to section 35 USB Type-C™ / USB Power Delivery interface (UCPD) of STM32G071 reference manual
(RM0444).
Click on Connectivity, and select UCPD1. STM32G0 has two instances of the UCPD block. The TCPP01 shield is
wired to UCPD instance 1. The use of instance 2 is possible, not plugging the shield onto the Nucleo board, but
with flying wires.
This demonstration runs a sink application. So for UCPD mode, the user selects Sink. Untick the Dead
Battery Signals to avoid using the internal dead-battery management of the UCPD peripheral, because, in the
demonstration, the ST-LINK is powering the kit (Nucleo and shield) and the dead-battery management of the
TCPP01 is bypassed, as DB-3.3V jumper is ON, to start with an easy application.
Note: The shield jumper configuration for a dead battery is detailed in Section 5.7

AN5418 - Rev 3 page 7/43


AN5418
Mandatory parts

Figure 4. UCPD peripheral basic configuration

Now the DMA for TX and RX paths must be added, and the interrupts enabled.

Figure 5. UCPD peripheral DMA configuration

AN5418 - Rev 3 page 8/43


AN5418
Mandatory parts

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.

Figure 6. UCPD peripheral IT activation

Two DMA handlers are enabled but they are not directly used by the firmware. All the UCPD treatments are done
through UCPD handlers.

5.1.3 FreeRTOS™ configuration


In the middleware category, enable FreeRTOS™.
Select CMSIS_V1 and set TOTAL_HEAP_SIZE to 5000. From STM32L5 and further firmware package deliveries,
CMSIS_V2 must be selected instead of CMSIS_V1.
Heap size here is for a start. It must be tuned later when optimizing the final application.

Figure 7. FreeRTOS™ configuration

AN5418 - Rev 3 page 9/43


AN5418
Mandatory parts

5.1.4 USB-PD middleware configuration


In the middleware category, select USB-PD, then Port 0: UCPD1 in the drop-down list inside USBPD Mode
and Configuration. For more details on the USB-PD stack role in the overall firmware, refer to the user manual
Managing USB power delivery systems with STM32 microcontrollers (UM2552).

Figure 8. USB-PD middleware configuration

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.

AN5418 - Rev 3 page 10/43


AN5418
Mandatory parts

Figure 9. Specification detail (table 6-14 in Universal Serial Bus Power Delivery Specification)

Figure 10 explains the applied value 0x02019096.

Figure 10. Detailed PDO decoding

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).

AN5418 - Rev 3 page 11/43


AN5418
Mandatory parts

Figure 11. Detailed stack configuration

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).

AN5418 - Rev 3 page 12/43


AN5418
Mandatory parts

5.1.5 ADC configuration for VBUS reading


VBUS detection is mandatory to respect the Type-C state machines. For this, we use an ADC connected to a
voltage divider bridge, to remain in the GPIO STM32 voltage range.

Figure 12. TCPP01 shield voltage divider

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

In the analog category, select ADC1.


Clicking on the right side in STM32CubeMX, PB1 is connected to the ADC1_IN9 alternate function input. So
select it in the mode part of the STM32CubeMX window, or select the ADC1_IN9 in the pinout view.

AN5418 - Rev 3 page 13/43


AN5418
Mandatory parts

Figure 14. ADC configuration

Then in the GPIO settings tab, add the User Label VSENSE for this signal:

Figure 15. ADC GPIO settings

AN5418 - Rev 3 page 14/43


AN5418
Mandatory parts

Select simple and basic settings for the ADC1:


• Clock Prescaler: Synchronous mode divided by 4
• Continuous conversion mode: Enabled
• Overrun behavior: Overrun data overwritten
• Sampling time: 160.5 Cycles
The sampling time must be adjusted with the impedance linked to the measure. In the case of the X-NUCLEO-
USBPDM1 TCPP01 shield, there are higher than 10 KΩ resistors, therefore a high number of cycles is preferred.

Figure 16. ADC parameters settings

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.

Figure 17. ADC user constant

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.

AN5418 - Rev 3 page 15/43


AN5418
Mandatory parts

5.1.6 Clock check


PLLCLK can be chosen as an input clock of the system clock multiplexor, to produce SYSCLK and HCLK set
to 16 MHz minimum. There is no limit for maximum. It can be 170 MHz for STM32G4. HSI is used to clock the
UCPD peripheral, so it must be enabled.

Figure 18. Clock configuration

The mandatory settings for the simple USB-PD sink application are finished. The following part is highly
recommended for debugging.

AN5418 - Rev 3 page 16/43


AN5418
Additional recommended optional debugging

5.2 Additional recommended optional debugging

5.2.1 UART configuration for debug


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

Figure 19. STM32G0 Nucleo-64 board STLK connection

This interface is activated in STM32CubeMX, in asynchronous mode.


Important:
The default STM32CubeMX pins used by LPUART1 must be changed to match the STM32G0 Nucleo-64 hardware: PA2 for
TX, and PA3 for RX.

Figure 20. LPUART1 activation and GPIO pin (TX and RX) update

AN5418 - Rev 3 page 17/43


AN5418
Additional recommended optional debugging

Then the DMA requests are activated for the TX path only: DMA1 channel 3.

Figure 21. DMA activation for LPUART

And the interrupt is activated:

Figure 22. DMA activation for LPUART1

Note: If the STM32G4 Nucleo-64 board is used, USART1 must be used.


Note: The default UART configuration is used. The debug trace runs at 921600 bauds.

AN5418 - Rev 3 page 18/43


AN5418
Additional recommended optional debugging

5.2.2 Activation of embedded tracer for debug


This is done in the utility category: TRACER_EMB is selected followed by the LPUART1 in the UART trace source
mode.

Figure 23. Activation of TRACER_EMB

AN5418 - Rev 3 page 19/43


AN5418
Additional recommended optional debugging

Back to the USB-PD middleware configuration, the trace evacuation is activated: check the tracer source for
TRACER_EMB.

Figure 24. Selection of USB-PD middleware TRACER_EMB source

The firmware interactive stack responder can be activated if interaction with the USB-PD stack is needed, using
the UCPD monitor tool STM32CubeMonUCPD.

AN5418 - Rev 3 page 20/43


AN5418
Additional recommended optional debugging

5.2.3 Activation of UCPD monitor firmware responder


The monitor can simply be activated in the utility category: GUI_INTERFACE. Then enter free text to describe the
board.

Figure 25. Activation of GUI_INTERFACE

AN5418 - Rev 3 page 21/43


AN5418
Update and save project configuration

5.3 Update and save project configuration


Once the configuration is finished, few parameters must be saved in the project manager tab before saving the
project.
Under the project manager tab, select a name for the project. For the project directory, avoid using One drive, if
STM32CubeMX is not in One drive too.
Configure the minimum stack size to 0xC00. This is the first version, which can be tuned later, depending on the
application needs.

Figure 26. Project manager settings

In the Code Generator tab, STMicroelectronics recommends checking the Add necessary library files as
reference tab.

AN5418 - Rev 3 page 22/43


AN5418
Update and save project configuration

Figure 27. Code generator settings

Click on Advanced Settings


LPUART is selected as LL to save a bit of memory heap size.

Figure 28. Project advanced setting

Work must be saved: menu file / save

AN5418 - Rev 3 page 23/43


AN5418
Code generation

5.4 Code generation


Click on 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.
Note: 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 below warning can be ignored by clicking Yes.

Figure 29. Generation warning

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.

AN5418 - Rev 3 page 24/43


AN5418
Compilation of generated application

5.5 Compilation of generated application


The compilation must be performed without error or warning.

Figure 30. First compilation

In this project, different folders can be found:


• The Application/User folder contains the source files that we need to edit to enrich the application.
• The Drivers folder contains the HAL drivers for the STM32.
• 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 Output folder contains the compilation result files.

5.6 Complete USB-PD application


Now that the peripherals are initialized by STM32CubeMX, some minimum level of the application needs to be
added:
• ADC needs to be calibrated, and conversion needs to start.
• Fill the handlers for the interrupts to wake up the UCPD peripheral.
• Fill BSP_USBPD_PWR_VBUSGetVoltage function with the right coefficient depending on the VBUS divider
bridge.
• Complete USBPD_DPM_SNK_EvaluateCapabilities to answer one source capability message.
• TCPP01 dead battery pin needs to be disabled, GPIO driven HIGH, to see the source Rp, or the jumper has
to be set on the shield.

AN5418 - Rev 3 page 25/43


AN5418
Complete USB-PD application

5.6.1 Modification in main.c


In this file, the ADC must start after its calibration, using HAL. The ADC is needed to read VBUS.
Code to be added between USER CODE ADC1_Init 2 tags:


/* 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.

5.6.2 Modification in stm32g0xx_it.c


• If GUI_INTERFACE is enabled, GUI_TimerCounter must be added in Systick handler to manage the GUI
timer.


/* USER CODE BEGIN SysTick_IRQn 0 */
#if defined( _GUI_INTERFACE)
GUI_TimerCounter(); /* needed with GUI_Interface */
#endif
/* USER CODE END SysTick_IRQn 0 */

AN5418 - Rev 3 page 26/43


AN5418
Complete USB-PD application

5.6.3 Modification in usbpd_dpm_user.c


To avoid a hard fault if the distant device asks for sink capabilities, some code must be added inside the
USBPD_DPM_GetDataInfofunction function.
In the case, before the default add :

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;

The USBPD_DPM_SNK_EvaluateCapabilities function needs to be added to establish the first contract. It is a


very basic example that requests the first default 5V PDO. This must be modified to match with real SINK PDOs,
which are not yet managed by STM32CubeMX.
In the user code for USBPD_DPM_SNK_EvaluateCapabilities replace

DPM_USER_DEBUG_TRACE(PortNum, "ADVICE: update USBPD_DPM_SNK_EvaluateCapabilities");

with the following text:


/* 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.

AN5418 - Rev 3 page 27/43


AN5418
Complete USB-PD application

5.6.4 Modification in usbpd_pwr_user.c


It is important to add this part to correctly read VBUS provided by the ADC. The stack needs to know the VBUS
level all along the cable presence to determine the action to take. In the case of SINK, the detachment is done
when VBUS is below vSafe0V.


/* USER CODE BEGIN include */
#include "main.h"
/* USER CODE END include */

/* USER CODE BEGIN BSP_USBPD_PWR_VBUSGetVoltage */

/* Check if instance is valid */


int32_t ret = BSP_ERROR_NONE;

if ((Instance >= USBPD_PWR_INSTANCES_NBR) || (NULL == pVoltage))


{
ret = BSP_ERROR_WRONG_PARAM;
*pVoltage = 0;
}
else
{
uint32_t val;
val = __LL_ADC_CALC_DATA_TO_VOLTAGE( VDDA_APPLI, \
LL_ADC_REG_ReadConversionData12(ADC1), \
LL_ADC_RESOLUTION_12B); /* mV */
/* X-NUCLEO-USBPDM board is used */
/* Value is multiplied by 5.97 (Divider R6/R7 (40.2K/200K) for VSENSE) */
val *= 597;
val /= 100;
*pVoltage = val;
}
return BSP_ERROR_NONE;

/* USER CODE END 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.

AN5418 - Rev 3 page 28/43


AN5418
Check jumpers

5.7 Check jumpers

Figure 31. TCPP01 shield picture

Fitted jumpers

Fitted jumpers

Verify that the two JP1 jumpers located on the right to select the STM32G0 and STM32G4 configuration are
inserted.

AN5418 - Rev 3 page 29/43


AN5418
Check jumpers

Then select the pins that are controlled by the MCU, using the left JP2 jumpers:

Figure 32. TCPP01 jumper settings

• 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.

AN5418 - Rev 3 page 30/43


AN5418
Establish the first explicit contract

6 Establish the first explicit 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").

Figure 33. Board selection

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".

Figure 34. Port selection

AN5418 - Rev 3 page 31/43


AN5418
Establish the first explicit contract

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:

Figure 35. Explicit contract visible in UCPD monitor

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.

AN5418 - Rev 3 page 32/43


AN5418
How to debug a bit deeper

6.1 How to debug a bit deeper


• For more information in trace about CAD state machine, refer to Figure 36, where CAD appears in column
Type.

Figure 36. Example of CAD debug information visible in the trace

AN5418 - Rev 3 page 33/43


AN5418
How to debug a bit deeper

• 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:

#define USBPD_CAD_STATE_RESET 0u /*!< USBPD CAD State


Reset */
#define USBPD_CAD_STATE_DETACHED 1u /*!< USBPD CAD State No cable
detected */
#define USBPD_CAD_STATE_ATTACHED_WAIT 2u /*!< USBPD CAD State Port partner
detected */
#define USBPD_CAD_STATE_ATTACHED 3u /*!< USBPD CAD State Port partner
attached */

Figure 37. cstate=1: detached

Figure 38. cstate=3: attached

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:

LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_4, LL_GPIO_MODE_ANALOG);


In STM32G4 versions before firmware 1.2.0, the correct compilation switch is not set. An easy way to correct
this issue is to activate the compilation switch MB1367.

AN5418 - Rev 3 page 34/43


AN5418
How to debug a bit deeper

• For further debugging, the VBUS measured value can be printed in the trace, using the user button:

Figure 39. User button on STM32G0 Nucleo-64 board schematics

Add the button from STM32CubeMX as described in Figure 40.

Figure 40. Add the user button in STM32CubeMX

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.

Figure 41. Modify TCPP01 controls

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).

AN5418 - Rev 3 page 36/43


AN5418
Conclusion

8 Conclusion

This demonstration is only the first step to a power delivery application.


It is not optimized from the low power point of view.
The USB PD application performed here is the minimum. No intelligence is added to select the power level by
looking at the proposal sent by the source. The mandatory hard reset management is also missing.
To continue further, various demonstrations are available on STM32G0, STM32G4, and STM32L5. Check the
Projects directory in the firmware package available in each serie, on the ST website. For instance, UCPD
demonstration on EVAL_G0 is available under the folder .\Projects\STM32G081B-EVAL\Demonstrations\
DemoUCPD.

AN5418 - Rev 3 page 37/43


AN5418

Revision history

Table 2. Document revision history

Date Version Changes

9-Jan-2020 1 Initial release


30-Apr-2020 2 Added specific technical data throughout the document
Updated:
Figures:
• Figure 7. FreeRTOS™ configuration
• Figure 11. Detailed stack configuration
• Figure 24. Selection of USB-PD middleware TRACER_EMB source
27-Nov-2020 3 • Figure 26. Project manager settings
• Figure 34. Port selection
Code blocks in:
• Section 5.6.2 Modification in stm32g0xx_it.c
• Section 5.6.3 Modification in usbpd_dpm_user.c
• Section 5.6.4 Modification in usbpd_pwr_user.c

AN5418 - Rev 3 page 38/43


AN5418
Contents

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

5.1.2 UCPD peripheral configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

5.1.3 FreeRTOS™ configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5.1.4 USB-PD middleware configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

5.1.5 ADC configuration for VBUS reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

5.1.6 Clock check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5.2 Additional recommended optional debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17


5.2.1 UART configuration for debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

5.2.2 Activation of embedded tracer for debug. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

5.2.3 Activation of UCPD monitor firmware responder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

5.3 Update and save project configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22


5.4 Code generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.5 Compilation of generated application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.6 Complete USB-PD application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.6.1 Modification in main.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

5.6.2 Modification in stm32g0xx_it.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

5.6.3 Modification in usbpd_dpm_user.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

5.6.4 Modification in usbpd_pwr_user.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

5.7 Check jumpers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

6 Establish the first explicit contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31


6.1 How to debug a bit deeper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

7 Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36


8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38

AN5418 - Rev 3 page 39/43


AN5418
Contents

Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
List of tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
List of figures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42

AN5418 - Rev 3 page 40/43


AN5418
List of tables

List of tables
Table 1. Acronym definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Table 2. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

AN5418 - Rev 3 page 41/43


AN5418
List of figures

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

AN5418 - Rev 3 page 42/43


AN5418

IMPORTANT NOTICE – PLEASE READ CAREFULLY


STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and improvements to ST
products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on ST products before placing orders. ST
products are sold pursuant to ST’s terms and conditions of sale in place at the time of order acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or the design of
Purchasers’ products.
No license, express or implied, to any intellectual property right is granted by ST herein.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. For additional information about ST trademarks, please refer to www.st.com/trademarks. All other product or service
names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.
© 2020 STMicroelectronics – All rights reserved

AN5418 - Rev 3 page 43/43

You might also like