Wire.stretchClock and default behavior

Hi all!
The docs state clearly that the default behavior of Wire.stretchClock will enable clock stretching: https://docs.particle.io/reference/device-os/firmware/photon/#stretchclock-

So - what if I never call Wire.stretchClock(true) or Wire.stretchClock(false). Is the first executed by default? So that Clock stretching is supported by default? Or do I have to explicitly call this to enable clock stretching?

I believe this is how it works:

I2C clock stretching is done by the I2C slave device, which is typically not the Particle device. SCL is normally driven by the master device, but like SDA, is open collector with a pull-up.

If the I2C slave device wants to use clock-stretching, it keeps SCL held low by using its open-collector output.

When the Particle device is the master, it always obeys clock stretching. It can detect it because when it stops driving SCL low, it stays low. Normally the pull-up would make it go high again. It will basically halt operations until the I2C slave releases SCL and it goes high again.

The Wire.stretchClock() call is only used when you use the Particle device as the I2C slave. That’s what controls whether the SCL is held low by the I2C slave device.

Also the part about calling it before begin() is only partially true. In order to be set during the initialization of I2C, it does need to be called before. However, once running it calls I2C_StretchClockCmd() which is a STM32 standard library call. It can be made any time, which is important, because otherwise there would no way to be able to stop stretching the clock, and no data would be transferred.

1 Like

Thanks! Answered all my questions :slight_smile: