What is I2C?
A protocol is a set of rules shared between two or more devices in order to transmit information without a protocol is impossible to have proper communication.
The number of devices on one I2C bus usually depends on the capacitance introduced by the various devices on the line, the limit is 400pF.
protocol definitions
The main actors are two of the communication, the master and slave. We will never have two masters on the same line, except in special cases and in general the MASTER is a microcontroller, other devices are slaves. The master manages the timing and the transmission begins, slave master handles requests.
Ogni dispositivo sul bus ha un indirizzo univoco a 7 o 10 bit.
Il master trasmette sulla linea data l'indirizzo dello slave sia che voglia ricevere dati, sia che li voglia trasmettere . Dopo il bit di acknowledge il master procede a trasmettere i dati, oppure li trasmette lo slave se il master lo richiede. L'Ack viene trasmesso dopo ogni byte [2] .
In figura vediamo un esempio di comunicazione, è importante osservare come la comunicazione inizi con una condizione di Start (o repeated START) and ends with a STOP condition.
After transferring the data with the second byte, if we want to send or receive again, instead of stopping transmission by the STOP condition, we repeat the START condition [3 ] . In this way, the Master never fails to transmit on the bus and no other potential masters can take control. The Repeated Start condition where we have only one master has the same function as the STOP condition.
In this photo you can see how it is allocated to the SDA bus to communicate between master and slave.
In an I2C communication then we will have three possible types of transmissions:
The master wants to transmit data to the slave
-
The master transmits the slave address to which it wants to transmit on SDA
-
The master sends the data you want to convey
-
The master terminates the communication
The but ster wants to receive data from the slave
-
Il master trasmette l'indirizzo dello slave da cui vuole ricevere dati su SDA
-
Lo slave trasmette i dati al master
-
Il master termina il trasferimento
In questo caso il master genera l'ACK dopo aver ricevuto i dati, ma per indicare allo slave che non ha più bisogno di dati genera un NOT ACKNOLEDGE.
Communication mixed (every part of this communication is broken down into one of the two previous cases)
In cases above we can see that the communication from master to slave managed entirely by the master and depending on how we place the address bits can read or write data in the slave.
Peripheral addressing
The 7-bit addressing procedure specifies that the first byte transmitted on the I2C line, after the START condition, slave address will be wanted to select. There is one exception called 'general call' in which the master transmits a generic address each slave sends an ACK. However, the slaves may also have been configured to ignore the 'general call'.
The second byte will be transmitted on the line that will contain important information for defining the communication, then actions to be made.
Slave address can be fixed or variable, all this will depend on the manufacturer. There are some I2C devices that allow the designer the freedom to choose (via hardware) address of the device. In fact, there are the pins, which typically represent the least significant bits of the address, which if put to ground or Vcc change their bit to 0 or 1. A practical example is the PCA9555 which has 3 pins that represent the I2C address bits and the 4 most significant bits are set to 0100 default by the parent. Then we address 2 3 PCA9555 sullo stesso bus.
In questa tabella vediamo vari tipi di indirizzi che vengono inviati sul bus per diversi scopi.
E' necessario dire che utilizzeremo soltanto lo Start byte e al limite il 10-bit slave addressing nei nostri progetti a microcontrollori, però per completezza inserisco informazioni riguardanti anche gli altri metodi di indirizzamento.
1. General call, you need to address all devices on the line able to handle it.
If a device receives a general call and is able to manage it sends an ACK and will act as a slave-receiver. The second and subsequent bytes on the line will be recognized by any device that generates an ack. If a device will not be able to handle this type of data then it will ignore the general call.
The general sense of call you can tell from the second byte sent, especially the bit B.
Then two cases are possible
- if bit B = 0 then the second byte will have 3 different forms:
- 00000110 (H'06 '): Resets and enables the devices to receive address on programmable hardware.
- 00000100 (H'04 ') : Enable the devices to receive the address via programmable hardware but does not reset the devices.
- 00000000 (H'00 '): This code can not be used as the second byte.
The various procedures for programming with sequences of data to send are published on the various I2C device datasheet.
- If bit B = 1 then the general call will be a 'hardware general call'. This means that the sequence of two bytes is transmitted by a hardware device such as a 'keyboard scanner' which can not be scheduled to address a single slave. Since these hardware devices do not know at the beginning of a communication device which will transmit the second byte contains its address to identify itself. Un dispositivo intelligente come un microcontrollore, che può comportarsi sia da master che da slave, quindi reindirizzerà il traffico da questa periferica allo slave desiderato.
2. (1) Dopo il bit di start nessuna periferica è abilitata a dare l'ack.
3. (2) Questo tipo di indirizzo serve a rendere compatibile un bus di tipo C-BUS con il Bus I2C.
C-Bus devices can be connected to an I2C bus in standard mode, but will need a third wire called DLEN and ack will be omitted. In this type of structure the devices and I2C-Bus C will share a 2-wire bus, but the I2C data not respond to C-Bus and vice versa.
4. (3) Restricted to other compatible with the I2C bus
5.Riservato for future use
6.Riservato communications in Hs-mode I2C which is one of three modes of I2C communication, in this case the bit rate will 3.4MBits / s . The other two modes are Fast-mode (400 kbits / s) and 10-bit addressing.
7.Riservato for future use
8. Reserved for communication between devices that have an address of 10 bits. To address a 10-bit device, simply send in two different 10-bit byte address. Devices directed to 7bit and 10 bits can be connected to the same bus and can communicate both in HS-mode and Fast-mode.
In the first byte sent after the Start condition or Sr (repeated start) is sent to 11,110 and the 2 most significant bits of the address to 10 bits, the bits R / W is set to zero and the second byte will the 8 bits of the address.
Communication with devices is explained in a 10bit this figure
CONCLUSIONI
Il protocollo I2C visto così, può sembrare molto complesso, ma se avrete la pazienza di leggere il tutorial sulla comunicazione i2c e arduino vi accorgerete che bastano poche funzioni per riuscire a indirizzare e trasferire dati a questo tipo di periferiche.
Io personalmente trovo questo protocollo utilissimo, pensate che con soltanto un bus di 2 wires you can expand your port I / O of 320 bits well. In addition there are sensors, i2c memory display and you can use with only 2 wires. For more i2c bus hub are also produced by NXP (most of the i2c devices are produced by NXP, a company that produces semiconductors by Philips).
NOTES
[1] Pull Up: If a line between the top and value (determined by the type of logic we use, eg in the TTL value of 1 or High or H is equal to 5v per definizione), mettiamo una resistenza questa prenderà il nome di resistenza di pull up. Come dice il nome tiene a livello H in tensione la linea, allo stesso tempo il valore alto è debole quanto basta da permettere la commutazione al livello basso senza far circolare grandi quantità di corrente che andrebbe sprecata.
[2] Un byte sono 8 bit. 1 bit è l'elemento fondamentale e può valere 1 o 0.
[3] La ricezione di una condizione di START o di Repeated START, comporta il reset di ogni periferica sul bus. This thing must be very careful in the planning stage of the Master.
BIBLIOGRAPHY
1. The I 2C-BUS SPECIFICATION VERSION 1.2 JANUARY 2000 by Philips Semiconductors
2. Simulation of communication between devices using the I2C protocol by Fabio Piccolo
3. Wikipedia