I2C: Diferență între versiuni

De la YO3ITI
Sari la navigare Sari la căutare
 
(Nu s-au afișat 22 de versiuni intermediare efectuate de același utilizator)
Linia 1: Linia 1:
Acest articol are legătură cu proiectul [[Hat nod mobil pentru RPI 4.0 (THT)]].
Pentru configurare [[SvxLink]], click aici.
==Detecție EEPROM==
==Detecție EEPROM==
===Condiții inițiale===
Detecția dispozitivelor I2C se face cu comanda:
Detecția dispozitivelor I2C se face cu comanda:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
tom@rpi-yo3iti:~ $ i2cdetect -l
tom@rpi-yo3iti:~ $ i2cdetect -l
</syntaxhighlight>
</syntaxhighlight>
În cazul plăcii mele, ne interesează doar <code>i2c-0</code>
<code>i2cdetect</code> face parte din pachetul <code>i2c-tools</code>. Se testează dacă este instalat cu comanda <code>dpkg -l i2c-tools</code>. Dacă acest pachet nu este instalat, se folosește secvența:
<syntaxhighlight lang="console">
tom@rpi-yo3iti:~ $ sudo apt-get update -y
tom@rpi-yo3iti:~ $ sudo apt-get install -y i2c-tools
</syntaxhighlight>
Este o idee bună să se facă un update cu <code>sudo apt-get update -y</code>înainte de orice instalare. Iată ce afișează comanda <code>dpkg -l i2c-tools</code> în cazul meu:
<syntaxhighlight lang="console">
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name          Version      Architecture Description
+++-==============-============-============-========================================
ii  i2c-tools      4.1-1        armhf        heterogeneous set of I2C tools for Linux
</syntaxhighlight>
 
===Probing===
În cazul [[Hat nod mobil pentru RPI 4.0 (THT)|plăcii mele]], ne interesează doar ''bus''-ul <code>i2c-0</code> pe care am montat un EEPROM la pinii 27 și 28 ai RPi (<code>BCM0</code> și <code>BCM1</code>).
<syntaxhighlight lang="bash" highlight="3">
<syntaxhighlight lang="bash" highlight="3">
i2c-1 i2c      bcm2835 (i2c@7e804000)          I2C adapter
i2c-1 i2c      bcm2835 (i2c@7e804000)          I2C adapter
Linia 11: Linia 30:
i2c-10 i2c      i2c-11-mux (chan_id 1)          I2C adapter
i2c-10 i2c      i2c-11-mux (chan_id 1)          I2C adapter
</syntaxhighlight>
</syntaxhighlight>
Apropos, comanda <code>pinout</code> oferă o imagine simpatică cu toții pinii RPi:
<syntaxhighlight lang="console">
tom@rpi-yo3iti:~ $ pinout
,--------------------------------.
| oooooooooooooooooooo J8  +======
| 1ooooooooooooooooooo  PoE |  Net
|  Wi                    oo +======
|  Fi  Pi Model 4B  V1.2 oo      |
|        ,----.              +====
| |D|    |SoC |              |USB3
| |S|    |    |              +====
| |I|    `----'                  |
|                  |C|      +====
|                  |S|      |USB2
| pwr  |HD|  |HD| |I||A|    +====
`-| |---|MI|---|MI|----|V|-------'
Revision          : c03112
SoC                : BCM2711
RAM                : 4096Mb
Storage            : MicroSD
USB ports          : 4 (excluding power)
Ethernet ports    : 1
Wi-fi              : True
Bluetooth          : True
Camera ports (CSI) : 1
Display ports (DSI): 1
J8:
  3V3  (1) (2)  5V   
GPIO2  (3) (4)  5V   
GPIO3  (5) (6)  GND 
GPIO4  (7) (8)  GPIO14
  GND  (9) (10) GPIO15
GPIO17 (11) (12) GPIO18
GPIO27 (13) (14) GND 
GPIO22 (15) (16) GPIO23
  3V3 (17) (18) GPIO24
GPIO10 (19) (20) GND 
GPIO9 (21) (22) GPIO25
GPIO11 (23) (24) GPIO8
  GND (25) (26) GPIO7
