Power Delivery Board - USB-C (Qwiic) Hookup Guide

Pages
Contributors: Alex the Giant, Ell C
Favorited Favorite 5

Arduino Library

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

SparkFun has written a library to control the Power Delivery board for the STUSB4500. You can obtain this library through the Arduino Library Manager. By searching for STUSB4500, you should see one written by SparkFun Electronics and you should be able to install the latest version. If you prefer downloading libraries manually, you can grab them from the GitHub Repository.

Functions

The library has a ton of functions available for reading each of the NVM parameters for the Power Delivery board. Below is a list of the functions available along with a description of what each function does and how to use it.

  • uint8_t begin( uint8_t deviceAddress, TwoWire &wirePort ) - Call at the beginning of the sketch to initialize the device. This function takes two optional parameters: deviceAddress and wirePort. This function will return true when the device is intialized, and false if it is unable to communicate with the device.

    • wirePort - By default, the library uses the standard Wire bus, but if your board has multiple I2C buses, you can define Wire1 here.

    • deviceAddress - If both of the address jumpers on the back of the board are closed, this parameter does not need to be provided. Otherwise, you can use the table below to get the I2C address of the Power Delivery Board.

ADDR0 ADDR1 Device Address
Closed Closed 0x28 (default)
Open Closed 0x29
Closed Open 0x2A
Open Open 0x2B


  • void read( void ) - This function reads the NVM parameters and loads them into memory. This function is called in the begin() function, and shouldn't need to be called in your program.

  • void write( uint8_t defaultVals ) - This function writes the NVM parameters to the STUSB4500. After all of the parameters have been changed, calling this function will save the parameters to the device. This function takes an optional argument of DEFAULT. When DEFAULT is passed to the write function, the default NVM parameters are written to the device.

  • void softReset( void ) - This function performs a soft reset of the STUSB4500. After a soft reset power is re-negotiated with the source using the values loaded in the PDO registers. To see this function in use, refer to Example4-ChangingVoltages in the Arduino library, or the “Changing Voltages on the Fly” example below.

  • float getVoltage( uint8_t pdo_numb ) - Returns the voltage requested for the PDO number (1-3).

  • float getCurrent( uint8_t pdo_numb ) - Returns the current requested for the PDO number (1-3).

  • uint8_t getLowerVoltageLimit( uint8_t pdo_numb ) - Returns the under voltage lockout parameter for the PDO number (1-3). PDO1 has a fixed value that cannot be changed and will always return 0. The under voltage limit is fixed at 3.3V. PDO2 and PDO3 will return a value between 5% and 20%.

  • uint8_t getUpperVoltageLimit( uint8_t pdo_numb ) - Returns the over voltage lockout parameter for the PDO number (1-3). The value returned will be between 5% and 20%.

  • float getFlexCurrent( void ) - Return the value for the flex current parameter, which is a global variable common to all PDOs. Refer to setFlexCurrent function for more information on how to use flex current.

  • uint8_t getPdoNumber( void ) - Returns value saved in memory for the highest priority PDO number (1-3).

  • uint8_t getExternalPower( void ) - Returns the value for the SNK_UNCONS_POWER parameter (0 or 1). SNK_UNCONS_POWER is the unconstrained power bit setting in capabilities message sent by the sink. A 0 means there is no external source of power (default), and a 1 means an external source of power is available and is sufficient to adequately power the system while charging external devices.

  • uint8_t getUsbCommCapable( void ) - Returns the value for the USB_COMM_CAPABLE parameter (0 or 1). USB_COMM_CAPABLE refers to USB2.0 or 3.x data communication capability by the sink system. A 0 means the sink device does not support data communication (default). A 1 means that the sink does support data communication.

  • uint8_t getConfigOkGpio( void ) - Returns the POWER_OK_CFG parameter value (0-3), which controls the behavior of the VBUS_EN_SNK, POWER_OK2, and POWER_OK3 pins. Refer to the setConfigOKGPIO function for more information.

    • 0 - Configuration 1
    • 1 - Not applicable
    • 2 - Configuration 2 (default)
    • 3 - Configuration 3
  • uint8_t getGpioCtrl( void ) - Returns the behavior setting for the GPIO pin (0-3). Refer to the setGpioCtrl function for more information.

    • 0 - Software controlled GPIO
    • 1 - Error recovery
    • 2 - Debug
    • 3 - Sink power
  • uint8_t getPowerAbove5vOnly( void ) - Returns the POWER_ONLY_ABOVE_5V parameter (0 or 1). If 0 is returned, the output will be enabled when the source is attached, regardless of the voltage. If 1 is returned, the output will be enabled only when the source is attached and VBUS voltage is negotiated to PDO2 or PDO3.

  • void setPdoNumber( uint8_t pdo_numb ) - Takes the parameter pdo_numb (1-3). There are three Power Data Objects (PDO) that the Power Delivery board can store. PDO3 has the highest priority, followed by PDO2, and finally PDO1. This function declares which of the three PDOs should be negotiated first. For example, if setPdoNumber is set to 3, PDO3 will be negotiated first, followed by PDO2 if a contract is not made, and finally PDO1 if PDO2 fails as well. If setPdoNumber is set to 2, PDO3 will be ignored and PDO2 will be negotiated, followed by PDO1. Lastly if setPdoNumber is set to 1, only PDO1 will be negotiated.

  • void setVoltage( uint8_t pdo_numb, float voltage ) - Takes two parameters: pdo_numb (1-3), and voltage (10-bit resolution). PDO1 is fixed at 5V and cannot be changed. PDO2 and PDO3 can be any voltage up to 20V.

    • The PDO3 has the highest priority and PDO1 has the lowest priority, starting at the number defined in the setPdoNumber()parameter. The source controls the voltage present on VBUS, or in the case of the Power Delivery board, VIN. Common voltages available from sources are: 5V, 9V, 12V, 15V, and 20V.
  • void setCurrent( uint8_t pdo_numb, float current ) - Takes two parameters: pdo_numb (1-3), and current (16 values). The current values are: 0 (Flex Current), 0.5A, 0.75A, 1.0A, 1.25A, 1.50A, 1.75A, 2.0A, 2.25A. 2.50A, 2.75A, 3.0A, 3.5A, 4.0A, 4.5A, and 5.0A.

    • The current value should be the amount that the source should be expected to output. If the current is higher than the source is able to provide, the contract will be rejected and the next PDO contract will be negotiated. The Power Delivery board will sink more current than negotiated if the source is able deliver it, but it's recommended to provide the maximum amount of current the project is expected to draw.
  • setFlexCurrent( float value ) - Takes a float value to set the current common to all PDOs. This value is only used in the power negotiation if the setCurrent value for that PDO is set to 0. The flex current has a resolution of 10mA. Just like the setCurrent function, the current value should be the amount that the source should be expected to deliver. If the current is higher than the source is able to provide, the contract will be rejected and the next PDO contract will be negotiated.

  • setLowerVoltageLimit (uint8_t pdo_numb, uint8_t value ) - Takes two parameters: pdo_numb (2-3), and a integer value (5-20%). Only PDO2 and PDO3 can be changed as PDO1 has a fixed value of 3.3V. In combination with the setUpperVoltageLimit function, this is used to define an acceptable window for the voltage output.

    • For example if the Power Delivery board requests 15V and has a under voltage tolerance of 5%, the controller will disable the output of the Power Delivery board if the voltage drops below 14.25V, which is 5% of 15V.
  • setUpperVoltageLimit( uint8_t pdo_numb, uint8_t value ) - Takes two parameters: pdo_numb (1-3), and a integer value (5-20%). In combination with the setLowerVoltageLimit function, this is used to define an acceptable window for the voltage output.

    • For example if the Power Delivery board requests 15V and has a over voltage tolerance of 5%, the controller will disable the output of the Power Delivery board if the voltage exceeds 15.75V, which is 5% of 15V.
  • setExternalPower( uint8_t value ) - Takes an integer value (0 or 1). Setting to 0, there is no external source of power. Setting to 1, means there is an external power source available, and is sufficient to adequately power the system while charging external devices.

  • setUsbCommCapable( uint8_t value ) - Takes an integer value (0 or 1). Setting to 0, there sink does not support data communication. Setting to 1, means the sink does support data communication.

  • setConfigOkGpio( uint8_t value ) - Takes an integer value (0-3):

    • 0 - Configuration 1
    • 1 - Not applicable
    • 2 - Configuration 2 (default)
    • 3 - Configuration 3
