Skip to content

Commit

Permalink
PHY configuration (Arduino IDE compatible)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Apr 4, 2024
1 parent b25d12e commit cf6e4d5
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 92 deletions.
12 changes: 10 additions & 2 deletions dev/ListCapabilities/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@
#include <Wire.h>
#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 void handleEvent(PDSinkEventType eventType);
static void listCapabilities();
Expand All @@ -41,8 +44,13 @@ void setup() {
delay(10);
Serial.println("USB BP for Arduino - List Capabilities");

// 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);
Expand Down
9 changes: 7 additions & 2 deletions dev/VoltageChange/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@
#include <Wire.h>
#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 void handleEvent(PDSinkEventType eventType);
static void switchVoltage();
Expand All @@ -51,6 +54,8 @@ void setup() {
Wire.begin(SDA, SCL, 1000000);
pdPhy.setTwoWire(&Wire);
pdPhy.setInterruptPin(10);
#elif defined(ARDUINO_ARCH_STM32)
pdPhy.setInstance(1);
#endif

sink.start(handleEvent);
Expand Down
10 changes: 10 additions & 0 deletions src/USBPowerDelivery.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
#include "PDSink.h"
#include "PDProtocolAnalyzer.h"


#define USB_PD_PHY_FUSB302 100
// Use UCPD1 only (instance 1)
#define USB_PD_PHY_UCPD1 201
// Use UCPD2 only (instance 2)
#define USB_PD_PHY_UCPD2 202
// Use UCPD1 and UCPD2 (instance 1 and 2)
#define USB_PD_PHY_UCPD1_2 203


#if defined(ARDUINO_ARCH_ESP32)

#include "phy/ESP32FUSB302/PDPhyFUSB302.h"
Expand Down
22 changes: 22 additions & 0 deletions src/USBPowerDeliveryCode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// USB Power Delivery for Arduino
// Copyright (c) 2024 Manuel Bleichenbacher
//
// Licensed under MIT License
// https://opensource.org/licenses/MIT
//

// Code that needs to be included in a single .cpp file as it is not just a header file
// with declarations but provides actual implementation.

#if USB_PD_PHY == USB_PD_PHY_UCPD1 || USB_PD_PHY == USB_PD_PHY_UCPD2 || USB_PD_PHY == USB_PD_PHY_UCPD1_2

#include "phy/STM32UCPD/STM32UCPDInterruptHandler.h"

#elif USB_PD_PHY == USB_PD_PHY_FUSB302

// nothing further to include

#else
#error "Invalid or missing value for USB_PD_PHY. Please define USB_PD_PHY before including this file."
#endif
Loading

0 comments on commit cf6e4d5

Please sign in to comment.