In I2C communication, each device takes a designated role: it is either a master or a slave. These terms refer to the one who dictates the speed of the clock, i.e. who actually generates it. Unlike an RS232 scenario, the bit rate is not fixed to a constant value. Instead, the master device pulses the clock for each transmitted bit, and the slave has to follow this speed.
Clock stretching
Since a slave may need more time to process the data, it is possible for a slave device to hold down the clock line until it is ready to continue communication. This mechanism is referred to as clock stretching and assures that a slave device has the ability to protect itself from being addressed too quickly.
Why a slave is harder to implement
While it is almost trivial to implement an I2C single master on a microcontroller, the same is barely possible in many cases for a slave. A master generates the clock and drives the communication whenever it is ready to do so — it is in control of the timing. In contrast, a slave device has to always be ready to detect and process a start condition and to recognize its own address, which can arrive at any moment, see addressing.
This is relatively easy to achieve with a hardware state machine and extremely difficult with a microcontroller, especially if it is busy with other tasks, too. The challenge is to assure that at any point in time, and regardless of the application state, the microcontroller is ready to process a master request at the maximum clock speed allowed for the communication.
Communication example
Let us assume we want to build a slave device which returns the square of a given number. It listens to the I2C address 0x56, receives a byte as input and returns the square value of this byte (ignoring any carry) in response to a subsequent read operation.
Now the master puts 0x56 on the bus in write mode and sends a 7 afterwards.
The slave interface recognizes that it has been addressed and reads in the 7. Now it needs to do the processing, and in order to calculate the 49 it has to contact the PC — the I2C engine itself does not compute anything.
Depending on the connection used, this takes anything between 80 µs and several milliseconds. The interface could stretch the clock, but unfortunately many master-mode implementations do not support clock stretching at all. On top of that, many masters have a timeout for maximum clock stretching somewhere around 1 ms, which comes quite close to the delay caused by the PC communication.
The lesson is simple: a slave must be able to react quickly, and long processing times inside the application are a common source of trouble on the bus. Where such delays cannot be avoided, clock stretching — supported by both sides — is the proper way out. If a slave instead gets stuck while holding the data line low, the result is a blocked bus.