I2C
Detecție EEPROM
Detecția dispozitivelor I2C se face cu comanda:
tom@rpi-yo3iti:~ $ i2cdetect -l
Î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
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
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
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
is a userspace program to scan an I2C bus for devices. It outputs a table with the list of detected devices on the specified bus. i2cbus
indicates the number or name of the I2C bus to be scanned, and should correspond to one of the busses listed by i2cdetect -l
. The optional parameters first and last restrict the scanning range (default: from 0x03 to 0x77).
tom@raspberrypi:~ $ 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@raspberrypi:~ $ sudo i2cdetect -y 0
I2cdetect
returns a grid of possible addresses with each device displayed as shown in the screenshot below.
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: -- -- -- -- -- -- -- --
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 I2C address.
Programare
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;
}
Python
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
- Device Trees, overlays, and parameters
- Hackaday Raspberry Pi Hat ID EEPROM Details
- Raspberry Pi SPI and I2C Tutorial