Search code examples
ci2cbitmask

How to format I2C data from device data sheet?


I recently started a project with a Nordic NRF52832 DK. To this board I have connected one DRV2605 linear resonant actuator driver. Before I moved to the Nordic board, I was prototyping on an Arduino with a provided library for the DRV2605, so that was pretty simple.

Now, I am attempting to initialize and control the DRV2605 on my own by writing to the specified registers from the device setup guide.

Section 1.6.2 is what I have been looking at. Let's say I want to write to the feedback control register. I know that the address is 0x1A and that I need to write a value that corresponds to the four listed settings. What I am stuck on is how to actually create the data I need to write. The table has a column for what I assume is the range of bits I'll be modifying for each setting?

From looking at the chart (using the default settings) I'd assume the data I would need to write would be 13331122. If I plug that value into a dec to hex converter I get CB6AB2. Does "B6" portion of that value correlate to the "Value (Hex)" column from the chart or is it coincidence?

Here's the code I would use to write to the FC reg:

#define DRV_ADDR 0x5A
uint8_t fc_reg[2] = {0x1A, 13331122};
nrf_drv_twi_tx(&m_twi, DRV_ADDR, fc_reg, sizeof(fc_reg), false);

From doing some research it seems bit masking might be what I'm missing? This still doesn't really explain the value mismatch from the chart.

I'd really appreciate any help I can get on this, thanks!


Solution

  • Based on the provided screenshot you can see that the Feedback Control register has an address 0x1A, more so, that register holds 1 byte of information. It effectively serves as a bitflag where each bit represents something different. For example only bit 7 represents LRA. So, if you were to write 0x80(DEC 128) into that register, it would turn on LRA and if you wanted to enable/configure something else, it would just be a bitwise OR of the 0x80. You have the right idea for the frames your constructing, however, it should instead look something like this for LRA.

    uint8_t fc_reg[2] = {0x1A, 0x80};

    Obviously, replace the 0x80 with whatever flags you would like to set.