Microchip® Advanced Software Framework

Initializing the FreeRTOS TWI driver

for asynchronous operation

This example configures the FreeRTOS TWI driver to:

  • Support simultaneous access from multiple RTOS tasks.
  • Exit the FreeRTOS transmit function as soon as the transmission is started.
  • Exit the receive function as soon as the receive has been requested.

If the driver is initialized with this configuration then only the freertos_twi_write_packet_async() and freertos_twi_read_packet_async() API functions can be used to transmit and receive data respectively. freertos_twi_write_packet() and freertos_twi_read_packet() 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_twi_master_init() function.
// Handle used to access the initialized port by other FreeRTOS ASF functions.
freertos_twi_if freertos_twi;
// 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. Note the
// WAIT_TX_COMPLETE and WAIT_RX_COMPLETE bits are *not* set.
};
// Call the TWI specific FreeRTOS ASF driver initialization function,
// storing the return value as the driver handle.
freertos_twi = freertos_twi_master_init(twi_base, &driver_options);
if (freertos_twi != NULL) {
// Calling freertos_twi_master_init() will enable the peripheral
// clock, and set the TWI into I2C master mode. Other ASF
// configuration functions, such as twi_set_speed(), can then be
// called here.
}