Posts: 15
Joined: Mon Aug 06, 2018 5:24 pm

[SOLVED] I2C clock error when repeatedly using read_byte

Fri Feb 22, 2019 1:40 pm

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.
3388-3_CAN2_INFTERM--00001.png (41.15 KiB) Viewed 806 times
Last edited by krisss on Wed Feb 27, 2019 6:00 pm, edited 1 time in total.

Posts: 15
Joined: Mon Aug 06, 2018 5:24 pm

Re: I2C clock error when repeatedly using read_byte

Wed Feb 27, 2019 5:59 pm

If anyone cares, I've found what seems to be the answer to what's wrong: ... c-bug.html
The Broadcomm processor has a glitchy I2C module that can't handle clock stretching apparently. I've implemented a workaround by splitting my data into nybbles and sending it 4 bits at a time.

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