Pin State
Description
Configuration1 VBUS_EN_SNK Hi-Z No Source Attached
0 Source Attached
POWER_OK2 Hi-Z No functionality
POWER_OK3 Hi-Z No functionality
Configuration2 VBUS_EN_SNK Hi-Z No Source Attached
0 Source Attached
POWER_OK2 Hi-Z No explicit PD contract
0 PD explicit contract with PDO2
POWER_OK3 Hi-Z No explicit PD contract
0 PD explicit contract with PDO3
Configuration3 VBUS_EN_SNK Hi-Z No Source Attached
0 Source Attached
POWER_OK2 Hi-Z No source attached or source supplies default USB Type-C current at 5V when source attached.
0 Source supplies 3.0A USB Type-C current at 5V when source is attached.
POWER_OK3 Hi-Z No source attached or source supplies default USB Type-C current at 5V when source attached.
0 Source supplies 1.5A USB Type-C current at 5V when source is attached.

*

Note: For configuration 3: "Source supplies 1.5A/3.0A USB Type-C current at 5V when source is attached" is based on what the source advertises when the cable is connected and does not indicate that the output voltage is actually 5V.
  • setGpioCtrl( uint8_t value ) - Takes an integer value (0-3) to control the GPIO pin (active LOW).
Value GPIO Function Function Value Description
0 Software Controlled GPIO The output state is controlled by the value stored in bit-0 of I2C register 0x2D Hi-Z When the value of bit-0 is 0 (default at start-up)
0 When the value of bit-0 is 1
1 Error Recovery Hardware fault detection such as over temperature, over voltage on the CC pins, or after a hard reset Hi-Z No hardware fault detected
0 Hardware fault detected
2 Debug Debug accessory detection (Refer to section 3.8 of datasheet for more information) Hi-Z No debug accessory detected
0 Debug accessory detected
3 Sink Power Indicates USB Type-C current capability advertised by the source Hi-Z Source supplies default or 1.5A USB Type-C current at 5V
0 Source supplies 3.0A USB Type-C current at 5V

*

Note: For value 3 - Sink Power: "Source supplies 1.5A/3.0A USB Type-C current at 5V when source is attached" is based on what the source advertises when the cable is connected and does not indicate that the output voltage is actually 5V.
  • setPowerAbove5vOnly( uint8_t value ) - Takes an integer value (0 or 1). Setting to 0, the output is enabled regardless of the of the negotiated voltage. When set to 1, the output is enabled only when the source is attached, and the voltage is negotiated for PDO2 or PDO3.

  • setReqSrcCurrent( uint8_t value ) - Takes an integer value (0 or 1). Setting to 0 requests the sink current as operating current in the RDO message. Setting to 1 requests the source current as operating current in the RDO message.