Rewrite of Arduino driver STUSB4500
- Re-written entirely in pure Go
- Improved stability and compatibility over Arduino predecessor STUSB4500
- Tested on TinyGo 1.14, 1.15, 1.16
- Compatible with all I²C-capable devices supported by TinyGo
- USB Power Delivery v2.0 and v3.0 compatible state machine
- Designed for STUSB4500 Compact Breakout
- Same chip used on the SparkFun Power Delivery Board, although it hasn't been tested. Feel free to send me one!
- Detect cable orientation (CC1/CC2 termination)
- Completely enable/disable power output
- Provides user callback support for:
Callback Description OnInitFailI²C initialization failure OnResetFailI²C reconnection to STUSB4500 failed OnConnectI²C connection to STUSB4500 succeeded OnConnectFailI²C connection to STUSB4500 failed OnErrorI²C or STUSB4500 runtime error OnCableAttachUSB Type-C cable connected OnCableDetachUSB Type-C cable disconnected OnCapabilitiesUSB PD capabilities received from source - Can analyze and identify all available power profiles
- Can request and use any arbitrary power profile +5-20V/0-5A in 50mV/10mA increments
- Does not lose VBUS on power transition, maintains power output
- Set up to 3 custom power profiles for fallback
- Reformat NVM for default power profiles in standalone mode (not yet implemented)
- Install TinyGo, of course.
- Fetch the TinyGo drivers:
go get tinygo.org/x/drivers
- If this driver has been merged into that repository, you are done with installation.
- Navigate into new
driversinstallation directory:cd $( go env GOPATH )/src/tinygo.org/x/drivers
- Fetch this driver, naming it
stusb4500:git clone https://github.com/ardnew/tinygo-stusb4500.git stusb4500
An example program is provided at examples/main.go, which performs the following described in its header comments:
// This example demonstrates how to use the STUSB4500 driver to monitor cable
// attach/detach, read the USB PD source capabilities, and negotiate different
// power profiles.
//
// An external button is used to cycle through each of the power profiles
// discovered.
//
// An LED is used to indicate USB PD negotiation activity.
//
// Note that your STUSB4500 should be powered via VBUS, which is connected to
// the USB Type-C cable itself, and NOT via VSYS/VCC (which should be connected
// to ground).- TBD
The PD protocol has very rigid timing requirements. These can be difficult to accommodate even in normal circumstances, but is even more difficult since there is another device (our STUSB4500) mediating communication over an I²C bus, which is rather slow. This is partly why the interrupts are vital to successful operation.
Point being, it may be difficult to achieve reliable results on some systems (such as AVR-based devices like Arduino Uno), and you may need to experiment with different microcontrollers.
If you have issues receiving cable attach/detach or PD capability discovery events, ensure the following:
- You are using a hardware I²C port on the microcontroller
- The I²C (SDA/SCL) and interrupt (ALRT/ATCH) wires connecting microcontroller and STUSB4500 are as short as possible
- GND on STUSB4500 is connected to GND on microcontroller
- VDD (VPP on oxplot breakout) is pulled up to 3.3V (or to same level as I2C bus voltage)
- VSYS (VCC on oxplot breakout) is pulled down to ground (use microcontroller ground pin)
- The Sparkfun breakout pulls this high to VDD (according to their schematic), but the datasheet says to connect it to ground if it is not used (and it isn't used, because the device is powered by VBUS from the USB-C receptacle). So I'm not sure about this one. Try pulling this in the opposite direction if you're having issues.