Close

Clock Stretching (Sigh)

A project log for PiDP-11 I/O Expander

16-Bit TTL Compatible I/O Expander for the PiDP-11 and Raspberry Pi

steven-stallionSteven Stallion 08/31/2021 at 02:350 Comments

Unfortunately it looks like the MCP23016 is no exception when it comes to clock stretching on the Raspberry Pi. For those that may not be familiar, clock stretching is a method for throttling an I2C master that is communicating too quickly by having the slave temporarily hold SCL low. Under normal circumstances, clock stretching goes unnoticed unless examined with a scope or logic analyzer, however the SoCs that power the Raspberry Pi have a long-standing bug that breaks this behavior.

The good news is there is a simple workaround to address the issue, though it requires reducing the bit rate for the entire bus to eliminate the need for clock stretching. The bad news is this is not enabled by default.

To reduce the bit rate to 10kHz, update the following line in /boot/config.txt and reboot:

dtparam=i2c_arm=on,i2c_arm_baudrate=10000

 After rebooting, you can verify proper communication by issuing:

$ i2cdump -y -r 0x00-0x0b 1 0x20 b
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 00 00 00 10 00 00 ff ff 01 00 00 00                ...?....?...

It's a shame this is still an issue on modern Raspberry Pis. The Raspberry Pi 4 added support for clock stretching via software I2C, but it looks like this limitation will continue to persist in hardware for the foreseeable future.

Discussions