Hi,
For the function calling
USART_RTOS_Receive(&handle, recv_buffer, sizeof(recv_buffer), &n)
The operating system will be blocked at the above function until number of characters predefined by length variables(sizeof(recv_buffer)) has been received.
I think the system behaves normally, if you send 5 characters for once transfer, after 4 characters have been received, USART_RTOS_Receive() will be unblocked. The remaining one character will be treated as next receiving process.
BTW, I suppose that you'd better not to change the low level driver of SDK or Freertos as you have done to call the USART_FIFOCFG_EMPTYRX(1);, which will miss the characters.
Hope it can help you
BR
XiangJun Rong
/*!
* brief Receives data.
*
* This function receives data from USART. It is a synchronous API. If data is immediately available,
* it is returned immediately and the number of bytes received.
*
* param handle The RTOS USART handle.
* param buffer The pointer to buffer where to write received data.
* param length The number of bytes to receive.
* param received The pointer to a variable of size_t where the number of received data is filled.
*/
int USART_RTOS_Receive(usart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length, size_t *received)
{
EventBits_t ev;
size_t n = 0;
int retval = kStatus_Fail;
size_t local_received = 0;
status_t status;
if (NULL == handle->base)
{
/* Invalid handle. */
return kStatus_Fail;
}
if (0U == length)
{
if (received != NULL)
{
*received = n;
}
return kStatus_Success;
}
if (NULL == buffer)
{
return kStatus_InvalidArgument;
}
/* New transfer can be performed only after current one is finished */
if (pdFALSE == xSemaphoreTake(handle->rxSemaphore, portMAX_DELAY))
{
/* We could not take the semaphore, exit with 0 data received */
return kStatus_Fail;
}
handle->rxTransfer.data = buffer;
handle->rxTransfer.dataSize = (uint32_t)length;
/* Non-blocking call */
status = USART_TransferReceiveNonBlocking(handle->base, handle->t_state, &handle->rxTransfer, &n);
if (status != kStatus_Success)
{
(void)xSemaphoreGive(handle->rxSemaphore);
return kStatus_Fail;
}
ev = xEventGroupWaitBits(handle->rxEvent, RTOS_USART_COMPLETE | RTOS_USART_RING_BUFFER_OVERRUN, pdTRUE, pdFALSE,
portMAX_DELAY);
if ((ev & RTOS_USART_RING_BUFFER_OVERRUN) != 0U)
{
/* Stop data transfer to application buffer, ring buffer is still active */
USART_TransferAbortReceive(handle->base, handle->t_state);
/* Prevent false indication of successful transfer in next call of USART_RTOS_Receive.
RTOS_USART_COMPLETE flag could be set meanwhile overrun is handled */
(void)xEventGroupClearBits(handle->rxEvent, RTOS_USART_COMPLETE);
retval = kStatus_USART_RxRingBufferOverrun;
local_received = 0;
}
else if ((ev & RTOS_USART_COMPLETE) != 0U)
{
retval = kStatus_Success;
local_received = length;
}
else
{
retval = kStatus_USART_RxError;
local_received = 0;
}
/* Prevent repetitive NULL check */
if (received != NULL)
{
*received = local_received;
}
/* Enable next transfer. Current one is finished */
if (pdFALSE == xSemaphoreGive(handle->rxSemaphore))
{
/* We could not post the semaphore, exit with error */
retval = kStatus_Fail;
}
return retval;
}