diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/Makefile b/bacnet-stack/ports/bdk-atxx4-mstp/Makefile index 0e29217c..9ed9b656 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/Makefile +++ b/bacnet-stack/ports/bdk-atxx4-mstp/Makefile @@ -88,6 +88,7 @@ DEMOSRC = $(BACNET_DEMO)/handler/txbuf.c \ # core BACnet stack files CORESRC = \ $(BACNET_CORE)/fifo.c \ + $(BACNET_CORE)/ringbuf.c \ $(BACNET_CORE)/memcopy.c \ $(BACNET_CORE)/crc.c \ $(BACNET_CORE)/apdu.c \ diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.aps b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.aps index b00c3f67..cd72ca0e 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.aps +++ b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.aps @@ -1 +1 @@ -bacnet29-Apr-2009 08:16:5311-Feb-2010 10:52:30241029-Apr-2009 08:16:5344, 15, 0, 623AVR GCCbacnet.elfD:\code\bacnet-stack\ports\bdk-atxx4-mstp\JTAGICE mkIIATmega644P.xmlfalseR00R01R02R03R04R05R06R07R08R09R10R11R12R13R14R15R16R17R18R19R20R21R22R23R24R25R26R27R28R29R30R31Auto000main.ctimer2.ceeprom.cinit.cinput.cled.crs485.cseeprom.cserial.cstack.cdlmstp.cbo.cbi.cai.cdevice.ctimer.heeprom.hhardware.hiar2gcc.hinit.hinput.hled.hnvdata.hrs485.hseeprom.hserial.hMakefiledefaultYESMakefileatmega644p111bacnet.elfdefault\1-Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enumsdefault1C:\WinAVR-20090313\bin\avr-gcc.exeC:\WinAVR-20090313\utils\bin\make.exe00000main.c25900001Makefile1 +bacnet29-Apr-2009 08:16:5312-Sep-2010 23:23:02241029-Apr-2009 08:16:5344, 15, 0, 623AVR GCCbacnet.elfD:\code\bacnet-stack\ports\bdk-atxx4-mstp\JTAGICE mkIIATmega644P.xmlfalseR00R01R02R03R04R05R06R07R08R09R10R11R12R13R14R15R16R17R18R19R20R21R22R23R24R25R26R27R28R29R30R31Auto000main.ctimer2.ceeprom.cinit.cinput.cled.crs485.cseeprom.cserial.cstack.cdlmstp.cbo.cbi.cai.cdevice.ctimer.heeprom.hhardware.hiar2gcc.hinit.hinput.hled.hnvdata.hrs485.hseeprom.hserial.hMakefiledefaultYESMakefileatmega644p111bacnet.elfdefault\1-Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enumsdefault1C:\WinAVR-20100110\bin\avr-gcc.exeC:\WinAVR-20100110\utils\bin\make.exe00000main.c25900001Makefile1 diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.ewp b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.ewp index a670ff64..30ce60dd 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.ewp +++ b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.ewp @@ -2041,6 +2041,9 @@ $PROJ_DIR$\..\..\src\reject.c + + $PROJ_DIR$\..\..\src\ringbuf.c + $PROJ_DIR$\..\..\src\rp.c diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/dlmstp.c b/bacnet-stack/ports/bdk-atxx4-mstp/dlmstp.c index cdab7b2f..a089c993 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/dlmstp.c +++ b/bacnet-stack/ports/bdk-atxx4-mstp/dlmstp.c @@ -38,12 +38,14 @@ #include #include "bacdef.h" #include "dlmstp.h" +#include "mstpdef.h" #include "rs485.h" #include "crc.h" #include "npdu.h" #include "bits.h" #include "bytes.h" #include "bacaddr.h" +#include "ringbuf.h" #include "timer.h" #include "eeprom.h" #include "nvdata.h" @@ -53,52 +55,6 @@ Only one MS/TP datalink layer */ -/* The value 255 is used to denote broadcast when used as a */ -/* destination address but is not allowed as a value for a station. */ -/* Station addresses for master nodes can be 0-127. */ -/* Station addresses for slave nodes can be 127-254. */ -#define MSTP_BROADCAST_ADDRESS 255 - -/* MS/TP Frame Type */ -/* Frame Types 8 through 127 are reserved by ASHRAE. */ -#define FRAME_TYPE_TOKEN 0 -#define FRAME_TYPE_POLL_FOR_MASTER 1 -#define FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER 2 -#define FRAME_TYPE_TEST_REQUEST 3 -#define FRAME_TYPE_TEST_RESPONSE 4 -#define FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY 5 -#define FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY 6 -#define FRAME_TYPE_REPLY_POSTPONED 7 -/* Frame Types 128 through 255: Proprietary Frames */ -/* These frames are available to vendors as proprietary (non-BACnet) frames. */ -/* The first two octets of the Data field shall specify the unique vendor */ -/* identification code, most significant octet first, for the type of */ -/* vendor-proprietary frame to be conveyed. The length of the data portion */ -/* of a Proprietary frame shall be in the range of 2 to 501 octets. */ -#define FRAME_TYPE_PROPRIETARY_MIN 128 -#define FRAME_TYPE_PROPRIETARY_MAX 255 - -/* receive FSM states */ -typedef enum { - MSTP_RECEIVE_STATE_IDLE = 0, - MSTP_RECEIVE_STATE_PREAMBLE = 1, - MSTP_RECEIVE_STATE_HEADER = 2, - MSTP_RECEIVE_STATE_DATA = 3 -} MSTP_RECEIVE_STATE; - -/* master node FSM states */ -typedef enum { - MSTP_MASTER_STATE_INITIALIZE = 0, - MSTP_MASTER_STATE_IDLE = 1, - MSTP_MASTER_STATE_USE_TOKEN = 2, - MSTP_MASTER_STATE_WAIT_FOR_REPLY = 3, - MSTP_MASTER_STATE_DONE_WITH_TOKEN = 4, - MSTP_MASTER_STATE_PASS_TOKEN = 5, - MSTP_MASTER_STATE_NO_TOKEN = 6, - MSTP_MASTER_STATE_POLL_FOR_MASTER = 7, - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST = 8 -} MSTP_MASTER_STATE; - /* The state of the Receive State Machine */ static MSTP_RECEIVE_STATE Receive_State; /* When a master node is powered up or reset, */ @@ -114,22 +70,18 @@ static struct mstp_flag_t { /* if a valid frame is received. */ /* Set to FALSE by the main state machine. */ unsigned ReceivedValidFrame:1; + /* set to TRUE when we get a frame not for us */ + unsigned ReceivedValidFrameNotForUs:1; /* A Boolean flag set to TRUE by the master machine if this node is the */ /* only known master node. */ unsigned SoleMaster:1; - /* A Boolean flag set TRUE by the datalink transmit if a - frame is pending */ - unsigned TransmitPacketPending:1; - /* A Boolean flag set TRUE by the datalink transmit if a - pending packet is DataExpectingReply */ - unsigned TransmitPacketDER:1; /* A Boolean flag set TRUE by the datalink if a packet has been received, but not processed. */ unsigned ReceivePacketPending:1; } MSTP_Flag; /* Used to store the data length of a received frame. */ -static uint16_t DataLength; +static uint32_t DataLength; /* Used to store the destination address of a received frame. */ static uint8_t DestinationAddress; /* Used to count the number of received octets or errors. */ @@ -160,23 +112,13 @@ static uint8_t This_Station; /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ -#define Nmax_info_frames 1 +static uint8_t Nmax_info_frames = 1; /* This parameter represents the value of the Max_Master property of the */ /* node's Device object. The value of Max_Master specifies the highest */ /* allowable address for master nodes. The value of Max_Master shall be */ /* less than or equal to 127. If Max_Master is not writable in a node, */ /* its value shall be 127. */ static uint8_t Nmax_master = 127; -/* An array of octets, used to store octets for transmitting */ -/* OutputBuffer is indexed from 0 to OutputBufferSize-1. */ -/* The MAX_PDU size of a frame is MAX_APDU + MAX_NPDU octets. */ -/* FIXME: assign this to an actual array of bytes! */ -/* Note: the buffer is designed as a pointer since some compilers - and microcontroller architectures have limits as to places to - hold contiguous memory. */ -static uint8_t *TransmitPacket; -static uint16_t TransmitPacketLen; -static uint8_t TransmitPacketDest; /* The time without a DataAvailable or ReceiveError event before declaration */ /* of loss of token: 500 milliseconds. */ @@ -239,6 +181,20 @@ static uint8_t TransmitPacketDest; /* we need to be able to increment without rolling over */ #define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} +/* data structure for MS/TP PDU Queue */ +struct mstp_pdu_packet { + bool data_expecting_reply; + uint8_t destination_mac; + uint16_t length; + uint8_t buffer[MAX_MPDU]; +}; +/* count must be a power of 2 for ringbuf library */ +#ifndef MSTP_PDU_PACKET_COUNT +#define MSTP_PDU_PACKET_COUNT 2 +#endif +static struct mstp_pdu_packet PDU_Buffer[MSTP_PDU_PACKET_COUNT]; +static RING_BUFFER PDU_Queue; + bool dlmstp_init( char *ifname) { @@ -248,6 +204,11 @@ bool dlmstp_init( if (Nmax_master > 127) { Nmax_master = 127; } + Ringbuf_Init( + &PDU_Queue, + (uint8_t *)&PDU_Buffer, + sizeof(struct mstp_pdu_packet), + MSTP_PDU_PACKET_COUNT); return true; } @@ -572,37 +533,38 @@ static void MSTP_Receive_Frame_FSM( /* wait for the start of the next frame. */ Receive_State = MSTP_RECEIVE_STATE_IDLE; } else { - /* Note: proposed change to BACnet MSTP state machine! - If we don't decode data that is not for us, we could - get confused about the start if the Preamble 55 FF - is part of the data. */ - if ((DataLength) && (DataLength <= InputBufferSize)) { - /* Data */ - Index = 0; - DataCRC = 0xFFFF; - /* receive the data portion of the frame. */ - Receive_State = MSTP_RECEIVE_STATE_DATA; - } else { - if (DataLength == 0) { - /* NoData */ - if ((DestinationAddress == This_Station) || - (DestinationAddress == - MSTP_BROADCAST_ADDRESS)) { - /* ForUs */ - /* indicate that a frame with - no data has been received */ - MSTP_Flag.ReceivedValidFrame = true; - } else { - /* NotForUs - drop */ - } + if (DataLength == 0) { + /* NoData */ + if ((DestinationAddress == This_Station) || + (DestinationAddress == + MSTP_BROADCAST_ADDRESS)) { + /* ForUs */ + /* indicate that a frame with + no data has been received */ + MSTP_Flag.ReceivedValidFrame = true; } else { - /* FrameTooLong */ - /* indicate that a frame with an illegal or */ - /* unacceptable data length has been received */ - MSTP_Flag.ReceivedInvalidFrame = true; + /* NotForUs */ + MSTP_Flag.ReceivedValidFrameNotForUs = true; } /* wait for the start of the next frame. */ Receive_State = MSTP_RECEIVE_STATE_IDLE; + } else { + /* receive the data portion of the frame. */ + if ((DestinationAddress == This_Station) || + (DestinationAddress == MSTP_BROADCAST_ADDRESS)) { + if (DataLength <= InputBufferSize) { + /* Data */ + Receive_State = MSTP_RECEIVE_STATE_DATA; + } else { + /* FrameTooLong */ + Receive_State = MSTP_RECEIVE_STATE_SKIP_DATA; + } + } else { + /* NotForUs */ + Receive_State = MSTP_RECEIVE_STATE_SKIP_DATA; + } + Index = 0; + DataCRC = 0xFFFF; } } } else { @@ -615,6 +577,7 @@ static void MSTP_Receive_Frame_FSM( } break; case MSTP_RECEIVE_STATE_DATA: + case MSTP_RECEIVE_STATE_SKIP_DATA: /* In the DATA state, the node waits for the data portion of a frame. */ if (rs485_silence_time_elapsed(Tframe_abort)) { @@ -639,7 +602,9 @@ static void MSTP_Receive_Frame_FSM( if (Index < DataLength) { /* DataOctet */ DataCRC = CRC_Calc_Data(DataRegister, DataCRC); - InputBuffer[Index] = DataRegister; + if (Index < InputBufferSize) { + InputBuffer[Index] = DataRegister; + } Index++; } else if (Index == DataLength) { /* CRC1 */ @@ -651,17 +616,21 @@ static void MSTP_Receive_Frame_FSM( /* STATE DATA CRC - no need for new state */ /* indicate the complete reception of a valid frame */ if (DataCRC == 0xF0B8) { - if ((DestinationAddress == This_Station) || - (DestinationAddress == MSTP_BROADCAST_ADDRESS)) { + if (Receive_State == MSTP_RECEIVE_STATE_DATA) { /* ForUs */ - /* indicate that a frame with no data - has been received */ MSTP_Flag.ReceivedValidFrame = true; + } else { + /* NotForUs */ + MSTP_Flag.ReceivedValidFrameNotForUs = true; } + } else { MSTP_Flag.ReceivedInvalidFrame = true; } Receive_State = MSTP_RECEIVE_STATE_IDLE; + } else { + MSTP_Flag.ReceivedInvalidFrame = true; + Receive_State = MSTP_RECEIVE_STATE_IDLE; } } break; @@ -722,6 +691,8 @@ static bool MSTP_Master_Node_FSM( bool matched = false; /* transition immediately to the next state */ bool transition_now = false; + /* packet from the PDU Queue */ + struct mstp_pdu_packet *pkt; /* some calculations that several states need */ next_poll_station = (Poll_Station + 1) % (Nmax_master + 1); @@ -752,6 +723,8 @@ static bool MSTP_Master_Node_FSM( some bytes and had a timeout for some reason */ MSTP_Flag.ReceivedValidFrame = false; MSTP_Flag.ReceivedInvalidFrame = false; + MSTP_Flag.ReceivedValidFrameNotForUs = false; + Master_State = MSTP_MASTER_STATE_NO_TOKEN; transition_now = true; } else if (MSTP_Flag.ReceivedInvalidFrame == true) { /* ReceivedInvalidFrame */ @@ -810,26 +783,27 @@ static bool MSTP_Master_Node_FSM( /* proprietary frames. */ case MSTP_MASTER_STATE_USE_TOKEN: /* Note: We could wait for up to Tusage_delay */ - if (!MSTP_Flag.TransmitPacketPending) { + if (Ringbuf_Empty(&PDU_Queue)) { /* NothingToSend */ FrameCount = Nmax_info_frames; Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; transition_now = true; } else { uint8_t frame_type; - if (MSTP_Flag.TransmitPacketDER) { + pkt = (struct mstp_pdu_packet *)Ringbuf_Pop_Front( + &PDU_Queue); + if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } - MSTP_Send_Frame(frame_type, TransmitPacketDest, This_Station, - (uint8_t *) & TransmitPacket[0], TransmitPacketLen); - MSTP_Flag.TransmitPacketPending = false; + MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, + (uint8_t *) & pkt->buffer[0], pkt->length); FrameCount++; switch (frame_type) { case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: /* SendAndWait */ - if (TransmitPacketDest == MSTP_BROADCAST_ADDRESS) + if (pkt->destination_mac == MSTP_BROADCAST_ADDRESS) Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; else Master_State = MSTP_MASTER_STATE_WAIT_FOR_REPLY; @@ -1140,13 +1114,16 @@ static bool MSTP_Master_Node_FSM( /* a proprietary frame that expects a reply is received. */ case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: /* Note: we could wait for up to Treply_delay */ - if (MSTP_Flag.TransmitPacketPending) { + matched = false; + if (!Ringbuf_Empty(&PDU_Queue)) { + pkt = (struct mstp_pdu_packet *)Ringbuf_Get_Front( + &PDU_Queue); matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0], - DataLength, SourceAddress, &TransmitPacket[0], - TransmitPacketLen, TransmitPacketDest); + DataLength, SourceAddress, &pkt->buffer[0], + pkt->length, pkt->destination_mac); } - if (MSTP_Flag.TransmitPacketPending && matched) { + if (matched) { /* Reply */ /* If a reply is available from the higher layers */ /* within Treply_delay after the reception of the */ @@ -1155,14 +1132,15 @@ static bool MSTP_Master_Node_FSM( /* then call MSTP_Send_Frame to transmit the reply frame */ /* and enter the IDLE state to wait for the next frame. */ uint8_t frame_type; - if (MSTP_Flag.TransmitPacketDER) { + pkt = (struct mstp_pdu_packet *)Ringbuf_Pop_Front( + &PDU_Queue); + if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } - MSTP_Send_Frame(frame_type, TransmitPacketDest, This_Station, - (uint8_t *) & TransmitPacket[0], TransmitPacketLen); - MSTP_Flag.TransmitPacketPending = false; + MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, + (uint8_t *) & pkt->buffer[0], pkt->length); Master_State = MSTP_MASTER_STATE_IDLE; } else { /* DeferredReply */ @@ -1197,14 +1175,18 @@ int dlmstp_send_pdu( unsigned pdu_len) { /* number of bytes of data */ int bytes_sent = 0; + struct mstp_pdu_packet *pkt; + uint16_t i = 0; - if (MSTP_Flag.TransmitPacketPending == false) { - MSTP_Flag.TransmitPacketDER = npdu_data->data_expecting_reply; - TransmitPacket = pdu; - TransmitPacketLen = pdu_len; + pkt = (struct mstp_pdu_packet *)Ringbuf_Alloc(&PDU_Queue); + if (pkt) { + pkt->data_expecting_reply = npdu_data->data_expecting_reply; + for (i = 0; i < pdu_len; i++) { + pkt->buffer[i] = pdu[i]; + } + pkt->length = pdu_len; + pkt->destination_mac = dest->mac[0]; bytes_sent = pdu_len; - TransmitPacketDest = dest->mac[0]; - MSTP_Flag.TransmitPacketPending = true; } return bytes_sent; @@ -1226,6 +1208,7 @@ uint16_t dlmstp_receive( } /* only do receive state machine while we don't have a frame */ if ((MSTP_Flag.ReceivedValidFrame == false) && + (MSTP_Flag.ReceivedValidFrameNotForUs == false) && (MSTP_Flag.ReceivedInvalidFrame == false)) { MSTP_Receive_Frame_FSM(); } @@ -1259,7 +1242,7 @@ void dlmstp_set_mac_address( mac_address, EEPROM_MSTP_MAC_ADDR); */ if (mac_address > Nmax_master) - dlmstp_set_max_master(mac_address); + dlmstp_set_max_master(127); } return; @@ -1281,7 +1264,11 @@ uint8_t dlmstp_mac_address( void dlmstp_set_max_info_frames( uint8_t max_info_frames) { - /* not writable - fixed at 1 */ + if (max_info_frames >= 1) { + Nmax_info_frames = max_info_frames; + } + + return; } uint8_t dlmstp_max_info_frames(