Modified the AT91SAM7S port and the ATmega168 port to use a slimmed down MS/TP state machine and datalink layer. Tested on AT91SAM7S-EK board.

This commit is contained in:
skarg
2007-09-07 01:34:19 +00:00
parent 4f34f6994b
commit 8fb328ce23
10 changed files with 1285 additions and 359 deletions
+4 -11
View File
@@ -56,21 +56,14 @@
#define BACNET_MIN_PRIORITY 1 #define BACNET_MIN_PRIORITY 1
#define BACNET_MAX_PRIORITY 16 #define BACNET_MAX_PRIORITY 16
/* embedded systems need fixed name sizes */
#define MAX_OBJECT_NAME 10
/* common object properties */
typedef struct BACnet_Object_Data {
uint32_t Object_Identifier;
char Object_Name[MAX_OBJECT_NAME];
BACNET_OBJECT_TYPE Object_Type;
} BACNET_OBJECT_DATA;
#define BACNET_BROADCAST_NETWORK 0xFFFF #define BACNET_BROADCAST_NETWORK 0xFFFF
/* IPv6 (16 octets) coupled with port number (2 octets) */ /* IPv6 (16 octets) coupled with port number (2 octets) */
/* note: you could be lame and only support 6 octets */
/* FIXME: mac[] only needs to be as big as our datalink MAC */
#define MAX_MAC_LEN 18 #define MAX_MAC_LEN 18
struct BACnet_Device_Address { struct BACnet_Device_Address {
/* mac_len = 0 if global address */ /* mac_len = 0 if global address */
int mac_len; uint8_t mac_len;
/* note: MAC for IP addresses uses 4 bytes for addr, 2 bytes for port */ /* note: MAC for IP addresses uses 4 bytes for addr, 2 bytes for port */
/* use de/encode_unsigned32/16 for re/storing the IP address */ /* use de/encode_unsigned32/16 for re/storing the IP address */
uint8_t mac[MAX_MAC_LEN]; uint8_t mac[MAX_MAC_LEN];
@@ -80,7 +73,7 @@ struct BACnet_Device_Address {
uint16_t net; /* BACnet network number */ uint16_t net; /* BACnet network number */
/* LEN = 0 denotes broadcast MAC ADR and ADR field is absent */ /* LEN = 0 denotes broadcast MAC ADR and ADR field is absent */
/* LEN > 0 specifies length of ADR field */ /* LEN > 0 specifies length of ADR field */
int len; /* length of MAC address */ uint8_t len; /* length of MAC address */
uint8_t adr[MAX_MAC_LEN]; /* hwaddr (MAC) address */ uint8_t adr[MAX_MAC_LEN]; /* hwaddr (MAC) address */
}; };
typedef struct BACnet_Device_Address BACNET_ADDRESS; typedef struct BACnet_Device_Address BACNET_ADDRESS;
File diff suppressed because it is too large Load Diff
+43 -27
View File
@@ -18,10 +18,14 @@ BACNET_FLAGS += -DPRINT_ENABLED=0
BACNET_FLAGS += -DMAX_APDU=480 BACNET_FLAGS += -DMAX_APDU=480
#BACNET_FLAGS += -DDLMSTP_TEST #BACNET_FLAGS += -DDLMSTP_TEST
INCLUDES = -I. -I../.. -I../../demo/handler -I../../demo/object BACNET_ROOT = ../..
INCLUDES = -I.
INCLUDES += -I$(BACNET_ROOT)
INCLUDES += -I$(BACNET_ROOT)/demo/handler
INCLUDES += -I$(BACNET_ROOT)/demo/object
#OPTIMIZATION = -O0 #OPTIMIZATION = -O0
OPTIMIZATION = -Os OPTIMIZATION = -Os
CFLAGS = -fno-common $(OPTIMIZATION) $(INCLUDES) $(BACNET_FLAGS) -Wall -g CFLAGS = -fno-common $(INCLUDES) $(BACNET_FLAGS) -Wall -g
LIBRARY = lib$(TARGET).a LIBRARY = lib$(TARGET).a
# -Wa,<options> Pass comma-separated <options> on to the assembler # -Wa,<options> Pass comma-separated <options> on to the assembler
AFLAGS = -Wa,-ahls,-mapcs-32,-adhlns=$(<:.s=.lst) AFLAGS = -Wa,-ahls,-mapcs-32,-adhlns=$(<:.s=.lst)
@@ -40,8 +44,7 @@ PORTSRC = main.c \
blinker.c \ blinker.c \
rs485.c \ rs485.c \
dlmstp.c \ dlmstp.c \
../../crc.c \ $(BACNET_ROOT)/crc.c
../../mstp.c
DEMOSRC = ai.c \ DEMOSRC = ai.c \
av.c \ av.c \
@@ -50,29 +53,29 @@ DEMOSRC = ai.c \
h_rp.c \ h_rp.c \
h_wp.c \ h_wp.c \
device.c \ device.c \
../../demo/handler/txbuf.c \ $(BACNET_ROOT)/demo/handler/txbuf.c \
../../demo/handler/h_whois.c \ $(BACNET_ROOT)/demo/handler/h_whois.c \
../../demo/handler/h_rd.c \ $(BACNET_ROOT)/demo/handler/h_rd.c \
../../demo/handler/h_dcc.c $(BACNET_ROOT)/demo/handler/h_dcc.c
CORESRC = ../../npdu.c \ CORESRC = $(BACNET_ROOT)/npdu.c \
../../bacint.c \ $(BACNET_ROOT)/bacint.c \
../../apdu.c \ $(BACNET_ROOT)/apdu.c \
../../bacdcode.c \ $(BACNET_ROOT)/bacdcode.c \
../../bacaddr.c \ $(BACNET_ROOT)/bacaddr.c \
../../bacstr.c \ $(BACNET_ROOT)/bacstr.c \
../../abort.c \ $(BACNET_ROOT)/abort.c \
../../bacerror.c \ $(BACNET_ROOT)/bacerror.c \
../../reject.c \ $(BACNET_ROOT)/reject.c \
../../bacapp.c \ $(BACNET_ROOT)/bacapp.c \
../../datetime.c \ $(BACNET_ROOT)/datetime.c \
../../rp.c \ $(BACNET_ROOT)/rp.c \
../../wp.c \ $(BACNET_ROOT)/wp.c \
../../dcc.c \ $(BACNET_ROOT)/dcc.c \
../../rd.c \ $(BACNET_ROOT)/rd.c \
../../whois.c \ $(BACNET_ROOT)/whois.c \
../../iam.c \ $(BACNET_ROOT)/iam.c \
../../version.c $(BACNET_ROOT)/version.c
CSRC = $(PORTSRC) $(DEMOSRC) CSRC = $(PORTSRC) $(DEMOSRC)
#CSRC = $(PORTSRC) #CSRC = $(PORTSRC)
@@ -96,8 +99,21 @@ lib: $(LIBRARY)
$(LIBRARY): $(COREOBJ) Makefile $(LIBRARY): $(COREOBJ) Makefile
$(AR) rcs $@ $(COREOBJ) $(AR) rcs $@ $(COREOBJ)
# allow a single file to be unoptimized for debugging purposes
#dlmstp.o:
# $(CC) -c $(CFLAGS) $*.c -o $@
#
#main.o:
# $(CC) -c $(CFLAGS) $*.c -o $@
#
#$(BACNET_ROOT)/npdu.o:
# $(CC) -c $(CFLAGS) $*.c -o $@
#
#$(BACNET_ROOT)/apdu.o:
# $(CC) -c $(CFLAGS) $*.c -o $@
.c.o: .c.o:
$(CC) -c $(CFLAGS) $*.c -o $@ $(CC) -c $(OPTIMIZATION) $(CFLAGS) $*.c -o $@
.s.o: .s.o:
$(CC) -c $(AFLAGS) $*.s -o $@ $(CC) -c $(AFLAGS) $*.s -o $@
+101 -59
View File
@@ -32,7 +32,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "mstp.h"
#include "timer.h" #include "timer.h"
/* This file has been customized for use with UART0 /* This file has been customized for use with UART0
@@ -44,16 +43,17 @@ static volatile AT91S_USART *RS485_Interface = AT91C_BASE_US0;
/* baud rate */ /* baud rate */
static int RS485_Baud = 38400; static int RS485_Baud = 38400;
/**************************************************************************** /* The minimum time after the end of the stop bit of the final octet of a */
* DESCRIPTION: Sets the interface - UART0 or UART1 or... /* received frame before a node may enable its EIA-485 driver: 40 bit times. */
* RETURN: none /* At 9600 baud, 40 bit times would be about 4.166 milliseconds */
* ALGORITHM: none /* At 19200 baud, 40 bit times would be about 2.083 milliseconds */
* NOTES: none /* At 38400 baud, 40 bit times would be about 1.041 milliseconds */
*****************************************************************************/ /* At 57600 baud, 40 bit times would be about 0.694 milliseconds */
void RS485_Set_Interface(char *ifname) /* At 76800 baud, 40 bit times would be about 0.520 milliseconds */
{ /* At 115200 baud, 40 bit times would be about 0.347 milliseconds */
RS485_Interface = (volatile AT91S_USART *)ifname; /* 40 bits is 4 octets including a start and stop bit with each octet */
} #define Tturnaround (40UL)
/* turnaround_time_milliseconds = (Tturnaround*1000UL)/RS485_Baud; */
/**************************************************************************** /****************************************************************************
* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in * DESCRIPTION: Initializes the RS485 hardware and variables, and starts in
@@ -152,27 +152,51 @@ bool RS485_Set_Baud_Rate(uint32_t baud)
return valid; return valid;
} }
/* Transmits a Frame on the wire */ /****************************************************************************
void RS485_Send_Frame( * DESCRIPTION: Waits on the SilenceTimer for 40 bits.
volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ * RETURN: none
uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ * ALGORITHM: none
uint16_t nbytes) /* number of bytes of data (up to 501) */ * NOTES: none
*****************************************************************************/
void RS485_Turnaround_Delay(void)
{ {
uint8_t turnaround_time; uint16_t turnaround_time;
/* delay after reception before trasmitting - per MS/TP spec */
/* wait a minimum 40 bit times since reception */
/* at least 1 ms for errors: rounding, clock tick */
turnaround_time = 1 + ((Tturnaround*1000UL)/RS485_Baud);
while (Timer_Silence() < turnaround_time) {
/* do nothing - wait for timer to increment */
};
}
/* delay after reception - per MS/TP spec */ /****************************************************************************
if (mstp_port) { * DESCRIPTION: Enable or disable the transmitter
/* wait a minimum 40 bit times since reception */ * RETURN: none
/* at least 1 ms for errors: rounding, clock tick */ * ALGORITHM: none
turnaround_time = 1 + ((Tturnaround*1000)/RS485_Baud); * NOTES: The Atmel ARM7 has an automatic enable/disable in RS485 mode.
while (mstp_port->SilenceTimer() < turnaround_time) { *****************************************************************************/
/* do nothing - wait for timer to increment */ void RS485_Transmitter_Enable(bool enable)
}; {
} (void)enable;
/* toggle LED on send */ }
/****************************************************************************
* DESCRIPTION: Send some data and wait until it is sent
* RETURN: none
* ALGORITHM: none
* NOTES: none
*****************************************************************************/
void RS485_Send_Data(
uint8_t * buffer, /* data to send */
uint16_t nbytes) /* number of bytes of data */
{
/* LED on send */
volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA;
/* LED ON */ /* LED ON */
pPIO->PIO_CODR = LED1; pPIO->PIO_CODR = LED1;
/* send all the bytes */
while (nbytes) { while (nbytes) {
while (!(RS485_Interface->US_CSR & AT91C_US_TXRDY)) { while (!(RS485_Interface->US_CSR & AT91C_US_TXRDY)) {
/* do nothing - wait until Tx buffer is empty */ /* do nothing - wait until Tx buffer is empty */
@@ -180,60 +204,78 @@ void RS485_Send_Frame(
RS485_Interface->US_THR = *buffer; RS485_Interface->US_THR = *buffer;
buffer++; buffer++;
nbytes--; nbytes--;
/* per MSTP spec */
if (mstp_port) {
mstp_port->SilenceTimerReset();
}
} }
while (!(RS485_Interface->US_CSR & AT91C_US_TXRDY)) { while (!(RS485_Interface->US_CSR & AT91C_US_TXRDY)) {
/* do nothing - wait until Tx buffer is empty */ /* do nothing - wait until Tx buffer is empty */
} }
/* per MSTP spec */
return; Timer_Silence_Reset();
} }
/* called by timer, interrupt(?) or other thread */ /****************************************************************************
void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port) * DESCRIPTION: Return true if a framing or overrun error is present
* RETURN: true if error
* ALGORITHM: none
* NOTES: Clears any error flags.
*****************************************************************************/
bool RS485_ReceiveError(void)
{ {
bool ReceiveError = false;
/* LED on send */
volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA;
if (mstp_port->ReceiveError == true) { /* check for data or error */
/* wait for state machine to clear this */ if (RS485_Interface->US_CSR & (AT91C_US_OVRE | AT91C_US_FRAME)) {
} else if (mstp_port->DataAvailable == false) { /* clear the error flag */
/* check for data or error */ RS485_Interface->US_CR = AT91C_US_RSTSTA;
if (RS485_Interface->US_CSR & (AT91C_US_OVRE | AT91C_US_FRAME)) { ReceiveError = true;
/* clear the error flag */ /* LED ON */
RS485_Interface->US_CR = AT91C_US_RSTSTA; pPIO->PIO_CODR = LED2;
mstp_port->ReceiveError = true;
/* LED ON */
pPIO->PIO_CODR = LED2;
} else if ( RS485_Interface->US_CSR & AT91C_US_RXRDY) {
/* data is available */
mstp_port->DataRegister = RS485_Interface->US_RHR;
mstp_port->DataAvailable = true;
/* LED ON */
pPIO->PIO_CODR = LED2;
}
} }
return ReceiveError;
}
/****************************************************************************
* DESCRIPTION: Return true if data is available
* RETURN: true if data is available, with the data in the parameter set
* ALGORITHM: none
* NOTES: none
*****************************************************************************/
bool RS485_DataAvailable(uint8_t *DataRegister)
{
bool DataAvailable = false;
/* LED on send */
volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA;
if ( RS485_Interface->US_CSR & AT91C_US_RXRDY) {
/* data is available */
*DataRegister = RS485_Interface->US_RHR;
DataAvailable = true;
/* LED ON */
pPIO->PIO_CODR = LED2;
}
return DataAvailable;
} }
#ifdef TEST_RS485 #ifdef TEST_RS485
static struct mstp_port_struct_t MSTP_Port;
int main(void) int main(void)
{ {
unsigned i = 0; unsigned i = 0;
uint8_t DataRegister;
RS485_Set_Baud_Rate(38400); RS485_Set_Baud_Rate(38400);
RS485_Initialize(); RS485_Initialize();
/* receive task */ /* receive task */
for (;;) { for (;;) {
RS485_Check_UART_Data(&MSTP_Port); if (RS485_DataAvailable(&DataRegister)) {
if (MSTP_Port.DataAvailable) { fprintf(stderr,"%02X ",DataRegister);
fprintf(stderr,"%02X ",MSTP_Port.DataRegister); } else if (RS485_ReceiveError()) {
MSTP_Port.DataAvailable = false; fprintf(stderr,"ERROR ");
} }
} }
} }
#endif /* TEST_ABORT */ #endif
+9 -9
View File
@@ -27,24 +27,24 @@
#define RS485_H #define RS485_H
#include <stdint.h> #include <stdint.h>
#include "mstp.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
void RS485_Set_Interface(char *ifname);
void RS485_Initialize(void); void RS485_Initialize(void);
void RS485_Send_Frame( void RS485_Transmitter_Enable(bool enable);
volatile struct mstp_port_struct_t *mstp_port, /* port specific data */
uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes); /* number of bytes of data (up to 501) */
uint8_t RS485_Check_UART_Data( /* NOTE: Only able to send up to 255 bytes at a time */
volatile struct mstp_port_struct_t *mstp_port); /* port specific data */ void RS485_Send_Data(
uint8_t * buffer, /* data to send */
uint16_t nbytes); /* number of bytes of data */
bool RS485_ReceiveError(void);
bool RS485_DataAvailable(uint8_t *data);
void RS485_Turnaround_Delay(void);
uint32_t RS485_Get_Baud_Rate(void); uint32_t RS485_Get_Baud_Rate(void);
bool RS485_Set_Baud_Rate(uint32_t baud); bool RS485_Set_Baud_Rate(uint32_t baud);
+3 -3
View File
@@ -48,7 +48,7 @@
/* global variable counts interrupts */ /* global variable counts interrupts */
volatile unsigned long Timer_Milliseconds; volatile unsigned long Timer_Milliseconds;
/* MS/TP Silence Timer */ /* MS/TP Silence Timer */
static volatile uint16_t SilenceTime; static volatile int SilenceTime;
static void Timer0_Setup(int milliseconds) { static void Timer0_Setup(int milliseconds) {
// TC Block Control Register TC_BCR (read/write) // TC Block Control Register TC_BCR (read/write)
@@ -306,11 +306,11 @@ static void Timer0IrqHandler (void) {
dummy = pTC->TC_SR; dummy = pTC->TC_SR;
// increment the tick count // increment the tick count
Timer_Milliseconds++; Timer_Milliseconds++;
if (SilenceTime < 0xFFFF) if (SilenceTime < 60000)
SilenceTime++; SilenceTime++;
} }
uint16_t Timer_Silence(void) int Timer_Silence(void)
{ {
return SilenceTime; return SilenceTime;
} }
+1 -1
View File
@@ -35,7 +35,7 @@ extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
void TimerInit(void); void TimerInit(void);
uint16_t Timer_Silence(void); int Timer_Silence(void);
void Timer_Silence_Reset(void); void Timer_Silence_Reset(void);
#ifdef __cplusplus #ifdef __cplusplus
+3 -1
View File
@@ -441,7 +441,6 @@ static void MSTP_Send_Frame(
RS485_Send_Data(buffer, 2); RS485_Send_Data(buffer, 2);
} }
RS485_Transmitter_Enable(false); RS485_Transmitter_Enable(false);
Timer_Silence_Reset();
} }
static void MSTP_Receive_Frame_FSM(void) static void MSTP_Receive_Frame_FSM(void)
@@ -1243,6 +1242,9 @@ uint16_t dlmstp_receive(
if (MSTP_Flag.ReceivePacketPending) { if (MSTP_Flag.ReceivePacketPending) {
MSTP_Flag.ReceivePacketPending = false; MSTP_Flag.ReceivePacketPending = false;
pdu_len = DataLength; pdu_len = DataLength;
src->mac_len = 1;
src->mac[0] = SourceAddress;
/* data is already in the pdu pointer */
} }
return pdu_len; return pdu_len;
+28 -5
View File
@@ -180,6 +180,8 @@ 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 */
Timer_Silence_Reset();
} }
@@ -191,25 +193,25 @@ void RS485_Send_Data(
*****************************************************************************/ *****************************************************************************/
bool RS485_ReceiveError(void) bool RS485_ReceiveError(void)
{ {
bool uart_error = false; bool ReceiveError = false;
uint8_t dummy_data; uint8_t dummy_data;
/* check for framing error */ /* check for framing error */
if (BIT_CHECK(UCSR0A,FE0)) { if (BIT_CHECK(UCSR0A,FE0)) {
uart_error = true; ReceiveError = true;
} }
/* check for overrun error */ /* check for overrun error */
if (BIT_CHECK(UCSR0A,DOR0)) { if (BIT_CHECK(UCSR0A,DOR0)) {
uart_error = true; ReceiveError = true;
} }
/* flush the receive buffer */ /* flush the receive buffer */
if (uart_error) { if (ReceiveError) {
while (BIT_CHECK(UCSR0A,RXC0)) { while (BIT_CHECK(UCSR0A,RXC0)) {
dummy_data = UDR0; dummy_data = UDR0;
} }
} }
return uart_error; return ReceiveError;
} }
/**************************************************************************** /****************************************************************************
@@ -230,3 +232,24 @@ bool RS485_DataAvailable(uint8_t *data)
return uart_data; return uart_data;
} }
#ifdef TEST_RS485
int main(void)
{
unsigned i = 0;
uint8_t DataRegister;
RS485_Set_Baud_Rate(38400);
RS485_Initialize();
/* receive task */
for (;;) {
if (RS485_DataAvailable(&DataRegister)) {
fprintf(stderr,"%02X ",DataRegister);
} else if (RS485_ReceiveError()) {
fprintf(stderr,"ERROR ");
}
}
}
#endif /* TEST_ABORT */
-1
View File
@@ -35,7 +35,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <string.h> /* memmove() */
#include "bits.h" #include "bits.h"
#include "apdu.h" #include "apdu.h"
#include "bacdef.h" #include "bacdef.h"