From dd340d3a2b437a757d5586d0fc43f6655338f16b Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Tue, 28 Jul 2020 20:05:47 +1000 Subject: [PATCH] Port from test env cleanups --- .../Drivers/FUSB302/policy_engine_user.cpp | 6 +- .../Core/Drivers/FUSB302/protocol_rx.cpp | 32 +++++++---- .../Core/Drivers/FUSB302/protocol_tx.cpp | 56 ++++++++----------- 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/workspace/TS100/Core/Drivers/FUSB302/policy_engine_user.cpp b/workspace/TS100/Core/Drivers/FUSB302/policy_engine_user.cpp index c82ebecc..a79b124f 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/policy_engine_user.cpp +++ b/workspace/TS100/Core/Drivers/FUSB302/policy_engine_user.cpp @@ -296,12 +296,12 @@ bool PolicyEngine::messageWaiting() { return uxQueueMessagesWaiting(messagesWaiting) > 0; } -void PolicyEngine::readMessage() { - xQueueReceive(messagesWaiting, &tempMessage, 1); +bool PolicyEngine::readMessage() { + return xQueueReceive(messagesWaiting, &tempMessage, 1) == pdTRUE; } void PolicyEngine::pdbs_dpm_transition_typec() { -//This means PD failed, so we either have a dumb 5V only type C or a QC charger +//This means PD failed, so we either have a dump 5V only type C or a QC charger //For now; treat this as failed neg pdNegotiationComplete = false; } diff --git a/workspace/TS100/Core/Drivers/FUSB302/protocol_rx.cpp b/workspace/TS100/Core/Drivers/FUSB302/protocol_rx.cpp index 68a897df..d3c06706 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/protocol_rx.cpp +++ b/workspace/TS100/Core/Drivers/FUSB302/protocol_rx.cpp @@ -18,7 +18,7 @@ #include "protocol_rx.h" #include - +#include "string.h" #include #include "policy_engine.h" #include "protocol_tx.h" @@ -36,7 +36,7 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_wait_phy() { /* Wait for an event */ _rx_messageid = 0; eventmask_t evt = waitForEvent( - PDB_EVT_PRLRX_RESET | PDB_EVT_PRLRX_I_GCRCSENT); + PDB_EVT_PRLRX_RESET | PDB_EVT_PRLRX_I_GCRCSENT | PDB_EVT_PRLRX_I_RXPEND); /* If we got a reset event, reset */ if (evt & PDB_EVT_PRLRX_RESET) { @@ -48,6 +48,7 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_wait_phy() { /* Get a buffer to read the message into. Guaranteed to not fail * because we have a big enough pool and are careful. */ union pd_msg *_rx_message = &tempMessage; + memset(&tempMessage, 0, sizeof(tempMessage)); /* Read the message */ fusb_read_message(_rx_message); /* If it's a Soft_Reset, go to the soft reset state */ @@ -58,6 +59,12 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_wait_phy() { /* Otherwise, check the message ID */ return PRLRxCheckMessageID; } + } else if (evt & PDB_EVT_PRLRX_I_RXPEND) { + //There is an RX message pending that is not a Good CRC + union pd_msg *_rx_message = &tempMessage; + /* Read the message */ + fusb_read_message(_rx_message); + return PRLRxWaitPHY; } return PRLRxWaitPHY; @@ -92,17 +99,18 @@ volatile uint32_t rxCounter = 0; */ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_check_messageid() { /* If we got a RESET signal, reset the machine */ - if (waitForEvent(PDB_EVT_PRLRX_RESET, 0) == PDB_EVT_PRLRX_RESET) { - return PRLRxWaitPHY; - } +// if (waitForEvent(PDB_EVT_PRLRX_RESET, 0) == PDB_EVT_PRLRX_RESET) { +// return PRLRxWaitPHY; +// } /* If the message has the stored ID, we've seen this message before. Free * it and don't pass it to the policy engine. */ /* Otherwise, there's either no stored ID or this message has an ID we * haven't just seen. Transition to the Store_MessageID state. */ - if (PD_MESSAGEID_GET(&tempMessage) == _rx_messageid) { - return PRLRxWaitPHY; - } else { +// if (PD_MESSAGEID_GET(&tempMessage) == _rx_messageid) { +// return PRLRxWaitPHY; +// } else + { rxCounter++; return PRLRxStoreMessageID; } @@ -125,7 +133,7 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_store_messageid( PolicyEngine::handleMessage(&tempMessage); PolicyEngine::notify(PDB_EVT_PE_MSG_RX); - + taskYIELD(); /* Don't check if we got a RESET because we'd do nothing different. */ return PRLRxWaitPHY; @@ -134,10 +142,10 @@ ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_store_messageid( EventGroupHandle_t ProtocolReceive::xEventGroupHandle; StaticEventGroup_t ProtocolReceive::xCreatedEventGroup; void ProtocolReceive::init() { - osThreadStaticDef(Task, thread, PDB_PRIO_PRL, 0, TaskStackSize, TaskBuffer, - &TaskControlBlock); + osThreadStaticDef(protRX, thread, PDB_PRIO_PRL, 0, TaskStackSize, + TaskBuffer, &TaskControlBlock); xEventGroupHandle = xEventGroupCreateStatic(&xCreatedEventGroup); - TaskHandle = osThreadCreate(osThread(Task), NULL); + TaskHandle = osThreadCreate(osThread(protRX), NULL); } void ProtocolReceive::thread(const void *args) { diff --git a/workspace/TS100/Core/Drivers/FUSB302/protocol_tx.cpp b/workspace/TS100/Core/Drivers/FUSB302/protocol_tx.cpp index 28bac16c..1382aae4 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/protocol_tx.cpp +++ b/workspace/TS100/Core/Drivers/FUSB302/protocol_tx.cpp @@ -26,7 +26,7 @@ osThreadId ProtocolTransmit::TaskHandle; uint32_t ProtocolTransmit::TaskBuffer[ProtocolTransmit::TaskStackSize]; osStaticThreadDef_t ProtocolTransmit::TaskControlBlock; StaticQueue_t ProtocolTransmit::xStaticQueue; - +bool ProtocolTransmit::messageSending = false; uint8_t ProtocolTransmit::ucQueueStorageArea[PDB_MSG_POOL_SIZE * sizeof(union pd_msg)]; QueueHandle_t ProtocolTransmit::messagesWaiting; @@ -67,9 +67,6 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_wait_message() if ((uint32_t) evt & (uint32_t) Notifications::PDB_EVT_PRLTX_RESET) { return PRLTxPHYReset; } - if ((uint32_t) evt & (uint32_t) Notifications::PDB_EVT_PRLTX_DISCARD) { - return PRLTxDiscardMessage; - } /* If the policy engine is trying to send a message */ if ((uint32_t) evt & (uint32_t) Notifications::PDB_EVT_PRLTX_MSG_TX) { @@ -87,7 +84,7 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_wait_message() } /* Silence the compiler warning */ - return PRLTxDiscardMessage; + return PRLTxWaitMessage; } ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_reset() { @@ -105,35 +102,24 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_reset() { * PRL_Tx_Construct_Message state */ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_construct_message() { - /* Make sure nobody wants us to reset */ - ProtocolTransmit::Notifications evt = waitForEvent( - (uint32_t) Notifications::PDB_EVT_PRLTX_RESET - | (uint32_t) Notifications::PDB_EVT_PRLTX_DISCARD, 0); - - if ((uint32_t) evt & (uint32_t) Notifications::PDB_EVT_PRLTX_RESET) { - return PRLTxPHYReset; - } - if ((uint32_t) evt & (uint32_t) Notifications::PDB_EVT_PRLTX_DISCARD) { - return PRLTxDiscardMessage; - } - + ProtocolTransmit::Notifications evt; /* Set the correct MessageID in the message */ temp_msg.hdr &= ~PD_HDR_MESSAGEID; temp_msg.hdr |= (_tx_messageidcounter % 8) << PD_HDR_MESSAGEID_SHIFT; /* PD 3.0 collision avoidance */ - if (PolicyEngine::isPD3_0()) { - /* If we're starting an AMS, wait for permission to transmit */ - evt = waitForEvent((uint32_t) Notifications::PDB_EVT_PRLTX_START_AMS, - 0); - if ((uint32_t) evt - & (uint32_t) Notifications::PDB_EVT_PRLTX_START_AMS) { - while (fusb_get_typec_current() != fusb_sink_tx_ok) { - osDelay(1); - } - } - } - +// if (PolicyEngine::isPD3_0()) { +// /* If we're starting an AMS, wait for permission to transmit */ +// evt = waitForEvent((uint32_t) Notifications::PDB_EVT_PRLTX_START_AMS, +// 0); +// if ((uint32_t) evt +// & (uint32_t) Notifications::PDB_EVT_PRLTX_START_AMS) { +// while (fusb_get_typec_current() != fusb_sink_tx_ok) { +// osDelay(1); +// } +// } +// } + messageSending = true; /* Send the message to the PHY */ fusb_send_message(&temp_msg); @@ -202,6 +188,7 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_transmission_e } ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_message_sent() { + messageSending = false; /* Increment MessageIDCounter */ _tx_messageidcounter = (_tx_messageidcounter + 1) % 8; @@ -213,11 +200,14 @@ ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_message_sent() ProtocolTransmit::protocol_tx_state ProtocolTransmit::protocol_tx_discard_message() { /* If we were working on sending a message, increment MessageIDCounter */ - _tx_messageidcounter = (_tx_messageidcounter + 1) % 8; + if (messageSending) { + _tx_messageidcounter = (_tx_messageidcounter + 1) % 8; - return PRLTxPHYReset; + return PRLTxPHYReset; + } else { + return PRLTxWaitMessage; + } } - void ProtocolTransmit::thread(const void *args) { (void) args; ProtocolTransmit::protocol_tx_state state = PRLTxPHYReset; @@ -270,7 +260,7 @@ void ProtocolTransmit::init() { messagesWaiting = xQueueCreateStatic(PDB_MSG_POOL_SIZE, sizeof(union pd_msg), ucQueueStorageArea, &xStaticQueue); - osThreadStaticDef(pd_txTask, thread, PDB_PRIO_PE, 0, TaskStackSize, + osThreadStaticDef(pd_txTask, thread, PDB_PRIO_PRL, 0, TaskStackSize, TaskBuffer, &TaskControlBlock); TaskHandle = osThreadCreate(osThread(pd_txTask), NULL); xEventGroupHandle = xEventGroupCreateStatic(&xCreatedEventGroup);