working on PIC port of MS/TP.
This commit is contained in:
@@ -46,9 +46,11 @@
|
|||||||
|
|
||||||
typedef struct dlmstp_packet
|
typedef struct dlmstp_packet
|
||||||
{
|
{
|
||||||
BACNET_ADDRESS address;
|
bool ready; /* true if ready to be sent or received */
|
||||||
unsigned pdu_len;
|
bool data_expecting_reply;
|
||||||
uint8_t pdu[MAX_MPDU];
|
BACNET_ADDRESS address; /* src or dest address*/
|
||||||
|
unsigned pdu_len; /* packet length */
|
||||||
|
uint8_t pdu[MAX_MPDU]; /* packet */
|
||||||
} DLMSTP_PACKET;
|
} DLMSTP_PACKET;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|||||||
@@ -29,30 +29,24 @@
|
|||||||
#include "bacdef.h"
|
#include "bacdef.h"
|
||||||
#include "mstp.h"
|
#include "mstp.h"
|
||||||
#include "dlmstp.h"
|
#include "dlmstp.h"
|
||||||
#include "ringbuf.h"
|
|
||||||
#include "rs485.h"
|
#include "rs485.h"
|
||||||
|
|
||||||
#define RB_PACKET_COUNT 2
|
static DLMSTP_PACKET Receive_Buffer;
|
||||||
#define RB_PACKET_SIZE sizeof(DLMSTP_PACKET)
|
static DLMSTP_PACKET Transmit_Buffer;
|
||||||
static RING_BUFFER Receive_Buffer;
|
|
||||||
static char Receive_Data_Store[RB_PACKET_COUNT * RB_PACKET_SIZE];
|
|
||||||
static RING_BUFFER Transmit_Buffer;
|
|
||||||
static char Transmit_Data_Store[RB_PACKET_COUNT * RB_PACKET_SIZE];
|
|
||||||
|
|
||||||
volatile struct mstp_port_struct_t MSTP_Port; /* port data */
|
volatile struct mstp_port_struct_t MSTP_Port; /* port data */
|
||||||
static uint8_t MSTP_MAC_Address = 0x05; /* local MAC address */
|
static uint8_t MSTP_MAC_Address = 0x05; /* local MAC address */
|
||||||
DLMSTP_PACKET Temp_Packet;
|
|
||||||
|
|
||||||
void dlmstp_init(void)
|
void dlmstp_init(void)
|
||||||
{
|
{
|
||||||
Ringbuf_Init(&Receive_Buffer,
|
/* initialize buffer */
|
||||||
&Receive_Data_Store[0],
|
Receive_Buffer.ready = false;
|
||||||
RB_PACKET_SIZE,
|
Receive_Buffer.data_expecting_reply = false;
|
||||||
RB_PACKET_COUNT);
|
Receive_Buffer.pdu_len = 0;
|
||||||
Ringbuf_Init(&Transmit_Buffer,
|
/* initialize buffer */
|
||||||
&Transmit_Data_Store[0],
|
Transmit_Buffer.ready = false;
|
||||||
RB_PACKET_SIZE,
|
Transmit_Buffer.data_expecting_reply = false;
|
||||||
RB_PACKET_COUNT);
|
Transmit_Buffer.pdu_len = 0;
|
||||||
|
/* initialize hardware */
|
||||||
RS485_Initialize();
|
RS485_Initialize();
|
||||||
MSTP_Init(&MSTP_Port, MSTP_MAC_Address);
|
MSTP_Init(&MSTP_Port, MSTP_MAC_Address);
|
||||||
}
|
}
|
||||||
@@ -62,7 +56,7 @@ void dlmstp_cleanup(void)
|
|||||||
/* nothing to do for static buffers */
|
/* nothing to do for static buffers */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* returns number of bytes sent on success, negative on failure */
|
/* returns number of bytes sent on success, zero on failure */
|
||||||
int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
|
int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
|
||||||
uint8_t * pdu, /* any data to be sent - may be null */
|
uint8_t * pdu, /* any data to be sent - may be null */
|
||||||
unsigned pdu_len)
|
unsigned pdu_len)
|
||||||
@@ -70,18 +64,24 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
|
|||||||
bool status;
|
bool status;
|
||||||
int bytes_sent = 0;
|
int bytes_sent = 0;
|
||||||
|
|
||||||
memmove(&Temp_Packet.address,dest,sizeof(Temp_Packet.address));
|
if (Transmit_Buffer.ready == false)
|
||||||
Temp_Packet.pdu_len = pdu_len;
|
{
|
||||||
memmove(Temp_Packet.pdu,pdu,sizeof(Temp_Packet.pdu));
|
/* FIXME: how do we get data_expecting_reply? */
|
||||||
status = Ringbuf_Put(&Transmit_Buffer, &Temp_Packet);
|
Transmit_Buffer.data_expecting_reply = false;
|
||||||
if (status)
|
Receive_Buffer.pdu_len = 0;
|
||||||
|
memmove(&Transmit_Buffer.address,dest,sizeof(Transmit_Buffer.address));
|
||||||
|
Transmit_Buffer.pdu_len = pdu_len;
|
||||||
|
/* FIXME: copy the whole PDU or just the pdu_len? */
|
||||||
|
memmove(Transmit_Buffer.pdu,pdu,sizeof(Transmit_Buffer.pdu));
|
||||||
bytes_sent = pdu_len;
|
bytes_sent = pdu_len;
|
||||||
|
Transmit_Buffer.ready = true;
|
||||||
|
}
|
||||||
|
|
||||||
return bytes_sent;
|
return bytes_sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* function for MS/TP to use to get a packet to transmit
|
/* function for MS/TP to use to get a packet to transmit
|
||||||
returns the number of bytes in the packet, or 0 if none. */
|
returns the number of bytes in the packet, or zero if none. */
|
||||||
int dlmstp_get_transmit_pdu(BACNET_ADDRESS * dest, /* destination address */
|
int dlmstp_get_transmit_pdu(BACNET_ADDRESS * dest, /* destination address */
|
||||||
uint8_t * pdu) /* any data to be sent - may be null */
|
uint8_t * pdu) /* any data to be sent - may be null */
|
||||||
{
|
{
|
||||||
@@ -89,9 +89,8 @@ int dlmstp_get_transmit_pdu(BACNET_ADDRESS * dest, /* destination address *
|
|||||||
DLMSTP_PACKET *packet;
|
DLMSTP_PACKET *packet;
|
||||||
unsigned pdu_len = 0;
|
unsigned pdu_len = 0;
|
||||||
|
|
||||||
if (!Ringbuf_Empty(&Transmit_Buffer))
|
if (Transmit_Buffer.ready)
|
||||||
{
|
{
|
||||||
packet = Ringbuf_Pop_Front(&Transmit_Buffer);
|
|
||||||
memmove(dest,&packet->address,sizeof(packet->address));
|
memmove(dest,&packet->address,sizeof(packet->address));
|
||||||
pdu_len = packet->pdu_len;
|
pdu_len = packet->pdu_len;
|
||||||
memmove(pdu,packet->pdu,sizeof(packet.pdu));
|
memmove(pdu,packet->pdu,sizeof(packet.pdu));
|
||||||
@@ -100,6 +99,11 @@ int dlmstp_get_transmit_pdu(BACNET_ADDRESS * dest, /* destination address *
|
|||||||
return pdu_len;
|
return pdu_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int dlmstp_set_transmit_pdu_ready(bool ready)
|
||||||
|
{
|
||||||
|
Transmit_Buffer.ready = ready;
|
||||||
|
}
|
||||||
|
|
||||||
void dlmstp_task(void)
|
void dlmstp_task(void)
|
||||||
{
|
{
|
||||||
RS485_Check_UART_Data(&MSTP_Port);
|
RS485_Check_UART_Data(&MSTP_Port);
|
||||||
|
|||||||
Reference in New Issue
Block a user