Getting started with USB Type-C only Source

Revision as of 12:45, 6 May 2024 by Registered User (1st Draft SRC5V with Software Pack)

Target description

This tutorial aims to help you to:

  • use the X-NUCLEO-SRC1M1 shield that includes a TCPP02-M18 protection circuit and provides a USB Type-C® connector
  • create a USB legacy 3A@5V Type C Source application with the NUCLEO-F446RE board that does not include any UCPD peripheral and the X-NUCLEO-SRC1M1 shield by using STM32CubeIDE software

Prerequisites

  • Computer with Windows 7 (or higher)

Hardware

  • NUCLEO-F446RE (tested on rev C-04) [1]
  • X-NUCLEO-SRC1M1 shield [2]
  • A 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 cable Type-A to Mini-B
  • USB Type-C® to Type-C® cable

Software

  • STM32CubeMX (tested with V6.11.0 - minimal release 6.11.0) [3]
  • STM32CubeIDE (tested with V1.14.0) [4]
  • X-CUBE-TCPP MCU Firmware Package (BSP) [5]

Literature

  • UM1724 NUCLEO-F446RE User Manual
  • UM2973 X-NUCLEO-SRC1M1 User Manual


Create a USB-PD Source Device

Clock.png Total 50min

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

Clock.png 5min

Open STM32CubeIDE and create a new STM32 project. As a target selection, choose the NUCLEO-F446RE 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".


When prompted for initializing peripherals with their default mode, click No.

3. Configuring the system

Clock.png 15min

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.

USBPD 0-pinoutConf.png


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 without UCPD application, the tcpp0203 Board part and the X-NUCLEO-SRC1M1 Board support.


3.3. Configure the system timebase

For this simple example, we use SysTick as the system timebase. In the System Core section, select SYS and change the Timebase Source to SysTick.

USBnoPD 0 sysConf v2.png


3.4. Configure ADC peripheral

For this application, VBUS, CC1, CC2 needs to be monitored. To do it, an ADC needs to be configured to measure the VBUS voltage and current, CC1, CC2 and Vprovider.
As we are going to use the X-NUCLEO-SRC1M1 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 software pack will reconfigure it.

USBPD 0-ADC1Conf.png


3.5. Configure I2C peripheral

As the X-NUCLEO-SRC1M1 shield includes a TCPP02-M18 that communicates via I2C, we need to enable the I2C peripheral in our project.

In the Connectivity section, enable I2C2 peripheral, in I2C mode. Leave the configuration as default, as the software pack will reconfigure it.

USBPD 0-I2C2Conf.png


Note: We need to enable the I2C2 peripheral in the CubeMX view for code generation to include the I2C drivers as we do for the ADC.

3.6. Enable the software pack

In the middleware and software pack category, select the X-CUBE-TCPP software pack. Enable the 'Source without UCPD' Application, the 'tcpp0203' Board Part and the 'X-NUCLEO-SRC1M1' Board support.


3.7. Check the clock configuration

Under Clock Configuration main tab.

USBnoPD 0 CLKConf2.png


4. Configure the project

Clock.png 5min

Under the Project Manager main tab, and the Advanced Settings tab, as we do not need I2C initialization functions (handled by the BSP drivers), uncheck Generate Code for the MX_I2C1_Init.


5. 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, clicking on Project/Generate Code, or by pressing Alt+K.

USBPD 0-projGen.png



In this project, different folders can be found:

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


6. Configure the shield's jumpers

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

USBnoPD SRC1M1 Shield.jpg


On the NUCLEO-F446, place a link between PA3 (CN10 - 37) and PC4 (CN10 - 34).

This X-NUCLEO-SRC1M1 shield default configuration allows SINK to source up to 0.5A @ 5V.
Plug an external 5V source with current capability >0.6A into the green "source" connector.
The current sense resistor R4 is 7mOhms, then TCPP02 current protection level is 6A. Refer to TCPP02-M18 datasheet. [6]

With this configuration, the board is powered by the ST-Link of the Nucleo board.
If you want 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.

Info white.png Information

