STM32 I2C Lecture 13: Clock Stretching

  • Post author:
  • Post category:Blog

 

Clock Stretching

 

 

Clock stretching is one of the most powerful features of the I2C protocol. Clock stretching means that holding the clock to 0 or ground level. The moment clock is kept low, then the whole I2C interface pauses until the clock is given up to its normal operating level.

 

Why is clock stretching used?

  • I2C devices, either master or slave, use this feature to slow down the communication by stretching SCL (or clock) to low, which prevents the clock from rising high again, and the I2C communication stops for a while.
  • In I2C communication, the master device determines the clock speed. However, there are situations where an I2C slave cannot cooperate with the clock speed given by the master and needs to slow down a little. If the slave needs time, it takes advantage of clock stretching by holding the clock at low (ground level or 0) and momentarily pauses the I2C operation.

Take an example shown in Figure 1. In the first figure, it’s a no clock stretched scenario where the slave is able to cope up with the master. First, the slave gets the data from the master up to the 8th clock cycle, and it sends back the ACK in the 9th clock cycle, as shown in Figure 2.

 

Figure 1. Example for no clock stretching and clock stretching.

 

clock stretching
Figure 2. Timing diagram for no clock stretching example.

 

But take a look at the second figure shown in Figure 1. Here master has sent a byte of data till the 8th clock cycle. Since the slave is busy doing something else, it does not let the clock rise high again after the 8th clock cycle. Instead, it stretched the clock to low (Figure 3).

If the slave does not stretch the clock, then the 9th clock would have triggered after the 8th clock cycle, and the master samples the SDA line and finds that it’s a NACK because the SDA is held high.

 

clock stretching
Figure 3. Timing diagram for clock stretching example.

 

So, clock stretching is the slave’s way of telling the master that it is busy to send the ACK. Please hold down.

Then the slave releases the clock and sends out the ACK at the 9th clock cycle, as shown in Figure 3. Between the 8th and 9th clock cycles I2C interface is paused momentarily due to clock stretching. Remember, there is no need to control the clock stretching and un-stretching by your code since it is done by the I2C hardware automatically. You only need to enable the clock stretching features.

 

In the next article, let’s see implementation of I2C init API

 

FastBit Embedded Brain Academy Courses available in Udemy

Click here: https://fastbitlab.com/course1

 

 

 

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.