Microchip® Advanced Software Framework

Receiving using the FreeRTOS USART driver

This example demonstrates using the FreeRTOS USART API to receive data.

The example below implements a function that attempts to read 20 bytes from a USART. The read operation will time out if 20 bytes could not be obtained within 20ms.

Refer to the FreeRTOS peripheral control projects in the Atmel ASF distribution for complete working examples, and the FreeRTOS web site for information on getting started with FreeRTOS.

void a_function(freertos_usart_if freertos_usart)
{
uint8_t receive_buffer[20];
uint32_t bytes_received;
portTickType max_wait_20ms = 20 / portTICK_RATE_MS;
// Attempt to read 20 bytes from freertos_usart. If fewer than 20 bytes are
// available, then wait a maximum of 20ms for the rest to arrive.
bytes_received = freertos_usart_serial_read_packet(freertos_usart,
receive_buffer, 20, max_wait_20ms);
if(bytes_received == 20)
{
// All the bytes were received. The RTOS task calling this function
// *may* have been placed into the Blocked state to wait for all the
// bytes to be available. Other tasks will execute while this task
// is in the Blocked state.
}
else
{
// Fewer than the requested number of bytes have been received, so
// the RTOS task calling this function did enter the blocked state
// for the full 20 milliseconds before giving up.
}
}