GPIO0 (27) (28) GPIO1
GPIO5 (29) (30) GND 
GPIO6 (31) (32) GPIO12
GPIO13 (33) (34) GND 
GPIO19 (35) (36) GPIO16
GPIO26 (37) (38) GPIO20
  GND (39) (40) GPIO21
</syntaxhighlight>
==Investigare EEPROM (probing)==
Putem scana fiecare bus pentru a vedea ce adrese există în fiecare. Pentru bus-ul 0:
<syntaxhighlight lang="console">
tom@rpi-yo3iti:~ $ i2cdetect -y 0
</syntaxhighlight>
Dacă comanda nu funcționează și dă erori, se poate adăuga un ''flag'' <code>-r</code> astfel:
<syntaxhighlight lang="console">
tom@rpi-yo3iti:~ $ i2cdetect -y -r 0
</syntaxhighlight>
Comanda afișează:
<syntaxhighlight lang="console">
    0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
</syntaxhighlight>
Lista de mai sus conține toate adresele dispozitivului pe ''bus''-ul respectiv, cu <code>--</code>, <code>UU</code> sau adresa dispozitivului ca valoare.
* <code>--</code> arată că dispozitivul a fost interogat, dar nu a răspuns nici un dispozitiv. Dacă se obține acest răspuns, înseamnă fie că dispozitivul nu este pe ''bus'', fie nu este detectat din cauza anumitor probleme hardware (conexiuni sua tensiuni greșite, altceva).
* <code>UU</code> arată că s-a sărit peste interogarea adresei respective deoarece adresa este utilizată de un driver. Acest lucru indică că dispozitivul este prezent pe ''bus'' și că este foarte probabil ca driver-ul să fie corect.
* Adresa unui dispozitiv in notație hexazecimală arată că a fost detectat. De pildă <code>50</code> arată că a fost detectat un dispozitiv la adresa 50. În cele două ultime cazuri de mai sus, componenta și conexiunile hardware sunt în regulă. Dacă dispozitivul tot nu funcționează cum trebuie:
# în cazul în care este indicat <code>UU</code>, este foarte probabil ca driver-ul să aibă nevoie de ajustări.
# În cazul în care este indicată o adresă în notație hexazecimală, fie este nevoie de un driver, fie poate fi accesat printr-un mecanism din spațiul utilizator (script, program etc).


==I2C Probing==
==I2C Probing==
<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
tom@@rpi-yo3iti:~ $ sudo i2cdetect -F 0
tom@rpi-yo3iti:~ $ sudo i2cdetect -F 0
Functionalities implemented by /dev/i2c-0:
Functionalities implemented by /dev/i2c-0:
I2C                              yes
I2C                              yes
Linia 32: Linia 130:
I2C Block Read                  yes
I2C Block Read                  yes
</syntaxhighlight>
</syntaxhighlight>
<code>i2cdetect</code> is  a  userspace  program to scan an '''<span style="color:red">I<sup>2</sup>C</span>''' bus for devices. It outputs a table with the list of detected devices on the specified bus. <code>i2cbus</code> indicates  the number or name of the I2C bus to be scanned, and should correspond to one of the busses listed by <code>i2cdetect -l</code>. The optional  parameters first and last restrict the scanning range (default: from '''0x03''' to '''0x77''').
<code>i2cdetect</code> este un program cu care se pot scana bus-urile '''<span style="color:red">I<sup>2</sup>C</span>''' în căutarea dispozitivelor conectate la ele. Furnizează un tabel cu lista dispozitivelor detectate pe magistrala (bus-ul) respectivă. <code>i2cbus</code> indică numărul sau numele magistralei I2C scanate și trebuie să corespundă cu unul din bus-urile afișate cu comanda <code>i2cdetect -l</code>. Primul și ultimul parametru (ambele opționale) limitează intervalul de adrese (implicit: de la '''0x03''' la '''0x77''').
<syntaxhighlight lang="shell">
<syntaxhighlight lang="console">
tom@raspberrypi:~ $ sudo i2cdetect -l
tom@rpi-yo3iti:~ $ sudo i2cdetect -l
i2c-1 i2c      bcm2835 (i2c@7e804000)          I2C adapter
i2c-1 i2c      bcm2835 (i2c@7e804000)          I2C adapter
i2c-11 i2c      bcm2835 (i2c@7e205000)          I2C adapter
i2c-11 i2c      bcm2835 (i2c@7e205000)          I2C adapter
Linia 41: Linia 139:
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="shell">
<syntaxhighlight lang="console">
tom@raspberrypi:~ $ sudo i2cdetect -y 0
tom@rpi-yo3iti:~ $ sudo i2cdetect -y 0
</syntaxhighlight>
</syntaxhighlight>
<code>I2cdetect</code> returns a grid of possible addresses with each device displayed as shown in the screenshot below. 
<code>I2cdetect</code> afișează o grilă cu adresele posibile ale fiecărui dispozitiv, ca în imaginea de mai jos:
<syntaxhighlight lang="shell">
<syntaxhighlight lang="shell">
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
Linia 56: Linia 154:
70: -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
</syntaxhighlight>
</syntaxhighlight>
If no devices are detected check that you have followed all the steps at the start of this tutorial for enabling the I²C bus and the devices connected to the Raspberry Pi are powered and do not share the same '''<span style="color:red">I<sup>2</sup>C</span>''' address.
Dacă nu este detectat nici un dispozitiv, trebuie verificați pașii de activare a bus-ului I²C. Deasemenea trebuie verificat dacă dispozitivele I²C sunt alimentate și dacă nu cumva folosesc aceeași adresă '''<span style="color:red">I<sup>2</sup>C</span>'''.


