写在前面,Jetson Nano关于I2C没有对C语言的支持,使用编程语言是python。因为I2C涉及寄存器部分设置,在没有硬件的指导手册情况下,新手向不是很友好。现有可直接使用的库都是python,如果需要使用C或者C++,可参考libi2c(https://github.com/amaork/libi2c )或者libgpiod(https://github.com/brgl/libgpiod )。
参考文章
- https://www.jetsonhacks.com/2019/07/22/jetson-nano-using-i2c/
- https://www.jianshu.com/p/f60b6a2fd736
- https://blog.csdn.net/thystar/article/details/53021992
- https://blog.csdn.net/qq_15807167/article/details/53435831
- https://www.ncnynl.com/archives/201704/1526.html
I2Ctool检查命令:i2cdetect/i2cdump/ i2cset/i2cget
I2Ctool安装
$ sudo apt-get update
$ sudo apt-get install -y i2c-tools
检查安装情况
$ apt-cache policy i2c-tools
输出如下即为安装成功
i2c-tools:
已安装:4.0-2
候选: 4.0-2
版本列表:
*** 4.0-2 500
500 http://ports.ubuntu.com/ubuntu-ports bionic/universe arm64 Packages
100 /var/lib/dpkg/status
i2cdetect
- 列出安装的所有总线
命令
$ sudo i2cdetect -l
效果
i2c-3 i2c 7000c700.i2c I2C adapter
i2c-1 i2c 7000c400.i2c I2C adapter
i2c-6 i2c Tegra I2C adapter I2C adapter
i2c-4 i2c 7000d000.i2c I2C adapter
i2c-2 i2c 7000c500.i2c I2C adapter
i2c-0 i2c 7000c000.i2c I2C adapter
i2c-5 i2c 7000d100.i2c I2C adapter
- 扫描某一总线bus上所有i2c设备,并且打印出设备i2c总线地址
命令(其中1为i2c设备号)
$ sudo i2cdetect -y -r -a 1
参数: -y是无视交互问题直接执行,-r是SMBus read byte命令,-a是所有地址,1是指i2c-1。
效果
0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- 77
其中,0x68和0x77 为i2c设备在I2C_1上的总线地址
i2cdump
作用是寄存器数据导出。总线上i2c设备的数据是要存储在寄存器上。
命令
sudo i2cdump -y 1 0x68
含义为:导出I2C-1中地址为0x68的设备的数据
效果,左边是寄存器地址,右侧数据
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: 0a 51 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ?Q.............. 10: 00 08 00 00 57 00 00 00 00 00 00 00 00 00 00 00 .?..W........... 20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 50: 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 ....?........... 60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 ............... 80: 0a 51 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ?Q.............. 90: 00 08 00 00 57 00 00 00 00 00 00 00 00 00 00 00 .?..W........... a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ b0: 00 00 00 00 00 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 03 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 20 ...............
i2cset
寄存器数据写入,即向寄存器中某一位地址上插入数据:
命令(sudo i2cset -y 1 i2c设备地址 写入位置 写入内容)
sudo i2cset -y 1 0x68 0x20 0x55
效果,(寄存器地址0x20上被插入数据0x55)
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: 0a 51 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ?Q.............. 10: 00 08 00 00 57 00 00 00 00 00 00 00 00 00 00 00 .?..W........... 20: 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 50: 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 ....?........... 60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 ............... 80: 0a 51 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ?Q.............. 90: 00 08 00 00 57 00 00 00 00 00 00 00 00 00 00 00 .?..W........... a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ b0: 00 00 00 00 00 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 03 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 20 ...............
i2cget
读出总线地址为0x77设备的0x90寄存器地址的数据
命令(sudo i2cget -y 1 i2c设备地址 寄存器位置)
sudo i2cget -y 1 0x68 0x80
效果
0x0a
Jetson Nano I2C设备
如图是jetson nano的i2c所有设备,共7组。
具体实例可以参考文章 https://www.jianshu.com/p/f60b6a2fd736
其中有几项需要说明
- i2c设备需要安装相应的库(主要是用来设置该设备的接口和寄存器地址等,或者部分功能输出函数),如果未安装适合该设备的库,可以在网址 https://pypi.org/ 中输入设备名称寻找并安装。
- 安装后可能需要修改部分库以适配当前的开发环境,例如修改设备总线地址等。库的安装路径为
/usr/local/lib/python3.6/dist-packages
可在其中寻找安装好的库,修改 ini.py文件。