In this use case, the I2C will used and set up as follows:
- Slave mode
- 100KHz operation speed
- Not operational in standby
- 10000 packet timeout value
Prerequisites
The device must be connected to an I2C master.
Setup
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:
#define DATA_LENGTH 10
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
};
Address to respond to:
#define SLAVE_ADDRESS 0x12
Globally accessible module structure:
Globally accessible packet:
Function for setting up the module:
{
#if SAMR30
config_i2c_slave.pinmux_pad0 = CONF_SLAVE_SDA_PINMUX;
config_i2c_slave.pinmux_pad1 = CONF_SLAVE_SCK_PINMUX;
#endif
}
Callback function for read request from a master:
Callback function for write request from a master:
Function for setting up the callback functionality of the driver:
Add to user application main()
:
Workflow
- Configure and enable module.
- Create and initialize configuration structure.
- Change address and address mode settings in the configuration.
#if SAMR30
config_i2c_slave.pinmux_pad0 = CONF_SLAVE_SDA_PINMUX;
config_i2c_slave.pinmux_pad1 = CONF_SLAVE_SCK_PINMUX;
#endif
- Initialize the module with the set configurations.
- Enable the module.
- Register and enable callback functions.
- Register and enable callbacks for read and write requests from master.
Implementation
Code
Add to user application main()
:
Workflow
- Infinite while loop, while waiting for interaction from master.
Callback
When an address packet is received, one of the callback functions will be called, depending on the DIR bit in the received packet.
Workflow
- Read request callback:
- Length of buffer and buffer to be sent to master is initialized.
- Write packet to master.
- Write request callback:
- Length of buffer and buffer to be read from master is initialized.
- Read packet from master.