Microchip® Advanced Software Framework

Transmitting using the FreeRTOS SPI driver in

fully asynchronous mode

This example demonstrates using the FreeRTOS SPI driver to send data using the fully asynchronous operation mode. See Initializing the FreeRTOS SPI driver

The example below implements a function that simply writes a block of data to an SPI port.

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.

// Write number_of_bytes from data_buffer to freertos_spi.
//
// 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 fully asynchronous operation
// mode.
//
// The example further assumes that notification_semaphore has already been
// initialised by a call to the vSemaphoreCreateBinary() FreeRTOS API
// function.
static void write_to_eeprom(freertos_spi_if freertos_spi,
uint8_t data_buffer size_t number_of_bytes,
xSemaphoreHandle notification_semaphore)
{
const portTickType max_block_time_ticks = 200UL / portTICK_RATE_MS;
// Attempt to initiate a DMA transfer of number_of_bytes bytes from
// data_buffer to the port referenced by the freertos_spi handle. Wait
// a maximum of 200ms to get exclusive access to the port (other
// FreeRTOS tasks will execute during any waiting time).
if(freertos_spi_write_packet_async(freertos_spi, data_buffer,
number_of_bytes, max_block_time_ticks,
notification_semaphore) != STATUS_OK)
{
// The DMA transfer was not started because exclusive access to the
// port was not obtained within 200ms.
}
else
{
// The DMA transfer was started successfully, but has not
// necessarily completed yet. Other processing can be performed
// here, but THE DATA IN data_buffer MUST NOT BE ALTERED UNTIL THE
// TRANSMISSION HAS COMPLETED.
// ...
// Ensure the transaction is complete before proceeding further by
// waiting for the notification_semaphore. Don't wait longer than
// 200ms. Other FreeRTOS tasks will execute during any waiting
// time.
if(xSemaphoreTake(notification_semaphore,
max_block_time_ticks) != pdPASS)
{
// The semaphore could not be obtained within 200ms. Either the
// data is still being transmitted, or an error occurred.
}
}
}