Multi byte read over I2C

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
b_div
Posts: 2
Joined: Sun Sep 25, 2016 1:00 pm

Multi byte read over I2C

Post by b_div » Sun Sep 25, 2016 1:31 pm

Hello everyone,

I'm working on a project using the PYBv1.0 with STM32F405RG Firmware v1.8 on 2016-05-03. Basically I connect a bunch of IMUs to the I2C-Bus, read their values and forward them via bluetooth to the platform used for analyzing the data.

During a development phase a few month ago I discovered some weird bug in I2C.mem_read. Using the first parameter to initialize a buffer of size X and reading register address 0xAA results in a buffer of size X, where each element has the value of register 0xAA and not as expected the values [0xAA, 0xAA+1, 0xAA+2].

Code:
>>> I2C.mem_read(1, 0x1d, 0x28)
b'\xa6'
>>> I2C.mem_read(3, 0x1d, 0x28)
b'\xa6\xa6\xa6'

Using a pre initialized buffer yields the same result:

Code:
>>> xyz = bytearray(3)
>>> I2C.mem_read(xyz, 0x1d, 0x28)
bytearray(b'\xa3\xa3\xa3')

Back then I had time constraints and just changed my program to read every entry separately, but now I want to do some optimization and reading all three values at once seemed like the better choice. I'm working my way through the firmware code and notice that it has been a while since I worked with lower level C.
Did someone experience the same problem and discovered a workaround already?
Does someone have the time to assist me with this a little bit, maybe patching it up in the process?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Multi byte read over I2C

Post by dhylands » Sun Sep 25, 2016 4:33 pm

It may depend on the configuration of the device itself.

Many devices support "auto-increment" and if auto-increment is enabled, then a read requesting multiple bytes will cause the address being read to increment after each read. If auto-increment is disabled, then multiple byte reads will read the same address over and over.

You didn't mention which devices you're using.

b_div
Posts: 2
Joined: Sun Sep 25, 2016 1:00 pm

Re: Multi byte read over I2C

Post by b_div » Sun Sep 25, 2016 5:18 pm

I'm using the MinIMU9-v2. After checking the datasheets I noticed that it is all written in there. I just didn't know what the auto-increment exactly meant and operated under false assumptions.
The register addresses are 7bit, the 8th bit is used to determine if auto-increment should be enabled.

Long story short, using 0xA8 instead of 0x28 enables the auto-increment, thus lets me do exactly what I need.

Thank you very much for your fast advice!

Post Reply