This tutorial give you the details of synchronous serial communication, which is the basis of data transfer in communication standards like SPI and I2C. The tutorial is aimed for a beginner who has no experience or any idea of serial communication. So intermediate user who already has some idea about serial data transfer can skip this tutorial and jump directly to SPI or I2C tutorials.






Data Transfer.

Knowledge of data transfer is very important for any embedded system developer. In any embed ed system data is moved between several units like between RAM and CPU. There are many methods and technique for data transfer each having its own pros and cons. So different data transfer technique is used in different situations. Some example of data transfer are

  • Simple parallel transfer. (Used to transfer 8,16,32 ... bits of data in the same time)
  • Asynchronous Serial Transfer (USART) - Old but still in use mode of serial communication using only 2 lines (+1 additional line for GND).
  • SPI - Serial Peripheral Interface - Standard Mode of communication between different ICs. Many ICs designed for a Variety of Jobs like Flash Memory, LCD controller, Ethernet Controllers etc uses this standard for communication. So if you want to use any of these functionality you need to have a knowledge of SPI.
  • IIC(or I2C) - Inter IC communication : Developed by Philips is also used by a variety of ICs like EEPROM,Temperature Sensor, Real Time Clock Chips etc.
  • USB: Who doesn't know USB ? - A very advance, high speed and thus complicated serial Bus used in PCs to connect almost anything to it!
  • Fire Wire
  • Ethernet
  • many more ...

Mode Data Transfer can be broadly divided into to types

  • Parallel Transfer - In this mode a number of bits (say 8,16 or 32) are transferred at a time. Thus they require as many electrical line as the number of bits to be transferred at once. This method is fast but disadvantage is more number of lines. So they are basically used when the units involved in data transfer are physically close and almost fixed with each other for a long time. Ex CPU and RAM, The PCI Cards inside your PC. These are close to each other and packet inside the CPU box and are disconnected from each other less frequently.
  • Serial Transfer - In this mode only one bit is transfer at once. So to transfer 8bits, 8 cycles are required. So these requires less number of physical lines (actual number of lines depends on technology used like SPI use 3 lines while I2C uses 2 lines, there are some technology that uses just a single wire !). The advantage is that due to less number of like IC using these technologies are small with low PIN count.

In this tutorial we will just focus on synchronous serial communication that is the basis of I2C and SPI.

Serial Communication

In serial communication basically there are two fundamental electrical lines (and one for GND), one is called CLOCK and Other DATA. The actual data is transferred BIT by BIT on the DATA line. The clock line signals that when a BIT ends and Other BIT starts. How clock line does it depends one the standard used. The clock line changes its level, that is when it goes HIGH from a LOW level or vice versa to signal this. Lets take the first case, that is the CLOCK line goes HIGH to signal a new bit is available. In this case the algorithm to TRANSMIT 8 bit will be as follows

  1. Initialize CLOCK and DATA lines to 0
  2. Check the 0th bit
  3. If it is 1 then make data line HIGH else make it LOW.
  4. Wait_1
  5. Make CLOCK line HIGH to signal new bit is on the DATA Line.
  6. Wait._2
  7. Make CLOCK line LOW again.
  8. Check the 1st bit
  9. If it is 1 then make data line HIGH else make it LOW.
  10. Wait_1
  11. Make CLOCK line HIGH to signal new bit is on the DATA Line.
  12. Wait_2
  13. ...
  14. REPEAT TILL ALL 8 BITS ARE TRANSFERED
  15. ...
  16. END

Here we are transmitting the 0th bit first (LSB first) but some standard may use MSB First (8th bit then 7th bit ... and so on). Also a HIGH on data LINE is signaling new bit but again this depends upon standard. The timing diagram of above steps is given below. The animation will make things clear for you.

i2c spi basics tutorial

Timing Diagram of Serial Data Transfer. Example Data = 10010111

The "other" device which is receiving the data follows the steps given below to receive the data.

  1. Wait till the CLOCK line is LOW.
  2. When CLOCK line becomes HIGH read the DATA line, this gives the 0th bit.
  3. Wait till the CLOCK line remains high.
  4. Go to step 1, repeat until we have got all 8bits.
  5. Merge all 8bits to get the data.
  6. END

Master And Slave

These are two terms often used with serial communication. The MASTER is the device which controls the CLOCK. So the First example given above is of Master Transmitter while the Second is of Slave Receiver.

Now you should have got the basic over view of serial communication. These will be helpful when we move to I2C and SPI tutorials. If you have any doubts please post a comment or better post a new topic in the forum . Your feedbacks can help me a lot in improving the tutorials.