I made a python program that reads the I2C bus and I've encountered a problem that arises when using smbus.read_byte repeatedly. It uses the smbus library. I've found that the clock HIGH signal on the first data bit of the third byte (sensb_val_low) is too short, causing the bit to always be read as 0. This happens at seemingly random times, with ~50/50 odds, but always on the third read byte. I've pasted the lines of code that are causing trouble below. I have also posted a scope shot of the issue.
Code: Select all
import smbus
bus.write_byte(addr, cmd_measure_data_prep)
sensa_val_low = int(bus.read_byte(addr))
sensa_val_hi = int(bus.read_byte(addr))
sensb_val_low = int(bus.read_byte(addr))
sensb_val_hi = int(bus.read_byte(addr))

- 3388-3_CAN2_INFTERM--00000.png (41.71 KiB) Viewed 812 times
Example of a 'bad read.' The Pi would read this as 0x77 instead of 0xf7 as the scope has decoded it.
I have tried to work around this issue to no avail. Even placing a dummy byte in the third place doesn't work as I've noticed the fourth byte is affected as well, but not as much. Any help is appreciated.