Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read_byte_data() is very slow with a IST8308 sensor #76

Open
FSet89 opened this issue Sep 30, 2021 · 1 comment
Open

read_byte_data() is very slow with a IST8308 sensor #76

FSet89 opened this issue Sep 30, 2021 · 1 comment
Labels

Comments

@FSet89
Copy link

FSet89 commented Sep 30, 2021

I am reading data from a IST8308 IMU sensor with this script on a Linux Arm64 board:

import smbus2
i2c_bus = smbus2.SMBus(2)
i2c_magnetometer_register = 12

# initialize magnetometer
i2c_bus.write_byte_data(i2c_magnetometer_register, 32, 0) # action register
i2c_bus.write_byte_data(i2c_magnetometer_register, 50, 1) # control register 3
i2c_bus.write_byte_data(i2c_magnetometer_register, 49, 4) # control register 2
# read values
dataxl = i2c_bus.read_byte_data(i2c_magnetometer_register, 17)
dataxh = i2c_bus.read_byte_data(i2c_magnetometer_register, 18)
datax = uint_to_int((dataxh << 8) + dataxl)
# same for Y and Z

the read_byte_data() function is very slow (in particular, the builtin ioctl() call in read_byte_data()). Is it a sensor problem, or a smbus2 problem?

@kplindegaard
Copy link
Owner

This not an smbus2 problem. The root of the problem is your code.

Even though the Y and Z operations are skipped here, I presume the question is the same as this: https://electronics.stackexchange.com/questions/588767/slow-read-from-ist8308-via-i2c-using-python-and-smbus2

You perform no less than 6 read operations in consequtive addresses from address 17 through 22. Your device must respond to each one of these separately, and that takes time and resources for your sensor to process. Why not try to do all reads in one go instead of 6? Be kind to your sensor whenever possible :)

Why don't you try to follow Example #2 in the readme? In your case, that should be something like (not tested - I don't have this sensor):

# Read 6 bytes starting at index 17
buf = i2c_bus.read_i2c_block_data(i2c_magnetometer_register, 17, 6)  # data is now a list/array
# Convert to 16 bit word
datax = buf[1] << 8 + buf[0]

If this doesn't work, you should at least try to read 3 words instead of 6 bytes. Rules of thumb:

  1. Always do as few IO ops as possible.
  2. Check out the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants