Getting started with USB-Power Delivery Sink

Revision as of 16:19, 29 October 2021 by Registered User (New format, new pictures)

Target description

This tutorial will help you to:

  • Use the X-NUCLEO-SNK1M1 or X-NUCLEO-USBPDM1 shield that includes a TCPP01-M12 protection circuit and provides a USB Type-C® connector
  • Create a USB-PD Sink Device with the NUCLEO-G071RB board and the X-NUCLEO-SNK1M1 or X-NUCLEO-USBPDM1 shieldby using STM32CubeIDE software

Prerequisites

  • Computer with Windows 7 (or higher)

Hardware

  • NUCLEO-G071RB (tested on rev B01) [1]
  • X-NUCLEO-SNK1M1 (tested on Rev 1) [2] or X-NUCLEO-USBPDM1 [3]
  • A USB-PD source device to test our USB-PD device (it can be a PD capable wall charger, a powerbank, etc.)
  • USB cable Type-A to Micro-B
  • USB Type-C® cable

Software

  • STM32CubeIDE (tested with V1.5.2) [4]
  • STM32CubeMonitor-UCPD (tested with V1.1.1) [5]

Literature


Create a USB-PD Sink Device

Clock.png Total 45min

1. Creating the project

Clock.png 5min

Open STM32CubeIDE and create a New STM32 Project. As a target selection, choose the STM32G071RB from the MCU/MPU Selector Tab

snk0-newproject.png


Click "Next", then enter you project's name. Leave the other fields as default and click "Finish".

snk1-newproject.png


2. Configuring the system

Clock.png 15min

At this point your project is created and you are left with the STM32CubeMX view. In the next steps, we will configure the peripherals and options needed for the project.

2.1. Configure UCPD peripheral

In the Connectivity tab, select the UCPD1 peripheral and enable it in sink mode. Under the NVIC Settings tab, enable USB global interrupts.

Info white.png Information
You don't need to include “Dead Battery Signals” in the configuration as this is managed by the TCPP01 protection device on the X-NUCLEO-SNK1M1 shield.
snk0-UCPD1Conf.png


Under the DMA Settings tab, add UCPD1_RX and UCPD1_TX DMA requests.

snk1-UCPD1Conf.png


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

0-FreeRTOSConf.png


Then, under the Include Parameters tab, Enable eTaskGetState include definition.

1-FreeRTOSConf.png


2.3. 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 0x26019096 (correspond to a 5V / 1.5A, dual-role data sink)
snk0-USBPDConf.png


The following table is extracted from USB Power Delivery Specification, Table 6-14 Fixed Supply PDO - Sink. 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 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 support 00b No
B22..20 Reserved - Shall de set to zero 0b No
B19..10 Voltage in 50mV units 0001100100b 5V
B9..0 Maximum current in 10mA units 0010010110b 1.5A


2.4. Configure ADC peripheral

In order 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.
In the Analog section, enable ADC1 peripheral channel 9 (connected to pin PB1). Configure the ADC with the following configuration:

  • Clock prescaler : Synch / 4
  • Continuous conversion mode: Enabled
  • Overrun behavior: Overrun data overwritten
  • Sampling time Common 1 & 2: 160.5 Cycles
snk0-ADC1Conf.png


Then, under the GPIO Settings tab, rename PB1 pin to VSENSE, a more descriptive name for this pin, used for VBUS monitoring:

snk1-ADC1Conf.png


Under the User Constants tab, Add a constant named VDDA_APPLI of value 3300.

2-ADC1Conf.png


2.5. Configure Clocks

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

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

2.6. [OPTIONAL] Configure Tracer for debug

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

0-LPUARTConf.png


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

snkLPUARTConf.png


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

1-LPUARTConf.png


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

2-LPUARTConf.png


2.6.2. Configure embedded tracer

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

0-tracerConf.png


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

1-tracerConf.png


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


3. 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 the application needs.

snk0-projectConf.png



Under the Advanced Settings tab, change LPUART driver to LL to save a bit of memory heap size.

snk2-projectConf.png


4. Generate code

Clock.png 5min

Save your file with Ctrl+s and select generate code if prompted. You can also generate code from the STM32CubeIDE menu by clicking Project/Generate Code, or by pressing Alt+K.

0-projGen.png


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.

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.



5. Complete USB-PD application

Clock.png 10min

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.
  • Handlers for the interrupts needs to be completed, in order to wake up the UCPD peripheral.
  • BSP_USBPD_PWR_VBUSGetVoltage function needs to be completed with the right coefficient depending on the VBUS divider bridge.
  • USBPD_DPM_SNK_EvaluateCapabilities function needs to be completed to answer one source capability message.
  • TCPP01‑M12 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.

5.1. Modification in main.c

Info white.png Information
You can double-clic the code zones to select it all, then copy it with Ctrl+c.

Add the following code between the /* USER CODE BEGIN 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 */

5.2. Modification in stm32g0xx_it.c

Add the following code between the /* USER CODE BEGIN SysTick_IRQn 0 */ tags:

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

5.3. Modification in usbpd_dpm_user.c

Add the following code USBPD_DPM_GetDataInfo function:

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;


Add the following code between the /* USER CODE BEGIN USBPD_DPM_SNK_EvaluateCapabilities */ tags:

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

5.4. Modification in usbpd_pwr_user.c

Add the following code between the /* USER CODE BEGIN include */ tags:

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

Add the following code between the /* USER CODE BEGIN BSP_USBPD_PWR_VBUSGetVoltage */ tags:

/* 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 ret;
/* USER CODE END BSP_USBPD_PWR_VBUSGetVoltage */

6. Configure the shield's jumpers

Place jumpers on the X-NUCLEO-SNK1M1 shield as shown in the picture.

NewShield.PNG


If you are instead using the X-NUCLEO-DRP1M1 shield, please follow this configuration:

Picture24.png


7. Compile and run the application

The compilation must be performed without error 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 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").

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

snkCubeMon2.png


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

snkCubeMon3.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 (OUT orange message).
  3. The ACCEPT and the PS_RDY are sent by the source (IN green message).
  4. The contract negociation 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.

8.1. References