Microchip® Advanced Software Framework

Transmitting using the FreeRTOS TWI driver in

standard mode

This example demonstrates using the FreeRTOS TWI driver to send data using the standard 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 standard operation mode.
static void write_page_to_eeprom(freertos_twi_if freertos_twi, uint16_t page)
{
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 write the data 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) != STATUS_OK)
{
// The data was not written successfully, either because there was
// an error on the TWI bus, or because exclusive access to the TWI
// port was not obtained within 200ms.
}
}