==Programare==
==Programare I<sup>2</sup>C==
===C/ C++===
===C/ C++===
Atenție la eroarea de compilare de mai jos:
Atenție la eroarea de compilare de mai jos:
Linia 91: Linia 189:
}
}
</syntaxhighlight>
</syntaxhighlight>
===Python===


==Link-uri externe==
==Link-uri externe==
Linia 111: Linia 207:
* Hackaday [https://hackaday.io/project/167888-not-quite-useless-raspberry-pi-replacement/log/170018-raspberry-pi-hat-id-eeprom-details Raspberry Pi Hat ID EEPROM Details]
* Hackaday [https://hackaday.io/project/167888-not-quite-useless-raspberry-pi-replacement/log/170018-raspberry-pi-hat-id-eeprom-details Raspberry Pi Hat ID EEPROM Details]
* [https://learn.sparkfun.com/tutorials/raspberry-pi-spi-and-i2c-tutorial/i2c-0-on-40-pin-pi-boards Raspberry Pi SPI and I2C Tutorial]
* [https://learn.sparkfun.com/tutorials/raspberry-pi-spi-and-i2c-tutorial/i2c-0-on-40-pin-pi-boards Raspberry Pi SPI and I2C Tutorial]
* [https://www.raspberrypi.org/documentation/configuration/config-txt/gpio.md GPIO control in config.txt]


===Discuții de prin forumuri===
===Discuții de prin forumuri===
* [https://www.raspberrypi.org/forums/viewtopic.php?t=108134 STICKY: HOWTO: Raspi HAT EEPROM and device-tree]
* [https://www.raspberrypi.org/forums/viewtopic.php?t=108134 STICKY: HOWTO: Raspi HAT EEPROM and device-tree]
* [https://www.raspberrypi.org/forums/viewtopic.php?t=87147 I2C EEPROM not read identically on RPi]
* [https://www.raspberrypi.org/forums/viewtopic.php?t=87147 I2C EEPROM not read identically on RPi]

Versiunea curentă din 1 noiembrie 2022 01:30

Acest articol are legătură cu proiectul Hat nod mobil pentru RPI 4.0 (THT). Pentru configurare SvxLink, click aici.

Detecție EEPROM

Condiții inițiale

Detecția dispozitivelor I2C se face cu comanda:

tom@rpi-yo3iti:~ $ i2cdetect -l

i2cdetect face parte din pachetul i2c-tools. Se testează dacă este instalat cu comanda dpkg -l i2c-tools. Dacă acest pachet nu este instalat, se folosește secvența:

tom@rpi-yo3iti:~ $ sudo apt-get update -y
tom@rpi-yo3iti:~ $ sudo apt-get install -y i2c-tools

Este o idee bună să se facă un update cu sudo apt-get update -yînainte de orice instalare. Iată ce afișează comanda dpkg -l i2c-tools în cazul meu:

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-========================================
ii  i2c-tools      4.1-1        armhf        heterogeneous set of I2C tools for Linux

Probing

În cazul plăcii mele, ne interesează doar bus-ul i2c-0 pe care am montat un EEPROM la pinii 27 și 28 ai RPi (BCM0 și BCM1).

i2c-1	i2c       	bcm2835 (i2c@7e804000)          	I2C adapter
i2c-11	i2c       	bcm2835 (i2c@7e205000)          	I2C adapter
i2c-0	i2c       	i2c-11-mux (chan_id 0)          	I2C adapter
i2c-10	i2c       	i2c-11-mux (chan_id 1)          	I2C adapter

Apropos, comanda pinout oferă o imagine simpatică cu toții pinii RPi:

tom@rpi-yo3iti:~ $ pinout
,--------------------------------.
| oooooooooooooooooooo J8   +======
| 1ooooooooooooooooooo  PoE |   Net
|  Wi                    oo +======
|  Fi  Pi Model 4B  V1.2 oo      |
|        ,----.               +====
| |D|    |SoC |               |USB3
| |S|    |    |               +====
| |I|    `----'                  |
|                   |C|       +====
|                   |S|       |USB2
| pwr   |HD|   |HD| |I||A|    +====
`-| |---|MI|---|MI|----|V|-------'

Revision           : c03112
SoC                : BCM2711
RAM                : 4096Mb
Storage            : MicroSD
USB ports          : 4 (excluding power)
Ethernet ports     : 1
Wi-fi              : True
Bluetooth          : True
Camera ports (CSI) : 1
Display ports (DSI): 1

J8:
   3V3  (1) (2)  5V    
 GPIO2  (3) (4)  5V    
 GPIO3  (5) (6)  GND   
 GPIO4  (7) (8)  GPIO14
   GND  (9) (10) GPIO15
GPIO17 (11) (12) GPIO18
GPIO27 (13) (14) GND   
GPIO22 (15) (16) GPIO23
   3V3 (17) (18) GPIO24
GPIO10 (19) (20) GND   
 GPIO9 (21) (22) GPIO25
GPIO11 (23) (24) GPIO8 
   GND (25) (26) GPIO7 
 GPIO0 (27) (28) GPIO1 
 GPIO5 (29) (30) GND   
 GPIO6 (31) (32) GPIO12
GPIO13 (33) (34) GND   
GPIO19 (35) (36) GPIO16
GPIO26 (37) (38) GPIO20
   GND (39) (40) GPIO21

Investigare EEPROM (probing)

Putem scana fiecare bus pentru a vedea ce adrese există în fiecare. Pentru bus-ul 0:

tom@rpi-yo3iti:~ $ i2cdetect -y 0

Dacă comanda nu funcționează și dă erori, se poate adăuga un flag -r astfel:

tom@rpi-yo3iti:~ $ i2cdetect -y -r 0

Comanda afișează:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

Lista de mai sus conține toate adresele dispozitivului pe bus-ul respectiv, cu --, UU sau adresa dispozitivului ca valoare.

  • -- arată că dispozitivul a fost interogat, dar nu a răspuns nici un dispozitiv. Dacă se obține acest răspuns, înseamnă fie că dispozitivul nu este pe bus, fie nu este detectat din cauza anumitor probleme hardware (conexiuni sua tensiuni greșite, altceva).
  • UU arată că s-a sărit peste interogarea adresei respective deoarece adresa este utilizată de un driver. Acest lucru indică că dispozitivul este prezent pe bus și că este foarte probabil ca driver-ul să fie corect.
  • Adresa unui dispozitiv in notație hexazecimală arată că a fost detectat. De pildă 50 arată că a fost detectat un dispozitiv la adresa 50. În cele două ultime cazuri de mai sus, componenta și conexiunile hardware sunt în regulă. Dacă dispozitivul tot nu funcționează cum trebuie:
  1. în cazul în care este indicat UU, este foarte probabil ca driver-ul să aibă nevoie de ajustări.
  2. În cazul în care este indicată o adresă în notație hexazecimală, fie este nevoie de un driver, fie poate fi accesat printr-un mecanism din spațiul utilizator (script, program etc).

I2C Probing

tom@rpi-yo3iti:~ $ sudo i2cdetect -F 0
Functionalities implemented by /dev/i2c-0:
I2C                              yes
SMBus Quick Command              yes
SMBus Send Byte                  yes
SMBus Receive Byte               yes
SMBus Write Byte                 yes
SMBus Read Byte                  yes
SMBus Write Word                 yes
SMBus Read Word                  yes
SMBus Process Call               yes
SMBus Block Write                yes
SMBus Block Read                 no
SMBus Block Process Call         no
SMBus PEC                        yes
I2C Block Write                  yes
I2C Block Read                   yes

i2cdetect este un program cu care se pot scana bus-urile I2C în căutarea dispozitivelor conectate la ele. Furnizează un tabel cu lista dispozitivelor detectate pe magistrala (bus-ul) respectivă. i2cbus indică numărul sau numele magistralei I2C scanate și trebuie să corespundă cu unul din bus-urile afișate cu comanda i2cdetect -l. Primul și ultimul parametru (ambele opționale) limitează intervalul de adrese (implicit: de la 0x03 la 0x77).

tom@rpi-yo3iti:~ $ sudo i2cdetect -l
i2c-1	i2c       	bcm2835 (i2c@7e804000)          	I2C adapter
i2c-11	i2c       	bcm2835 (i2c@7e205000)          	I2C adapter
i2c-0	i2c       	i2c-11-mux (chan_id 0)          	I2C adapter
i2c-10	i2c       	i2c-11-mux (chan_id 1)          	I2C adapter
tom@rpi-yo3iti:~ $ sudo i2cdetect -y 0

I2cdetect afișează o grilă cu adresele posibile ale fiecărui dispozitiv, ca în imaginea de mai jos:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

Dacă nu este detectat nici un dispozitiv, trebuie verificați pașii de activare a bus-ului I²C. Deasemenea trebuie verificat dacă dispozitivele I²C sunt alimentate și dacă nu cumva folosesc aceeași adresă I2C.

Programare I2C

C/ C++

Atenție la eroarea de compilare de mai jos:

# make
gcc -g -Wall -c eeprom-tinker.c
gcc -g -Wall -c linux-i2c.c
linux-i2c.c:13:10: fatal error: i2c/smbus.h: No such file or directory
 #include <i2c/smbus.h>
          ^~~~~~~~~~~~~
compilation terminated.
Makefile:12: recipe for target 'linux-i2c.o' failed
make: *** [linux-i2c.o] Error 1

Indică că nu a fost instalat libi2c-dev și se rezolvă prin sudo apt-get install libi2c-dev:

#include <unistd.h>             //Needed for I2C port
#include <fcntl.h>              //Needed for I2C port
#include <sys/ioctl.h>          //Needed for I2C port
#include <linux/i2c-dev.h>      //Needed for I2C port

int file_i2c;
int length;
unsigned char buffer[60] = {0};

// Open the I2C BUS
char *filename = (char*)"/dev/i2c-0";
if ((file_i2c = open(filename, O_RDWR)) < 0) {
        // ERROR_HANDLING
        printf("Failed to open the i2c bus");
        return;
}

Link-uri externe

Documentație

  • A nano Cheat Sheet
  • SMBus Quick Start Guide — The System Management Bus (SMBus) is a two-wire interface through which various system component chips can communicate with each other and with the rest of the 4 system. It is based on the principles of operation of I2C. SMBus provides a control bus for the system to pass messages to and from devices instead of using individual control lines, helping to reduce pin count and system wires.
  • AN10216-01 I2C MANUAL — The I2C Manual provides a broad overview of the various serial buses, why the I2C bus should be considered, technical detail of the I2C bus and how it works, previous limitations/solutions, comparison to the SMBus, Intelligent Platform Management Interface implementations, review of the different I2C devices that are available and patent/royalty information. The I2C Manual was presented during the 3 hour TecForum at DesignCon 2003 in San Jose, CA on 27 January 2003.
  • I2C Part 1 - Introducing I2C
  • I2C Part 2 - Enabling I²C on the Raspberry Pi
  • I2C Part 3 - I²C tools in Linux
  • I2C Part 4 - Programming I²C with Python

Specificații

Utile

Discuții de prin forumuri