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:
+4
-11
@@ -56,21 +56,14 @@
|
||||
#define BACNET_MIN_PRIORITY 1
|
||||
#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
|
||||
/* 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
|
||||
struct BACnet_Device_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 */
|
||||
/* use de/encode_unsigned32/16 for re/storing the IP address */
|
||||
uint8_t mac[MAX_MAC_LEN];
|
||||
@@ -80,7 +73,7 @@ struct BACnet_Device_Address {
|
||||
uint16_t net; /* BACnet network number */
|
||||
/* LEN = 0 denotes broadcast MAC ADR and ADR field is absent */
|
||||
/* 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 */
|
||||
};
|
||||
typedef struct BACnet_Device_Address BACNET_ADDRESS;
|
||||
|
||||
+1093
-242
File diff suppressed because it is too large
Load Diff
@@ -18,10 +18,14 @@ BACNET_FLAGS += -DPRINT_ENABLED=0
|
||||
BACNET_FLAGS += -DMAX_APDU=480
|
||||
#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 = -Os
|
||||
CFLAGS = -fno-common $(OPTIMIZATION) $(INCLUDES) $(BACNET_FLAGS) -Wall -g
|
||||
CFLAGS = -fno-common $(INCLUDES) $(BACNET_FLAGS) -Wall -g
|
||||
LIBRARY = lib$(TARGET).a
|
||||
# -Wa,<options> Pass comma-separated <options> on to the assembler
|
||||
AFLAGS = -Wa,-ahls,-mapcs-32,-adhlns=$(<:.s=.lst)
|
||||
@@ -40,8 +44,7 @@ PORTSRC = main.c \
|
||||
blinker.c \
|
||||
rs485.c \
|
||||
dlmstp.c \
|
||||
../../crc.c \
|
||||
../../mstp.c
|
||||
$(BACNET_ROOT)/crc.c
|
||||
|
||||
DEMOSRC = ai.c \
|
||||
av.c \
|
||||
@@ -50,29 +53,29 @@ DEMOSRC = ai.c \
|
||||
h_rp.c \
|
||||
h_wp.c \
|
||||
device.c \
|
||||
../../demo/handler/txbuf.c \
|
||||
../../demo/handler/h_whois.c \
|
||||
../../demo/handler/h_rd.c \
|
||||
../../demo/handler/h_dcc.c
|
||||
$(BACNET_ROOT)/demo/handler/txbuf.c \
|
||||
$(BACNET_ROOT)/demo/handler/h_whois.c \
|
||||
$(BACNET_ROOT)/demo/handler/h_rd.c \
|
||||
$(BACNET_ROOT)/demo/handler/h_dcc.c
|
||||
|
||||
CORESRC = ../../npdu.c \
|
||||
../../bacint.c \
|
||||
../../apdu.c \
|
||||
../../bacdcode.c \
|
||||
../../bacaddr.c \
|
||||
../../bacstr.c \
|
||||
../../abort.c \
|
||||
../../bacerror.c \
|
||||
../../reject.c \
|
||||
../../bacapp.c \
|
||||
../../datetime.c \
|
||||
../../rp.c \
|
||||
../../wp.c \
|
||||
../../dcc.c \
|
||||
../../rd.c \
|
||||
../../whois.c \
|
||||
../../iam.c \
|
||||
../../version.c
|
||||
CORESRC = $(BACNET_ROOT)/npdu.c \
|
||||
$(BACNET_ROOT)/bacint.c \
|
||||
$(BACNET_ROOT)/apdu.c \
|
||||
$(BACNET_ROOT)/bacdcode.c \
|
||||
$(BACNET_ROOT)/bacaddr.c \
|
||||
$(BACNET_ROOT)/bacstr.c \
|
||||
$(BACNET_ROOT)/abort.c \
|
||||
$(BACNET_ROOT)/bacerror.c \
|
||||
$(BACNET_ROOT)/reject.c \
|
||||
$(BACNET_ROOT)/bacapp.c \
|
||||
$(BACNET_ROOT)/datetime.c \
|
||||
$(BACNET_ROOT)/rp.c \
|
||||
$(BACNET_ROOT)/wp.c \
|
||||
$(BACNET_ROOT)/dcc.c \
|
||||
$(BACNET_ROOT)/rd.c \
|
||||
$(BACNET_ROOT)/whois.c \
|
||||
$(BACNET_ROOT)/iam.c \
|
||||
$(BACNET_ROOT)/version.c
|
||||
|
||||
CSRC = $(PORTSRC) $(DEMOSRC)
|
||||
#CSRC = $(PORTSRC)
|
||||
@@ -96,8 +99,21 @@ lib: $(LIBRARY)
|
||||
$(LIBRARY): $(COREOBJ) Makefile
|
||||
$(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:
|
||||
$(CC) -c $(CFLAGS) $*.c -o $@
|
||||
$(CC) -c $(OPTIMIZATION) $(CFLAGS) $*.c -o $@
|
||||
|
||||
.s.o:
|
||||
$(CC) -c $(AFLAGS) $*.s -o $@
|
||||
|
||||
@@ -32,7 +32,6 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "mstp.h"
|
||||
#include "timer.h"
|
||||
|
||||
/* This file has been customized for use with UART0
|
||||
@@ -44,16 +43,17 @@ static volatile AT91S_USART *RS485_Interface = AT91C_BASE_US0;
|
||||
/* baud rate */
|
||||
static int RS485_Baud = 38400;
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Sets the interface - UART0 or UART1 or...
|
||||
* RETURN: none
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
*****************************************************************************/
|
||||
void RS485_Set_Interface(char *ifname)
|
||||
{
|
||||
RS485_Interface = (volatile AT91S_USART *)ifname;
|
||||
}
|
||||
/* The minimum time after the end of the stop bit of the final octet of a */
|
||||
/* received frame before a node may enable its EIA-485 driver: 40 bit times. */
|
||||
/* At 9600 baud, 40 bit times would be about 4.166 milliseconds */
|
||||
/* At 19200 baud, 40 bit times would be about 2.083 milliseconds */
|
||||
/* At 38400 baud, 40 bit times would be about 1.041 milliseconds */
|
||||
/* At 57600 baud, 40 bit times would be about 0.694 milliseconds */
|
||||
/* At 76800 baud, 40 bit times would be about 0.520 milliseconds */
|
||||
/* At 115200 baud, 40 bit times would be about 0.347 milliseconds */
|
||||
/* 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
|
||||
@@ -152,27 +152,51 @@ bool RS485_Set_Baud_Rate(uint32_t baud)
|
||||
return valid;
|
||||
}
|
||||
|
||||
/* Transmits a Frame on the wire */
|
||||
void RS485_Send_Frame(
|
||||
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) */
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Waits on the SilenceTimer for 40 bits.
|
||||
* RETURN: none
|
||||
* ALGORITHM: none
|
||||
* 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) {
|
||||
/* wait a minimum 40 bit times since reception */
|
||||
/* at least 1 ms for errors: rounding, clock tick */
|
||||
turnaround_time = 1 + ((Tturnaround*1000)/RS485_Baud);
|
||||
while (mstp_port->SilenceTimer() < turnaround_time) {
|
||||
/* do nothing - wait for timer to increment */
|
||||
};
|
||||
}
|
||||
/* toggle LED on send */
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Enable or disable the transmitter
|
||||
* RETURN: none
|
||||
* ALGORITHM: none
|
||||
* NOTES: The Atmel ARM7 has an automatic enable/disable in RS485 mode.
|
||||
*****************************************************************************/
|
||||
void RS485_Transmitter_Enable(bool enable)
|
||||
{
|
||||
(void)enable;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* 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;
|
||||
/* LED ON */
|
||||
pPIO->PIO_CODR = LED1;
|
||||
/* send all the bytes */
|
||||
while (nbytes) {
|
||||
while (!(RS485_Interface->US_CSR & AT91C_US_TXRDY)) {
|
||||
/* do nothing - wait until Tx buffer is empty */
|
||||
@@ -180,60 +204,78 @@ void RS485_Send_Frame(
|
||||
RS485_Interface->US_THR = *buffer;
|
||||
buffer++;
|
||||
nbytes--;
|
||||
/* per MSTP spec */
|
||||
if (mstp_port) {
|
||||
mstp_port->SilenceTimerReset();
|
||||
}
|
||||
}
|
||||
while (!(RS485_Interface->US_CSR & AT91C_US_TXRDY)) {
|
||||
/* do nothing - wait until Tx buffer is empty */
|
||||
}
|
||||
|
||||
return;
|
||||
/* per MSTP spec */
|
||||
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;
|
||||
|
||||
if (mstp_port->ReceiveError == true) {
|
||||
/* wait for state machine to clear this */
|
||||
} else if (mstp_port->DataAvailable == false) {
|
||||
/* check for data or error */
|
||||
if (RS485_Interface->US_CSR & (AT91C_US_OVRE | AT91C_US_FRAME)) {
|
||||
/* clear the error flag */
|
||||
RS485_Interface->US_CR = AT91C_US_RSTSTA;
|
||||
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;
|
||||
}
|
||||
/* check for data or error */
|
||||
if (RS485_Interface->US_CSR & (AT91C_US_OVRE | AT91C_US_FRAME)) {
|
||||
/* clear the error flag */
|
||||
RS485_Interface->US_CR = AT91C_US_RSTSTA;
|
||||
ReceiveError = 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
|
||||
static struct mstp_port_struct_t MSTP_Port;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned i = 0;
|
||||
uint8_t DataRegister;
|
||||
|
||||
RS485_Set_Baud_Rate(38400);
|
||||
RS485_Initialize();
|
||||
/* receive task */
|
||||
for (;;) {
|
||||
RS485_Check_UART_Data(&MSTP_Port);
|
||||
if (MSTP_Port.DataAvailable) {
|
||||
fprintf(stderr,"%02X ",MSTP_Port.DataRegister);
|
||||
MSTP_Port.DataAvailable = false;
|
||||
if (RS485_DataAvailable(&DataRegister)) {
|
||||
fprintf(stderr,"%02X ",DataRegister);
|
||||
} else if (RS485_ReceiveError()) {
|
||||
fprintf(stderr,"ERROR ");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif /* TEST_ABORT */
|
||||
#endif
|
||||
|
||||
|
||||
@@ -27,24 +27,24 @@
|
||||
#define RS485_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "mstp.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
void RS485_Set_Interface(char *ifname);
|
||||
|
||||
void RS485_Initialize(void);
|
||||
|
||||
void RS485_Send_Frame(
|
||||
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) */
|
||||
void RS485_Transmitter_Enable(bool enable);
|
||||
|
||||
uint8_t RS485_Check_UART_Data(
|
||||
volatile struct mstp_port_struct_t *mstp_port); /* port specific data */
|
||||
/* NOTE: Only able to send up to 255 bytes at a time */
|
||||
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);
|
||||
bool RS485_Set_Baud_Rate(uint32_t baud);
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
/* global variable counts interrupts */
|
||||
volatile unsigned long Timer_Milliseconds;
|
||||
/* MS/TP Silence Timer */
|
||||
static volatile uint16_t SilenceTime;
|
||||
static volatile int SilenceTime;
|
||||
|
||||
static void Timer0_Setup(int milliseconds) {
|
||||
// TC Block Control Register TC_BCR (read/write)
|
||||
@@ -306,11 +306,11 @@ static void Timer0IrqHandler (void) {
|
||||
dummy = pTC->TC_SR;
|
||||
// increment the tick count
|
||||
Timer_Milliseconds++;
|
||||
if (SilenceTime < 0xFFFF)
|
||||
if (SilenceTime < 60000)
|
||||
SilenceTime++;
|
||||
}
|
||||
|
||||
uint16_t Timer_Silence(void)
|
||||
int Timer_Silence(void)
|
||||
{
|
||||
return SilenceTime;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
void TimerInit(void);
|
||||
uint16_t Timer_Silence(void);
|
||||
int Timer_Silence(void);
|
||||
void Timer_Silence_Reset(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -441,7 +441,6 @@ static void MSTP_Send_Frame(
|
||||
RS485_Send_Data(buffer, 2);
|
||||
}
|
||||
RS485_Transmitter_Enable(false);
|
||||
Timer_Silence_Reset();
|
||||
}
|
||||
|
||||
static void MSTP_Receive_Frame_FSM(void)
|
||||
@@ -1243,6 +1242,9 @@ uint16_t dlmstp_receive(
|
||||
if (MSTP_Flag.ReceivePacketPending) {
|
||||
MSTP_Flag.ReceivePacketPending = false;
|
||||
pdu_len = DataLength;
|
||||
src->mac_len = 1;
|
||||
src->mac[0] = SourceAddress;
|
||||
/* data is already in the pdu pointer */
|
||||
}
|
||||
|
||||
return pdu_len;
|
||||
|
||||
@@ -180,6 +180,8 @@ void RS485_Send_Data(
|
||||
/* do nothing - wait until the entire frame in the
|
||||
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 uart_error = false;
|
||||
bool ReceiveError = false;
|
||||
uint8_t dummy_data;
|
||||
|
||||
/* check for framing error */
|
||||
if (BIT_CHECK(UCSR0A,FE0)) {
|
||||
uart_error = true;
|
||||
ReceiveError = true;
|
||||
}
|
||||
/* check for overrun error */
|
||||
if (BIT_CHECK(UCSR0A,DOR0)) {
|
||||
uart_error = true;
|
||||
ReceiveError = true;
|
||||
}
|
||||
/* flush the receive buffer */
|
||||
if (uart_error) {
|
||||
if (ReceiveError) {
|
||||
while (BIT_CHECK(UCSR0A,RXC0)) {
|
||||
dummy_data = UDR0;
|
||||
}
|
||||
}
|
||||
|
||||
return uart_error;
|
||||
return ReceiveError;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@@ -230,3 +232,24 @@ bool RS485_DataAvailable(uint8_t *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 */
|
||||
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h> /* memmove() */
|
||||
#include "bits.h"
|
||||
#include "apdu.h"
|
||||
#include "bacdef.h"
|
||||
|
||||
Reference in New Issue
Block a user