UM2063 User manual STM32 USB-PD (Power Delivery) software expansion for STM32Cube Introduction This document describes the STM32 USB-PD (Power Delivery) software expansion for STM32Cube, referenced as X-CUBE-USB-PD. The USB Type-C™ is the newest USB connector ecosystem, it addresses the evolving needs of platform and devices, while retaining the functional benefits of USB. The USB Power Delivery protocol is embedded in USB Type-C™ connectors, resulting in easy connection/disconnection of USB cables, and ensuring much more than data transfer. This protocol enables to carry more than the regular 5 V / 1.5 A, with a maximum power supply of 100 W. STM32 USB-PD package (X-CUBE-USB-PD) consists of a library (in a binary format) and application examples for STM32F0 devices acting as USB Power Delivery controllers. It will be extended to other STM32 Series. This package includes examples covering most of applicative use cases, allowing the user to develop applications based on USB-PD (Provider, Consumer, and Dual-Role Port). The library and the associated examples can be used with the STM32F0 devices and a dedicated P-NUCLEO-USB001 shield containing Analog Front-Ends and USB Type-C™ connectors. This document describes how to use the USB-PD library for regular use and to create a customized application. It covers the following topics to ease the use of the library: • USB-PD Standard overview • USB-PD library architecture • USB-PD Stack usage description • How to use the library for setting an USB-PD application. June 2016 DocID029258 Rev 1 1/30 www.st.com 1 Contents UM2063 Contents 1 2 3 4 5 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.1 Acronyms and abbreviations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 USB-C PD architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1 Architecture overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.2 USB-PD layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.3 Message flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.4 Data flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 USB-PD library description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3.2 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.3 Library architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.4 Hardware related components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 USB-PD library programming guidelines . . . . . . . . . . . . . . . . . . . . . . . 18 4.1 Library initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 4.2 USB-PD library functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 4.3 USB-C PD Library Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Examples description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 5.1 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 5.2 USB-PD provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 5.3 5.4 5.5 2/30 5.2.1 Example setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 5.2.2 Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 USB-PD consumer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 5.3.1 Example setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 5.3.2 Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 USB-PD provider DRP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 5.4.1 Example setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 5.4.2 Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 USB-PD consumer DRP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 DocID029258 Rev 1 UM2063 Contents 5.5.1 Example setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 5.5.2 Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 6 Memory footprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 7 Frequently asked questions (FAQs) . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 8 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 DocID029258 Rev 1 3/30 3 List of tables UM2063 List of tables Table 1. Table 2. Table 3. Table 4. Table 5. Table 6. Table 7. Table 8. Table 9. Table 10. Table 11. Table 12. 4/30 List of acronyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Use of different IPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 GPIOs used by Port 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 GPIOs used by Port 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Interrupt priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 DPM files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 USB-PD user functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 USB-C PD callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 USB-PD - Provider memory footprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 USB-PD - Consumer memory footprint. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 USB-PD - Dual role port memory footprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 DocID029258 Rev 1 UM2063 List of figures List of figures Figure 1. Figure 2. Figure 3. Figure 4. Figure 5. Figure 6. USB power delivery architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 USB-PD - Messages flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 USB-PD stack architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Project files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 USB-PD stack architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 STM32FO72RB Nucleo with P-Nucleo-USB001 shield . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 DocID029258 Rev 1 5/30 5 Overview UM2063 1 Overview 1.1 Acronyms and abbreviations Table 1. List of acronyms Term 1.2 6/30 Meaning API Application Programming Interface CAD Cable Detection Module DFP Downstream Facing Port DPM Device Policy Manager FW Firmware HW Hardware PD Power Delivery PE Policy Engine PRL Protocol Layer UFP Upstream Facing Port USB Universal Serial Bus VDM Vendor Defined Messages References • Universal Serial Bus Power Delivery Specification, Revision 2.0, 7 May 2015 • Universal Serial Bus Type-C Cable and Connector Specification 1.2, March 25, 2016 DocID029258 Rev 1 UM2063 USB-C PD architecture 2 USB-C PD architecture 2.1 Architecture overview The USB power delivery specification document defines the communicating layers of a PD device (either provider or consumer) as shown in Figure 1. Figure 1. USB power delivery architecture 3URYLGHU &RQVXPHU 'HYLFHSROLF\PDQDJHU 'HYLFHSROLF\PDQDJHU 3ROLF\HQJLQH 3ROLF\HQJLQH 3URWRFRO 3URWRFRO 3K\VLFDOOD\HU 3K\VLFDOOD\HU 9%86&& 069 A PD-capable device is assumed to be made up of at least one port, which can be: • • Upstream Facing Port (UFPs) – Sink power (a consumer). – Optionally source power (a consumer/provider). – Optionally communicate via USB – Communicate using SOP Packets. Downstream Facing Port (DFPs): – Source power (a provider). – Optionally Sink power (a provider/consumer). – Optionally communicate via USB. – Communicate using SOP Packets – Optionally communicates using SOP packets DocID029258 Rev 1 7/30 29 USB-C PD architecture UM2063 Where USB products support USB Power Delivery protocols a USB DFP is initially a Source and a USB UFP is initially a Sink, although USB-PD enables the Source/Sink and the DFP/UFP roles to be swapped. Note: There is only one Source port and one Sink port in each PD communication between port partners. 2.2 USB-PD layers Note: 8/30 • Device Policy Manager (DPM) The DPM role is to manage the power used by one USB Power Delivery port. It delivers power to a consumer in case of a provider application, or asks for it in case of a consumer application. The DPM also allows exchanging VDM messages once an Explicit Contract is established. The Device Policy Manger is the upper layer of the USB-C Power Delivery Stack. • Policy Engine (PE) layer The Policy engine (PE) role is to drive the message sequences according to the sent message and to its expected response. It allows negotiating power, establishing an Explicit Contract for the power exchange. The acceptance or the refusal of a request depends on the response of the DPM towards a specific power profile. The PE also handles the Vendor Defined Messages flow, allowing to discover, enter or exit specified modes according to those supported by both provider and consumer sides. • Protocol (PRL) layer The PRL layer role is to drive messages construction, transmission and reception independently of their nature. It permits to check the message flow to detect communication errors. The PRL layer is a wrapping layer between the PE and the PHY. • PHY layer The PHY layer is responsible for sending and receiving messages across the CC wire. It consists of a transceiver that superimposes a signal on the wire. It is responsible for managing data on the wire and for collision avoidance and detects errors in the messages using a CRC. For more Information about the USB Power Delivery protocol, refer to the official specification document mentioned in Section 1.2. DocID029258 Rev 1 UM2063 2.3 USB-C PD architecture Message flow Each message sent from a device should be replied by a GoodCRC message. This permits to the receiver to acknowledge the sender that its message was correctly received, and would be treated by upper layers. The wrong messages received should be ignored, in the case of a persistent communication error, a soft reset permits to reset protocol parameters to re-establish the communication. If the error persists, a hard reset of the system is performed. In normal conditions, negotiating a power contract should be performed according to the sequence illustrated in Figure 2. DocID029258 Rev 1 9/30 29 USB-C PD architecture UM2063 Figure 2. USB-PD - Messages flow 3URYLGHU &RQVXPHU 'HILQH&DSDELOLWLHV 6HQG B&DS DELOLW LHV $QDO\]HUHFHLYHG &DSDELOLWLHVDQG FKRRVHDSRZHU SURILOHWRUHTXHVW & &5 *RRG ELOLW\ &DSD HVWB 5HTX $QDO\]HUHFHLYHG UHTXHVWDQGGHFLGHLI LWPXVWEHDFFHSWHG RUUHMHFWHG *RRG &5& $FFH SW &5& *RRG 6ZLWFKWRWKH UHTXLUHGSRZHU SURILOHDQGLQIRUP WKHFRQVXPHU 36B5 HDG\ &5& *RRG 069 10/30 DocID029258 Rev 1 UM2063 2.4 USB-C PD architecture Data flow In the normal case, every USB-PD basic message should pass from the PE layer down to the PHY layer. Each received message should be replied to with a GoodCRC message to inform the sender of the correct reception (see Figure 3). BIST messages are an exception, as they should not be passed to the PE layer once received, and should only be replied to with a GoodCRC. Figure 3. USB-PD stack architecture 0HVVDJHVHQGHU 3ROLF\(QJLQH 3URWRFRO 0HVVDJHUHFHLYHU 3+< 3+< 3URWRFRO 3ROLF\(QJLQH 6HQGPHVVDJH 0HVVDJH 0HVVDJH&5& 0HVVDJH 6WDUW&5&5HFHLYH7LPHU &KHFN0HVVDJH,' DJDLQVWORFDOFRS\ 6WRUHFRS\RI0HVVDJH,' 0HVVDJHUHFHLYHG *RRG&5& *RRG&5&&5& &RQVXPHPHVVDJH *RRG&5& &KHFNDQGLQFUHPHQW 0HVVDJH,'&RXQWHU 6WRS&5&5HFHLYH7LPHU 0HVVDJHVHQW 069 DocID029258 Rev 1 11/30 29 USB-PD library description UM2063 3 USB-PD library description 3.1 Overview STMicroelectronics offers an USB-C Power Delivery Library supporting STM32F0xRB microcontrollers. The library (see Figure 4) is provided in binary format, comes on top of the STM32Cube HAL driver and offers all the APIs required to develop an USB PD application. This section describes the USB-PD library middleware module and illustrates how users can develop their own power delivery application using this library. The USB-PD library is developed following the Universal Serial Bus Power Delivery Specification Revision 2.0, V1.1, 7 May 2015 and Universal Serial Bus type-C Cable and Connector Specification, Revision 1.2, March 25, 2016. It has passed successfully the official certification. Figure 4. Project files 12/30 DocID029258 Rev 1 UM2063 USB-PD library description The STM32 USB-PD package contains: 3.2 • The USB-PD stack driver • A set of examples that illustrate how to use the driver for developing a power delivery application • An STM32 PD provider example • An STM32 PD consumer example • Two STM32 Dual Role Port example • VDM and billboard usage guidelines Features To enable PD communication the USB-PD library features: • compliant with USB Type-C 1.2 and USB Power Delivery 2.0 Standards • provider, consumer and Dual Role mode (DRP) support • Dual Port support • PD communication supported for the two sides of the cable • cable detection (detection of an USB-C cable plug/unplug) • cable orientation (detection of orientation, to allow the user to choose one communication line CC1/CC2) • USB-PD messages transmission/reception: Communication through CC line and message exchanging, coding/decoding using BMC and 5b4b coding • drive VCONN switch and SuperSpeed switches for flippable connector or Alternate Modes • BIST test mode support: BIST test mode to enable testing the platform at runtime • structured VDM support allowing Alternate modes and extensions implementation DocID029258 Rev 1 13/30 29 USB-PD library description 3.3 UM2063 Library architecture The USB-PD library uses the USB-C PD specification architecture as a reference for building the library. A hardware expansion board (P-NUCLEO-USB001 shield), containing analog front ends and USB-C connectors is used with the Nucleo board to ensure the communication. Figure 5. USB-PD stack architecture 670) ([WHUQDOSRZHU 8VHUDSSOLFDWLRQ 'HYLFHSROLF\PDQDJHU 670 86%3' OLEUDU\ 3ROLF\HQJLQH 3RZHU VRXUFHVLQN 3URWRFROOD\HU &DEOH GHWHFWLRQ 3K\VLFDOOD\HU 318&/(286% $QDORJ)URQW(QG 86%7\SH&FRQQHFWRU 069 14/30 DocID029258 Rev 1 UM2063 USB-PD library description The library covers two main parts: • Generic components (independent from HW): – • 3.4 Protocol Layer – Policy Engine Layer – Device Policy Manager – Application Layer Specific Components: – Physical Layer – Cable Detection Module (CAD) Hardware related components The physical layer and the Cable Detection are directly related to HW and need the use of a set of IPs (see Table 2). Table 2. Use of different IPs IP Use CRC GoodCRC calculation ADC1 Detection of Cable Attach/Detach COMP1 Data reception DMA (Channel 5, Channel 3) Buffering data SPI2 Data transmission TIM2, TIM3, TIM4, TIM16 Power Delivery timers calculation Providing a time base for the communication process When using those IPs, some GPIOs (see tables 3 and 4) are reserved for the library and cannot be used for other purposes. Table 3. GPIOs used by Port 1 GPIO GPIOA GPIOB Pin IP Use 0 COMP1-ADC1 CC1 Data reception 1 COMP1 CC reference voltage value 5 COMP1-ADC1 CC2 Data reception 1 TIM14 Data Transmission 8 - 9 - 12 - 13 SPI2 Data Transmission 14 SPI2 CC1 Data Transission pin DocID029258 Rev 1 P-NUCLEO-USB001 shield control 15/30 29 USB-PD library description UM2063 Table 3. GPIOs used by Port 1 (continued) GPIO GPIOC GPIOD Pin IP Use 2 SPI2 CC2 Data Transmission pin 3 - 8 - 2 - P-NUCLEO-USB001 shield control P-NUCLEO-USB001 shield control Table 4. GPIOs used by Port 2 GPIO GPIOA GPIOB GPIOC Note: Pin IP Use 1 COMP1 CC reference voltage value 2 COMP1-ADC1 CC1 Data reception 4 COMP1-ADC1 CC2 Data reception 6 SPI2 CC2 Data transmission pin 15 - P-NUCLEO-USB001 shield control 2 - P-NUCLEO-USB001 shield control 3 SPI2 Data Transmission 4 SPI2 CC1 Data Transmission pin 5 - P-NUCLEO-USB001 shield control 15 TIM15 Data Transmission 6 - 7 - 14 - P-NUCLEO-USB001 shield control For more information on P-NUCLEO-USB001 shield and the needed connections with the STM32, refer to the hardware dedicated user manual UM2050, available on www.st.com. The USB-C PD library defines and uses the three groups of interrupt priorities listed in Table 5. Table 5. Interrupt priorities Priority level Process Priority value Critical Transmission 0 High Reception 2 Low/medium Others 3 The user application shouldn’t define an interrupt with a priority equal or lower than three, or no PD/VDM communication will occur. Interrupts with priority value higher than two can be used at any time.Consequently; the user application shouldn’t define an interrupt with a 16/30 DocID029258 Rev 1 UM2063 USB-PD library description priority equal or lower than three, or no PD/VDM communication will occur. Interrupts with priority value higher than two can be used at any time. 82.5 ms are typically needed to establish an Explicit Contract. During this period of time, the STM32F0 CPU load is 43%, calculated with the following configuration: • Compiler: IAR Embedded Workbench® for ARM®, version 7.50.1 • Optimization: high speed • MCU: STM32F072RB, operating at 48 MHz • Expansion board: P-NUCLEO-USB001 shield DocID029258 Rev 1 17/30 29 USB-PD library programming guidelines 4 UM2063 USB-PD library programming guidelines The X-CUBE-USB-PD package includes the library in a binary format, its headers files ".h" indicate the APIs. 4.1 Library initialization To use and initialize the library, follow the steps listed below. 1. Include the STM32 USB-C PD library: “.\Middlewares\ST\STM32_USBPD_Library\Lib\STM32_USBPD100_CM0_ot_IAR.a” 2. Include the DPM files indicated in Table 6 in your application. 3. Configure the MCU clock, a template is implemented and could be used in the SystemClock_Config() function in the main.c file of each project example. 4. Call the USBPD_HW_IF_GlobalHwInit() function to Initialize the Hardware Interface layer. 5. Call the USBPD_DPM_Init() function to initialize the Device Policy Manager layer and all the communication layers (PE, VDM, PRL, PHY). Table 6. DPM files Application Provider Consumer Provider Consumer Note: 18/30 File location .\STM32F072_Nucleo\ Applications \Provider_RTOS\AppSource\inc\usbpd_dpm.h .\STM32F072_Nucleo\ Applications \Provider_RTOS\AppSource\src\usbpd_dpm.c .\STM32F072_Nucleo\ Applications \Consumer_RTOS\AppSource\inc\usbpd_dpm.h .\STM32F072_Nucleo\ Applications \Consumer_RTOS\AppSource\src\usbpd_dpm.c .\STM32F072_Nucleo\ Applications \Dual_Role_Port_RTOS\AppSource\inc\usbpd_dpm.h .\STM32F072_Nucleo\ Applications \Dual_Role_Port_RTOS\AppSource\src\usbpd_dpm.c .\STM32F072_Nucleo\ Applications \Consumer_DRP_RTOS\AppSource\inc\usbpd_dpm.h .\STM32F072_Nucleo\ Applications \Consumer_DRP_RTOS\AppSource\src\usbpd_dpm.c The USB-C PD library uses FreeRTOS to manage high level layers. DocID029258 Rev 1 UM2063 4.2 USB-PD library programming guidelines USB-PD library functions The DPM layer calls functions from the PE and the VDM layers to trigger PD contract establishment, and to enable the use of vendor defined messages. Table 7. USB-PD user functions Function Description void USBPD_CAD_PortEnable(uint8_t hport, USBPD_CAD_activation act); Enables or disables the CAD port USBPD_CAD_StatusTypeDef USBPD_CAD_Init(uint8_t hport, USBPD_PortPowerRole_TypeDef PortPowerRole, USBPD_CAD_Callbacks callbackfunctions); Initializes the CAD module for a specified port void USBPD_CAD_AssertRd(uint8_t hport); Sets the port as Sink void USBPD_CAD_AssertRp(uint8_t hport); Sets the port as Source USBPD_StatusTypeDef USBPD_PE_Init (uint8_t portnum, USBPD_PortPowerRole_TypeDef role, USBPD_PE_Callbacks pecallbacks); Initializes the Policy Engine layer USBPD_StatusTypeDef USBPD_PE_SRCInit(uint8_t portnum, USBPD_PortPowerRole_TypeDef role, USBPD_PE_Callbacks pecallbacks); Initializes the Policy Engine processing instance as a source USBPD_StatusTypeDef USBPD_PE_SNKInit(uint8_t portnum, USBPD_PortPowerRole_TypeDef role, USBPD_PE_Callbacks pecallbacks); Initializes the Policy Engine processing instance as a sink USBPD_StatusTypeDef USBPD_PE_DeInit(uint8_t portnum); De-initializes the Policy Engine layer void USBPD_PE_SRCProcess (uint8_t portnum); Processing function for the Policy Engine Source application USBPD_StatusTypeDef USBPD_PE_AddPowerProfile(uint8_t portnum, uint32_t *pPDO, uint32_t nbpdo); Adds a power profile to be negotiated USBPD_StatusTypeDef USBPD_PE_IsCableConnected(uint8_t portnum, uint8_t isconnected); Checks if the cable is connected USBPD_StatusTypeDef USBPD_SVDM_Init(uint8_t portnum, USBPD_VDM_Callbacks cbs); Initializes VDM layer USBPD_StatusTypeDef USBPD_SVDM_UFPProcess(uint8_t portnum); VDM processing function for an Upstream Facing Port USBPD_StatusTypeDef USBPD_SVDM_DFPProcess(uint8_t portnum); VDM processing function for a Downstream Facing Port DocID029258 Rev 1 19/30 29 USB-PD library programming guidelines 4.3 UM2063 USB-C PD Library Callbacks Policy Engine or VDM layers need to inform the DPM of a notification or of a request. This is enabled by using structures of callbacks. Table 8. USB-C PD callbacks Function Description void (*USBPD_CAD_CallbackEvent)(uint8_t hport, USBPD_CAD_STATE State, CCxPin_TypeDef Cc); Called by the CAD to inform of a cable Attach/Detach, and the CC line void (*USBPD_PE_RequestSetupNewPower)(uint8_t hport, uint8_t rdoposition); Requests the DPM to setup the new power level void (*USBPD_PE_HardReset)(uint8_t hport); Requests the DPM to perform a HardReset USBPD_StatusTypeDef (*USBPD_PE_EvaluatPRSwap)(uint8_t hport); Gets evaluation of swap request from DPM void (*USBPD_PE_TurnOnPower)(uint8_t hport); Requests the DPM to turn On power supply void (*USBPD_PE_AssertRd)(uint8_t hport); Requests the DPM to assert Rd void (*USBPD_PE_AssertRp)(uint8_t hport); Requests the DPM to assert Rp void (*USBPD_PE_ExplicitContractDone)(uint8_t hport); Informs DPM that an Explicit contract is established USBPD_StatusTypeDef (*USBPD_VDM_DiscoverIdentity)(uint8_t hport); Reports to the DPM the result of Discover Identity USBPD_StatusTypeDef (*USBPD_VDM_DiscoverSVIDs)(uint8_t hport); Reports to the DPM the result of discover SVIDs USBPD_StatusTypeDef (*USBPD_VDM_DiscoverModes)(uint8_t hport); Reports to the DPM the result of Discover Modes USBPD_StatusTypeDef (*USBPD_VDM_ModeEnter)(uint8_t hport); Reports to the DPM the result of Mode Entry USBPD_StatusTypeDef (*USBPD_VDM_ModeExit)(uint8_t hport, USBPD_VDM_CommandType_Typedef cmdtpe); Reports to the DPM the result of Mode Exit USBPD_StatusTypeDef (*USBPD_VDM_Attention)(uint8_t hport); Reports to the VDM that an attention command is being processed void (*USBPD_VDM_HardReset)(uint8_t hport); Requests the DPM to perform a Hard Reset 20/30 DocID029258 Rev 1 UM2063 Examples description 5 Examples description 5.1 Hardware description The P-NUCLEO-USB001 shield is needed as an expansion board for the STM32F072RB Nucleo in order to use the library. The P-NUCLEO-USB001 shield is an expansion board with two USB Type-C connectors for management of two ports, with the following features: • two dual role ports • dedicated power connector to interface with external power supply board providing different profiles (up to 20 V and 5 A) and VCONN • on-board power management, able to provide internal needed voltages from VBUS • six debug LEDs • USB 2.0 interface capability available on one port • compatible with STM32 Nucleo boards • equipped with ST morpho connectors P-NUCLEO-USB001 shield must be connected on CN7 and CN10 connectors of the Nucleo board. Figure 6. STM32FO72RB Nucleo with P-Nucleo-USB001 shield DocID029258 Rev 1 21/30 29 Examples description UM2063 For all the examples, user has to • open EWARM toolchain • rebuild all files and load the image into target memory • run the application. The USB-PD applications can be used with any USB-C device that is Power Delivery capable, according to its specific power role (provider, consumer, DRP). 5.2 USB-PD provider 5.2.1 Example setup The USB-PD provider application can be found under: “.\Projects\STM32F072_Nucleo\Applications\Provider_RTOS\” The provider role can be managed with two different supply options that correspond to two several configuration settings. The provider is supplied by the on-board NUCLEO-F072RB voltage regulator, by mean of a USB Type-A to Mini-B cable plugged to the CN1 connector and then to a PC. On NUCLEO-F072RB board, verify that the jumper JP1 is open, JP5 (PWR) closed on U5V (fitting the pins 1-2), and JP6 (IDD) closed. Following setting will permit to manage the VBUS on the selected port, starting from the NUCLEOF072RB USB PWR voltage (CN1 connector). • If the provider is equipped with an external board by power connector CN4: – on the NUCLEO-F072RB board, the following jumper settings must be guaranteed: JP1 closed, JP5 (PWR) closed on E5V (fitting the pins 2-3), and JP6 (IDD) closed – on the P-NUCLEO-USB001 expansion board, the jumpers J500 and JP501 must be left open. This setting configuration will permit to the external power board to supply the entire system and, particularly for the USB-PD application, to offer a voltage level for the VBUS of the port. 5.2.2 Application description When the application starts, connecting an USB-PD consumer device (Sink mode) should trig the power negotiation: • at start, Role LED (LED D203) will be blinking until cable attach and VBUS green LED (D 204) is off. User should plug the USB-C cable on the dedicated connector: • when attached, CC LEDs (D202, D205) will blink once if connected on CC1, twice if connected on CC2 • blue LED (D203) will be blinking one time, to show that the device behaves as a provider. The STM32 MCU behaves as a provider (Source mode), it exchanges Power profiles with the connected device and waits for Power Request message from the attached consumer: • 22/30 While Communicating, VBUS LED (D204) will be blinking. DocID029258 Rev 1 UM2063 Examples description If the requested power can be met, the STM32 MCU shall send the Accept message followed by PS_RDY message: • once the Explicit Contract established, VBUS LED (D 204) is on to indicate that the Power Contract was established. 5.3 USB-PD consumer 5.3.1 Example setup The USB-PD consumer application could be found under: “.\Projects\STM32F072_Nucleo\Applications\Consumer_RTOS\” The system can manage two supply options for the consumer configuration. The first one is supplied by NUCLEO-F072RB, while the second implements a specific feature of the USB-PD solutions (i.e. when a consumer is supplied by the provider by mean of its VBUS). Both configurations correspond to two different setting: If the consumer is supplied by mean of NUCLEO-F072RB voltage regulator, the system setting is the following one: • on NUCLEO-F072RB board, verify that the jumper JP1 is open, JP5 (PWR) closed on U5V (fitting the pins 1-2), and JP6 (IDD) closed • on P-NUCLEO-USB001 expansion board, open the jumpers JP100, J500, JP501. If the consumer is supplied by mean of the VBUS delivered by the provider attached by the USB Type-C cable, the system setting is the following one: 5.3.2 • on the NUCLEO-F072RB board, the jumper JP1 must be closed, JP5 (PWR) closed on E5V (fitting the pins 2-3), and JP6 (IDD) closed • on P-NUCLEO-USB001 expansion board, while the jumpers J500, JP501 are opened, the jumper JP100 must be set according to the port chosen for supplying the system (fit 2-3 for PORT_0 or 1-2 for PORT_1). Application description When the application starts, connecting an USB-C provider device (source mode) triggers the power negotiation: • at start, Role LED (LED D203) will be blinking until cable attach and VBUS green LED (D 204) is off. User should plug the USB-C cable on the dedicated connector: • when attached, CC LEDs (D202, D205) will blink once if connected on CC1, twice if connected on CC2 • blue LED (D203) will be blinking twice each time, to show that the device behaves as a consumer. The STM32 MCU behaves as a consumer (Sink mode), it waits for Power. Capabilities message from the attached provider. When a Source Capabilities message is received, the STM32 starts the evaluation of the received capabilities and check if one of the received power objects can meet its power requirement: • while communicating, VBUS LED (D204) will be blinking. DocID029258 Rev 1 23/30 29 Examples description UM2063 The STM32 shall send the Request message to request the new power level from the offered Source Capabilities: • once the Explicit Contract established (PS_Ready) message received), VBUS LED (D 204) is on to indicate that the Power Contract is established. 5.4 USB-PD provider DRP 5.4.1 Example setup The USB-PD Provider DRP application could be found under: “.\Projects\STM32F072_Nucleo\Applications\Provider_DRP_RTOS\” If the provider DRP is supplied by the NUCLEO-F072RB voltage regulator, the system setting is the following one: • on NUCLEO-F072RB board, verify that the jumper JP1 is open, JP5 (PWR) closed on U5V (fitting the pins 1-2), and JP6 (IDD) closed • on P-NUCLEO-USB001 expansion board, open the jumpers JP100, J500, JP501. Following setting will permit to manage the VBUS on the selected port, starting from the NUCLEOF072RB USB PWR voltage (CN1 connector). • 5.4.2 If the provider DRP is equipped with an external board by power connector CN4: – on the NUCLEO-F072RB board, the jumper JP1 must be closed, JP5 (PWR) closed on E5V (fitting the pins 2-3), and JP6 (IDD) closed – on P-NUCLEO-USB001 expansion board, while the jumpers J500, JP501 are opened, the jumper JP100 must be set according to the port chosen for supplying the system (fit 2-3 for PORT_0 or 1-2 for PORT_1). Application description In this application the STM32 behaves as a provider (default mode), a power profile negotiation occurs on initial attachment of a port pairs: • when attached, CC LEDs (D202, D205) will blink once if connected on CC1, twice if connected on CC2 • blue LED (D203) will be blinking one time, to show that the device behaves as a provider. Port pairs are required to negotiate an Explicit Contract, if there is one in place, user can start a power role swap process by pressing USER button B1 in the Nucleo board: • when the Explicit Contract is established, VBUS LED (D 204) is on to indicate that the Power Contract was established. The power role swap message shall only be sent and received after an Explicit Contract has been established between provider and consumer: • 24/30 when the swap process is completed, blue LED (D203) will be blinking twice each time, to show that the device behaves now as a consumer. DocID029258 Rev 1 UM2063 Examples description 5.5 USB-PD consumer DRP 5.5.1 Example setup The USB-PD consumer DRP application can be found under: “.\Projects\STM32F072_Nucleo\Applications\Consumer_DRP_RTOS\” If the consumer DRP is supplied by mean of NUCLEO-F072RB voltage regulator, the system setting is the following one: • on NUCLEO-F072RB board, verify that the jumper JP1 is open, JP5 (PWR) closed on U5V (fitting the pins 1-2), and JP6 (IDD) closed • on P-NUCLEO-USB001 expansion board, open the jumpers JP100, J500, JP501. If the consumer DRP is supplied by mean of the VBUS delivered by the provider attached by the USB Type-C cable, the system setting is the following one: 5.5.2 • on the NUCLEO-F072RB board, the jumper JP1 must be closed, JP5 (PWR) closed on E5V (fitting the pins 2-3), and JP6 (IDD) closed • on P-NUCLEO-USB001 expansion board, while the jumpers J500, JP501 are opened, the jumper JP100 must be set according to the port chosen for supplying the system (fit 2-3 for PORT_0 or 1-2 for PORT_1). Application description In this application the STM32 behaves as a consumer (default mode), a power profile negotiation occurs on initial attachment of a port pairs: • when attached, CC LEDs (D202, D205) will blink once if connected on CC1, twice if connected on CC2 • blue LED (D203) will be blinking twice each time, to show that the device behaves as a consumer. Port pairs are required to negotiate an Explicit Contract, if there is one in place, user can start a power role swap process by pressing USER button B1 in the Nucleo board: • when the Explicit Contract is established, VBUS LED (D 204) is on to indicate that the Power Contract is established. The power role swap message shall only be sent and received after an Explicit Contract has been established between consumer and provider: • when the swap process is completed, blue LED (D203) will be blinking twice each time, to show that the device behaves now as a consumer. DocID029258 Rev 1 25/30 29 Memory footprint 6 UM2063 Memory footprint The values in tables 9 to 11 are calculated according to the following configuration: • Compiler: IAR Embedded Workbench® for ARM®, Version 7.50.1 • Optimization: high speed • MCU: STM32F072RB • Expansion board: P-NUCLEO-USB001 shield Table 9. USB-PD - Provider memory footprint Provider (RTOS-based) Project Description Flash memory (bytes) RAM (bytes) USB-PD library 30367 3420 Memory needed for the USB-PD library Application layer 1218 128 DRP, user application and LEDs management FreeRTOS 3320 4324 FreeRTOS memory use HouseKeeping 1300 1029 Memory allocation, arithmetic libraries Total 36205 8901 Total memory Table 10. USB-PD - Consumer memory footprint Consumer (RTOS-based) Project 26/30 Description Flash memory (bytes) RAM (bytes) USB-PD library 30341 3420 Memory needed for the USB-PD library Application layer 1022 128 DRP, user application and LEDs management FreeRTOS 3319 4324 FreeRTOS memory use HouseKeeping 1300 1541 Memory allocation, arithmetic libraries Total 35982 9413 Total memory DocID029258 Rev 1 UM2063 Memory footprint Table 11. USB-PD - Dual role port memory footprint DRP (RTOS-based) Project Note: Description Flash memory (bytes) RAM (bytes) USB-PD library 32259 3420 Memory needed for the USB-PD library Application layer 1323 128 DRP, user application and LEDs management FreeRTOS 3321 4324 FreeRTOS memory use HouseKeeping 1300 1541 Memory allocation, arithmetic libraries Total 38203 9413 Total memory DRP values are valid for both provider DRP and consumer DRP. DocID029258 Rev 1 27/30 29 Frequently asked questions (FAQs) 7 UM2063 Frequently asked questions (FAQs) How can I get the STM32 USB-PD library? The library is provided for free download in a binary format, from www.st.com. Does the library support USB data communication? The library is responsible only of PD communication, however, the first port can carry USB data. It is possible to add the STM32 USB library to allow USB communication through the first port. I want to use only the USB-C feature (cable detachment attachment and cable orientation). Is this possible? Yes, this is possible since the CAD (Cable Attachement and Detachement) module and the PD communication are driven by two separated processes. You can call only the CAD process to ensure cable detection. Does the library work on others platforms than F0? Only STM32F0 platforms support the library for now, the stack will be implemented for other STM32 microcontrollers in the future. Using a provider, how can I power a consumer port partner that needs voltage values than 5 V? For providing higher voltage values (up to 20 V), an external power board must be connected to P-NUCLEO-USB001 shield through connector CN4 (refer to UM2050 for more details). 28/30 DocID029258 Rev 1 UM2063 8 Revision history Revision history Table 12. Document revision history Date Revision 08-Jun-2016 1 Changes Initial release. DocID029258 Rev 1 29/30 29 UM2063 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. 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. © 2016 STMicroelectronics – All rights reserved 30/30 DocID029258 Rev 1