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:
skarg
2015-09-09 14:54:02 +00:00
parent a36ac1ec45
commit 07bf4eba3b
11 changed files with 75 additions and 31 deletions
+3 -1
View File
@@ -60,8 +60,10 @@ extern "C" {
bool Ringbuf_Put_Front( bool Ringbuf_Put_Front(
RING_BUFFER * b, /* ring buffer structure */ RING_BUFFER * b, /* ring buffer structure */
uint8_t * data_element); uint8_t * data_element);
volatile uint8_t *Ringbuf_Alloc( volatile uint8_t *Ringbuf_Data_Peek(
RING_BUFFER * b); RING_BUFFER * b);
bool Ringbuf_Data_Put(
RING_BUFFER * b, volatile uint8_t *data_element);
/* Note: element_count must be a power of two */ /* Note: element_count must be a power of two */
void Ringbuf_Init( void Ringbuf_Init(
RING_BUFFER * b, /* ring buffer structure */ RING_BUFFER * b, /* ring buffer structure */
+4 -2
View File
@@ -1232,7 +1232,7 @@ int dlmstp_send_pdu(
struct mstp_pdu_packet *pkt; struct mstp_pdu_packet *pkt;
uint16_t i = 0; 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) { if (pkt) {
pkt->data_expecting_reply = npdu_data->data_expecting_reply; pkt->data_expecting_reply = npdu_data->data_expecting_reply;
for (i = 0; i < pdu_len; i++) { for (i = 0; i < pdu_len; i++) {
@@ -1245,7 +1245,9 @@ int dlmstp_send_pdu(
/* mac_len = 0 is a broadcast address */ /* mac_len = 0 is a broadcast address */
pkt->destination_mac = MSTP_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; return bytes_sent;
+4 -2
View File
@@ -1194,7 +1194,7 @@ int dlmstp_send_pdu(
struct mstp_pdu_packet *pkt; struct mstp_pdu_packet *pkt;
uint16_t i = 0; 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) { if (pkt) {
pkt->data_expecting_reply = npdu_data->data_expecting_reply; pkt->data_expecting_reply = npdu_data->data_expecting_reply;
for (i = 0; i < pdu_len; i++) { for (i = 0; i < pdu_len; i++) {
@@ -1207,7 +1207,9 @@ int dlmstp_send_pdu(
/* mac_len = 0 is a broadcast address */ /* mac_len = 0 is a broadcast address */
pkt->destination_mac = MSTP_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; return bytes_sent;
+4 -2
View File
@@ -155,7 +155,7 @@ int dlmstp_send_pdu(
struct mstp_pdu_packet *pkt; struct mstp_pdu_packet *pkt;
unsigned i = 0; unsigned i = 0;
pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&PDU_Queue); pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue);
if (pkt) { if (pkt) {
pkt->data_expecting_reply = npdu_data->data_expecting_reply; pkt->data_expecting_reply = npdu_data->data_expecting_reply;
for (i = 0; i < pdu_len; i++) { for (i = 0; i < pdu_len; i++) {
@@ -168,7 +168,9 @@ int dlmstp_send_pdu(
/* mac_len = 0 is a broadcast address */ /* mac_len = 0 is a broadcast address */
pkt->destination_mac = MSTP_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; return bytes_sent;
+4 -2
View File
@@ -151,7 +151,7 @@ int dlmstp_send_pdu(
return 0; 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) { if (pkt) {
pkt->data_expecting_reply = pkt->data_expecting_reply =
BACNET_DATA_EXPECTING_REPLY(pdu[BACNET_PDU_CONTROL_BYTE_OFFSET]); BACNET_DATA_EXPECTING_REPLY(pdu[BACNET_PDU_CONTROL_BYTE_OFFSET]);
@@ -160,7 +160,9 @@ int dlmstp_send_pdu(
} }
pkt->length = pdu_len; pkt->length = pdu_len;
pkt->destination_mac = dest->mac[0]; 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; return bytes_sent;
+7 -4
View File
@@ -444,10 +444,10 @@ static void MSTP_Send_Frame(
{ /* number of bytes of data (up to 501) */ { /* number of bytes of data (up to 501) */
uint8_t crc8 = 0xFF; /* used to calculate the crc value */ uint8_t crc8 = 0xFF; /* used to calculate the crc value */
uint16_t crc16 = 0xFFFF; /* 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 */ 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) { if (pkt) {
/* create the MS/TP header */ /* create the MS/TP header */
pkt->buffer[0] = 0x55; pkt->buffer[0] = 0x55;
@@ -476,6 +476,7 @@ static void MSTP_Send_Frame(
pkt->length += data_len; pkt->length += data_len;
pkt->length += 2; pkt->length += 2;
} }
Ringbuf_Data_Put(&Transmit_Queue, (uint8_t *)pkt);
} else { } else {
pkt = NULL; pkt = NULL;
} }
@@ -1274,7 +1275,7 @@ int dlmstp_send_pdu(
struct mstp_pdu_packet *pkt; struct mstp_pdu_packet *pkt;
uint16_t i = 0; 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) { if (pkt) {
pkt->data_expecting_reply = npdu_data->data_expecting_reply; pkt->data_expecting_reply = npdu_data->data_expecting_reply;
for (i = 0; i < pdu_len; i++) { for (i = 0; i < pdu_len; i++) {
@@ -1286,7 +1287,9 @@ int dlmstp_send_pdu(
} else { } else {
pkt->destination_mac = MSTP_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; return bytes_sent;
@@ -46,7 +46,7 @@
#include "stm32f10x_tim.h" #include "stm32f10x_tim.h"
#include "stm32f10x_usart.h" #include "stm32f10x_usart.h"
#include "stm32f10x_wwdg.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 types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/
+5 -4
View File
@@ -113,11 +113,12 @@ void bacnet_task_timed(
pdu_len = dlmstp_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 5); pdu_len = dlmstp_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 5);
if (pdu_len) { if (pdu_len) {
pkt = (struct mstp_rx_packet *) Ringbuf_Alloc(&Receive_Queue); pkt = (struct mstp_rx_packet *) Ringbuf_Data_Peek(&Receive_Queue);
if (pkt) { if (pkt) {
memcpy(pkt->buffer, PDUBuffer, MAX_MPDU); memcpy(pkt->buffer, PDUBuffer, MAX_MPDU);
bacnet_address_copy(&pkt->src, &src); bacnet_address_copy(&pkt->src, &src);
pkt->length = pdu_len; pkt->length = pdu_len;
Ringbuf_Data_Put(&Receive_Queue, pkt);
} }
} }
} }
@@ -133,7 +134,7 @@ static void bacnet_test_task(void)
uint32_t instance; uint32_t instance;
float float_value; float float_value;
uint16_t adc_value; uint16_t adc_value;
instance = Analog_Input_Index_To_Instance(index); instance = Analog_Input_Index_To_Instance(index);
if (!Analog_Input_Out_Of_Service(instance)) { if (!Analog_Input_Out_Of_Service(instance)) {
adc_value = adc_result_12bit(index); adc_value = adc_result_12bit(index);
@@ -194,7 +195,7 @@ void bacnet_task(void)
void bacnet_init(void) void bacnet_init(void)
{ {
unsigned i; unsigned i;
Ringbuf_Init(&Receive_Queue, (uint8_t *) & Receive_Buffer, Ringbuf_Init(&Receive_Queue, (uint8_t *) & Receive_Buffer,
sizeof(struct mstp_rx_packet), MSTP_RECEIVE_PACKET_COUNT); sizeof(struct mstp_rx_packet), MSTP_RECEIVE_PACKET_COUNT);
dlmstp_init(NULL); dlmstp_init(NULL);
@@ -229,5 +230,5 @@ void bacnet_init(void)
Analog_Input_Units_Set( Analog_Input_Units_Set(
Analog_Input_Index_To_Instance(i), Analog_Input_Index_To_Instance(i),
UNITS_PERCENT); UNITS_PERCENT);
} }
} }
+6 -4
View File
@@ -1267,7 +1267,7 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
struct mstp_pdu_packet *pkt; struct mstp_pdu_packet *pkt;
uint16_t i = 0; 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) { if (pkt) {
pkt->data_expecting_reply = npdu_data->data_expecting_reply; pkt->data_expecting_reply = npdu_data->data_expecting_reply;
for (i = 0; i < pdu_len; i++) { for (i = 0; i < pdu_len; i++) {
@@ -1279,7 +1279,9 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
} else { } else {
pkt->destination_mac = dest->mac[0]; pkt->destination_mac = dest->mac[0];
} }
bytes_sent = pdu_len; if (Ringbuf_Data_Put(&PDU_Queue, pkt)) {
bytes_sent = pdu_len;
}
} }
return bytes_sent; 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 max_pdu [in] - the size of the PDU buffer
* @param timeout [in] - the number of milliseconds to wait for a packet * @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, uint16_t dlmstp_receive(BACNET_ADDRESS * src,
uint8_t * pdu, uint8_t * pdu,
uint16_t max_pdu, uint16_t max_pdu,
unsigned timeout) unsigned timeout)
{ {
uint16_t pdu_len = 0; /* return value */ uint16_t pdu_len = 0; /* return value */
bool transmitting = false; bool transmitting = false;
+1 -1
View File
@@ -207,7 +207,7 @@ bool bitstring_same(
int bytes_used = 0; int bytes_used = 0;
uint8_t compare_mask = 0; uint8_t compare_mask = 0;
if (bitstring1 && bitstring1) { if (bitstring1 && bitstring2) {
if ((bitstring1->bits_used == bitstring2->bits_used) && if ((bitstring1->bits_used == bitstring2->bits_used) &&
(bitstring1->bits_used / 8 <= MAX_BITSTRING_BYTES)) { (bitstring1->bits_used / 8 <= MAX_BITSTRING_BYTES)) {
bytes_used = (int) (bitstring1->bits_used / 8); bytes_used = (int) (bitstring1->bits_used / 8);
+36 -8
View File
@@ -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 * RETURN: pointer to the data, or NULL if nothing in the list
* ALGORITHM: none * ALGORITHM: none
* NOTES: 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 * RETURN: true on succesful add, false if not added
* ALGORITHM: none * ALGORITHM: none
* NOTES: none * NOTES: none
@@ -197,13 +197,13 @@ bool Ringbuf_Put_Front(
} }
/**************************************************************************** /****************************************************************************
* DESCRIPTION: Reserves and gets the next data portion of the buffer. * DESCRIPTION: Gets a pointer to the next free data element of the buffer
* RETURN: pointer to the data, or NULL if the list is full * without adding it to the ring.
* RETURN: pointer to the next data chunk, or NULL if ring buffer is full.
* ALGORITHM: none * ALGORITHM: none
* NOTES: none * NOTES: Use Ringbuf_Data_Peek with Ringbuf_Data_Put
*****************************************************************************/ *****************************************************************************/
volatile uint8_t *Ringbuf_Alloc( volatile uint8_t *Ringbuf_Data_Peek(RING_BUFFER * b)
RING_BUFFER * b)
{ {
volatile uint8_t *ring_data = NULL; /* used to help point ring data */ 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)) { if (!Ringbuf_Full(b)) {
ring_data = b->buffer; ring_data = b->buffer;
ring_data += ((b->head % b->element_count) * b->element_size); ring_data += ((b->head % b->element_count) * b->element_size);
b->head++;
} }
} }
return ring_data; 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 * DESCRIPTION: Configures the ring buffer
* RETURN: none * RETURN: none