Microchip® Advanced Software Framework

Quickstart guide for Common service TWI

This is the quickstart guide for the Common service TWI, with step-by-step instructions on how to configure and use the driver in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

Basic use case

In the most basic use case, the TWI module is configured for

  • Master operation
  • addressing one slave device of the bus at address 0x50
  • TWI clock of 50kHz
  • polled read/write handling

Setup steps

Example code

Add to your application C-file:

void twi_init(void)
{
.speed = 50000,
.chip = 0x50
};
twi_master_setup(&TWIM0, &opt);
}

Workflow

  1. Ensure that board_init() has configured selected I/Os for TWI function.
  2. Ensure that conf_twim.h is present for the driver.
    • Note
      This file is only for the driver and should not be included by the user.
  3. Define and initialize config structs for TWI module in your TWI initialization function:
    • .speed = 50000,
      .chip = 0x50
      };
    • field speed sets the baudrate of the TWI bus
    • field chip sets the address of the slave device you want to communicate with
  4. Call twi_master_setup and optionally check its return code
    • Note
      The config structs can be reused for other TWI modules after this step. Simply reconfigure and write to others modules.

Usage steps

Example code : Writing to a slave device

Use in application C-file:

const uint8_t test_pattern[] = {0x55,0xA5,0x5A,0x77,0x99};
twi_package_t packet_write = {
.addr = EEPROM_MEM_ADDR, // TWI slave memory address data
.addr_length = sizeof (uint16_t), // TWI slave memory address data size
.chip = EEPROM_BUS_ADDR, // TWI slave bus address
.buffer = (void *)test_pattern, // transfer data source buffer
.length = sizeof(test_pattern) // transfer data size (bytes)
};
while (twi_master_write(&TWIM0, &packet_write) != TWI_SUCCESS);

Workflow

  1. Prepare the data you want to send to the slave device:
    • const uint8_t test_pattern[] = {0x55,0xA5,0x5A,0x77,0x99};
  2. Prepare a twi_package_t structure
    twi_package_t packet_write;
    Fill all the fields of the structure :
    • addr is the address in the slave device
    • addr_length is the size of the address in the slave (support for large TWI memory devices)
    • chip sets the 7 bit address of the slave device you want to communicate with
    • buffer is a pointer on the data to write to slave
    • length is the number of data to write
  3. Finally, call twi_master_write
    twi_master_write(&TWIM0, &packet_write);
    and optionally check its return value for TWI_SUCCESS.

Example code : Reading from a slave device

Use in application C-file:

uint8_t data_received[10];
twi_package_t packet_read = {
.addr = EEPROM_MEM_ADDR, // TWI slave memory address data
.addr_length = sizeof (uint16_t), // TWI slave memory address data size
.chip = EEPROM_BUS_ADDR, // TWI slave bus address
.buffer = data_received, // transfer data destination buffer
.length = 10 // transfer data size (bytes)
};
// Perform a multi-byte read access then check the result.
if(twi_master_read(&TWIM0, &packet_read) == TWI_SUCCESS){
//Check read content
if(data_received[0]==0x55)
do_something();
}

Workflow

  1. Prepare a data buffer that will receive the data from the slave device:
    uint8_t data_received[10];
  2. Prepare a twi_package_t structure
    twi_package_t packet_read;
    Fill all the fields of the structure :
    • addr is the address in the slave device
    • addr_length is the size of the address in the slave (support for large TWI memory devices)
    • chip sets the 7 bit address of the slave device you want to communicate with
    • buffer is a pointer on the data buffer that will receive the data from the slave device
    • length is the number of data to read
  3. Finally, call twi_master_read
    twi_master_read(&TWIM0, &packet_read);
    and optionally check its return value for TWI_SUCCESS. the data read from the device are now in data_received.