To increase the solution current capability to 3A @ 5V,

  • Remove R35 and place it on SH19
  • Remove R39 and place it on SH21
  • Replace R4 sense resistor (initially 7mOhms) with a 10mOhms resistor

Next, plug an external 5V source with current capability > 4.5A into the green "source" connector.

In SRC1M1_conf.h change SRC1M1_ISENSE_RS value from 7 milliohms to 10 milliohms:

#define SRC1M1_ISENSE_RS                            10u  /* Current measure shunt resistor in milliohm */

7. Compile and run the application

The compilation must be performed without errors or warnings.
Build the application, clicking on the Built Button.png button (or select Project/Build Project).
Run the application, clicking on the DownloadRun Button.png button (or select Run/Run)

8. Evaluate the application

Clock.png 5min

Vbus is active 120 ms after plugging an USB Type-C source device.
Vbus is immediately shut down and placed in safe mode (0V) when unplugging the sink device.
During connection, if an overcurrent or malfunction is detected, the Source is placed in safe mode (0V) and needs a disconnection/reconnection to restart.

9. For information, code inserted by the software pack

Following code has been automatically inserted by the software pack in:

10. Project with a custom board

This chapter allows to build an USBPD Source application using a custom board with any STM32 MCU with 5 ADC Channels.

Info white.png Information
Selected resources: ADC1-IN0, ADC1-IN1, ADC1-IN4, ADC1-IN10, ADC1-IN11, I2C1, PC4 and PC8, are for the example, replace by the custom board resources affectation.
  • 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


  • Configure ADC Peripheral

Five signals must be monitored to ensure proper and safe USB 3A-5V delivery:
CC1 and CC2 lines voltage, Vbus and Vprovider voltages and Iana, the current through Vbus.
The ADC needs to be configured:
In the pin view, with a left click, select the pin PC0 and configure it as ADC1 Channel 10 (CC2).
Repeat the operation for PC1 as ADC1 Channel 11 (IANA); for PA0 as ADC1 Channel 0 (VBusc); for PA1 as ADC1 Channel 1(Vprov); and for PA4 as ADC1 Channel 4 (CC1).

USBnoPD 0 ADCConf.png


Note: it is not mandatory to set their name as each value is stored in a table by the DMA in function of its rank.

  • In the Analog section click on ADC and select, in the Parameter Settings tab:

Scan conversion mode: Enabled
Continuous conversion mode: Enabled
Number of conversions: 5


Define for each ADC input its channel and 84 cycles sampling time Rank 1: CC2: Channel 10
Rank 2: CC1: Channel 4
Rank 3: Vbus: Channel 0
Rank 4: Iana: Channel 11
Rank 5: Vprov: Channel 1

USBnoPD 2 ADCConf.png


  • DMA Configuration for ADC1

in the System Core section, select the DMA, tab DMA2 click on "Add" and select ADC1 in the DMA2 request column.

USBnoPD 0 DMAConf.png


Set the DMA mode to: Circular

USBnoPD 1 DMAConf.png


Then back to the ADC1 parameters configuration, set DMA Continuous Requests to Enabled

USBnoPD 3 ADCConf.png


  • 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


  • Timer configuration

in the Timers section, select timer2: "TIM2", affect the "Internal clock" as clock source.
In the Parameter Settings tab,
set the Internal clock division to No division; Then the timer peripheral frequency is 84MHz
set the Prescaler value to 2099; Then the timer counter frequency is 84 / (2099+1) = 40kHz
set the counter period to 39; Then the timer period is 40kHz / (39+1) = 1ms
And set Auto-reload preload to Enable

USBnoPD 0 Tim2Conf2.png


Finally, select the NVIC Settings tab to enable the TIM2 global interrupt

USBnoPD 1 Tim2Conf.png


  • Enable the software Pack

In the middleware category, select the X-Cube-TCPP Software pack and enable its application and Board Part


  • Assign ressources to the application requirements


  • As in the chapter 4,Configure Project

But in the Advanced settings keep ADC and I2C initialization code generation.

  • As in the chapter 5, Generate code
  • As in the chapter 7, Compile and run the application



You can find other applicative examples on GitHub: x-cube-tcpp

11. References