Microchip® Advanced Software Framework

Receiving using the FreeRTOS SPI driver in

standard mode

This example demonstrates using the FreeRTOS SPI driver to receive data using the standard operation mode. See Initializing the FreeRTOS SPI driver

The example below implements a function that simply reads a block of data from an SPI port on each iteration of a loop.

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.

// This examples assumes freertos_spi has already been set by a successful
// call to freertos_spi_master_init(), and that freertos_spi_master_init()
// configured the FreeRTOS SPI driver to use the standard operation mode.
void a_function(freertos_spi_if freertos_spi)
{
// The receive buffer is declared static to ensure it does not overflow the
// task stack.
static uint8_t receive_buffer[50];
const max_block_time_50ms = 50 / portTICK_RATE_MS;
// Loop around, reading and then processing 20 bytes from freertos_spi
// on each iteration.
for(;;)
{
// Receive 20 bytes from freertos_spi into receive_buffer. Don't
// wait any longer than 50ms to get exclusive access to the port
// (other FreeRTOS tasks will execute during any wait time).
if(freertos_spi_read_packet(freertos_spi, receive_buffer, 20,
max_block_time_50ms) == STATUS_OK)
{
// freertos_spi_read_packet() does not return until all the
// requested bytes have been received, so it is known that the
// data in receive_buffer is already complete, and can be
// processed immediately.
// ... Process received data here ...
do_something(receive_buffer);
}
else
{
// Either an error occurred on the bus or exclusive access
// to the port was not obtained within 50ms.
}
}
}