Data Structures for I2C Programmer#
There are three new data structures created for the I2C Programmer.
typedef struct {
uint16_t address;
uint16_t data;
uint32_t delayUsec;
} DevBlkCDII2CReg;
This structure defines an address/data pair for a register, with an optional delay value. Both address and data are defined as 16-bit data in this structure to accommodate the largest possible size for address and data values, but the actual size of these values used for I2C programming depends on how the I2C Programmer is initialized.
typedef struct {
const DevBlkCDII2CReg *regs;
uint32_t numRegs;
} DevBlkCDII2CRegList;
The second structure defines a register table. It includes a pointer to an array of address/data pairs regs, and a numRegs variable that is used to hold how many address/data pairs are in the register table. Obtain the value of numRegs by using the I2C_ARRAY_SIZE macro with the regs pointer.
typedef struct {
DevBlkCDII2CReg *regs;
uint32_t numRegs;
} DevBlkCDII2CRegListWritable;
The third structure is same as the second structure, except the array of address/data pairs regsis writable. It should be used when trying to read an array of address/data pairs from the device.
A typical register table looks like this:
static const DevBlkCDII2CReg mysensor_raw10_default_regs[] = {
//Disable Streaming
{0x301A, 0x1058, 10000}, // RESET_REGISTER, then delay 10ms
// Sensor gain
{0x3362, 0x0000},
{0x3363, 0x0060},
{0x3364, 0x7777},
....
};
static const DevBlkCDII2CRegList mysensor_raw10_default = {
.regs = mysensor_raw10_default_regs,
.numRegs = I2C_ARRAY_SIZE(mysensor_raw10_default_regs),
};
By using the new data structure for address/data pairs, you no longer need to manually construct the I2C payload data and calculate the payload buffer size. This greatly reduces the risk of user errors.