This shows you the differences between two versions of the page.
pm:lab:lab0xc0-7 [2020/05/03 22:50] iuliana.brinzoi [Setup] |
pm:lab:lab0xc0-7 [2020/05/04 20:26] (current) iuliana.brinzoi [I2C (Inter-Integrated Circuit)] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Laboratorul 0xC6: I2C ====== | + | ===== Laboratory 0xC6: I2C ====== |
- | This lab covers the topic of I2C. For more in-depth knowledge about working with I2C you can consult the {{:pm:doc8272.pdf|ATmega324 datasheet}}. | + | This lab covers the topic of I2C. For more in-depth knowledge about working with I2C you can consult the {{: pm: doc8272.pdf | ATmega324 datasheet}}. |
===== I2C (Inter-Integrated Circuit) ===== | ===== I2C (Inter-Integrated Circuit) ===== | ||
- | Protocolul I2C (sau IIC - Inter-Integrated Circuit) este un protocol de comunicație serială sincron, multi-master - multi-slave, dezvoltat de către Phillips în anul 1982. O magistrală I2C este formată din următoarele semnale: | + | The I2C (or IIC - Inter-Integrated Circuit) protocol is a synchronous, multi-master - multi-slave serial communication protocol, developed by Phillips in 1982. An I2C bus consists of the following signals: |
- | * SDA - linia de date | + | * SDA - data line |
- | * SCL - semnalul de ceas | + | * SCL - clock signal |
- | Semnalul de ceas este generat de către master iar linia de date este controlată atât de master cât și de către slave. La un moment dat un singur dispozitiv de pe magistrală poate controla linia de date. Din acestă cauză protocolul I2C este half-duplex. | + | The clock signal is generated by the master and the data line is controlled by both the master and the slave. Only one device can control the data line at a time. Because of this, the I2C protocol is half-duplex. |
- | === Modul de functionare === | + | === How to operate === |
- | Față de SPI unde master-ul activa, prin intermediul semnalului de Slave Select, dispozitivul cu care dorea să comunice, I2C nu necesită un asemenea semnal adițional. Protocolul I2C introduce noțiunea de Slave Address. Adresa unui dispozitiv de tip slave este un număr pe 7 biți (cel mai comun), pe 8 biți sau 10 biți. Comunicația dintre un master și un slave se face prin mesaje și este tot timpul inițiată de către master. Aceste mesaje pot fi sparte în două tipuri de cadre: | + | Compared to the SPI where the master activates, via the Slave Select signal, the device with which he wanted to communicate, I2C does not require such an additional signal. The I2C protocol introduces the notion of Slave Address. The address of a slave device is a 7-bit (most common), 8-bit, or 10-bit number. Communication between a master and a slave is done through messages and is always initiated by the master. These messages can be broken into two types of frames: |
- | * un cadru de adresa | + | * an address frame |
- | * unul sau mai multe cadrea de date | + | * one or more data frames |
- | Aceste cadre sunt interschimbate numai după ce master-ul a trimis condiția de start. Sfârșitul unui mesaj este identificat prin condiția de stop. | + | These frames are exchanged only after the master has sent the start condition. The end of a message is identified by the stop condition. |
- | === Conditia de Start === | + | === Start condition === |
- | Înainte ca master-ul să trimită pe linia de date adresa slave-ului cu care dorește să comunice, acesta trebuie sa genereze o condiție de start. Condiția de start determină toate dispozitivele slave să ”asculte” linia de date pentru că va urma o adresă. Pentru a genera această condiție, master-ul lasă linia SCL in HIGH și pune linia SDA pe LOW. | + | Before the master sends to the data line the address of the slave with which it wants to communicate, it must generate a start condition. The start condition causes all slave devices to "listen" to the data line because an address will follow. To generate this condition, the master leaves the SCL line in HIGH and sets the SDA line to LOW. |
+ | |||
+ | === Address Frame === | ||
+ | After the master has generated the start condition, it sends to the data line (SDA) the address of the slave device with which it wants to communicate. The address is (most often) a 7-bit number (bits A6-A0 in the frame in the figure below). Bit 0 indicates whether the master initiates a Read operation (bit 0 is 1) or a Write operation (bit 0 is 0), as can be seen in the figure. | ||
- | === Cadrul de Adresa === | ||
- | După ce masterul a generat condiția de start, acesta trimite pe linia de date (SDA) adresa dispozitivului slave cu care dorește să comunice. Adresa este (de cele mai multe ori) un număr pe 7 biți (biții A6-A0 din frame-ul din figura de mai jos). Bitul 0 menționează dacă master-ul inițiază o operație de Citire (bitul 0 este 1) sau o operație de Scriere (bitul 0 este 0), așa cum se poate observa în figura. | ||
{{:pm:lab:i2c1.png?500|}} | {{:pm:lab:i2c1.png?500|}} | ||
- | Slave-ul care își recunoaște adresa trimite un ACK master-ului prin punerea liniei SDA pe LOW în al nouălea ciclu de ceas. Starea default a liniilor SDA/SCL este HIGH datorită rezistențelor de pull-up. Master-ul/Slave-ul doar ”trag” liniile pe LOW. | + | The slave that recognizes its address sends an ACK to the master by setting the SDA line to LOW in the ninth clock cycle. The default state of SDA / SCL lines is HIGH due to pull-up resistors. The Master / Slave just "pulls" the lines on LOW. |
- | Master-ul identifică dacă a primit ACK (SDA pus pe LOW) sau NACK (SDA a rămas HIGH pe durata celui de-al nouălea ciclu de ceas). | + | The master identifies whether it received ACK (SDA set to LOW) or NACK (SDA remained HIGH during the ninth clock cycle). |
- | === Cadrele de Date === | + | === Data Frames === |
- | Dacă master-ul a primit ACK (dacă există un slave pe magistrală cu adresa respectivă), el poate continua cu transmiterea datelor (operație de scriere), sau cu recepția datelor (operație de citire). Numărul de cadre de date este arbitrar, pot fi interschimbate oricâte. Fiecare cadru trimis/recepționat este ACK'd sau NACK'd. În funcție de operație (citire sau scriere), ACK-ul/NACK-ul este trimis fie de master fie de slave. | + | If the master has received the ACK (if there is a slave on the bus with the respective address), it can continue with the data transmission (write operation), or with the data reception (read operation). The number of data frames is arbitrary, they can be exchanged as many times. Each frame sent / received is ACK'd or NACK'd. Depending on the operation (read or write), the ACK / NACK is sent by either the master or the slave. |
- | * Daca master-ul a inițiat o operație de scriere, fiecare cadru trimis este confirmat (ACK'd) de către slave | + | * If the master has initiated a write operation, each frame sent is confirmed (ACK'd) by the slave |
- | * Daca master-ul a inițiat o operatie de citire, fiecare cadru recepționat este confirmat de (ACK'd) de master. Când master-ul dorește să oprească tranzacția după ce un anumit număr de cadre a fost recepționat, în loc să trimtă ACK trimite NACK. Astfel slave-ul se va opri din transmitere | + | * If the master has initiated a read operation, each frame received is confirmed by (ACK'd) the master . When the master wants to stop the transaction after a certain number of frames has been received, instead of sending ACK sends NACK. Thus the slave will stop transmitting |
- | === Conditia de Stop === | + | === Stop Condition === |
- | După ce toate cadrele de date au fost interschimbate, master-ul generează condiția de stop. Aceasta este realizată prin eliberarea linie SDA (trecere din LOW în HIGH) după eliberarea liniei SCL (trecere din LOW în HIGH). | + | After all data frames have been exchanged, the master generates the stop condition. This is done by releasing the SDA line (switching from LOW to HIGH) after releasing the SCL line (switching from LOW to HIGH). |
+ | |||
+ | {{:pm:lab:i2c14.png?500|}} | ||
===== I2C in Atmega324 ===== | ===== I2C in Atmega324 ===== | ||
- | Atmega324p poate funcționa atât în modul I2C Master cât și I2C Slave. Acesta mai este cunoscut și cu numele de TWI (Two-Wire Interface). | + | Atmega324p can work in both I2C Master and I2C Slave mode. It is also known as TWI (Two-Wire Interface). |
==== Register Description ==== | ==== Register Description ==== | ||
- | === TWBR - TWI Bit Rate Register (datasheet 21.9.1. - pagina 235) === | + | === TWBR - TWI Bit Rate Register (datasheet 21.9.1. - page 235) === |
- | === TWCR - TWI Control Register (datasheet 21.9.2. - pagina 235) === | + | === TWCR - TWI Control Register (datasheet 21.9.2. - page 235) === |
- | === TWSR - TWI Status Register (datasheet 21.9.3. - pagina 237) === | + | === TWSR - TWI Status Register (datasheet 21.9.3. - page 237) === |
- | === TWDR - TWI Data Register (datasheet 21.9.4. - pagina 237) === | + | === TWDR - TWI Data Register (datasheet 21.9.4. - page 237) === |
- | ==== Biblioteca I2C Master - API ==== | + | {{:pm:lab:i2c13.png?500|}} |
- | În acest laborator vom utiliza o bibliotecă pentru a ușura lucrul cu dispozitivele I2C. API-ul acesteia poate fi văzut în headerul ”i2c_master.h” din scheletul de laborator. | + | ==== Library I2C Master - API ==== |
- | {{:pm:lab:i2c2.png?900|}} | + | |
- | {{:pm:lab:i2c3.png?500|}} | + | In this lab we will use a library to make working with I2C devices easier. Its API can be seen in the "i2c_master.h" header in the lab framework. |
+ | {{: pm: lab: i2c2.png? 900 |}} | ||
- | {{:pm:lab:i2c4.png?500|}} | + | {{: pm: lab: i2c3.png? 500 |}} |
- | {{:pm:lab:i2c5.png?500|}} | + | {{: pm: lab: i2c4.png? 500 |}} |
- | {{:pm:lab:i2c6.png?500|}} | + | {{: pm: lab: i2c5.png? 500 |}} |
- | {{:pm:lab:i2c7.png?500|}} | + | {{: pm: lab: i2c6.png? 500 |}} |
- | ==== Memoria Seriala 24c02 ==== | + | {{: pm: lab: i2c7.png? 500 |}} |
- | Caracteristici: | + | |
- | * Capacitate: 2048 biti = 256 bytes | + | |
- | * Tehnologie de stocare: EEPROM | + | |
- | * Interfata de conectare: I2C | + | |
- | * Numar maxim de slave-uri pe acelasi bus: 8 | + | |
- | * Header I2C: 0xA | + | |
- | * Numar de bytes de adresa memorie: 1 | + | |
- | * Tip de asteptare dupa scriere: 4 msec | + | |
- | Header-ul, primul byte care este trimis intotdeauna dupa Start, este un byte compus din header + adresa slave-ului + un bit care indica daca urmeaza o transmitere (0)(write) sau o receptie (1)(read). | + | ===== Serial Memory 24c02 ===== |
+ | Features: | ||
+ | * Capacity: 2048 bits = 256 bytes | ||
+ | * Storage technology: EEPROM | ||
+ | * Connection interface: I2C | ||
+ | * Maximum number of slaves on the same bus: 8 | ||
+ | * I2C header: 0xA | ||
+ | * Number of bytes of memory address: 1 | ||
+ | * Type of wait after writing: 4 msec | ||
+ | |||
+ | The header, the first byte that is always sent after Start, is a byte composed of the header + the slave address + a bit that indicates whether a transmission (0) (write) or a reception follows (1) (read). | ||
+ | |||
+ | In order to write to memory, a sequence must be sent: | ||
- | Pentru a putea scrie in memorie, trebuie trimisa o secventa: | ||
START | START | ||
- | * Send header + slave address + bit transmisie | + | * Send header + slave address + transmission bit |
* Send memory address | * Send memory address | ||
* Send the data byte | * Send the data byte | ||
Line 81: | Line 87: | ||
{{:pm:lab:i2c10.png?500|}} | {{:pm:lab:i2c10.png?500|}} | ||
- | Pentru a putea citi din memorie, trebuie trimisa urmatoarea secventa: | + | In order to read from memory, the following sequence must be sent: |
START | START | ||
- | * Send header + slave address + bit transmisie | + | * Send header + slave address + transmission bit |
* Send memory address | * Send memory address | ||
START | START | ||
- | * Send header + slave address + bit reception | + | * Send header + slave address + reception bit |
* Receive data and send ACK | * Receive data and send ACK | ||
* ... | * ... | ||
- | * Receive data and send ACK | + | * Receive data and send ACK |
* Receive data and send NO_ACK | * Receive data and send NO_ACK | ||
STOP | STOP | ||
Line 95: | Line 102: | ||
{{:pm:lab:i2c9.png?500|}} | {{:pm:lab:i2c9.png?500|}} | ||
- | ==== Setup ==== | + | ===== Setup ===== |
{{:pm:lab:i2c11.png?500|}} | {{:pm:lab:i2c11.png?500|}} | ||
+ | |||
+ | After executing all tasks: | ||
{{:pm:lab:i2c8.png?500|}} | {{:pm:lab:i2c8.png?500|}} | ||
- | |||
- | Dupa executia tuturor task-urilor: | ||
{{:pm:lab:i2c12.png?500|}} | {{:pm:lab:i2c12.png?500|}} | ||
- | ==== Tasks ==== | + | ===== Tasks ===== |
- | Skeletul programului este aici: {{:pm:lab:lab_0xc6_skeleton.zip|}}. | + | The skeleton of the program is here: {{: pm : lab: lab_0xc6_skeleton.zip |}}. |
+ | |||
+ | <hidden> | ||
+ | Solution: {{:pm:lab:arhiva.zip|}} | ||
+ | </hidden> | ||
- | Memorie seriala 2kbits - {{:pm:lab:n24c02-d.pdf|}} | + | 2kbits serial memory - {{: pm: lab: n24c02-d.pdf |}} |
- | - In primul task trebuie trimisa o simpla secventa pentru a obtine confirmarea (acknowledge-ul) de la device-ul slave cu adresa de slave 0. | + | - In the first task a simple sequence must be sent to obtain the acknowledgment from the slave device with the slave address 0. |
- | - Folosind metoda de la task-ul anterior, sa se scaneze si astfel sa se detecteze ce device-uri exista pe bus-ul I2C | + | - Use the method from the previous task to scan and thus to detect which devices exist on the I2C bus. |
- | - In task-ul acesta trebuie scris in memoria seriala ce are adresa de slave 0. Scrierea se va face la adresa de memorie 0 si adresa de memorie 1. | + | - In this task it must be written in the serial memory that has the slave address 0. The writing will be done at memory address 0 and memory address 1. |
- | - In acest task trebuie citit continutul memorie (primii 4 bytes) ai slave-ului aflat la adresa 0 | + | - In this task the memory content (first 4 bytes) of the slave at address 0 must be read. |
- | Responsabil: [[ | Adrian Mocanu]] | + | Responsible: Adrian Mocanu |