openSUSE:I2C
i2c-tools
i2c-tools 包含系列工具,用于从命令行访问 I2C 。
安装
To install i2c-tools, just install it from main OSS repo.
One click installation is also available : https://software.opensuse.org/package/i2c-tools
载入 i2c-dev 内核模块
To be able to access I2C from userspace, you need to load i2c-dev kernel module, at each boot.
modprobe i2c-dev
To load it automatically at each boot, create /etc/modules-load.d/10-i2c.conf file with the following content:
# Load i2c-dev.ko i2c-dev
For Raspberry Pi (all versions), you need to enable I2C-1 access by adding the following lines to /boot/efi/extraconfig.txt:
# Enable I2C access from Linux dtparam=i2c_arm=on # Uncomment following line to set i2c-1 baudrate to 400 000 Hz (default i2c-1 baudrate is 100 000 Hz) # dtparam=i2c_arm_baudrate=400000
检测可用 I2C 总线: i2cdetect -l
i2cdetect -l will list all I2C buses.
i2cdetect -l will return for the RPi3:
i2c-1 i2c bcm2835 I2C adapter I2C adapter i2c-2 i2c bcm2835 I2C adapter I2C adapter
检测 I2C 总线上可用的设备
i2cdetect -y 1 will list all devices on bus 1.
i2cdetect -y 2 will list all devices on bus 2, and will return for the RPi3:
0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- 3a -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
which means that we have devices responding to address 0x3A (58 in decimal) and 0x50 (80 in decimal).
打印 I2C 总线上设备寄存器
i2cdump -y 2 0x50 will dump registers from I2C device at address 0x50 on I2C bus number 2, and will return for the RPi3 connected to an HP LE2201w monitor:
No size specified (using byte-data access) 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef 00: 00 ff ff ff ff ff ff 00 22 f0 43 28 01 01 01 01 ........"?C(???? 10: 2f 13 01 03 a0 2f 1e 78 ee b1 30 a5 56 4a 9a 25 /????/?x??0?VJ?% 20: 11 50 54 a5 6b f0 81 40 81 80 95 00 a9 00 b3 00 ?PT?k??@???.?.?. 30: 01 01 01 01 01 01 21 39 90 30 62 1a 27 40 68 b0 ??????!9?0b?'@h? 40: 36 00 d9 28 11 00 00 1c 00 00 00 fd 00 32 4c 18 6.?(?..?...?.2L? 50: 50 11 00 0a 20 20 20 20 20 20 00 00 00 fc 00 48 P?.? ...?.H 60: 50 20 4c 45 32 32 30 31 77 0a 20 20 00 00 00 ff P LE2201w? .... 70: 00 43 4e 4b 39 34 37 30 34 33 44 0a 20 20 01 f2 .CNK947043D? ?? 80: 02 03 23 f1 4b 10 1f 84 13 00 00 02 03 00 00 00 ??#?K????..??... 90: 23 09 07 07 83 01 00 00 e2 00 0f 67 03 0c 00 20 #?????..?.?g??. a0: 00 80 2d 01 1d 00 72 51 d0 1e 20 6e 28 55 00 10 .?-??.rQ?? n(U.? b0: 09 00 00 00 1e 00 00 00 00 00 00 00 00 00 00 00 ?...?........... c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ce ...............?
Read a register from a device on an I2C bus (if register is readable)
i2cget -y 2 0x50 0x60 will read register 0x60 from I2C device at address 0x50 on I2C bus number 2, and will return for the RPi3 (connected to an HP LE2201w monitor):
0x50
You can also use i2ctransfer -y 2 w1@0x50 0x60 r8 to read 8 bytes at offset (register) 0x60 from I2C device at address 0x50 on I2C bus number 2, and will return for the RPi3 (connected to an HP LE2201w monitor):
0x50 0x20 0x4c 0x45 0x32 0x32 0x30 0x31
Write a register to a device on an I2C bus (if register is writable)
i2cset -y 2 0x50 0x60 0x0 will write value 0x0 to register 0x60 to I2C device at address 0x50 on I2C bus number 2, and will return for the RPi3 (connected to an HP LE2201w monitor):
0x50
python3-smbus
python3-smbus package contains the python binding from i2c-tools to access I2C from python3.
Installation
To install python3-smbus, you need to add Base:System repo for your openSUSE flavor. For example, for Factory ARM: https://download.opensuse.org/repositories/Base:/System/openSUSE_Factory_ARM/Base:System.repo
One click installation is also available : https://software.opensuse.org/package/python3-smbus
Example
Example of a script which get data from bus i2c-1, device 0x5C, register 0x20:
#!/usr/bin/python3 import smbus # Define I2C bus to use bus = smbus.SMBus(1) # Define I2C device address device = 0x5C # Define register adress reg = 0x20 data = bus.read_i2c_block_data(device, reg) #data = bus.read_word_data(device, reg) print("Data: " + str(data))
You can get more details on documentation from smbus2 except you need to use import smbus instad of import smbus2