• AVR Freaks

Buffer Queue data transfer model - Problem with managing receive buffers

Author
dvvrcognizant
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2019/03/13 07:48:11
  • Location: 0
  • Status: offline
2019/05/09 23:51:10 (permalink)
0

Buffer Queue data transfer model - Problem with managing receive buffers

I try to receive data from UART in my application using "UART Dynamic Driver - Buffer Queue Data Transfer Model with DMA". I took an array of buffers. When application tries to read, it checks if there is any data in buffer, if so, then it takes the data and then adds unused buffers.
 
The problem i see is that the sequence i get data in buffers is not same as the sequence in which i add buffers. For example, if add buffers in the sequence: b1, b2, b3 and b4, then i get data in buffers in the sequence b1, b4, b3 and b2. Ideally the data in buffers should be received in the sequence in which the buffers are added i.e, b1, b2, b3 and b4.
 
PFA the code files.
1. physical_layer.c
2. physical_layer.h
 
Function that reads data is "int8_t receive_data_from_physical_layer(uint8_t *ptr_data, uint16_t *ptrdata_size);"
The receive buffers in the code are: 
/* Receive Buffers */
static RCV_BUFFER g_receiveBuffers[NUM_RCV_BUFFS];
 
Code adding receive buffers to receive queue is:
/* Add free receive buffers to receive queue */
for ( bufferCount = 0; bufferCount < NUM_RCV_BUFFS; bufferCount++ )
{
if (false == g_receiveBuffers[bufferCount].inUse)
{
g_receiveBuffers[bufferCount].inUse = true;
for ( loopVar = 0; loopVar < MAX_NUM_OF_BYTES_TO_RECEIVE; loopVar++ )
{
g_receiveBuffers[bufferCount].rx_data[loopVar] = '\0';

/* Clear UART Receiver Overrun error */
USART_ReceiverOverrunErrorClear_Default(USART_ID_1);

do
{
uart1DrvCliSts = DRV_USART_ClientStatus( g_handleUSART1 );
if ( uart1DrvCliSts == DRV_USART_CLIENT_STATUS_READY )
{
DRV_USART_BufferAddRead( g_handleUSART1,
&g_receiveBuffers[bufferCount].drvUsart1RxBufHandle,
&g_receiveBuffers[bufferCount].rx_data[0],
MAX_NUM_OF_BYTES_TO_RECEIVE );
if( DRV_USART_BUFFER_HANDLE_INVALID == \
g_receiveBuffers[bufferCount].drvUsart1RxBufHandle )
{
#ifdef PHYSICAL_LAYER_ERROR
printf("PL: Error: Adding the buffer to 'Receive Queue' failed \n");
#endif
g_receiveBuffers[bufferCount].inUse = false;
}
else
{
#ifdef PHYSICAL_LAYER_TRACE
printf("PL: Read Buffer is added to Receive Queue \n");
#endif
}
}
else
{
#ifdef PHYSICAL_LAYER_ERROR
printf("PL: Error: UART1 Driver Client not ready \n");
#endif
}
} while ( uart1DrvCliSts != DRV_USART_CLIENT_STATUS_READY );
}
}
}
 
Code reading data from buffers is:
/* Check if any data is present in the buffered added to receive queue */
for ( bufferCount = 0; bufferCount < NUM_RCV_BUFFS; bufferCount++ )
{
if (true == g_receiveBuffers[bufferCount].dataRcvd)
{
g_receiveBuffers[bufferCount].dataRcvd = false;
g_receiveBuffers[bufferCount].inUse = false;
for ( loopVar = 0; loopVar < MAX_NUM_OF_BYTES_TO_RECEIVE; loopVar++ )
{
//#ifdef PHYSICAL_LAYER_DEBUG
#if 0
printf("g_receiveBuffers[%d].rx_data[%d] = 0x%x \n", bufferCount, loopVar, g_receiveBuffers[bufferCount].rx_data[loopVar]);
#endif

ptr_data[receivedBytes] = g_receiveBuffers[bufferCount].rx_data[loopVar];

#ifdef PHYSICAL_LAYER_DEBUG
//#if 0
printf("ptr_data[%d] = 0x%x \n", receivedBytes, ptr_data[receivedBytes]);
#endif
++receivedBytes;
}
}
}
 
The call back function called by driver and in which is data is put into receive buffers is:
static void buffer_event_handler_uart1(DRV_USART_BUFFER_EVENT buffEvent, DRV_USART_BUFFER_HANDLE bufferHandle,
uintptr_t context )
 
Please look into my code and let me know if there is anything wrong in the design/code that causes the problem.
#1

0 Replies Related Threads

    Jump to:
    © 2019 APG vNext Commercial Version 4.5