Deprecated Ringbuf_Alloc, and replaced with Ringbuf_Data_Peek() and Ringbuf_Data_Put() functions. Ringbuf_Alloc() was not interrupt or thread safe.
This commit is contained in:
@@ -60,8 +60,10 @@ extern "C" {
|
||||
bool Ringbuf_Put_Front(
|
||||
RING_BUFFER * b, /* ring buffer structure */
|
||||
uint8_t * data_element);
|
||||
volatile uint8_t *Ringbuf_Alloc(
|
||||
volatile uint8_t *Ringbuf_Data_Peek(
|
||||
RING_BUFFER * b);
|
||||
bool Ringbuf_Data_Put(
|
||||
RING_BUFFER * b, volatile uint8_t *data_element);
|
||||
/* Note: element_count must be a power of two */
|
||||
void Ringbuf_Init(
|
||||
RING_BUFFER * b, /* ring buffer structure */
|
||||
|
||||
@@ -1232,7 +1232,7 @@ int dlmstp_send_pdu(
|
||||
struct mstp_pdu_packet *pkt;
|
||||
uint16_t i = 0;
|
||||
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue);
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue);
|
||||
if (pkt) {
|
||||
pkt->data_expecting_reply = npdu_data->data_expecting_reply;
|
||||
for (i = 0; i < pdu_len; i++) {
|
||||
@@ -1245,7 +1245,9 @@ int dlmstp_send_pdu(
|
||||
/* mac_len = 0 is a broadcast address */
|
||||
pkt->destination_mac = MSTP_BROADCAST_ADDRESS;
|
||||
}
|
||||
bytes_sent = pdu_len;
|
||||
if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) {
|
||||
bytes_sent = pdu_len;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes_sent;
|
||||
|
||||
@@ -1194,7 +1194,7 @@ int dlmstp_send_pdu(
|
||||
struct mstp_pdu_packet *pkt;
|
||||
uint16_t i = 0;
|
||||
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue);
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue);
|
||||
if (pkt) {
|
||||
pkt->data_expecting_reply = npdu_data->data_expecting_reply;
|
||||
for (i = 0; i < pdu_len; i++) {
|
||||
@@ -1207,7 +1207,9 @@ int dlmstp_send_pdu(
|
||||
/* mac_len = 0 is a broadcast address */
|
||||
pkt->destination_mac = MSTP_BROADCAST_ADDRESS;
|
||||
}
|
||||
bytes_sent = pdu_len;
|
||||
if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) {
|
||||
bytes_sent = pdu_len;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes_sent;
|
||||
|
||||
@@ -155,7 +155,7 @@ int dlmstp_send_pdu(
|
||||
struct mstp_pdu_packet *pkt;
|
||||
unsigned i = 0;
|
||||
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue);
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue);
|
||||
if (pkt) {
|
||||
pkt->data_expecting_reply = npdu_data->data_expecting_reply;
|
||||
for (i = 0; i < pdu_len; i++) {
|
||||
@@ -168,7 +168,9 @@ int dlmstp_send_pdu(
|
||||
/* mac_len = 0 is a broadcast address */
|
||||
pkt->destination_mac = MSTP_BROADCAST_ADDRESS;
|
||||
}
|
||||
bytes_sent = pdu_len;
|
||||
if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) {
|
||||
bytes_sent = pdu_len;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes_sent;
|
||||
|
||||
@@ -151,7 +151,7 @@ int dlmstp_send_pdu(
|
||||
return 0;
|
||||
}
|
||||
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&poSharedData->PDU_Queue);
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&poSharedData->PDU_Queue);
|
||||
if (pkt) {
|
||||
pkt->data_expecting_reply =
|
||||
BACNET_DATA_EXPECTING_REPLY(pdu[BACNET_PDU_CONTROL_BYTE_OFFSET]);
|
||||
@@ -160,7 +160,9 @@ int dlmstp_send_pdu(
|
||||
}
|
||||
pkt->length = pdu_len;
|
||||
pkt->destination_mac = dest->mac[0];
|
||||
bytes_sent = pdu_len;
|
||||
if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) {
|
||||
bytes_sent = pdu_len;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes_sent;
|
||||
|
||||
@@ -444,10 +444,10 @@ static void MSTP_Send_Frame(
|
||||
{ /* number of bytes of data (up to 501) */
|
||||
uint8_t crc8 = 0xFF; /* used to calculate the crc value */
|
||||
uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */
|
||||
static struct mstp_tx_packet *pkt;
|
||||
struct mstp_tx_packet *pkt;
|
||||
uint16_t i = 0; /* used to calculate CRC for data */
|
||||
|
||||
pkt = (struct mstp_tx_packet *) Ringbuf_Alloc(&Transmit_Queue);
|
||||
pkt = (struct mstp_tx_packet *) Ringbuf_Data_Peek(&Transmit_Queue);
|
||||
if (pkt) {
|
||||
/* create the MS/TP header */
|
||||
pkt->buffer[0] = 0x55;
|
||||
@@ -476,6 +476,7 @@ static void MSTP_Send_Frame(
|
||||
pkt->length += data_len;
|
||||
pkt->length += 2;
|
||||
}
|
||||
Ringbuf_Data_Put(&Transmit_Queue, (uint8_t *)pkt);
|
||||
} else {
|
||||
pkt = NULL;
|
||||
}
|
||||
@@ -1274,7 +1275,7 @@ int dlmstp_send_pdu(
|
||||
struct mstp_pdu_packet *pkt;
|
||||
uint16_t i = 0;
|
||||
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue);
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue);
|
||||
if (pkt) {
|
||||
pkt->data_expecting_reply = npdu_data->data_expecting_reply;
|
||||
for (i = 0; i < pdu_len; i++) {
|
||||
@@ -1286,7 +1287,9 @@ int dlmstp_send_pdu(
|
||||
} else {
|
||||
pkt->destination_mac = MSTP_BROADCAST_ADDRESS;
|
||||
}
|
||||
bytes_sent = pdu_len;
|
||||
if (Ringbuf_Data_Put(&PDU_Queue, (uint8_t *)pkt)) {
|
||||
bytes_sent = pdu_len;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes_sent;
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
#include "stm32f10x_tim.h"
|
||||
#include "stm32f10x_usart.h"
|
||||
#include "stm32f10x_wwdg.h"
|
||||
#include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */
|
||||
#include "stm32f10x_misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
|
||||
@@ -113,11 +113,12 @@ void bacnet_task_timed(
|
||||
|
||||
pdu_len = dlmstp_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 5);
|
||||
if (pdu_len) {
|
||||
pkt = (struct mstp_rx_packet *) Ringbuf_Alloc(&Receive_Queue);
|
||||
pkt = (struct mstp_rx_packet *) Ringbuf_Data_Peek(&Receive_Queue);
|
||||
if (pkt) {
|
||||
memcpy(pkt->buffer, PDUBuffer, MAX_MPDU);
|
||||
bacnet_address_copy(&pkt->src, &src);
|
||||
pkt->length = pdu_len;
|
||||
Ringbuf_Data_Put(&Receive_Queue, pkt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,7 +134,7 @@ static void bacnet_test_task(void)
|
||||
uint32_t instance;
|
||||
float float_value;
|
||||
uint16_t adc_value;
|
||||
|
||||
|
||||
instance = Analog_Input_Index_To_Instance(index);
|
||||
if (!Analog_Input_Out_Of_Service(instance)) {
|
||||
adc_value = adc_result_12bit(index);
|
||||
@@ -194,7 +195,7 @@ void bacnet_task(void)
|
||||
void bacnet_init(void)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
|
||||
Ringbuf_Init(&Receive_Queue, (uint8_t *) & Receive_Buffer,
|
||||
sizeof(struct mstp_rx_packet), MSTP_RECEIVE_PACKET_COUNT);
|
||||
dlmstp_init(NULL);
|
||||
@@ -229,5 +230,5 @@ void bacnet_init(void)
|
||||
Analog_Input_Units_Set(
|
||||
Analog_Input_Index_To_Instance(i),
|
||||
UNITS_PERCENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1267,7 +1267,7 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
|
||||
struct mstp_pdu_packet *pkt;
|
||||
uint16_t i = 0;
|
||||
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue);
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue);
|
||||
if (pkt) {
|
||||
pkt->data_expecting_reply = npdu_data->data_expecting_reply;
|
||||
for (i = 0; i < pdu_len; i++) {
|
||||
@@ -1279,7 +1279,9 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
|
||||
} else {
|
||||
pkt->destination_mac = dest->mac[0];
|
||||
}
|
||||
bytes_sent = pdu_len;
|
||||
if (Ringbuf_Data_Put(&PDU_Queue, pkt)) {
|
||||
bytes_sent = pdu_len;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes_sent;
|
||||
@@ -1294,13 +1296,13 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
|
||||
* @param max_pdu [in] - the size of the PDU buffer
|
||||
* @param timeout [in] - the number of milliseconds to wait for a packet
|
||||
*
|
||||
* @return Return the length of the packet
|
||||
* @return Return the length of the packet
|
||||
*/
|
||||
uint16_t dlmstp_receive(BACNET_ADDRESS * src,
|
||||
uint8_t * pdu,
|
||||
uint16_t max_pdu,
|
||||
unsigned timeout)
|
||||
{
|
||||
{
|
||||
uint16_t pdu_len = 0; /* return value */
|
||||
bool transmitting = false;
|
||||
|
||||
|
||||
@@ -207,7 +207,7 @@ bool bitstring_same(
|
||||
int bytes_used = 0;
|
||||
uint8_t compare_mask = 0;
|
||||
|
||||
if (bitstring1 && bitstring1) {
|
||||
if (bitstring1 && bitstring2) {
|
||||
if ((bitstring1->bits_used == bitstring2->bits_used) &&
|
||||
(bitstring1->bits_used / 8 <= MAX_BITSTRING_BYTES)) {
|
||||
bytes_used = (int) (bitstring1->bits_used / 8);
|
||||
|
||||
@@ -86,7 +86,7 @@ bool Ringbuf_Empty(
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Looks at the data from the head of the list without removing it
|
||||
* DESCRIPTION: Looks at the data from the front of the list without removing it
|
||||
* RETURN: pointer to the data, or NULL if nothing in the list
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
@@ -134,7 +134,7 @@ bool Ringbuf_Pop(
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Adds an element of data to the ring buffer
|
||||
* DESCRIPTION: Adds an element of data to the end of the ring buffer
|
||||
* RETURN: true on succesful add, false if not added
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
@@ -197,13 +197,13 @@ bool Ringbuf_Put_Front(
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Reserves and gets the next data portion of the buffer.
|
||||
* RETURN: pointer to the data, or NULL if the list is full
|
||||
* DESCRIPTION: Gets a pointer to the next free data element of the buffer
|
||||
* without adding it to the ring.
|
||||
* RETURN: pointer to the next data chunk, or NULL if ring buffer is full.
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
* NOTES: Use Ringbuf_Data_Peek with Ringbuf_Data_Put
|
||||
*****************************************************************************/
|
||||
volatile uint8_t *Ringbuf_Alloc(
|
||||
RING_BUFFER * b)
|
||||
volatile uint8_t *Ringbuf_Data_Peek(RING_BUFFER * b)
|
||||
{
|
||||
volatile uint8_t *ring_data = NULL; /* used to help point ring data */
|
||||
|
||||
@@ -212,13 +212,41 @@ volatile uint8_t *Ringbuf_Alloc(
|
||||
if (!Ringbuf_Full(b)) {
|
||||
ring_data = b->buffer;
|
||||
ring_data += ((b->head % b->element_count) * b->element_size);
|
||||
b->head++;
|
||||
}
|
||||
}
|
||||
|
||||
return ring_data;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Adds the previously peeked element of data to the end of the
|
||||
* ring buffer
|
||||
* RETURN: true if the buffer has space and the data element points to the
|
||||
* same memory previously peeked.
|
||||
* ALGORITHM: none
|
||||
* NOTES: Use Ringbuf_Data_Peek with Ringbuf_Data_Put
|
||||
*****************************************************************************/
|
||||
bool Ringbuf_Data_Put(RING_BUFFER * b, volatile uint8_t *data_element)
|
||||
{
|
||||
bool status = false;
|
||||
volatile uint8_t *ring_data = NULL; /* used to help point ring data */
|
||||
|
||||
if (b) {
|
||||
/* limit the amount of elements that we accept */
|
||||
if (!Ringbuf_Full(b)) {
|
||||
ring_data = b->buffer;
|
||||
ring_data += ((b->head % b->element_count) * b->element_size);
|
||||
if (ring_data == data_element) {
|
||||
/* same chunk of memory - okay to signal the head */
|
||||
b->head++;
|
||||
status = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Configures the ring buffer
|
||||
* RETURN: none
|
||||
|
||||
Reference in New Issue
Block a user