Microchip® Advanced Software Framework

Receiving using the FreeRTOS TWI driver in

standard mode

This example demonstrates using the FreeRTOS TWI driver to receive data using the standard operation mode. See Initializing the FreeRTOS TWI driver

The example below implements a function that simply reads a block of data from 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.

// 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 read_page_from_eeprom(freertos_twi_if freertos_twi,
uint16_t page, uint8_t receive_buffer)
{
twi_packet_t read_parameters;
uint16_t calculated_address;
const portTickType max_block_time_ticks = 200UL / portTICK_RATE_MS;
// Calculate the address being read from.
calculated_address = page * PAGE_SIZE;
// Configure the read_parameters structure to define the read operation.
read_parameters.chip = BOARD_AT24C_ADDRESS;
read_parameters.buffer = receive_buffer;
read_parameters.length = PAGE_SIZE;
read_parameters.addr[0] = (uint8_t) ((calculated_address >> 8) & 0xff);
read_parameters.addr[1] = (uint8_t) (calculated_address & 0xff);
read_parameters.addr_length = 2;
// Receive the data into receive_buffer. Don't wait any longer than 200ms
// to get exclusive access to the port (other FreeRTOS tasks will
// execute during any wait time.
if(freertos_twi_read_packet(freertos_twi, &read_parameters,
max_block_time_ticks) != STATUS_OK)
{
// Either an error occurred on the bus or exclusive access
// to the port was not obtained within 200ms.
}
else
{
// freertos_twi_read_packet() does not return until all the
// requested bytes have been received, so it is known that the
// data in receive_buffer is already complete, and can be
// processed immediately.
// ... Process received data here ...
do_something(receive_buffer);
}
}