Microchip® Advanced Software Framework

Initializing the FreeRTOS TWI driver

for standard operation

This example configures the FreeRTOS TWI 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_twi_write_packet() and freertos_twi_read_packet() API functions can be used to transmit and receive data respectively. freertos_twi_write_packet_async() and freertos_twi_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_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, 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 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.
}