Trimmed unnecessary code from MS/TP receive FSM. Updated rs485 for AVR and ARM.

This commit is contained in:
skarg
2007-09-07 05:42:53 +00:00
parent 8fb328ce23
commit 62a7475817
5 changed files with 29 additions and 63 deletions
+17 -47
View File
@@ -238,7 +238,6 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
mstp_port->SilenceTimerReset(); mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
/* wait for the start of a frame. */ /* wait for the start of a frame. */
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
} else if (mstp_port->DataAvailable == true) { } else if (mstp_port->DataAvailable == true) {
#if PRINT_ENABLED_RECEIVE_DATA #if PRINT_ENABLED_RECEIVE_DATA
#if (defined(_WIN32) && defined(TEST_DLMSTP)) #if (defined(_WIN32) && defined(TEST_DLMSTP))
@@ -252,9 +251,6 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
#endif #endif
/* Preamble1 */ /* Preamble1 */
if (mstp_port->DataRegister == 0x55) { if (mstp_port->DataRegister == 0x55) {
mstp_port->DataAvailable = false;
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
/* receive the remainder of the frame. */ /* receive the remainder of the frame. */
mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE;
} }
@@ -263,12 +259,11 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
#if PRINT_ENABLED_RECEIVE_DATA #if PRINT_ENABLED_RECEIVE_DATA
fprintf(stderr, "\n"); fprintf(stderr, "\n");
#endif #endif
mstp_port->DataAvailable = false;
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
/* wait for the start of a frame. */ /* wait for the start of a frame. */
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
} }
mstp_port->DataAvailable = false;
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
} }
break; break;
/* In the PREAMBLE state, the node waits for the second octet of the preamble. */ /* In the PREAMBLE state, the node waits for the second octet of the preamble. */
@@ -292,9 +287,6 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
#endif #endif
/* Preamble2 */ /* Preamble2 */
if (mstp_port->DataRegister == 0xFF) { if (mstp_port->DataRegister == 0xFF) {
mstp_port->DataAvailable = false;
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
mstp_port->Index = 0; mstp_port->Index = 0;
mstp_port->HeaderCRC = 0xFF; mstp_port->HeaderCRC = 0xFF;
/* receive the remainder of the frame. */ /* receive the remainder of the frame. */
@@ -302,20 +294,16 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
} }
/* ignore RepeatedPreamble1 */ /* ignore RepeatedPreamble1 */
else if (mstp_port->DataRegister == 0x55) { else if (mstp_port->DataRegister == 0x55) {
mstp_port->DataAvailable = false;
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
/* wait for the second preamble octet. */ /* wait for the second preamble octet. */
mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE;
} }
/* NotPreamble */ /* NotPreamble */
else { else {
mstp_port->DataAvailable = false;
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
/* wait for the start of a frame. */ /* wait for the start of a frame. */
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
} }
mstp_port->DataAvailable = false;
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
} }
break; break;
/* In the HEADER state, the node waits for the fixed message header. */ /* In the HEADER state, the node waits for the fixed message header. */
@@ -349,73 +337,50 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
#endif #endif
/* FrameType */ /* FrameType */
if (mstp_port->Index == 0) { if (mstp_port->Index == 0) {
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
mstp_port->HeaderCRC = mstp_port->HeaderCRC =
CRC_Calc_Header(mstp_port->DataRegister, CRC_Calc_Header(mstp_port->DataRegister,
mstp_port->HeaderCRC); mstp_port->HeaderCRC);
mstp_port->FrameType = mstp_port->DataRegister; mstp_port->FrameType = mstp_port->DataRegister;
mstp_port->DataAvailable = false;
mstp_port->Index = 1; mstp_port->Index = 1;
mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER;
} }
/* Destination */ /* Destination */
else if (mstp_port->Index == 1) { else if (mstp_port->Index == 1) {
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
mstp_port->HeaderCRC = mstp_port->HeaderCRC =
CRC_Calc_Header(mstp_port->DataRegister, CRC_Calc_Header(mstp_port->DataRegister,
mstp_port->HeaderCRC); mstp_port->HeaderCRC);
mstp_port->DestinationAddress = mstp_port->DataRegister; mstp_port->DestinationAddress = mstp_port->DataRegister;
mstp_port->DataAvailable = false;
mstp_port->Index = 2; mstp_port->Index = 2;
mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER;
} }
/* Source */ /* Source */
else if (mstp_port->Index == 2) { else if (mstp_port->Index == 2) {
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
mstp_port->HeaderCRC = mstp_port->HeaderCRC =
CRC_Calc_Header(mstp_port->DataRegister, CRC_Calc_Header(mstp_port->DataRegister,
mstp_port->HeaderCRC); mstp_port->HeaderCRC);
mstp_port->SourceAddress = mstp_port->DataRegister; mstp_port->SourceAddress = mstp_port->DataRegister;
mstp_port->DataAvailable = false;
mstp_port->Index = 3; mstp_port->Index = 3;
mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER;
} }
/* Length1 */ /* Length1 */
else if (mstp_port->Index == 3) { else if (mstp_port->Index == 3) {
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
mstp_port->HeaderCRC = mstp_port->HeaderCRC =
CRC_Calc_Header(mstp_port->DataRegister, CRC_Calc_Header(mstp_port->DataRegister,
mstp_port->HeaderCRC); mstp_port->HeaderCRC);
mstp_port->DataLength = mstp_port->DataRegister * 256; mstp_port->DataLength = mstp_port->DataRegister * 256;
mstp_port->DataAvailable = false;
mstp_port->Index = 4; mstp_port->Index = 4;
mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER;
} }
/* Length2 */ /* Length2 */
else if (mstp_port->Index == 4) { else if (mstp_port->Index == 4) {
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
mstp_port->HeaderCRC = mstp_port->HeaderCRC =
CRC_Calc_Header(mstp_port->DataRegister, CRC_Calc_Header(mstp_port->DataRegister,
mstp_port->HeaderCRC); mstp_port->HeaderCRC);
mstp_port->DataLength += mstp_port->DataRegister; mstp_port->DataLength += mstp_port->DataRegister;
mstp_port->DataAvailable = false;
mstp_port->Index = 5; mstp_port->Index = 5;
mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER;
} }
/* HeaderCRC */ /* HeaderCRC */
else if (mstp_port->Index == 5) { else if (mstp_port->Index == 5) {
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
mstp_port->HeaderCRC = mstp_port->HeaderCRC =
CRC_Calc_Header(mstp_port->DataRegister, CRC_Calc_Header(mstp_port->DataRegister,
mstp_port->HeaderCRC); mstp_port->HeaderCRC);
mstp_port->HeaderCRCActual = mstp_port->DataRegister; mstp_port->HeaderCRCActual = mstp_port->DataRegister;
mstp_port->DataAvailable = false;
/* don't wait for next state - do it here */ /* don't wait for next state - do it here */
/* MSTP_RECEIVE_STATE_HEADER_CRC */ /* MSTP_RECEIVE_STATE_HEADER_CRC */
if (mstp_port->HeaderCRC != 0x55) { if (mstp_port->HeaderCRC != 0x55) {
@@ -430,9 +395,10 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
/* wait for the start of the next frame. */ /* wait for the start of the next frame. */
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
} else { } else {
/* FIXME: if we don't decode the data, we could /* Note: proposed change to BACnet MSTP state machine!
get confused if the Preamble 55 FF is part If we don't decode data that is not for us, we could
of the data. */ get confused about the start if the Preamble 55 FF
is part of the data. */
/* Data */ /* Data */
if ((mstp_port->DataLength) && if ((mstp_port->DataLength) &&
(mstp_port->DataLength <= mstp_port->InputBufferSize)) { (mstp_port->DataLength <= mstp_port->InputBufferSize)) {
@@ -444,7 +410,7 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
MSTP_RECEIVE_STATE_DATA; MSTP_RECEIVE_STATE_DATA;
} else { } else {
/* FrameTooLong */ /* FrameTooLong */
if (mstp_port->DataLength > MAX_MPDU) { if (mstp_port->DataLength) {
#if PRINT_ENABLED_RECEIVE_ERRORS #if PRINT_ENABLED_RECEIVE_ERRORS
fprintf(stderr,"MSTP: Rx Header: FrameTooLong %d\n", fprintf(stderr,"MSTP: Rx Header: FrameTooLong %d\n",
mstp_port->DataLength); mstp_port->DataLength);
@@ -480,8 +446,6 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
/* not per MS/TP standard, but it is a case not covered */ /* not per MS/TP standard, but it is a case not covered */
else { else {
mstp_port->ReceiveError = false; mstp_port->ReceiveError = false;
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
/* indicate that an error has occurred during */ /* indicate that an error has occurred during */
/* the reception of a frame */ /* the reception of a frame */
mstp_port->ReceivedInvalidFrame = true; mstp_port->ReceivedInvalidFrame = true;
@@ -492,11 +456,17 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port)
/* wait for the start of a frame. */ /* wait for the start of a frame. */
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
} }
mstp_port->SilenceTimerReset();
INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);
mstp_port->DataAvailable = false;
} }
break; break;
/* In the HEADER_CRC state, the node validates the CRC on the fixed */ /* In the HEADER_CRC state, the node validates the CRC on the fixed */
/* message header. */ /* message header. */
case MSTP_RECEIVE_STATE_HEADER_CRC: case MSTP_RECEIVE_STATE_HEADER_CRC:
/* note: we should never get to this state since we shortcut
it earlier in the state machine, and never set this state */
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
break; break;
/* In the DATA state, the node waits for the data portion of a frame. */ /* In the DATA state, the node waits for the data portion of a frame. */
case MSTP_RECEIVE_STATE_DATA: case MSTP_RECEIVE_STATE_DATA:
+3 -4
View File
@@ -269,12 +269,11 @@ int main(void)
RS485_Initialize(); RS485_Initialize();
/* receive task */ /* receive task */
for (;;) { for (;;) {
if (RS485_DataAvailable(&DataRegister)) { if (RS485_ReceiveError()) {
fprintf(stderr,"%02X ",DataRegister);
} else if (RS485_ReceiveError()) {
fprintf(stderr,"ERROR "); fprintf(stderr,"ERROR ");
} else if (RS485_DataAvailable(&DataRegister)) {
fprintf(stderr,"%02X ",DataRegister);
} }
} }
} }
#endif #endif
-1
View File
@@ -36,7 +36,6 @@ extern "C" {
void RS485_Transmitter_Enable(bool enable); void RS485_Transmitter_Enable(bool enable);
/* NOTE: Only able to send up to 255 bytes at a time */
void RS485_Send_Data( void RS485_Send_Data(
uint8_t * buffer, /* data to send */ uint8_t * buffer, /* data to send */
uint16_t nbytes); /* number of bytes of data */ uint16_t nbytes); /* number of bytes of data */
+8 -9
View File
@@ -166,7 +166,7 @@ void RS485_Transmitter_Enable(bool enable)
*****************************************************************************/ *****************************************************************************/
void RS485_Send_Data( void RS485_Send_Data(
uint8_t * buffer, /* data to send */ uint8_t * buffer, /* data to send */
uint8_t nbytes) /* number of bytes of data */ uint16_t nbytes) /* number of bytes of data */
{ {
while (nbytes) { while (nbytes) {
while (!BIT_CHECK(UCSR0A,UDRE0)) { while (!BIT_CHECK(UCSR0A,UDRE0)) {
@@ -180,7 +180,7 @@ void RS485_Send_Data(
/* do nothing - wait until the entire frame in the /* do nothing - wait until the entire frame in the
Transmit Shift Register has been shifted out */ Transmit Shift Register has been shifted out */
} }
/* per MSTP spec */ /* per MSTP spec, sort of */
Timer_Silence_Reset(); Timer_Silence_Reset();
} }
@@ -223,13 +223,13 @@ bool RS485_ReceiveError(void)
bool RS485_DataAvailable(uint8_t *data) bool RS485_DataAvailable(uint8_t *data)
{ {
bool uart_data = false; bool uart_data = false;
/* check for data */ /* check for data */
if (BIT_CHECK(UCSR0A,RXC0)) { if (BIT_CHECK(UCSR0A,RXC0)) {
*data = UDR0; *data = UDR0;
uart_data = true; uart_data = true;
} }
return uart_data; return uart_data;
} }
@@ -243,13 +243,12 @@ int main(void)
RS485_Initialize(); RS485_Initialize();
/* receive task */ /* receive task */
for (;;) { for (;;) {
if (RS485_DataAvailable(&DataRegister)) { if (RS485_ReceiveError()) {
fprintf(stderr,"%02X ",DataRegister);
} else if (RS485_ReceiveError()) {
fprintf(stderr,"ERROR "); fprintf(stderr,"ERROR ");
} else if (RS485_DataAvailable(&DataRegister)) {
fprintf(stderr,"%02X ",DataRegister);
} }
} }
} }
#endif /* TEST_ABORT */ #endif /* TEST_RS485 */
+1 -2
View File
@@ -46,10 +46,9 @@ extern "C" {
void RS485_Transmitter_Enable(bool enable); void RS485_Transmitter_Enable(bool enable);
/* NOTE: Only able to send up to 255 bytes at a time */
void RS485_Send_Data( void RS485_Send_Data(
uint8_t * buffer, /* data to send */ uint8_t * buffer, /* data to send */
uint8_t nbytes); /* number of bytes of data */ uint16_t nbytes); /* number of bytes of data */
bool RS485_ReceiveError(void); bool RS485_ReceiveError(void);
bool RS485_DataAvailable(uint8_t *data); bool RS485_DataAvailable(uint8_t *data);