static void dlmstp_master_fsm_task( void *pArg) { DWORD dwMilliseconds = 0; (void) pArg; (void) SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); while (TRUE) { switch (MSTP_Port.master_state) { case MSTP_MASTER_STATE_IDLE: dwMilliseconds = Tno_token; break; case MSTP_MASTER_STATE_WAIT_FOR_REPLY: dwMilliseconds = Treply_timeout; break; case MSTP_MASTER_STATE_POLL_FOR_MASTER: dwMilliseconds = Tusage_timeout; break; default: dwMilliseconds = 0; break; } if (dwMilliseconds) WaitForSingleObject(Received_Frame_Flag, dwMilliseconds); MSTP_Master_Node_FSM(&MSTP_Port); } }
static void *dlmstp_master_fsm_task( void *pArg) { unsigned long milliseconds = 0; struct timespec abstime; (void) pArg; for (;;) { switch (MSTP_Port.master_state) { case MSTP_MASTER_STATE_IDLE: milliseconds = Tno_token; break; case MSTP_MASTER_STATE_WAIT_FOR_REPLY: milliseconds = Treply_timeout; break; case MSTP_MASTER_STATE_POLL_FOR_MASTER: milliseconds = Tusage_timeout; break; default: milliseconds = 0; break; } if (milliseconds) { get_abstime(&abstime, milliseconds); /* we want an OS effecient way to wait for a frame */ pthread_cond_timedwait(&Received_Frame_Flag, &Received_Frame_Mutex, &abstime); } MSTP_Master_Node_FSM(&MSTP_Port); } return NULL; }
void dlmstp_task( void) { bool bytes_remaining; bool received_frame; /* only do receive state machine while we don't have a frame */ if ((MSTP_Port.ReceivedValidFrame == false) && (MSTP_Port.ReceivedInvalidFrame == false)) { do { bytes_remaining = RS485_Check_UART_Data(&MSTP_Port); MSTP_Receive_Frame_FSM(&MSTP_Port); received_frame = MSTP_Port.ReceivedValidFrame || MSTP_Port.ReceivedInvalidFrame; if (received_frame) break; } while (bytes_remaining); } /* only do master state machine while rx is idle */ if (MSTP_Port.receive_state == MSTP_RECEIVE_STATE_IDLE) { if (MSTP_Port.This_Station <= DEFAULT_MAX_MASTER) { while (MSTP_Master_Node_FSM(&MSTP_Port)) { /* do nothing while some states fast transition */ }; } } /* see if there is a packet available, and a place to put the reply (if necessary) and process it */ if (Receive_Buffer.ready && !MSTP_Port.TxReady) { if (Receive_Buffer.pdu_len) { MSTP_Packets++; npdu_handler(&Receive_Buffer.address, &Receive_Buffer.pdu[0], Receive_Buffer.pdu_len); } Receive_Buffer.ready = false; } return; }