Simplify rx

This commit is contained in:
Ben V. Brown
2021-04-05 15:52:54 +10:00
parent af818537f5
commit b682f6cd70
2 changed files with 30 additions and 97 deletions

View File

@@ -29,77 +29,6 @@ StaticEventGroup_t ProtocolReceive::xCreatedEventGroup;
uint32_t ProtocolReceive::TaskBuffer[ProtocolReceive::TaskStackSize]; uint32_t ProtocolReceive::TaskBuffer[ProtocolReceive::TaskStackSize];
osStaticThreadDef_t ProtocolReceive::TaskControlBlock; osStaticThreadDef_t ProtocolReceive::TaskControlBlock;
union pd_msg ProtocolReceive::tempMessage; union pd_msg ProtocolReceive::tempMessage;
/*
* PRL_Rx_Wait_for_PHY_Message state
*/
ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_wait_phy() {
/* 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) {
waitForEvent(PDB_EVT_PRLRX_RESET, 0);
return PRLRxWaitPHY;
}
/* 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. */
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 */
if (PD_MSGTYPE_GET(_rx_message) == PD_MSGTYPE_SOFT_RESET && PD_NUMOBJ_GET(_rx_message) == 0) {
return PRLRxReset;
} else {
/* Otherwise, check the message ID */
return PRLRxCheckMessageID;
}
}
return PRLRxWaitPHY;
}
/*
* PRL_Rx_Layer_Reset_for_Receive state
*/
ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_reset() {
/* TX transitions to its reset state */
ProtocolTransmit::notify(ProtocolTransmit::Notifications::PDB_EVT_PRLTX_RESET);
taskYIELD();
/* If we got a RESET signal, reset the machine */
if (waitForEvent(PDB_EVT_PRLRX_RESET, 0) != 0) {
return PRLRxWaitPHY;
}
/* Go to the Check_MessageID state */
return PRLRxCheckMessageID;
}
/*
* PRL_Rx_Check_MessageID state
*/
ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_check_messageid() { return PRLRxStoreMessageID; }
/*
* PRL_Rx_Store_MessageID state
*/
ProtocolReceive::protocol_rx_state ProtocolReceive::protocol_rx_store_messageid() {
/* 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);
taskYIELD();
/* Don't check if we got a RESET because we'd do nothing different. */
return PRLRxWaitPHY;
}
void ProtocolReceive::init() { void ProtocolReceive::init() {
osThreadStaticDef(protRX, thread, PDB_PRIO_PRL, 0, TaskStackSize, TaskBuffer, &TaskControlBlock); osThreadStaticDef(protRX, thread, PDB_PRIO_PRL, 0, TaskStackSize, TaskBuffer, &TaskControlBlock);
@@ -109,27 +38,36 @@ void ProtocolReceive::init() {
void ProtocolReceive::thread(const void *args) { void ProtocolReceive::thread(const void *args) {
(void)args; (void)args;
ProtocolReceive::protocol_rx_state state = PRLRxWaitPHY;
while (true) { while (true) {
switch (state) { /* Wait for an event */
case PRLRxWaitPHY: EventBits_t evt = waitForEvent(PDB_EVT_PRLRX_RESET | PDB_EVT_PRLRX_I_GCRCSENT);
state = protocol_rx_wait_phy();
break; /* If we got a reset event, reset */
case PRLRxReset: if (evt & PDB_EVT_PRLRX_RESET) {
state = protocol_rx_reset(); /* TX transitions to its reset state */
break; ProtocolTransmit::notify(ProtocolTransmit::Notifications::PDB_EVT_PRLTX_RESET);
case PRLRxCheckMessageID: } else {
state = protocol_rx_check_messageid(); /* If we got an I_GCRCSENT event, read the message and decide what to do */
break; if (evt & PDB_EVT_PRLRX_I_GCRCSENT) {
case PRLRxStoreMessageID: /* Get a buffer to read the message into. Guaranteed to not fail
state = protocol_rx_store_messageid(); * because we have a big enough pool and are careful. */
break; memset(&tempMessage, 0, sizeof(tempMessage));
default: /* Read the message */
/* This is an error. It really shouldn't happen. We might fusb_read_message(&tempMessage);
* want to handle it anyway, though. */ /* If it's a Soft_Reset, go to the soft reset state */
state = PRLRxWaitPHY; if (PD_MSGTYPE_GET(&tempMessage) == PD_MSGTYPE_SOFT_RESET && PD_NUMOBJ_GET(&tempMessage) == 0) {
break; /* 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);
}
}
} }
} }
} }

View File

@@ -46,13 +46,8 @@ private:
* There is no Send_GoodCRC state because the PHY sends the GoodCRC for us. * 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. * All transitions that would go to that state instead go to Check_MessageID.
*/ */
enum protocol_rx_state { PRLRxWaitPHY, PRLRxReset, PRLRxCheckMessageID, PRLRxStoreMessageID }; static union pd_msg tempMessage;
static protocol_rx_state protocol_rx_store_messageid(); static uint32_t waitForEvent(uint32_t mask, TickType_t ticksToWait = portMAX_DELAY);
static protocol_rx_state protocol_rx_check_messageid();
static protocol_rx_state protocol_rx_reset();
static protocol_rx_state protocol_rx_wait_phy();
static union pd_msg tempMessage;
static uint32_t waitForEvent(uint32_t mask, TickType_t ticksToWait = portMAX_DELAY);
}; };
#endif /* PDB_PROTOCOL_RX_H */ #endif /* PDB_PROTOCOL_RX_H */