From fb24ba18667d48cb0bcfc79ac1b7184d59ca21e9 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Mon, 5 Apr 2021 15:57:31 +1000 Subject: [PATCH] Collapse rx into irq --- source/Core/Drivers/FUSB302/fusbpd.cpp | 3 +- source/Core/Drivers/FUSB302/int_n.cpp | 26 ++++++- source/Core/Drivers/FUSB302/int_n.h | 3 + source/Core/Drivers/FUSB302/protocol_rx.cpp | 86 --------------------- source/Core/Drivers/FUSB302/protocol_rx.h | 53 ------------- source/Core/Drivers/FUSB302/protocol_tx.cpp | 5 +- source/Core/Drivers/FUSB302/protocol_tx.h | 2 +- 7 files changed, 30 insertions(+), 148 deletions(-) delete mode 100644 source/Core/Drivers/FUSB302/protocol_rx.cpp delete mode 100644 source/Core/Drivers/FUSB302/protocol_rx.h diff --git a/source/Core/Drivers/FUSB302/fusbpd.cpp b/source/Core/Drivers/FUSB302/fusbpd.cpp index 56539f28..54f026c0 100644 --- a/source/Core/Drivers/FUSB302/fusbpd.cpp +++ b/source/Core/Drivers/FUSB302/fusbpd.cpp @@ -11,7 +11,7 @@ #include "fusb302b.h" #include "int_n.h" #include "policy_engine.h" -#include "protocol_rx.h" + #include "protocol_tx.h" #include #include @@ -21,7 +21,6 @@ void fusb302_start_processing() { if (fusb_setup()) { PolicyEngine::init(); ProtocolTransmit::init(); - ProtocolReceive::init(); InterruptHandler::init(); } } diff --git a/source/Core/Drivers/FUSB302/int_n.cpp b/source/Core/Drivers/FUSB302/int_n.cpp index 2baa0cc0..7e4b5697 100644 --- a/source/Core/Drivers/FUSB302/int_n.cpp +++ b/source/Core/Drivers/FUSB302/int_n.cpp @@ -21,14 +21,16 @@ #include "fusb302b.h" #include "fusbpd.h" #include "policy_engine.h" -#include "protocol_rx.h" + #include "protocol_tx.h" #include "task.h" #include +#include volatile osThreadId InterruptHandler::TaskHandle = NULL; uint32_t InterruptHandler::TaskBuffer[InterruptHandler::TaskStackSize]; osStaticThreadDef_t InterruptHandler::TaskControlBlock; +union pd_msg InterruptHandler::tempMessage; void InterruptHandler::init() { TaskHandle = NULL; @@ -36,6 +38,26 @@ void InterruptHandler::init() { TaskHandle = osThreadCreate(osThread(intTask), NULL); } +void InterruptHandler::readPendingMessage() { + /* Get a buffer to read the message into. Guaranteed to not fail + * because we have a big enough pool and are careful. */ + memset(&tempMessage, 0, sizeof(tempMessage)); + /* Read the message */ + fusb_read_message(&tempMessage); + /* If it's a Soft_Reset, go to the soft reset state */ + if (PD_MSGTYPE_GET(&tempMessage) == PD_MSGTYPE_SOFT_RESET && PD_NUMOBJ_GET(&tempMessage) == 0) { + /* TX transitions to its reset state */ + ProtocolTransmit::notify(ProtocolTransmit::Notifications::PDB_EVT_PRLTX_RESET); + } else { + /* Tell ProtocolTX to discard the message being transmitted */ + ProtocolTransmit::notify(ProtocolTransmit::Notifications::PDB_EVT_PRLTX_DISCARD); + + /* Pass the message to the policy engine. */ + PolicyEngine::handleMessage(&tempMessage); + PolicyEngine::notify(PolicyEngine::Notifications::PDB_EVT_PE_MSG_RX); + } +} + void InterruptHandler::Thread(const void *arg) { (void)arg; union fusb_status status; @@ -50,7 +72,7 @@ void InterruptHandler::Thread(const void *arg) { /* If the I_GCRCSENT flag is set, tell the Protocol RX thread */ // This means a message was recieved with a good CRC if (status.interruptb & FUSB_INTERRUPTB_I_GCRCSENT) { - ProtocolReceive::notify(PDB_EVT_PRLRX_I_GCRCSENT); + readPendingMessage(); } /* If the I_TXSENT or I_RETRYFAIL flag is set, tell the Protocol TX diff --git a/source/Core/Drivers/FUSB302/int_n.h b/source/Core/Drivers/FUSB302/int_n.h index fb5dc1d8..ed18b50d 100644 --- a/source/Core/Drivers/FUSB302/int_n.h +++ b/source/Core/Drivers/FUSB302/int_n.h @@ -44,6 +44,9 @@ private: static enum hardrst_state hardrst_hard_reset_requested(); static enum hardrst_state hardrst_wait_pe(); static enum hardrst_state hardrst_complete(); + // Mesage rx + static void readPendingMessage(); + static union pd_msg tempMessage; }; #endif /* PDB_INT_N_OLD_H */ diff --git a/source/Core/Drivers/FUSB302/protocol_rx.cpp b/source/Core/Drivers/FUSB302/protocol_rx.cpp deleted file mode 100644 index 000ce9f5..00000000 --- a/source/Core/Drivers/FUSB302/protocol_rx.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * PD Buddy Firmware Library - USB Power Delivery for everyone - * Copyright 2017-2018 Clayton G. Hobbs - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "protocol_rx.h" - -#include "fusb302b.h" -#include "policy_engine.h" -#include "protocol_tx.h" -#include "string.h" -#include -#include -osThreadId ProtocolReceive::TaskHandle = NULL; -EventGroupHandle_t ProtocolReceive::xEventGroupHandle = NULL; -StaticEventGroup_t ProtocolReceive::xCreatedEventGroup; -uint32_t ProtocolReceive::TaskBuffer[ProtocolReceive::TaskStackSize]; -osStaticThreadDef_t ProtocolReceive::TaskControlBlock; -union pd_msg ProtocolReceive::tempMessage; - -void ProtocolReceive::init() { - osThreadStaticDef(protRX, thread, PDB_PRIO_PRL, 0, TaskStackSize, TaskBuffer, &TaskControlBlock); - xEventGroupHandle = xEventGroupCreateStatic(&xCreatedEventGroup); - TaskHandle = osThreadCreate(osThread(protRX), NULL); -} - -void ProtocolReceive::thread(const void *args) { - (void)args; - - while (true) { - /* Wait for an event */ - EventBits_t evt = waitForEvent(PDB_EVT_PRLRX_RESET | PDB_EVT_PRLRX_I_GCRCSENT); - - /* If we got a reset event, reset */ - if (evt & PDB_EVT_PRLRX_RESET) { - /* TX transitions to its reset state */ - ProtocolTransmit::notify(ProtocolTransmit::Notifications::PDB_EVT_PRLTX_RESET); - } else { - /* If we got an I_GCRCSENT event, read the message and decide what to do */ - if (evt & PDB_EVT_PRLRX_I_GCRCSENT) { - /* Get a buffer to read the message into. Guaranteed to not fail - * because we have a big enough pool and are careful. */ - memset(&tempMessage, 0, sizeof(tempMessage)); - /* Read the message */ - fusb_read_message(&tempMessage); - /* If it's a Soft_Reset, go to the soft reset state */ - if (PD_MSGTYPE_GET(&tempMessage) == PD_MSGTYPE_SOFT_RESET && PD_NUMOBJ_GET(&tempMessage) == 0) { - /* TX transitions to its reset state */ - ProtocolTransmit::notify(ProtocolTransmit::Notifications::PDB_EVT_PRLTX_RESET); - } else { - /* Tell ProtocolTX to discard the message being transmitted */ - ProtocolTransmit::notify(ProtocolTransmit::Notifications::PDB_EVT_PRLTX_DISCARD); - - /* Pass the message to the policy engine. */ - PolicyEngine::handleMessage(&tempMessage); - PolicyEngine::notify(PolicyEngine::Notifications::PDB_EVT_PE_MSG_RX); - } - } - } - } -} - -void ProtocolReceive::notify(uint32_t notification) { - if (xEventGroupHandle != NULL) { - xEventGroupSetBits(xEventGroupHandle, notification); - } -} - -uint32_t ProtocolReceive::waitForEvent(uint32_t mask, TickType_t ticksToWait) { - if (xEventGroupHandle != NULL) { - return xEventGroupWaitBits(xEventGroupHandle, mask, mask, pdFALSE, ticksToWait); - } - return 0; -} diff --git a/source/Core/Drivers/FUSB302/protocol_rx.h b/source/Core/Drivers/FUSB302/protocol_rx.h deleted file mode 100644 index 81ed76c8..00000000 --- a/source/Core/Drivers/FUSB302/protocol_rx.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * PD Buddy Firmware Library - USB Power Delivery for everyone - * Copyright 2017-2018 Clayton G. Hobbs - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PDB_PROTOCOL_RX_H -#define PDB_PROTOCOL_RX_H - -#include - -#include - -/* Events for the Protocol RX thread */ -#define PDB_EVT_PRLRX_RESET EVENT_MASK(0) -#define PDB_EVT_PRLRX_I_GCRCSENT EVENT_MASK(1) - -class ProtocolReceive { -public: - static void init(); - static void notify(uint32_t notification); - -private: - static void thread(const void *args); - - static EventGroupHandle_t xEventGroupHandle; - static StaticEventGroup_t xCreatedEventGroup; - static osThreadId TaskHandle; - static const size_t TaskStackSize = 1024 / 4; - static uint32_t TaskBuffer[TaskStackSize]; - static osStaticThreadDef_t TaskControlBlock; - /* - * Protocol RX machine states - * - * There is no Send_GoodCRC state because the PHY sends the GoodCRC for us. - * All transitions that would go to that state instead go to Check_MessageID. - */ - static union pd_msg tempMessage; - static uint32_t waitForEvent(uint32_t mask, TickType_t ticksToWait = portMAX_DELAY); -}; - -#endif /* PDB_PROTOCOL_RX_H */ diff --git a/source/Core/Drivers/FUSB302/protocol_tx.cpp b/source/Core/Drivers/FUSB302/protocol_tx.cpp index bd65b81b..d08d4304 100644 --- a/source/Core/Drivers/FUSB302/protocol_tx.cpp +++ b/source/Core/Drivers/FUSB302/protocol_tx.cpp @@ -19,7 +19,7 @@ #include "fusb302b.h" #include "fusbpd.h" #include "policy_engine.h" -#include "protocol_rx.h" + #include osThreadId ProtocolTransmit::TaskHandle = NULL; @@ -88,9 +88,6 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_reset() { /* Clear MessageIDCounter */ _tx_messageidcounter = 0; - /* Tell the Protocol RX thread to reset */ - ProtocolReceive::notify(PDB_EVT_PRLRX_RESET); - return PRLTxConstructMessage; } diff --git a/source/Core/Drivers/FUSB302/protocol_tx.h b/source/Core/Drivers/FUSB302/protocol_tx.h index 1d12e18b..0231f455 100644 --- a/source/Core/Drivers/FUSB302/protocol_tx.h +++ b/source/Core/Drivers/FUSB302/protocol_tx.h @@ -19,7 +19,7 @@ #define PDB_PROTOCOL_TX_H #include "policy_engine.h" -#include "protocol_rx.h" + #include #include