From ac6d81b96b45532f2d780cbce75aab8964b5ba11 Mon Sep 17 00:00:00 2001 From: Manuel Bl Date: Sun, 7 Apr 2024 21:17:17 +0200 Subject: [PATCH] Replace templates with abstract class for PDPhy --- dev/ListCapabilities/src/main.cpp | 10 +-- dev/VoltageChange/src/main.cpp | 12 ++-- .../ListCapabilities/ListCapabilities.ino | 10 +-- examples/TriggerBoard/TriggerBoard.ino | 10 +-- .../TriggerBoardAdvanced.ino | 10 +-- src/PDController.cpp | 65 +++++-------------- src/PDController.h | 36 +++++----- src/PDPhy.h | 54 +++++++++++++++ src/PDProtocolAnalyzer.cpp | 39 +++-------- src/PDProtocolAnalyzer.h | 7 +- src/PDSink.cpp | 53 ++++----------- src/PDSink.h | 7 +- src/phy/ESP32FUSB302/PDPhyFUSB302.cpp | 2 +- src/phy/ESP32FUSB302/PDPhyFUSB302.h | 10 +-- src/phy/STM32UCPD/PDPhySTM32UCPD.cpp | 2 +- src/phy/STM32UCPD/PDPhySTM32UCPD.h | 10 +-- 16 files changed, 155 insertions(+), 182 deletions(-) create mode 100644 src/PDPhy.h diff --git a/dev/ListCapabilities/src/main.cpp b/dev/ListCapabilities/src/main.cpp index c217dad..894cd50 100644 --- a/dev/ListCapabilities/src/main.cpp +++ b/dev/ListCapabilities/src/main.cpp @@ -17,12 +17,12 @@ #if defined(ARDUINO_ARCH_ESP32) -typedef PDPhyFUSB302 PDPhy; +typedef PDPhyFUSB302 Phy; #define USB_PD_PHY USB_PD_PHY_FUSB302 #elif defined(ARDUINO_ARCH_STM32) -typedef PDPhySTM32UCPD PDPhy; +typedef PDPhySTM32UCPD Phy; #define USB_PD_PHY USB_PD_PHY_UCPD1 #endif @@ -34,9 +34,9 @@ static void handleEvent(PDSinkEventType eventType); static void listCapabilities(); static const char* getSupplyTypeName(PDSupplyType type); -static PDPhy pdPhy; -static PDController powerController(&pdPhy); -static PDSink> sink(&powerController); +static Phy pdPhy; +static PDController powerController(&pdPhy); +static PDSink sink(&powerController); void setup() { Serial.begin(115200); diff --git a/dev/VoltageChange/src/main.cpp b/dev/VoltageChange/src/main.cpp index 8b316b9..05a49c4 100644 --- a/dev/VoltageChange/src/main.cpp +++ b/dev/VoltageChange/src/main.cpp @@ -17,12 +17,12 @@ #if defined(ARDUINO_ARCH_ESP32) -typedef PDPhyFUSB302 PDPhy; +typedef PDPhyFUSB302 Phy; #define USB_PD_PHY USB_PD_PHY_FUSB302 #elif defined(ARDUINO_ARCH_STM32) -typedef PDPhySTM32UCPD PDPhy; +typedef PDPhySTM32UCPD Phy; #define USB_PD_PHY USB_PD_PHY_UCPD1 #endif @@ -38,10 +38,10 @@ static bool isUSBPDSource = false; static uint32_t nextVoltageChangeTime = 0; static int voltageIndex = 0; -static PDPhy pdPhy; -static PDController powerController(&pdPhy); -static PDProtocolAnalyzer> protocolAnalyzer(&powerController); -static PDSink> sink(&powerController); +static Phy pdPhy; +static PDController powerController(&pdPhy); +static PDProtocolAnalyzer protocolAnalyzer(&powerController); +static PDSink sink(&powerController); void setup() { Serial.begin(115200); diff --git a/examples/ListCapabilities/ListCapabilities.ino b/examples/ListCapabilities/ListCapabilities.ino index 8e22682..d845d49 100644 --- a/examples/ListCapabilities/ListCapabilities.ino +++ b/examples/ListCapabilities/ListCapabilities.ino @@ -18,12 +18,12 @@ #if defined(ARDUINO_ARCH_ESP32) -typedef PDPhyFUSB302 PDPhy; +typedef PDPhyFUSB302 Phy; #define USB_PD_PHY USB_PD_PHY_FUSB302 #elif defined(ARDUINO_ARCH_STM32) -typedef PDPhySTM32UCPD PDPhy; +typedef PDPhySTM32UCPD Phy; #define USB_PD_PHY USB_PD_PHY_UCPD1 #endif @@ -31,9 +31,9 @@ typedef PDPhySTM32UCPD PDPhy; #include "USBPowerDeliveryCode.h" -static PDPhy pdPhy; -static PDController powerController(&pdPhy); -static PDSink> sink(&powerController); +static Phy pdPhy; +static PDController powerController(&pdPhy); +static PDSink sink(&powerController); void setup() { diff --git a/examples/TriggerBoard/TriggerBoard.ino b/examples/TriggerBoard/TriggerBoard.ino index 9ed4872..d423f87 100644 --- a/examples/TriggerBoard/TriggerBoard.ino +++ b/examples/TriggerBoard/TriggerBoard.ino @@ -20,12 +20,12 @@ #if defined(ARDUINO_ARCH_ESP32) -typedef PDPhyFUSB302 PDPhy; +typedef PDPhyFUSB302 Phy; #define USB_PD_PHY USB_PD_PHY_FUSB302 #elif defined(ARDUINO_ARCH_STM32) -typedef PDPhySTM32UCPD PDPhy; +typedef PDPhySTM32UCPD Phy; #define USB_PD_PHY USB_PD_PHY_UCPD1 #endif @@ -33,9 +33,9 @@ typedef PDPhySTM32UCPD PDPhy; #include "USBPowerDeliveryCode.h" -static PDPhy pdPhy; -static PDController powerController(&pdPhy); -static PDSink> sink(&powerController); +static Phy pdPhy; +static PDController powerController(&pdPhy); +static PDSink sink(&powerController); void setup() { diff --git a/examples/TriggerBoardAdvanced/TriggerBoardAdvanced.ino b/examples/TriggerBoardAdvanced/TriggerBoardAdvanced.ino index 6822aba..60f6559 100644 --- a/examples/TriggerBoardAdvanced/TriggerBoardAdvanced.ino +++ b/examples/TriggerBoardAdvanced/TriggerBoardAdvanced.ino @@ -19,12 +19,12 @@ #if defined(ARDUINO_ARCH_ESP32) -typedef PDPhyFUSB302 PDPhy; +typedef PDPhyFUSB302 Phy; #define USB_PD_PHY USB_PD_PHY_FUSB302 #elif defined(ARDUINO_ARCH_STM32) -typedef PDPhySTM32UCPD PDPhy; +typedef PDPhySTM32UCPD Phy; #define USB_PD_PHY USB_PD_PHY_UCPD1 #endif @@ -32,9 +32,9 @@ typedef PDPhySTM32UCPD PDPhy; #include "USBPowerDeliveryCode.h" -static PDPhy pdPhy; -static PDController powerController(&pdPhy); -static PDSink> sink(&powerController); +static Phy pdPhy; +static PDController powerController(&pdPhy); +static PDSink sink(&powerController); void setup() { diff --git a/src/PDController.cpp b/src/PDController.cpp index 0432635..21d4e00 100644 --- a/src/PDController.cpp +++ b/src/PDController.cpp @@ -11,33 +11,28 @@ #include "TaskScheduler.h" -template -PDController::PDController(Phy* phy) : +PDController::PDController(PDPhy* phy) : ccPin(0), eventHandler(nullptr), phy(phy), isMonitorOnly(true), isPhyGoodCrc(false), rxMessageHead(rxBuffer), txMessage((PDMessage*)txBuffer), txMessageId(0), txRetryCount(0), lastRxMessageId(-1), lastSpecRev(1), lastMessage(nullptr) {} -template -void PDController::setGoodCrcHandling(bool phyGoodCrc) { +void PDController::setGoodCrcHandling(bool phyGoodCrc) { isPhyGoodCrc = phyGoodCrc; } -template -void PDController::startController(EventHandlerFunction handler) { +void PDController::startController(EventHandlerFunction handler) { eventHandler = handler; isMonitorOnly = false; phy->startSink(this); reset(); } -template -const PDLogEntry* PDController::popLogEntry() { +const PDLogEntry* PDController::popLogEntry() { bool found = logEntries.get(currentLogEntry); return found ? ¤tLogEntry : nullptr; } -template -void PDController::log(PDLogEntryType type, const PDMessage* message) { +void PDController::log(PDLogEntryType type, const PDMessage* message) { PDLogEntry entry; entry.type = type; entry.time = micros(); @@ -45,8 +40,7 @@ void PDController::log(PDLogEntryType type, const PDMessage* message) { logEntries.put(entry); } -template -void PDController::reset() { +void PDController::reset() { lastRxMessageId = -1; txMessageId = 0; txRetryCount = 0; @@ -60,16 +54,14 @@ void PDController::reset() { eventHandler(PDControllerEvent(PDControllerEventType::reset)); } -template -void PDController::onReset(PDSOPSequence seq) { +void PDController::onReset(PDSOPSequence seq) { reset(); log(seq == PDSOPSequence::hardReset ? PDLogEntryType::hardReset : PDLogEntryType::cableReset); if (eventHandler != nullptr) eventHandler(PDControllerEvent(PDControllerEventType::reset)); } -template -bool PDController::sendControlMessage(PDMessageType messageType) { +bool PDController::sendControlMessage(PDMessageType messageType) { if (isTransmitting()) return false; txMessage->initControl(messageType, lastSpecRev); @@ -77,8 +69,7 @@ bool PDController::sendControlMessage(PDMessageType messageType) { return sendMessage(); } -template -bool PDController::sendDataMessage(PDMessageType messageType, int numObjects, const uint32_t* objects) { +bool PDController::sendDataMessage(PDMessageType messageType, int numObjects, const uint32_t* objects) { if (isTransmitting()) return false; txMessage->initData(messageType, numObjects, lastSpecRev); @@ -87,8 +78,7 @@ bool PDController::sendDataMessage(PDMessageType messageType, int numObject return sendMessage(); } -template -bool PDController::sendMessage() { +bool PDController::sendMessage() { // add message ID if (isPhyGoodCrc) { txMessage->setMessageId(txMessageId); @@ -105,8 +95,7 @@ bool PDController::sendMessage() { return true; } -template -void PDController::onMessageTransmitted(bool successful) { +void PDController::onMessageTransmitted(bool successful) { if (!successful) { log(PDLogEntryType::transmissionFailed); @@ -140,8 +129,7 @@ void PDController::onMessageTransmitted(bool successful) { } } -template -void PDController::onNoGoodCrcReceived() { +void PDController::onNoGoodCrcReceived() { Scheduler.cancelTask(TaskIdNoGoodCrcReceived); txRetryCount -= 1; @@ -155,8 +143,7 @@ void PDController::onNoGoodCrcReceived() { } } -template -void PDController::onMessageReceived(PDMessage* message) { +void PDController::onMessageReceived(PDMessage* message) { int messageId = message->messageId(); PDMessageType type = message->type(); PDSOPSequence sopSeq = message->sopSequence; @@ -209,8 +196,7 @@ void PDController::onMessageReceived(PDMessage* message) { } } -template -void PDController::prepareNextTxMessage() { +void PDController::prepareNextTxMessage() { txRetryCount = 0; if (isPhyGoodCrc) { @@ -233,35 +219,16 @@ void PDController::prepareNextTxMessage() { txMessage = (PDMessage*)head; } -template -void PDController::onError() { +void PDController::onError() { log(PDLogEntryType::error); // re-setup same buffer for reception phy->prepareRead(reinterpret_cast(rxMessageHead)); } -template -void PDController::onVoltageChanged(int cc) { +void PDController::onVoltageChanged(int cc) { ccPin = cc; log(cc == 0 ? PDLogEntryType::sinkSourceDisconnected : PDLogEntryType::sinkSourceConnected); if (eventHandler != nullptr) eventHandler(PDControllerEvent(cc == 0 ? PDControllerEventType::disconnected : PDControllerEventType::connected)); } - - -// template instantiation - -#if defined(ARDUINO_ARCH_ESP32) - -#include "phy/ESP32FUSB302/PDPhyFUSB302.h" -template class PDController; - -#elif defined(ARDUINO_ARCH_STM32) - -#if defined(STM32G0xx) || defined(STM32G4xx) -#include "phy/STM32UCPD/PDPhySTM32UCPD.h" -template class PDController; -#endif - -#endif diff --git a/src/PDController.h b/src/PDController.h index ccfdad8..ad3426d 100644 --- a/src/PDController.h +++ b/src/PDController.h @@ -9,7 +9,7 @@ #pragma once #include -#include "PDMessage.h" +#include "PDPhy.h" #include "RingBuffer.h" @@ -94,10 +94,7 @@ struct PDControllerEvent { * * As USB PD communication is timing sensitive, most code will run as part of * an interrupt handler. - * - * @tparam Phy Physical layer implementation (e.g. PDPhySTM32F4) */ -template class PDController { public: /// Event handler function to be called when an event occurs (called from interrupt) @@ -108,7 +105,7 @@ class PDController { * * @param phy Physical layer instance */ - PDController(Phy* phy); + PDController(PDPhy* phy); /** * @brief Set the GoodCRC handling. @@ -154,6 +151,24 @@ class PDController { /// Starts sending a data message bool sendDataMessage(PDMessageType messageType, int numObjects, const uint32_t* objects); + // --- handlers called from PDPhy IRQ handlers + + /// Called when the voltage has changed + /// @param cc indicates the active CC line (1 or 2), or 0 if no USB PD communication is active + void onVoltageChanged(int cc); + + /// Called when an error has occurred + void onError(); + + /// Called when a message has been transmitted + void onMessageTransmitted(bool successful); + + /// Called when a message has been received + void onMessageReceived(PDMessage* message); + + /// Called when a reset has been detected (hard reset, soft reset etc.) + void onReset(PDSOPSequence seq); + private: static constexpr int LogSize = 32; static constexpr int RxBufferLength = 512; @@ -166,7 +181,7 @@ class PDController { static constexpr int TaskIdNoGoodCrcReceived = 801; EventHandlerFunction eventHandler; - Phy* phy; + PDPhy* phy; bool isMonitorOnly; bool isPhyGoodCrc; @@ -195,13 +210,4 @@ class PDController { void prepareNextTxMessage(); void onNoGoodCrcReceived(); - - // handlers called from PhyPD IRQ handler - void onVoltageChanged(int cc); - void onError(); - void onMessageTransmitted(bool successful); - void onMessageReceived(PDMessage* message); - void onReset(PDSOPSequence seq); - - friend Phy; }; diff --git a/src/PDPhy.h b/src/PDPhy.h new file mode 100644 index 0000000..796cecd --- /dev/null +++ b/src/PDPhy.h @@ -0,0 +1,54 @@ +// +// USB Power Delivery for Arduino +// Copyright (c) 2024 Manuel Bleichenbacher +// +// Licensed under MIT License +// https://opensource.org/licenses/MIT +// +// Physical layer for USB PD communication +// + +#pragma once + +#include "PDMessage.h" + +class PDController; + + +/** + * @brief Physical layer for USB PD communication. + * + */ +class PDPhy { +public: + /** + * @brief Initializes the USB PD PHY as a sink. + * + * In the sink role, the PHY will interact in the USB PD communication. + * For PHYs with controllable pull-up/down resistors, it will + * activate the pull-down resistors to present itself as a power sink. + * + * @param controller the PD controller to be notified about events + */ + virtual void startSink(PDController* controller) = 0; + + /** + * @brief Sets the message and buffer to be used for the next incoming message. + * + * @param msg the message + */ + virtual void prepareRead(PDMessage* msg) = 0; + + /** + * @brief Transmits a message. + * + * The method is asynchronous. It will start the transmissoin when the CC + * line is idle and trigger an event to report when the message has been + * transmitted or the transmission has failed. + * + * @param msg the message + * @return `true` if transmission was started, `false` if it failed + * (TX activity by this or the other device, no active CC line) + */ + virtual bool transmitMessage(const PDMessage* msg) = 0; +}; \ No newline at end of file diff --git a/src/PDProtocolAnalyzer.cpp b/src/PDProtocolAnalyzer.cpp index 09b31a7..81d7434 100644 --- a/src/PDProtocolAnalyzer.cpp +++ b/src/PDProtocolAnalyzer.cpp @@ -8,7 +8,6 @@ #include #include "PDProtocolAnalyzer.h" -#include "PDController.h" static const char* const ControlMessageNames[] = { [0] = nullptr, @@ -65,13 +64,11 @@ static const char* const SOPSequenceNames[] = { }; -template -PDProtocolAnalyzer::PDProtocolAnalyzer(Controller* controller) : controller(controller) { +PDProtocolAnalyzer::PDProtocolAnalyzer(PDController* controller) : controller(controller) { memset(&capabilities, 0, sizeof(capabilities)); } -template -void PDProtocolAnalyzer::poll() { +void PDProtocolAnalyzer::poll() { auto logEntry = controller->popLogEntry(); if (logEntry == nullptr) return; @@ -112,8 +109,7 @@ void PDProtocolAnalyzer::poll() { } } -template -void PDProtocolAnalyzer::printMessage(const PDMessage* message) { +void PDProtocolAnalyzer::printMessage(const PDMessage* message) { Serial.printf("CC%d %-5s %-7s %-20s %d %04x", message->cc, getSOPSequenceName(message->sopSequence), getSender(message),getMessageName(message->type()), @@ -139,8 +135,7 @@ void PDProtocolAnalyzer::printMessage(const PDMessage* message) { } } -template -void PDProtocolAnalyzer::printCapabilitiesDetails(const PDMessage* message) { +void PDProtocolAnalyzer::printCapabilitiesDetails(const PDMessage* message) { auto numbObjects = message->numObjects(); // remember source capabilities @@ -194,8 +189,7 @@ void PDProtocolAnalyzer::printCapabilitiesDetails(const PDMessage* m } } -template -void PDProtocolAnalyzer::printRequestDetails(const PDMessage* message) { +void PDProtocolAnalyzer::printRequestDetails(const PDMessage* message) { auto object = message->objects[0]; int objPos = (object >> 28) & 0x07; bool giveBack = (object & (1 << 27)) != 0; @@ -225,8 +219,7 @@ void PDProtocolAnalyzer::printRequestDetails(const PDMessage* messag Serial.println(); } -template -const char* PDProtocolAnalyzer::getMessageName(PDMessageType messageType) { +const char* PDProtocolAnalyzer::getMessageName(PDMessageType messageType) { unsigned int index = (unsigned int)messageType; const char* name = nullptr; if (index < 0x80) { @@ -242,8 +235,7 @@ const char* PDProtocolAnalyzer::getMessageName(PDMessageType message return name != nullptr ? name : ""; } -template -const char* PDProtocolAnalyzer::getSOPSequenceName(PDSOPSequence sequence) { +const char* PDProtocolAnalyzer::getSOPSequenceName(PDSOPSequence sequence) { constexpr unsigned int arraySize = sizeof(SOPSequenceNames) / sizeof(SOPSequenceNames[0]); unsigned int index = (unsigned int)sequence; if (index >= arraySize) @@ -251,8 +243,7 @@ const char* PDProtocolAnalyzer::getSOPSequenceName(PDSOPSequence seq return SOPSequenceNames[index]; } -template -const char* PDProtocolAnalyzer::getSender(const PDMessage* message) { +const char* PDProtocolAnalyzer::getSender(const PDMessage* message) { auto seq = message->sopSequence; if (seq == PDSOPSequence::sop) { return (message->header & 0x0100) != 0 ? "Source" : "Sink"; @@ -262,17 +253,3 @@ const char* PDProtocolAnalyzer::getSender(const PDMessage* message) return ""; } - -#if defined(ARDUINO_ARCH_ESP32) - -#include "phy/ESP32FUSB302/PDPhyFUSB302.h" -template class PDProtocolAnalyzer>; - -#elif defined(ARDUINO_ARCH_STM32) - -#if defined(STM32G0xx) || defined(STM32G4xx) -#include "phy/STM32UCPD/PDPhySTM32UCPD.h" -template class PDProtocolAnalyzer>; -#endif - -#endif diff --git a/src/PDProtocolAnalyzer.h b/src/PDProtocolAnalyzer.h index 4d3d0e2..76a988a 100644 --- a/src/PDProtocolAnalyzer.h +++ b/src/PDProtocolAnalyzer.h @@ -8,11 +8,10 @@ #pragma once -#include "PDMessage.h" +#include "PDController.h" -template struct PDProtocolAnalyzer { - PDProtocolAnalyzer(Controller* controller); + PDProtocolAnalyzer(PDController* controller); void poll(); private: @@ -23,6 +22,6 @@ struct PDProtocolAnalyzer { const char* getSOPSequenceName(PDSOPSequence sequence); const char* getSender(const PDMessage* message); - Controller* controller; + PDController* controller; PDMessage capabilities; }; diff --git a/src/PDSink.cpp b/src/PDSink.cpp index 280ca76..515ae5d 100644 --- a/src/PDSink.cpp +++ b/src/PDSink.cpp @@ -12,19 +12,16 @@ #include "TaskScheduler.h" -template -PDSink::PDSink(Controller* controller) : +PDSink::PDSink(PDController* controller) : controller(controller), eventCallback(nullptr), isBusPowered(true), ppsIndex(-1), desiredVoltage(5000), desiredCurrent(0), capabilitiesChanged(false), flagSourceCapChanged(false), flagVoltageChanged(false), flagPowerRejected(false) {} -template -void PDSink::setBusPowered(bool busPowered) { +void PDSink::setBusPowered(bool busPowered) { isBusPowered = busPowered; } -template -void PDSink::reset(bool connected) { +void PDSink::reset(bool connected) { activeVoltage = isBusPowered || connected ? 5000 : 0; activeCurrent = isBusPowered || connected ? 900 : 0; requestedVoltage = 5000; @@ -37,15 +34,13 @@ void PDSink::reset(bool connected) { Scheduler.cancelTask(TaskIdRequestPPS); } -template -void PDSink::start(EventCallbackFunction callback) { +void PDSink::start(EventCallbackFunction callback) { eventCallback = callback; reset(false); controller->startController([this](const PDControllerEvent& event) { handleEvent(event); }); } -template -void PDSink::poll() { +void PDSink::poll() { if (eventCallback == nullptr) return; @@ -65,8 +60,7 @@ void PDSink::poll() { } } -template -bool PDSink::requestPower(int voltage, int maxCurrent) { +bool PDSink::requestPower(int voltage, int maxCurrent) { desiredVoltage = voltage; desiredCurrent = maxCurrent; @@ -95,8 +89,7 @@ bool PDSink::requestPower(int voltage, int maxCurrent) { } } -template -typename PDSink::ErrorCode PDSink::requestPowerCore(int voltage, int maxCurrent) { +typename PDSink::ErrorCode PDSink::requestPowerCore(int voltage, int maxCurrent) { if (!isConnected()) return notConnected; @@ -126,8 +119,7 @@ typename PDSink::ErrorCode PDSink::requestPowerCore(int return ok; } -template -void PDSink::handleEvent(const PDControllerEvent& event) { +void PDSink::handleEvent(const PDControllerEvent& event) { switch (event.type) { case PDControllerEventType::reset: @@ -144,8 +136,7 @@ void PDSink::handleEvent(const PDControllerEvent& event) { } } -template -void PDSink::onMessageReceived(const PDMessage* message) { +void PDSink::onMessageReceived(const PDMessage* message) { switch (message->type()) { case PDMessageType::dataSourceCapabilities: @@ -164,8 +155,7 @@ void PDSink::onMessageReceived(const PDMessage* message) { } } -template -void PDSink::onSourceCapabilities(const PDMessage* message) { +void PDSink::onSourceCapabilities(const PDMessage* message) { // parse source capabilities message PDSourceCapability::parseMessage(message, numSourceCapabilities, sourceCapabilities); @@ -176,8 +166,7 @@ void PDSink::onSourceCapabilities(const PDMessage* message) { requestPowerCore(5000, 0); // fallback: select 5V } -template -void PDSink::onPsReady() { +void PDSink::onPsReady() { if (capabilitiesChanged) { flagSourceCapChanged = true; capabilitiesChanged = false; @@ -194,8 +183,7 @@ void PDSink::onPsReady() { Scheduler.scheduleTaskAfter(TaskIdRequestPPS, [this](){ onRerequestPPS(); }, 8000000); } -template -void PDSink::onRerequestPPS() { +void PDSink::onRerequestPPS() { // request the same voltage again uint32_t requestObject = PDSourceCapability::powerRequestObject(ppsIndex, requestedVoltage, requestedCurrent, numSourceCapabilities, sourceCapabilities); @@ -203,20 +191,3 @@ void PDSink::onRerequestPPS() { if (!successful) Scheduler.scheduleTaskAfter(TaskIdRequestPPS, [this](){ onRerequestPPS(); }, 100000); } - - -// template instantiation - -#if defined(ARDUINO_ARCH_ESP32) - -#include "phy/ESP32FUSB302/PDPhyFUSB302.h" -template class PDSink>; - -#elif defined(ARDUINO_ARCH_STM32) - -#if defined(STM32G0xx) || defined(STM32G4xx) -#include "phy/STM32UCPD/PDPhySTM32UCPD.h" -template class PDSink>; -#endif - -#endif diff --git a/src/PDSink.h b/src/PDSink.h index f9bb1f9..9038256 100644 --- a/src/PDSink.h +++ b/src/PDSink.h @@ -25,10 +25,7 @@ enum class PDSinkEventType { * * Communicates with a power source (power supply) to control the voltage * and current being supplied. - * - * @tparam Controller PD controller class */ -template class PDSink { public: /** @@ -50,7 +47,7 @@ class PDSink { * * @param controller PD controller */ - PDSink(Controller* controller); + PDSink(PDController* controller); /** * @brief Set if this sink is bus powered. @@ -134,7 +131,7 @@ class PDSink { static constexpr int TaskIdRequestPPS = 1001; - Controller* controller; + PDController* controller; EventCallbackFunction eventCallback; bool isBusPowered; diff --git a/src/phy/ESP32FUSB302/PDPhyFUSB302.cpp b/src/phy/ESP32FUSB302/PDPhyFUSB302.cpp index 12494b7..558cc28 100644 --- a/src/phy/ESP32FUSB302/PDPhyFUSB302.cpp +++ b/src/phy/ESP32FUSB302/PDPhyFUSB302.cpp @@ -27,7 +27,7 @@ PDPhyFUSB302::PDPhyFUSB302() : wire(&Wire), rxMessage(nullptr), controller(nullptr), state(FUSB302State::NotStarted), i2CAddress(0x22), interruptPin(10), activeCC(0) {} -void PDPhyFUSB302::startSink(PDController* controller) { +void PDPhyFUSB302::startSink(PDController* controller) { controller->setGoodCrcHandling(true); this->controller = controller; diff --git a/src/phy/ESP32FUSB302/PDPhyFUSB302.h b/src/phy/ESP32FUSB302/PDPhyFUSB302.h index 75963af..699fe78 100644 --- a/src/phy/ESP32FUSB302/PDPhyFUSB302.h +++ b/src/phy/ESP32FUSB302/PDPhyFUSB302.h @@ -13,7 +13,8 @@ #if defined(ARDUINO_ARCH_ESP32) #include -#include "PDController.h" +#include "PDPhy.h" + /// FUSB302 state enum class FUSB302State { @@ -54,7 +55,8 @@ enum class FUSB302Event { * For I2C communication, the global `Wire` object is used. It should * be configured with a frequency of at least 1 MHz. */ -struct PDPhyFUSB302 { +class PDPhyFUSB302 : public PDPhy { +public: /** * @brief Construct a new instance. @@ -114,7 +116,7 @@ struct PDPhyFUSB302 { * * @param controller the PD controller to be notified about events */ - void startSink(PDController* controller); + void startSink(PDController* controller); /** * @brief Sets the message and buffer to be used for the next incoming message. @@ -145,7 +147,7 @@ struct PDPhyFUSB302 { TwoWire* wire; QueueHandle_t eventQueue; PDMessage* rxMessage; - PDController* controller; + PDController* controller; FUSB302State state; uint8_t i2CAddress; uint8_t interruptPin; diff --git a/src/phy/STM32UCPD/PDPhySTM32UCPD.cpp b/src/phy/STM32UCPD/PDPhySTM32UCPD.cpp index 46dabb9..a771e05 100644 --- a/src/phy/STM32UCPD/PDPhySTM32UCPD.cpp +++ b/src/phy/STM32UCPD/PDPhySTM32UCPD.cpp @@ -87,7 +87,7 @@ PDPhySTM32UCPD* PDPhySTM32UCPD::instances[] = {0}; PDPhySTM32UCPD::PDPhySTM32UCPD() : controller(nullptr), rxMessage(nullptr), ccActive(0), instance(0) {} -void PDPhySTM32UCPD::startSink(PDController* controller) { +void PDPhySTM32UCPD::startSink(PDController* controller) { this->controller = controller; init(false); } diff --git a/src/phy/STM32UCPD/PDPhySTM32UCPD.h b/src/phy/STM32UCPD/PDPhySTM32UCPD.h index e2ee3b8..28bc279 100644 --- a/src/phy/STM32UCPD/PDPhySTM32UCPD.h +++ b/src/phy/STM32UCPD/PDPhySTM32UCPD.h @@ -10,7 +10,7 @@ #if defined(STM32G0xx) || defined(STM32G4xx) -#include "PDController.h" +#include "PDPhy.h" extern "C" void UCPD1_IRQHandler(); extern "C" void UCPD1_2_IRQHandler(); @@ -19,7 +19,8 @@ extern "C" void UCPD1_2_IRQHandler(); * @brief Physical layer for USB PD communication. * */ -struct PDPhySTM32UCPD { +class PDPhySTM32UCPD : public PDPhy { +public: /** * @brief Creates a new PDPhySTM32UCPD instance @@ -46,7 +47,7 @@ struct PDPhySTM32UCPD { * * @param controller the PD controller to be notified about events */ - void startSink(PDController* controller); + void startSink(PDController* controller); /** * @brief Sets the message and buffer to be used for the next incoming message. @@ -97,7 +98,7 @@ struct PDPhySTM32UCPD { static PDPhySTM32UCPD* instances[NumInstances]; const Peripheral* peripheral; - PDController* controller; + PDController* controller; PDMessage* rxMessage; int ccActive; int instance; @@ -110,7 +111,6 @@ struct PDPhySTM32UCPD { static PDSOPSequence mapSOPSequence(uint32_t orderedSet); - friend PDController; friend void UCPD1_IRQHandler(); friend void UCPD1_2_IRQHandler();