Skip to content

Commit

Permalink
PHY configuration (STM32 G4)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Apr 4, 2024
1 parent cf6e4d5 commit 2eeccf7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 45 deletions.
17 changes: 15 additions & 2 deletions examples/ListCapabilities/ListCapabilities.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@

#include "USBPowerDelivery.h"


#if defined(ARDUINO_ARCH_ESP32)

typedef PDPhyFUSB302 PDPhy;
#define USB_PD_PHY USB_PD_PHY_FUSB302

#elif defined(ARDUINO_ARCH_STM32)

#if defined(STM32G0xx) || defined(STM32G4xx)
typedef PDPhySTM32UCPD PDPhy;
#endif
#define USB_PD_PHY USB_PD_PHY_UCPD1

#endif

#include "USBPowerDeliveryCode.h"


static PDPhy pdPhy;
static PDController<PDPhy> powerController(&pdPhy);
Expand All @@ -35,6 +38,16 @@ static PDSink<PDController<PDPhy>> sink(&powerController);

void setup() {
Serial.begin(115200);

// configure PHY (if needed)
#if defined(ARDUINO_ARCH_ESP32)
Wire.begin(SDA, SCL, 1000000);
pdPhy.setTwoWire(&Wire);
pdPhy.setInterruptPin(10);
#elif defined(ARDUINO_ARCH_STM32)
pdPhy.setInstance(1);
#endif

sink.start(handleEvent);

// Uncomment if using X-NUCLEO-SNK1MK1 shield
Expand Down
17 changes: 15 additions & 2 deletions examples/TriggerBoard/TriggerBoard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,37 @@

#include "USBPowerDelivery.h"


#if defined(ARDUINO_ARCH_ESP32)

typedef PDPhyFUSB302 PDPhy;
#define USB_PD_PHY USB_PD_PHY_FUSB302

#elif defined(ARDUINO_ARCH_STM32)

#if defined(STM32G0xx) || defined(STM32G4xx)
typedef PDPhySTM32UCPD PDPhy;
#endif
#define USB_PD_PHY USB_PD_PHY_UCPD1

#endif

#include "USBPowerDeliveryCode.h"


static PDPhy pdPhy;
static PDController<PDPhy> powerController(&pdPhy);
static PDSink<PDController<PDPhy>> sink(&powerController);


void setup() {
// configure PHY (if needed)
#if defined(ARDUINO_ARCH_ESP32)
Wire.begin(SDA, SCL, 1000000);
pdPhy.setTwoWire(&Wire);
pdPhy.setInterruptPin(10);
#elif defined(ARDUINO_ARCH_STM32)
pdPhy.setInstance(1);
#endif

sink.start();
// request 12V @ 1A once power supply is connected
sink.requestPower(12000, 1000);
Expand Down
16 changes: 14 additions & 2 deletions examples/TriggerBoardAdvanced/TriggerBoardAdvanced.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,33 @@
#if defined(ARDUINO_ARCH_ESP32)

typedef PDPhyFUSB302 PDPhy;
#define USB_PD_PHY USB_PD_PHY_FUSB302

#elif defined(ARDUINO_ARCH_STM32)

#if defined(STM32G0xx) || defined(STM32G4xx)
typedef PDPhySTM32UCPD PDPhy;
#endif
#define USB_PD_PHY USB_PD_PHY_UCPD1

#endif

#include "USBPowerDeliveryCode.h"


static PDPhy pdPhy;
static PDController<PDPhy> powerController(&pdPhy);
static PDSink<PDController<PDPhy>> sink(&powerController);


void setup() {
// configure PHY (if needed)
#if defined(ARDUINO_ARCH_ESP32)
Wire.begin(SDA, SCL, 1000000);
pdPhy.setTwoWire(&Wire);
pdPhy.setInterruptPin(10);
#elif defined(ARDUINO_ARCH_STM32)
pdPhy.setInstance(1);
#endif

Serial.begin(115200);
sink.start(handleEvent);

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"license": "MIT",
"frameworks": ["arduino"],
"platforms": ["ststm32", "esp32"],
"headers": ["USBPowerDelivery.h", "PDController.h", "PDSink.h", "PDProtocolAnalyzer.h"],
"headers": ["USBPowerDelivery.h", "USBPowerDeliveryCode.h", "PDController.h", "PDSink.h", "PDProtocolAnalyzer.h"],
"examples": [
{
"name": "Protocol Analyzer",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ paragraph=Build a USB PD protocol analyzer, a trigger board or a more sophistica
category=Device Control
url=https://github.com/manuelbl/usb-pd-arduino
dot_a_linkage=true
includes=USBPowerDelivery.h
includes=USBPowerDelivery.h,USBPowerDeliveryCode.h
architectures=stm32,esp32
57 changes: 20 additions & 37 deletions src/phy/STM32UCPD/PDPhySTM32UCPD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,26 @@


#if defined(STM32G4xx)
const PDPhySTM32UCPD::Peripheral PDPhySTM32UCPD::peripherals[] = {
// STM32G4 family: CC1 -> PB6, CC2 -> PB4
#define GPIO_CC1 GPIOB
#define PIN_CC1 LL_GPIO_PIN_6
#define ARDUINO_PIN_CC1 PB6
#define GPIO_CC2 GPIOB
#define PIN_CC2 LL_GPIO_PIN_4
#define ARDUINO_PIN_CC2 PB4
#define DMA_RX DMA1
#define DMA_CHANNEL_RX LL_DMA_CHANNEL_1
#define DMA_TX DMA1
#define DMA_CHANNEL_TX LL_DMA_CHANNEL_2
#define UCPD_IRQ UCPD1_IRQn
{
.ucpd = UCPD1,
.gpioCC1 = GPIOB,
.gpioCC2 = GPIOB,
.pinCC1 = LL_GPIO_PIN_6,
.pinCC2 = LL_GPIO_PIN_4,
.arduinoPinCC1 = PB6,
.arduinoPinCC2 = PB4,
.dmaRx = DMA1,
.dmaTx = DMA1,
.dmaChannelRx = LL_DMA_CHANNEL_1,
.dmaRequestRx = LL_DMAMUX_REQ_UCPD1_RX,
.dmaChannelTx = LL_DMA_CHANNEL_2,
.dmaRequestTx = LL_DMAMUX_REQ_UCPD1_TX,
.ucpdIrq = UCPD1_IRQn,
.dbatt = 0,
}
};

#elif defined(STM32G0xx)

Expand Down Expand Up @@ -73,34 +81,9 @@ const PDPhySTM32UCPD::Peripheral PDPhySTM32UCPD::peripherals[] = {
}
};

PDPhySTM32UCPD* PDPhySTM32UCPD::instances[] = {0};

// #define GPIO_CC1 GPIOA
// #define PIN_CC1 LL_GPIO_PIN_8
// #define ARDUINO_PIN_CC1 PA8
// #define GPIO_CC2 GPIOB
// #define PIN_CC2 LL_GPIO_PIN_15
// #define ARDUINO_PIN_CC2 PB15
// #define DMA_RX DMA1
// #define DMA_CHANNEL_RX LL_DMA_CHANNEL_1
// #define DMA_TX DMA1
// #define DMA_CHANNEL_TX LL_DMA_CHANNEL_2
// #define UCPD_IRQ UCPD1_2_IRQn

// #define GPIO_CC1 GPIOD
// #define PIN_CC1 LL_GPIO_PIN_0
// #define ARDUINO_PIN_CC1 PD0
// #define GPIO_CC2 GPIOD
// #define PIN_CC2 LL_GPIO_PIN_2
// #define ARDUINO_PIN_CC2 PD2
// #define DMA_RX DMA1
// #define DMA_CHANNEL_RX LL_DMA_CHANNEL_1
// #define DMA_TX DMA1
// #define DMA_CHANNEL_TX LL_DMA_CHANNEL_2
// #define UCPD_IRQ UCPD1_2_IRQn

#endif

PDPhySTM32UCPD* PDPhySTM32UCPD::instances[] = {0};

PDPhySTM32UCPD::PDPhySTM32UCPD() : controller(nullptr), rxMessage(nullptr), ccActive(0), instance(0) {}

Expand Down

0 comments on commit 2eeccf7

Please sign in to comment.