w52918
Posts: 3
Joined: Fri May 03, 2019 8:35 pm

Read a block of data from a specific register(fifo) using c/c++ and i2c in raspberry Pi

Fri May 03, 2019 8:45 pm

Hi,
I need to read 4 bytes of data from MAX30100 chip using c/c++ and I2C on raspberry Pi. I tried doing it with python. And it worked. But the problem was the data rate is too slow, I need to update the data at least 250 times/sec, which means the frequency of reading is >= 250Hz. So, i switched the reading from python to c/c++.

There is no problem when just read or write one byte with wiringPi. However, I need to read 4 bytes from fifo (address of the fifo is 0x04), wiringPi doesn't provide a function to do the block data reading. There are only read byte/word functions.

Then, I tried use SMBus to do the block reading, which can be found here:
https://github.com/leon-anavi/rpi-examp ... /c/smbus.c

But, as soon as I call the i2c_smbus_read_block_data(), my raspberry Pi freezes completely.

Anyone knows what's going on there? Or has a better solution?

Infraviolet
Posts: 24
Joined: Wed Jan 30, 2019 10:47 pm

Re: Read a block of data from a specific register(fifo) using c/c++ and i2c in raspberry Pi

Sun May 05, 2019 5:21 am

Try the bcm_2835 C library instead, it does have I2C functions for working with single registers of devices.

https://www.airspayce.com/mikem/bcm2835 ... a6eae2275a

User avatar
joan
Posts: 14839
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Read a block of data from a specific register(fifo) using c/c++ and i2c in raspberry Pi

Sun May 05, 2019 8:46 am

Don't use the I2C wrappers. Often it is simplest to use I2C directly from the device.

See http://abyz.me.uk/rpi/pigpio/code/adxl345_c.zip for a C example of reading multiple bytes from a register.

w52918
Posts: 3
Joined: Fri May 03, 2019 8:35 pm

Re: Read a block of data from a specific register(fifo) using c/c++ and i2c in raspberry Pi

Sun May 05, 2019 7:17 pm

joan wrote:
Sun May 05, 2019 8:46 am
Don't use the I2C wrappers. Often it is simplest to use I2C directly from the device.

See http://abyz.me.uk/rpi/pigpio/code/adxl345_c.zip for a C example of reading multiple bytes from a register.
It works, thanks a lot!

w52918
Posts: 3
Joined: Fri May 03, 2019 8:35 pm

Re: Read a block of data from a specific register(fifo) using c/c++ and i2c in raspberry Pi

Sun May 05, 2019 7:18 pm

Infraviolet wrote:
Sun May 05, 2019 5:21 am
Try the bcm_2835 C library instead, it does have I2C functions for working with single registers of devices.

https://www.airspayce.com/mikem/bcm2835 ... a6eae2275a
Thanks, I'll try it!

Return to “Interfacing (DSI, CSI, I2C, etc.)”