In this use case, the I2C will used and set up as follows:
- Master mode
- 100KHz operation speed
- Not operational in standby
- 65535 unknown bus state timeout value
Prerequisites
The device must be connected to an I2C slave.
Setup
Code
The following must be added to the user application:
A sample buffer to write from, a reversed buffer to write from and length of buffers.
#define DATA_LENGTH 8
static uint8_t wr_buffer[DATA_LENGTH] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
};
static uint8_t wr_buffer_reversed[DATA_LENGTH] = {
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
};
static uint8_t rd_buffer[DATA_LENGTH];
Address of slave:
#define SLAVE_ADDRESS 0x12
Globally accessible module structure:
Globally accessible packet:
Function for setting up module:
void configure_i2c(void)
{
config_i2c_master.buffer_timeout = 65535;
#if SAMR30
config_i2c_master.pinmux_pad0 = CONF_MASTER_SDA_PINMUX;
config_i2c_master.pinmux_pad1 = CONF_MASTER_SCK_PINMUX;
#endif
while(
i2c_master_init(&i2c_master_instance, CONF_I2C_MASTER_MODULE, &config_i2c_master) \
}
Callback function for write complete:
void i2c_write_complete_callback(
{
}
Function for setting up the callback functionality of the driver:
void configure_i2c_callbacks(void)
{
}
Add to user application main()
:
configure_i2c();
configure_i2c_callbacks();
Workflow
- Configure and enable module.
- Create and initialize configuration structure.
- Change settings in the configuration.
config_i2c_master.buffer_timeout = 65535;
#if SAMR30
config_i2c_master.pinmux_pad0 = CONF_MASTER_SDA_PINMUX;
config_i2c_master.pinmux_pad1 = CONF_MASTER_SCK_PINMUX;
#endif
- Initialize the module with the set configurations.
while(
i2c_master_init(&i2c_master_instance, CONF_I2C_MASTER_MODULE, &config_i2c_master) \
- Enable the module.
- Configure callback functionality.
configure_i2c_callbacks();
- Register write complete callback.
- Enable write complete callback.
- Create a packet to send to slave.
wr_packet.address = SLAVE_ADDRESS;
wr_packet.data_length = DATA_LENGTH;
wr_packet.data = wr_buffer;
Implementation
Code
Add to user application main()
:
while (true) {
}
if (wr_packet.data[0] == 0x00) {
wr_packet.data = &wr_buffer_reversed[0];
} else {
wr_packet.data = &wr_buffer[0];
}
}
}
Workflow
- Write packet to slave.
wr_packet.address = SLAVE_ADDRESS;
wr_packet.data_length = DATA_LENGTH;
wr_packet.data = wr_buffer;
- Infinite while loop, while waiting for interaction with slave.
while (true) {
}
if (wr_packet.data[0] == 0x00) {
wr_packet.data = &wr_buffer_reversed[0];
} else {
wr_packet.data = &wr_buffer[0];
}
}
}
Callback
Each time a packet is sent, the callback function will be called.
Workflow
- Write complete callback:
- Send every other packet in reversed order.
if (wr_packet.data[0] == 0x00) {
wr_packet.data = &wr_buffer_reversed[0];
} else {
wr_packet.data = &wr_buffer[0];
}
- Write new packet to slave.
wr_packet.address = SLAVE_ADDRESS;
wr_packet.data_length = DATA_LENGTH;
wr_packet.data = wr_buffer;