17.8.1.2.1 Code

The following must be added to the user application:

  • A sample buffer to send, a sample buffer to read:
    #define DATA_LENGTH 10
    static uint8_t write_buffer[DATA_LENGTH] = {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
    };
    
    static uint8_t read_buffer[DATA_LENGTH];
    
  • Slave address to access:
    #define SLAVE_ADDRESS 0x12
    
  • Number of times to try to send packet if it fails:
    #define TIMEOUT 1000
    
  • Globally accessible module structure:
    struct i2c_master_module i2c_master_instance;
    
  • Function for setting up the module:
    void configure_i2c_master(void)
    {
        /* Initialize config structure and software module. */
        struct i2c_master_config config_i2c_master;
        i2c_master_get_config_defaults(&config_i2c_master);
    
        /* Change buffer timeout to something longer. */
        config_i2c_master.buffer_timeout = 10000;
    
        /* Initialize and enable device with config. */
        i2c_master_init(&i2c_master_instance, CONF_I2C_MASTER_MODULE, &config_i2c_master);
    
        i2c_master_enable(&i2c_master_instance);
    }
    
  • Add to user application main():
    /* Configure device and enable. */
    configure_i2c_master();
    
    /* Timeout counter. */
    uint16_t timeout = 0;
    
    /* Init i2c packet. */
    struct i2c_master_packet packet = {
        .address     = SLAVE_ADDRESS,
        .data_length = DATA_LENGTH,
        .data        = write_buffer,
        .ten_bit_address = false,
        .high_speed      = false,
        .hs_master_code  = 0x0,
    };