Microchip® Advanced Software Framework

Transmitting using the FreeRTOS TWI driver in

fully asynchronous mode

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

The example below implements a function that simply writes a block of data to a TWI 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_twi.
//
// This examples assumes freertos_twi has already been set by a successful
// call to freertos_twi_master_init(), and that freertos_twi_master_init()
// configured the FreeRTOS TWI 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_page_to_eeprom(freertos_twi_if freertos_twi,
uint16_t page, xSemaphoreHandle notification_semaphore)
{
twi_packet_t write_parameters;
uint16_t calculated_address;
const portTickType max_block_time_ticks = 200UL / portTICK_RATE_MS;
// Calculate the address being written to
calculated_address = page * PAGE_SIZE;
// Configure the twi_packet_t structure to define the write operation
write_parameters.chip = BOARD_AT24C_ADDRESS;
write_parameters.buffer = data_buffer;
write_parameters.length = PAGE_SIZE;
write_parameters.addr[0] = (uint8_t) ((calculated_address >> 8) & 0xff);
write_parameters.addr[1] = (uint8_t) (calculated_address & 0xff);
write_parameters.addr_length = 2;
// Attempt to initiate a DMA transfer to write the data from data_buffer
// to the port referenced by the freertos_twi handle. Wait a maximum of
// 200ms to get exclusive access to the port (other FreeRTOS tasks will
// execute during any waiting time).
if(freertos_twi_write_packet(freertos_twi, &write_parameters,
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.
}
}
}