In this use case, the I2C will used and set up as follows:
- Master mode
- 100KHz operation speed
- Not operational in standby
- 10000 packet timeout value
- 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 send, a sample buffer to read:
#define DATA_LENGTH 10
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
};
- Slave address to access:
#define SLAVE_ADDRESS 0x12
- Number of times to try to send packet if it fails:
- Globally accessible module structure:
- Function for setting up the module:
{
config_i2c_master.buffer_timeout = 10000;
#if SAMR30
config_i2c_master.pinmux_pad0 = CONF_MASTER_SDA_PINMUX;
config_i2c_master.pinmux_pad1 = CONF_MASTER_SCK_PINMUX;
#endif
}
- Add to user application
main()
:
uint16_t timeout = 0;
.ten_bit_address = false,
.high_speed = false,
.hs_master_code = 0x0,
};
Workflow
- Configure and enable module.
{
config_i2c_master.buffer_timeout = 10000;
#if SAMR30
config_i2c_master.pinmux_pad0 = CONF_MASTER_SDA_PINMUX;
config_i2c_master.pinmux_pad1 = CONF_MASTER_SCK_PINMUX;
#endif
}
- Create and initialize configuration structure.
- Change settings in the configuration.
config_i2c_master.buffer_timeout = 10000;
#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.
- Enable the module.
- Create a variable to see when we should stop trying to send packet.
- Create a packet to send.
.ten_bit_address = false,
.high_speed = false,
.hs_master_code = 0x0,
};
Implementation
Code
Add to user application main()
:
Workflow
- Write packet to slave. The module will try to send the packet TIMEOUT number of times or until it is successfully sent.
- Read packet from slave. The module will try to read the packet TIMEOUT number of times or until it is successfully read.