18.8.2.2.1 Code
The following must be added to the user application:
A sample buffer to write from, a sample buffer to read to and length of buffers:
Address to respond to:#define DATA_LENGTH 10staticuint8_t write_buffer[DATA_LENGTH] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,};staticuint8_t read_buffer [DATA_LENGTH];
#define SLAVE_ADDRESS 0x12
Globally accessible module structure: Globally accessible packet:structi2c_slave_module i2c_slave_instance;
Function for setting up the module:staticstructi2c_slave_packet packet;
Callback function for read request from a master:voidconfigure_i2c_slave(void){/* Initialize config structure and module instance */structi2c_slave_config config_i2c_slave;i2c_slave_get_config_defaults(&config_i2c_slave);/* Change address and address_mode */config_i2c_slave.address = SLAVE_ADDRESS;config_i2c_slave.address_mode = I2C_SLAVE_ADDRESS_MODE_MASK;/* Initialize and enable device with config */i2c_slave_init(&i2c_slave_instance, CONF_I2C_SLAVE_MODULE, &config_i2c_slave);i2c_slave_enable(&i2c_slave_instance);}
Callback function for write request from a master:voidi2c_read_request_callback(structi2c_slave_module *constmodule){/* Init i2c packet */packet.data_length = DATA_LENGTH;packet.data = write_buffer;/* Write buffer to master */i2c_slave_write_packet_job(module, &packet);}
Function for setting up the callback functionality of the driver:voidi2c_write_request_callback(structi2c_slave_module *constmodule){/* Init i2c packet */packet.data_length = DATA_LENGTH;packet.data = read_buffer;/* Read buffer from master */if(i2c_slave_read_packet_job(module, &packet) != STATUS_OK) {}}
Add to user application main():voidconfigure_i2c_slave_callbacks(void){/* Register and enable callback functions */i2c_slave_register_callback(&i2c_slave_instance, i2c_read_request_callback,I2C_SLAVE_CALLBACK_READ_REQUEST);i2c_slave_enable_callback(&i2c_slave_instance,I2C_SLAVE_CALLBACK_READ_REQUEST);i2c_slave_register_callback(&i2c_slave_instance, i2c_write_request_callback,I2C_SLAVE_CALLBACK_WRITE_REQUEST);i2c_slave_enable_callback(&i2c_slave_instance,I2C_SLAVE_CALLBACK_WRITE_REQUEST);}
/* Configure device and enable */configure_i2c_slave();configure_i2c_slave_callbacks();
