Microchip® Advanced Software Framework

Initializing the FreeRTOS SPI driver

for standard operation

This example configures the FreeRTOS SPI driver to:

  • Support simultaneous access from multiple RTOS tasks.
  • Wait in the FreeRTOS transmit function until all the data has been completely sent
  • Wait in the FreeRTOS receive function until all the requested data has been received

Other FreeRTOS tasks will execute during any wait periods.

If the driver is initialized with this configuration then only the freertos_spi_write_packet() and freertos_spi_read_packet() API functions can be used to transmit and receive data respectively. freertos_spi_write_packet_async() and freertos_spi_read_packet_async() must not be used.

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.

// Declare the variables used as parameters to the
// freertos_spi_master_init() function.
// Handle used to access the initialized port by other FreeRTOS ASF functions.
freertos_spi_if freertos_spi;
// Configuration structure.
// This peripheral is synchronous and so does not need a receive buffer.
// The receive_buffer value is just set to NULL.
// There isn't a receive buffer, so the receive_buffer_size value can
// take any value.
0,
// The interrupt priority. The FreeRTOS driver provides the interrupt
// service routine, and handles all interrupt interactions. The
// application writer does not need to provide any interrupt handling
// code, but does need to specify the priority of the DMA interrupt here.
// IMPORTANT!!! see <a href="http://www.freertos.org/RTOS-Cortex-M3-M4.html">how to set interrupt priorities to work with FreeRTOS</a>
0x0e,
// The operation_mode value.
// Flags set to allow access from multiple tasks, wait in the transmit
// function until the transmit is complete, and wait in the receive
// function until reception is complete. Note that other FreeRTOS tasks
// will execute during the wait period.
};
// Call the SPI specific FreeRTOS ASF driver initialization function,
// storing the return value as the driver handle.
freertos_spi = freertos_spi_master_init(spi_base, &driver_options);
if (freertos_spi != NULL) {
// Calling freertos_spi_master_init() will enable the peripheral clock,
// and set the SPI into master mode. Other ASF configuration functions,
// such as spi_set_clock_polarity(), and spi_set_baudrate_div() can then
// be called here.
}