From 19a823350edd073e03abd840fcf91667b285f9bc Mon Sep 17 00:00:00 2001 From: skarg Date: Tue, 29 Aug 2006 20:57:30 +0000 Subject: [PATCH] Updated files for PIC18F port. --- bacnet-stack/ports/pic18/dlmstp.c | 70 +++++++++++++++++++++++-------- bacnet-stack/ports/pic18/main.c | 2 - 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/bacnet-stack/ports/pic18/dlmstp.c b/bacnet-stack/ports/pic18/dlmstp.c index 4c9774e2..29793470 100644 --- a/bacnet-stack/ports/pic18/dlmstp.c +++ b/bacnet-stack/ports/pic18/dlmstp.c @@ -26,6 +26,9 @@ #include #include #include +#if PRINT_ENABLED +#include +#endif #include "bacdef.h" #include "mstp.h" #include "dlmstp.h" @@ -60,9 +63,8 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) { /* number of bytes of data */ - bool status; int bytes_sent = 0; - int npdu_len = 0; + unsigned npdu_len = 0; uint8_t frame_type = 0; uint8_t destination = 0; /* destination address */ BACNET_ADDRESS src; @@ -92,7 +94,8 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ return -4; } memmove(&PDU_Buffer[npdu_len], pdu, pdu_len); - bytes_sent = MSTP_Create_Frame(&MSTP_Port.TxBuffer[0], + bytes_sent = MSTP_Create_Frame( + (uint8_t *) & MSTP_Port.TxBuffer[0], sizeof(MSTP_Port.TxBuffer), MSTP_Port.TxFrameType, destination, @@ -104,15 +107,6 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ return bytes_sent; } -void dlmstp_task(void) -{ - RS485_Check_UART_Data(&MSTP_Port); - MSTP_Receive_Frame_FSM(&MSTP_Port); - - RS485_Process_Tx_Message(); - MSTP_Master_Node_FSM(&MSTP_Port); -} - /* called about once a millisecond */ void dlmstp_millisecond_timer(void) { @@ -128,7 +122,17 @@ uint16_t dlmstp_receive(BACNET_ADDRESS * src, /* source address */ { uint16_t pdu_len = 0; - (void) timeout; + (void) timeout; + /* only do receive state machine while we don't have a frame */ + if ((MSTP_Port.ReceivedValidFrame == false) && + (MSTP_Port.ReceivedInvalidFrame == false)) { + RS485_Check_UART_Data(&MSTP_Port); + MSTP_Receive_Frame_FSM(&MSTP_Port); + } + /* only do master state machine while rx is idle */ + if (MSTP_Port.receive_state == MSTP_RECEIVE_STATE_IDLE) { + while (MSTP_Master_Node_FSM(&MSTP_Port)) {}; + } /* see if there is a packet available */ if (Receive_Buffer.ready) { memmove(src, &Receive_Buffer.address, @@ -141,14 +145,46 @@ uint16_t dlmstp_receive(BACNET_ADDRESS * src, /* source address */ return pdu_len; } +void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) +{ + int i = 0; + + if (mstp_address == MSTP_BROADCAST_ADDRESS) { + /* mac_len = 0 if broadcast address */ + src->mac_len = 0; + src->mac[0] = 0; + } else { + src->mac_len = 1; + src->mac[0] = mstp_address; + } + /* fill with 0's starting with index 1; index 0 filled above */ + for (i = 1; i < MAX_MAC_LEN; i++) { + src->mac[i] = 0; + } + src->net = 0; + src->len = 0; + for (i = 0; i < MAX_MAC_LEN; i++) { + src->adr[i] = 0; + } +} + /* for the MS/TP state machine to use for putting received data */ -uint16_t dlmstp_put_receive(BACNET_ADDRESS * src, /* source address */ +uint16_t dlmstp_put_receive(uint8_t src, /* source MS/TP address */ uint8_t * pdu, /* PDU data */ uint16_t pdu_len) { - memmove(&Receive_Buffer.address, src, sizeof(Receive_Buffer.address)); - Receive_Buffer.pdu_len = pdu_len; - memmove(Receive_Buffer.pdu, pdu, sizeof(Receive_Buffer.pdu)); + if (Receive_Buffer.ready) { + /* FIXME: what to do when we miss a message? */ + pdu_len = 0; + } else if (pdu_len < sizeof(Receive_Buffer.pdu)) { + dlmstp_fill_bacnet_address(&Receive_Buffer.address, src); + Receive_Buffer.pdu_len = pdu_len; + memmove(Receive_Buffer.pdu, pdu, pdu_len); + Receive_Buffer.ready = true; + } else { + /* FIXME: message too large? */ + pdu_len = 0; + } return pdu_len; } diff --git a/bacnet-stack/ports/pic18/main.c b/bacnet-stack/ports/pic18/main.c index b6fb5c70..5b8b6adb 100644 --- a/bacnet-stack/ports/pic18/main.c +++ b/bacnet-stack/ports/pic18/main.c @@ -172,9 +172,7 @@ void main(void) /* input */ Check_Timer_Milliseconds(); - dlmstp_task(); pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); - /* process */ if (pdu_len) { npdu_handler(&src, &Rx_Buf[0], pdu_len);