I2C Registers

I2C Register API

The following structures and functions are used to read and write I2C registers.

enum i2c_regop_res_t

This type is used by the supplementary I2C register read/write functions to report back on whether the operation was a success or not.

Values:

enumerator I2C_REGOP_SUCCESS

The operation was successful.

enumerator I2C_REGOP_DEVICE_NACK

The operation was NACKed when sending the device address, so either the device is missing or busy.

enumerator I2C_REGOP_INCOMPLETE

The operation was NACKed halfway through by the slave.

inline uint8_t read_reg(i2c_master_t *ctx, uint8_t device_addr, uint8_t reg, i2c_regop_res_t *result)

Read an 8-bit register on a slave device.

This function reads from an 8-bit addressed, 8-bit register in an I2C device. The function reads the data by sending the register address followed reading the register data from the device at the specified device address.

Note

No stop bit is transmitted between the write and the read. The operation is performed as one transaction using a repeated start.

Parameters
  • ctx – A pointer to the I2C master context to use.

  • device_addr – The address of the device to read from.

  • reg – The address of the register to read from.

  • result – Indicates whether the read completed successfully. Will be set to I2C_REGOP_DEVICE_NACK if the slave NACKed, and I2C_REGOP_SUCCESS on successful completion of the read.

Returns

The value of the register.

inline uint8_t read_reg8_addr16(i2c_master_t *ctx, uint8_t device_addr, uint16_t reg, i2c_regop_res_t *result)

Read an 8-bit register on a slave device.

This function reads from an 16-bit addressed, 8-bit register in an I2C device. The function reads the data by sending the register address followed reading the register data from the device at the specified device address.

Note

No stop bit is transmitted between the write and the read. The operation is performed as one transaction using a repeated start.

Parameters
  • ctx – A pointer to the I2C master context to use.

  • device_addr – The address of the device to read from.

  • reg – The address of the register to read from.

  • result – Indicates whether the read completed successfully. Will be set to I2C_REGOP_DEVICE_NACK if the slave NACKed, and I2C_REGOP_SUCCESS on successful completion of the read.

Returns

The value of the register.

inline uint16_t read_reg16_addr8(i2c_master_t *ctx, uint8_t device_addr, uint8_t reg, i2c_regop_res_t *result)

Read an 16-bit register on a slave device.

This function reads from an 8-bit addressed, 16-bit register in an I2C device. The function reads the data by sending the register address followed reading the register data from the device at the specified device address.

Note

No stop bit is transmitted between the write and the read. The operation is performed as one transaction using a repeated start.

Parameters
  • ctx – A pointer to the I2C master context to use.

  • device_addr – The address of the device to read from.

  • reg – The address of the register to read from.

  • result – Indicates whether the read completed successfully. Will be set to I2C_REGOP_DEVICE_NACK if the slave NACKed, and I2C_REGOP_SUCCESS on successful completion of the read.

Returns

The value of the register.

inline uint16_t read_reg16(i2c_master_t *ctx, uint8_t device_addr, uint16_t reg, i2c_regop_res_t *result)

Read an 16-bit register on a slave device.

This function reads from an 16-bit addressed, 16-bit register in an I2C device. The function reads the data by sending the register address followed reading the register data from the device at the specified device address.

Note

No stop bit is transmitted between the write and the read. The operation is performed as one transaction using a repeated start.

Parameters
  • ctx – A pointer to the I2C master context to use.

  • device_addr – The address of the device to read from.

  • reg – The address of the register to read from.

  • result – Indicates whether the read completed successfully. Will be set to I2C_REGOP_DEVICE_NACK if the slave NACKed, and I2C_REGOP_SUCCESS on successful completion of the read.

Returns

The value of the register.

inline i2c_regop_res_t write_reg(i2c_master_t *ctx, uint8_t device_addr, uint8_t reg, uint8_t data)

Write to an 8-bit register on an I2C device.

This function writes to an 8-bit addressed, 8-bit register in an I2C device. The function writes the data by sending the register address followed by the register data to the device at the specified device address.

Parameters
  • ctx – A pointer to the I2C master context to use.

  • device_addr – The address of the device to write to.

  • reg – The address of the register to write to.

  • data – The 8-bit value to write.

Returns

I2C_REGOP_DEVICE_NACK if the address is NACKed.

Returns

I2C_REGOP_INCOMPLETE if not all data was ACKed.

Returns

I2C_REGOP_SUCCESS on successful completion of the write.

inline i2c_regop_res_t write_reg8_addr16(i2c_master_t *ctx, uint8_t device_addr, uint16_t reg, uint8_t data)

Write to an 8-bit register on an I2C device.

This function writes to a 16-bit addressed, 8-bit register in an I2C device. The function writes the data by sending the register address followed by the register data to the device at the specified device address.

Parameters
  • ctx – A pointer to the I2C master context to use.

  • device_addr – The address of the device to write to.

  • reg – The address of the register to write to.

  • data – The 8-bit value to write.

Returns

I2C_REGOP_DEVICE_NACK if the address is NACKed.

Returns

I2C_REGOP_INCOMPLETE if not all data was ACKed.

Returns

I2C_REGOP_SUCCESS on successful completion of the write.

inline i2c_regop_res_t write_reg16_addr8(i2c_master_t *ctx, uint8_t device_addr, uint8_t reg, uint16_t data)

Write to a 16-bit register on an I2C device.

This function writes to an 8-bit addressed, 16-bit register in an I2C device. The function writes the data by sending the register address followed by the register data to the device at the specified device address.

Parameters
  • ctx – A pointer to the I2C master context to use.

  • device_addr – The address of the device to write to.

  • reg – The address of the register to write to.

  • data – The 16-bit value to write.

Returns

I2C_REGOP_DEVICE_NACK if the address is NACKed.

Returns

I2C_REGOP_INCOMPLETE if not all data was ACKed.

Returns

I2C_REGOP_SUCCESS on successful completion of the write.

inline i2c_regop_res_t write_reg16(i2c_master_t *ctx, uint8_t device_addr, uint16_t reg, uint16_t data)

Write to a 16-bit register on an I2C device.

This function writes to a 16-bit addressed, 16-bit register in an I2C device. The function writes the data by sending the register address followed by the register data to the device at the specified device address.

Parameters
  • ctx – A pointer to the I2C master context to use.

  • device_addr – The address of the device to write to.

  • reg – The address of the register to write to.

  • data – The 16-bit value to write.

Returns

I2C_REGOP_DEVICE_NACK if the address is NACKed.

Returns

I2C_REGOP_INCOMPLETE if not all data was ACKed.

Returns

I2C_REGOP_SUCCESS on successful completion of the write.