Modified the PIC port MS/TP RS-485 handling to use an interrupt safe FIFO library (untested).
This commit is contained in:
@@ -70,6 +70,11 @@ bool FIFO_Put(
|
|||||||
FIFO_BUFFER * b,
|
FIFO_BUFFER * b,
|
||||||
uint8_t data_byte);
|
uint8_t data_byte);
|
||||||
|
|
||||||
|
bool FIFO_Add(
|
||||||
|
FIFO_BUFFER * b,
|
||||||
|
uint8_t *data_bytes,
|
||||||
|
unsigned count);
|
||||||
|
|
||||||
/* note: buffer_len must be a power of two */
|
/* note: buffer_len must be a power of two */
|
||||||
void FIFO_Init(
|
void FIFO_Init(
|
||||||
FIFO_BUFFER * b,
|
FIFO_BUFFER * b,
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ int dlmstp_send_pdu(
|
|||||||
void dlmstp_task(
|
void dlmstp_task(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
uint8_t bytes_remaining;
|
bool bytes_remaining;
|
||||||
bool received_frame;
|
bool received_frame;
|
||||||
|
|
||||||
/* only do receive state machine while we don't have a frame */
|
/* only do receive state machine while we don't have a frame */
|
||||||
|
|||||||
@@ -33,6 +33,7 @@
|
|||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
#include "mstp.h"
|
#include "mstp.h"
|
||||||
#include "rs485.h"
|
#include "rs485.h"
|
||||||
|
#include "fifo.h"
|
||||||
|
|
||||||
/* public port info */
|
/* public port info */
|
||||||
extern volatile struct mstp_port_struct_t MSTP_Port;
|
extern volatile struct mstp_port_struct_t MSTP_Port;
|
||||||
@@ -40,16 +41,14 @@ extern volatile struct mstp_port_struct_t MSTP_Port;
|
|||||||
/* the baud rate is adjustable */
|
/* the baud rate is adjustable */
|
||||||
uint32_t RS485_Baud_Rate = 38400;
|
uint32_t RS485_Baud_Rate = 38400;
|
||||||
|
|
||||||
/* the ISR and other use this for status and control */
|
|
||||||
COMSTAT RS485_Comstat;
|
|
||||||
|
|
||||||
/*#pragma udata MSTPPortData
|
/*#pragma udata MSTPPortData
|
||||||
*/
|
*/
|
||||||
/* the buffer for receiving characters */
|
/* the buffer for receiving data (size must be a power of 2) */
|
||||||
volatile uint8_t RS485_Rx_Buffer[MAX_MPDU];
|
volatile uint8_t RS485_Rx_Buffer[128];
|
||||||
|
FIFO_BUFFER FIFO_Rx;
|
||||||
/* UART transmission buffer and index */
|
/* the buffer for sending data (size must be a power of 2) */
|
||||||
volatile uint8_t RS485_Tx_Buffer[MAX_MPDU];
|
volatile uint8_t RS485_Tx_Buffer[128];
|
||||||
|
FIFO_BUFFER FIFO_Tx;
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* DESCRIPTION: Transmits a frame using the UART
|
* DESCRIPTION: Transmits a frame using the UART
|
||||||
@@ -68,12 +67,8 @@ void RS485_Send_Frame(
|
|||||||
if (!buffer)
|
if (!buffer)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* bounds check */
|
while (!Empty(&FIFO_Tx)) {
|
||||||
if (nbytes >= sizeof(RS485_Tx_Buffer))
|
/* buffer is not empty. Wait for ISR to transmit. */
|
||||||
return;
|
|
||||||
|
|
||||||
/* buffer is full. Wait for ISR to transmit. */
|
|
||||||
while (RS485_Comstat.Tx_Bytes) {
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* wait 40 bit times since reception */
|
/* wait 40 bit times since reception */
|
||||||
@@ -85,18 +80,10 @@ void RS485_Send_Frame(
|
|||||||
turnaround_time = 1;
|
turnaround_time = 1;
|
||||||
|
|
||||||
while (mstp_port->SilenceTimer < turnaround_time) {
|
while (mstp_port->SilenceTimer < turnaround_time) {
|
||||||
|
/* The line has not been silent long enough, so wait. */
|
||||||
};
|
};
|
||||||
|
|
||||||
RS485_Comstat.TxHead = 0;
|
FIFO_Add(&FIFO_Tx, buffer, nbytes);
|
||||||
memcpy((void *) &RS485_Tx_Buffer[0], (void *) buffer, nbytes);
|
|
||||||
#if 0
|
|
||||||
for (i = 0; i < nbytes; i++) {
|
|
||||||
/* put the data into the buffer */
|
|
||||||
RS485_Tx_Buffer[i] = *buffer;
|
|
||||||
buffer++;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
RS485_Comstat.Tx_Bytes = nbytes;
|
|
||||||
/* disable the receiver */
|
/* disable the receiver */
|
||||||
PIE3bits.RC2IE = 0;
|
PIE3bits.RC2IE = 0;
|
||||||
RCSTA2bits.CREN = 0;
|
RCSTA2bits.CREN = 0;
|
||||||
@@ -106,7 +93,7 @@ void RS485_Send_Frame(
|
|||||||
/* enable the transmitter */
|
/* enable the transmitter */
|
||||||
TXSTA2bits.TXEN = 1;
|
TXSTA2bits.TXEN = 1;
|
||||||
PIE3bits.TX2IE = 1;
|
PIE3bits.TX2IE = 1;
|
||||||
/* per MSTP spec, sort of */
|
/* reset the silence timer per MSTP spec, sort of */
|
||||||
mstp_port->SilenceTimer = 0;
|
mstp_port->SilenceTimer = 0;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -118,29 +105,16 @@ void RS485_Send_Frame(
|
|||||||
* ALGORITHM: none
|
* ALGORITHM: none
|
||||||
* NOTES: none
|
* NOTES: none
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
uint8_t RS485_Check_UART_Data(
|
bool RS485_Check_UART_Data(
|
||||||
volatile struct mstp_port_struct_t * mstp_port)
|
volatile struct mstp_port_struct_t * mstp_port)
|
||||||
{
|
{
|
||||||
/* check for data */
|
/* check for data */
|
||||||
if (RS485_Comstat.Rx_Bytes) {
|
if (!FIFO_Empty(&FIFO_Rx)) {
|
||||||
mstp_port->DataRegister = RS485_Rx_Buffer[RS485_Comstat.RxTail];
|
mstp_port->DataRegister = FIFO_Get(&FIFO_Rx);
|
||||||
if (RS485_Comstat.RxTail >= (sizeof(RS485_Rx_Buffer) - 1))
|
mstp_port->DataAvailable = TRUE;
|
||||||
RS485_Comstat.RxTail = 0;
|
|
||||||
else
|
|
||||||
RS485_Comstat.RxTail++;
|
|
||||||
/* FIXME: disable interrupts around Rx_Bytes */
|
|
||||||
RS485_Comstat.Rx_Bytes--;
|
|
||||||
/* errors? let the state machine know */
|
|
||||||
if (RS485_Comstat.Rx_Bufferoverrun) {
|
|
||||||
RS485_Comstat.Rx_Bufferoverrun = FALSE;
|
|
||||||
mstp_port->ReceiveError = TRUE;
|
|
||||||
}
|
|
||||||
/* We read a good byte */
|
|
||||||
else
|
|
||||||
mstp_port->DataAvailable = TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return RS485_Comstat.Rx_Bytes;
|
return (!FIFO_Empty(&FIFO_Rx));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* *************************************************************************
|
/* *************************************************************************
|
||||||
@@ -155,25 +129,17 @@ uint8_t RS485_Check_UART_Data(
|
|||||||
void RS485_Interrupt_Rx(
|
void RS485_Interrupt_Rx(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
char dummy;
|
uint8_t data_byte;
|
||||||
|
|
||||||
if ((RCSTA2bits.FERR) || (RCSTA2bits.OERR)) {
|
if ((RCSTA2bits.FERR) || (RCSTA2bits.OERR)) {
|
||||||
/* Clear the error */
|
/* Clear the error */
|
||||||
RCSTA2bits.CREN = 0;
|
RCSTA2bits.CREN = 0;
|
||||||
RCSTA2bits.CREN = 1;
|
RCSTA2bits.CREN = 1;
|
||||||
RS485_Comstat.Rx_Bufferoverrun = TRUE;
|
/* FIXME: flag the MS/TP state machine on buffer overrun */
|
||||||
dummy = RCREG2;
|
data_byte = RCREG2;
|
||||||
} else if (RS485_Comstat.Rx_Bytes < sizeof(RS485_Rx_Buffer)) {
|
|
||||||
RS485_Rx_Buffer[RS485_Comstat.RxHead] = RCREG2;
|
|
||||||
if (RS485_Comstat.RxHead >= (sizeof(RS485_Rx_Buffer) - 1))
|
|
||||||
RS485_Comstat.RxHead = 0;
|
|
||||||
else
|
|
||||||
RS485_Comstat.RxHead++;
|
|
||||||
RS485_Comstat.Rx_Bytes++;
|
|
||||||
} else {
|
} else {
|
||||||
RS485_Comstat.Rx_Bufferoverrun = TRUE;
|
data_byte = RCREG2;
|
||||||
dummy = RCREG2;
|
FIFO_Put(&FIFO_Rx, data_byte);
|
||||||
(void) dummy;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,13 +155,8 @@ void RS485_Interrupt_Rx(
|
|||||||
void RS485_Interrupt_Tx(
|
void RS485_Interrupt_Tx(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
if (RS485_Comstat.Tx_Bytes) {
|
if (!FIFO_Empty(&FIFO_Tx)) {
|
||||||
/* Get the data byte */
|
TXREG2 = FIFO_Get(&FIFO_Tx);
|
||||||
TXREG2 = RS485_Tx_Buffer[RS485_Comstat.TxHead];
|
|
||||||
/* point to the next byte */
|
|
||||||
RS485_Comstat.TxHead++;
|
|
||||||
/* reduce the buffer size */
|
|
||||||
RS485_Comstat.Tx_Bytes--;
|
|
||||||
} else {
|
} else {
|
||||||
/* wait for the USART to be empty */
|
/* wait for the USART to be empty */
|
||||||
while (!TXSTA2bits.TRMT);
|
while (!TXSTA2bits.TRMT);
|
||||||
@@ -204,8 +165,7 @@ void RS485_Interrupt_Tx(
|
|||||||
/* enable the receiver */
|
/* enable the receiver */
|
||||||
RS485_TX_ENABLE = 0;
|
RS485_TX_ENABLE = 0;
|
||||||
RS485_RX_DISABLE = 0;
|
RS485_RX_DISABLE = 0;
|
||||||
/* FIXME: might not be necessary
|
/* enable the this interrupt */
|
||||||
*/
|
|
||||||
PIE3bits.RC2IE = 1;
|
PIE3bits.RC2IE = 1;
|
||||||
RCSTA2bits.CREN = 1;
|
RCSTA2bits.CREN = 1;
|
||||||
}
|
}
|
||||||
@@ -355,6 +315,12 @@ void RS485_Disable_Port(
|
|||||||
PIE3 &= 0xCF; /* Disable both interrupts */
|
PIE3 &= 0xCF; /* Disable both interrupts */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* DESCRIPTION: Reinitializes the port
|
||||||
|
* RETURN: none
|
||||||
|
* ALGORITHM: none
|
||||||
|
* NOTES: none
|
||||||
|
*****************************************************************************/
|
||||||
void RS485_Reinit(
|
void RS485_Reinit(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
@@ -371,15 +337,10 @@ void RS485_Initialize(
|
|||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
/* Init the Rs485 buffers */
|
/* Init the Rs485 buffers */
|
||||||
RS485_Comstat.RxHead = 0;
|
FIFO_Init(&FIFO_Rx, RS485_Rx_Buffer, sizeof(RS485_Rx_Buffer));
|
||||||
RS485_Comstat.RxTail = 0;
|
FIFO_Init(&FIFO_Tx, RS485_Tx_Buffer, sizeof(RS485_Tx_Buffer));
|
||||||
RS485_Comstat.Rx_Bytes = 0;
|
|
||||||
RS485_Comstat.Rx_Bufferoverrun = FALSE;
|
|
||||||
RS485_Comstat.TxHead = 0;
|
|
||||||
RS485_Comstat.TxTail = 0;
|
|
||||||
RS485_Comstat.Tx_Bytes = 0;
|
|
||||||
|
|
||||||
/* FIXME: read the data from storage */
|
/* FIXME: read the stored baud rate */
|
||||||
/* I2C_Read_Block(
|
/* I2C_Read_Block(
|
||||||
EEPROM_DEVICE_ADDRESS,
|
EEPROM_DEVICE_ADDRESS,
|
||||||
(char *)&RS485_Baud_Rate,
|
(char *)&RS485_Baud_Rate,
|
||||||
|
|||||||
@@ -39,20 +39,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "mstp.h"
|
#include "mstp.h"
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t RxHead;
|
|
||||||
uint8_t RxTail;
|
|
||||||
uint8_t Rx_Bytes;
|
|
||||||
uint8_t TxHead;
|
|
||||||
uint8_t TxTail;
|
|
||||||
uint8_t Tx_Bytes;
|
|
||||||
uint8_t Rx_Bufferoverrun:1;
|
|
||||||
uint8_t Tx_Bufferoverrun:1;
|
|
||||||
} COMSTAT;
|
|
||||||
|
|
||||||
extern COMSTAT RS485_Comstat;
|
|
||||||
extern volatile uint8_t RS485_Rx_Buffer[MAX_MPDU];
|
|
||||||
extern volatile uint8_t RS485_Tx_Buffer[MAX_MPDU];
|
|
||||||
extern uint32_t RS485_Baud_Rate;
|
extern uint32_t RS485_Baud_Rate;
|
||||||
|
|
||||||
|
|
||||||
@@ -73,7 +59,8 @@ extern "C" {
|
|||||||
uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
|
uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
|
||||||
uint16_t nbytes); /* number of bytes of data (up to 501) */
|
uint16_t nbytes); /* number of bytes of data (up to 501) */
|
||||||
|
|
||||||
uint8_t RS485_Check_UART_Data(
|
/* returns true if there is more data waiting */
|
||||||
|
bool RS485_Check_UART_Data(
|
||||||
volatile struct mstp_port_struct_t *mstp_port); /* port specific data */
|
volatile struct mstp_port_struct_t *mstp_port); /* port specific data */
|
||||||
|
|
||||||
void RS485_Interrupt_Rx(
|
void RS485_Interrupt_Rx(
|
||||||
|
|||||||
@@ -65,6 +65,19 @@ static bool FIFO_Full (
|
|||||||
return (b ? (FIFO_Count(b) == b->buffer_len) : true);
|
return (b ? (FIFO_Count(b) == b->buffer_len) : true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* DESCRIPTION: Tests to see if space is available
|
||||||
|
* RETURN: true if the number of bytes is available
|
||||||
|
* ALGORITHM: none
|
||||||
|
* NOTES: none
|
||||||
|
*****************************************************************************/
|
||||||
|
static bool FIFO_Available (
|
||||||
|
FIFO_BUFFER const *b,
|
||||||
|
unsigned count)
|
||||||
|
{
|
||||||
|
return (b ? (count < (b->buffer_len - FIFO_Count(b))) : false);
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* DESCRIPTION: Returns the empty/full status of the ring buffer
|
* DESCRIPTION: Returns the empty/full status of the ring buffer
|
||||||
* RETURN: true if the ring buffer is empty, false if it is not.
|
* RETURN: true if the ring buffer is empty, false if it is not.
|
||||||
@@ -135,6 +148,32 @@ bool FIFO_Put(
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* DESCRIPTION: Adds one or more elements of data to the FIFO
|
||||||
|
* RETURN: true if space available and added, false if not added
|
||||||
|
* ALGORITHM: none
|
||||||
|
* NOTES: none
|
||||||
|
*****************************************************************************/
|
||||||
|
bool FIFO_Add(
|
||||||
|
FIFO_BUFFER * b,
|
||||||
|
uint8_t *data_bytes,
|
||||||
|
unsigned count)
|
||||||
|
{
|
||||||
|
bool status = false; /* return value */
|
||||||
|
|
||||||
|
/* limit the ring to prevent overwriting */
|
||||||
|
if (FIFO_Available (b, count)) {
|
||||||
|
while (count) {
|
||||||
|
b->buffer[b->head % b->buffer_len] = data_byte;
|
||||||
|
b->head++;
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
status = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* DESCRIPTION: Configures the ring buffer
|
* DESCRIPTION: Configures the ring buffer
|
||||||
* RETURN: none
|
* RETURN: none
|
||||||
|
|||||||
Reference in New Issue
Block a user