I2C Master Driver
The driver only handles when the device is used as an I2C master, because it’s unlikely the MCU is going to used as an I2C slave on the rocket. Functions for reading 8 and 16 bit registers are provided.
Assumptions in this revision
I2C Address(exclude R/W bits) are 7 bits
Only one I2C controller could be used
Register address are 8 bit wide.
I2C Controller Functions
-
void i2c_init(uint8_t freq_div)
Initialize I2C module and set up pins
- Parameters
freq_div (uint8_t) – frequency divider, I2C Frequency = 100 kHz / freq_div
-
bool i2c_write_data(uint8_t i2c_addr, const uint8_t *data, uint8_t len)
Send data through I2C, this is usually used for sending register address and write data.
- Parameters
i2c_addr (uint8_t) – I2C peripheral address, where addr[7:1] is the 7-bit address, and addr[0] is the RW bit
data (const uint8_t*) – data to be transmitted
len (uint8_t) – length of data to be transmitted in bytes, the I2C address byte is not included
- Returns
success or not
- Return values
true – success
false – failed
-
bool i2c_read_data(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *data, uint8_t len)
Receive data through I2C, this is usually used for receiving read data.
- Parameters
i2c_addr (uint8_t) – I2C peripheral address, where addr[7:1] is the 7-bit address, and addr[0] is the RW bit
reg_addr (uint8_t) – device register map address, max 8 bits wide
data (uint8_t*) – buffer for received data
len (uint8_t) – length of data to be received in bytes
- Returns
success or not
- Return values
true – success
false – failed
-
bool i2c_read_reg8(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *value)
Read a byte-wide device register
- Parameters
i2c_addr (uint8_t) – I2C peripheral address, where addr[7:1] is the 7-bit address, and addr[0] is the RW bit
reg_addr (uint8_t) – device register map address, max 8 bits wide
value (uint8_t*) – pointer to register value buffer
- Returns
success or not
- Return values
true – success
false – failed
-
bool i2c_read_reg16(uint8_t i2c_addr, uint8_t reg_addr, uint16_t *value)
Read a 2-byte device register
- Parameters
i2c_addr (uint8_t) – I2C peripheral address, where addr[7:1] is the 7-bit address, and addr[0] is the RW bit
reg_addr (uint8_t) – device register map address, max 8 bits wide
value (uint16_t*) – pointer to register value buffer
- Returns
success or not
- Return values
true – success
false – failed
-
bool i2c_write_reg8(uint8_t i2c_addr, uint8_t reg_addr, uint8_t value)
Write a byte-wide device register
- Parameters
i2c_addr (uint8_t) – I2C peripheral address, where addr[7:1] is the 7-bit address, and addr[0] is the RW bit
reg_addr (uint8_t) – device register map address, max 8 bits wide
value (uint8_t) – value to be written to the register
- Returns
success or not
- Return values
true – success
false – failed
-
bool i2c_write_reg16(uint8_t i2c_addr, uint8_t reg_addr, uint16_t value)
Write a 2-byte device register
- Parameters
i2c_addr (uint8_t) – I2C peripheral address, where addr[7:1] is the 7-bit address, and addr[0] is the RW bit
reg_addr (uint8_t) – device register map address, max 8 bits wide
value (uint16_t) – value to be written to the register
- Returns
success or not
- Return values
true – success
false – failed