i2c调试工具分享
i2c-tools简介
在嵌入式开发仲,有时候需要确认硬件是否正常连接,设备是否正常工作,设备的地址是多少等等,这里我们就需要使用一个用于测试I2C总线的工具——i2c-tools。
i2c-tools工具是一个专门调试i2c的,开源,可获取挂载的设备及设备地址,还可以读写I2C设备寄存器。
下面我们就对这个工具的安装和使用做个简单介绍。
i2c-tools安装
下载地址:https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/。本文使用的是4.3版本。
解压
下载完成后,放到linux环境下解压。
tar -zxvf i2c-tools-4.3.tar.gz
cd i2c-tools-4.3
编译
使用以下命令进行编译
make CC=arm-linux-gnueabihf-gcc USE_STATIC_LIB=1
USE_STATIC_LIB 的意思是使用静态编译。编译完成后,会在tools目录下产生i2cdetect,i2cdump,i2cget,i2cset,i2ctransfer五个产物。将这些可执行文件拷贝到设备上。
不加 USE_STATIC_LIB 编译选项,会使用动态链接的方式编译。编译完成后需要拷贝i2c-tools-4.3/lib 目录下的libi2c.so.0动态库到设备上的/usr/bin 目录下。
i2c-tools使用方法
i2cdetect
i2cdetect用來列举I2C bus和上面所有的设备,可接受的参数有
Usage: i2cdetect [-y] [-a] [-q|-r] I2CBUS [FIRST LAST]
i2cdetect -F I2CBUS
i2cdetect -l
I2CBUS is an integer or an I2C bus name
If provided, FIRST and LAST limit the probing range.
-V:输出当前版本号
debian@npi:/mnt/mnt$ ./i2cdetect -V
i2cdetect version 4.3
-l:输出所有 i2c 总线,如下总线编号有twi1和twi2,或者1和2
debian@npi:/mnt/mnt$ ./i2cdetect -l
i2c-0 unknown 21a0000.i2c N/A
i2c-1 unknown 21a4000.i2c N/A
debian@npi:/mnt/mnt$ ls -l /dev/i2c-*
crw------- 1 root root 89, 0 Mar 19 09:42 /dev/i2c-0
crw------- 1 root root 89, 1 Mar 19 09:42 /dev/i2c-1
I2CBUS:i2c总线编号
-F:此 i2c 支持的功能
root@npi:/mnt/mnt# ./i2cdetect -F 1
Functionalities implemented by /dev/i2c-1:
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 yes
SMBus Block Process Call no
SMBus PEC yes
I2C Block Write yes
I2C Block Read yes
root@npi:/mnt/mnt#
-y:指令执行自动yes,否则会提示确认执行Continue? [Y/n] Y,不加参数y会有很多执行提示,可以帮助判断
-a:输出总线上所有地址(00-7f),没有 -a,只显示 08-77,UU 表示该设备在驱动中已使用。
如下0x1a,0x39的地址正在被使用。
root@npi:/mnt/mnt# ./i2cdetect -a 1
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-1.
I will probe address range 0x00-0x7f.
Continue? [Y/n] y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: 00 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- UU -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
root@npi:/mnt/mnt# ./i2cdetect -y -a 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: 00 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- UU -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
root@npi:/mnt/mnt#
i2cdump
i2cdump读取设备上所有寄存器的值,可接受的参数有
Usage: i2cdump [-f] [-y] [-r first-last] [-a] I2CBUS ADDRESS [MODE [BANK [BANKREG]]]
I2CBUS is an integer or an I2C bus name
ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given)
MODE is one of:
b (byte, default)
w (word)
W (word on even register addresses)
s (SMBus block)
i (I2C block)
c (consecutive byte)
Append p for SMBus PEC
-V:输出当前版本号
-f:强制使用此设备地址,即使此设备地址已经被使用;若不添加此参数,地址可能写失败
-y:指令执行自动 yes,否则会提示确认执行Continue? [Y/n] Y,不加参数y会有很多执行提示,可以帮助判断
-r:读取从 first-last 之间的寄存器值
-a:读取0x00-0xff范围的地址
I2CBUS:i2c总线编号
ADDRESS:设备地址,建议使用十六进制
MODE:数据长度类型
root@npi:/mnt/mnt# ./i2cdump -f -y -a 1 0x39
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 00 00 00 00 00 00 00 70 00 00 00 00 00 00 00 ........p.......
10: 00 00 00 00 00 00 00 00 00 00 10 b0 02 03 02 00 ..........?????.
20: 95 00 00 00 00 00 40 00 00 00 00 fb ff dd c4 00 ?.....@....?.??.
30: 00 00 00 00 00 00 90 22 a0 00 00 00 01 10 00 00 ......?"?...??..
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
60: 04 03 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 03 00 00 .............?..
80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
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 77 14 76 01 00 01 00 .........w?v?.?.
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 00 ................
# 只读取0x50-0x7f寄存器范围的值
root@npi:/mnt/mnt# ./i2cdump -f -y -r 0x50-0x7f 1 0x39
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
60: 04 03 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 03 00 00 .............?..
root@npi:/mnt/mnt#
通过读取i2c设备寄存器的值与芯片手册的值进行比对,确认我们配置的是否正确。
i2cget
i2cget读取设备上寄存器的值,可接受的参数有
Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]
I2CBUS is an integer or an I2C bus name
ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given)
MODE is one of:
b (read byte data, default)
w (read word data)
c (write byte/read byte)
Append p for SMBus PEC
I2CBUS:i2c总线编号
CHIP-ADDRESS:设备地址
DATA-ADDRESS:要读取的寄存器地址
MODE:数据长度类型
# 0x39设备地址,0x04要读取的寄存器
root@npi:/mnt/mnt# ./i2cget -f -y 1 0x39 0x04
0x03
i2cset
i2cset设置设备上寄存器的值,可接受的参数有
Usage: i2cset [-f] [-y] [-m MASK] [-r] [-a] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]
I2CBUS is an integer or an I2C bus name
ADDRESS is an integer (0x03 - 0x77, or 0x00 - 0x7f if -a is given)
MODE is one of:
c (byte, no value)
b (byte data, default)
w (word data)
i (I2C block data)
s (SMBus block data)
Append p for SMBus PEC
I2CBUS:i2c总线编号
CHIP-ADDRESS:设备地址
DATA-ADDRESS:要写入的寄存器地址
VALUE:要写入的值
MODE:数据长度类型
root@npi:/mnt/mnt# ./i2cset -f -m 0xff -r 1 0x39 0x04 0x03
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will write to device file /dev/i2c-1, chip address 0x39,
data address 0x04, data 0x03 (masked), mode byte.
Continue? [Y/n] y
Old value 0x00, write mask 0xff, will write 0x03
Continue? [Y/n] y
Value 0x03 written, readback matched
root@npi:/mnt/mnt#
把写入寄存器的值读出来。确认与我们写入的值相同。
root@npi:/mnt/mnt# ./i2cdump -f -y -r 0x00-0x0f 1 0x39
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 00 00 00 03 00 00 00 70 00 00 00 00 00 00 00 ....?...p.......
root@npi:/mnt/mnt#
i2ctransfer
i2ctransfer通过一次传输发送用户定义的I2C消息,用于创建I2C消息并将其作为一次传输合并发送。
Usage: i2ctransfer [-f] [-y] [-v] [-V] [-a] I2CBUS DESC [DATA] [DESC [DATA]]...
I2CBUS is an integer or an I2C bus name
DESC describes the transfer in the form: {r|w}LENGTH[@address]
1) read/write-flag 2) LENGTH (range 0-65535) 3) I2C address (use last one if omitted)
DATA are LENGTH bytes for a write message. They can be shortened by a suffix:
= (keep value constant until LENGTH)
+ (increase value by 1 until LENGTH)
- (decrease value by 1 until LENGTH)
p (use pseudo random generator until LENGTH with value as seed)
Example (bus 0, read 8 byte at offset 0x64 from EEPROM at 0x50):
# i2ctransfer 0 w1@0x50 0x64 r8
Example (same EEPROM, at offset 0x42 write 0xff 0xfe ... 0xf0):
# i2ctransfer 0 w17@0x50 0x42 0xff-
I2CBUS:i2c总线编号
DESC:{r | w}<消息长度>[@设备地址]
{r | w}指定消息是读还是写<消息长度>指定在此消息中读取或写入的字节数。它被解析为一个无符号的16位整数
[@设备地址]指定此消息要访问的芯片的7位地址,并且是整数。
#参数2为i2c2,w2表示写两个字节,@0x39为i2c设备(注意要7位地址),0x02 0xd3 为高低位地址,r1为读取的数据是一个byte。
root@npi:/mnt/mnt# ./i2ctransfer -f -y 1 w2@0x60 0x02 0xd3 r1
0x00
总结
本文只是抛砖引玉,i2c-tools还有更多的使用方法,需要大家在实际工作中去探索。
i2c调试工具分享的更多相关文章
- web前端开发教程系列-3 - 前端开发调试工具分享
前言 一. Bug出现的原因 1. 主观原因或者是出现这么多浏览器的原因 2. 另外客观原因: 二. 调试工具 1. Firebug 2. Chrome 3. IE11 4. IETester 5. ...
- WebGL调试工具分享
学习WebGL,我们需要一些好用的调试工具,下面分享3个常用的调试工具. WebGL Inspector 下载地址:https://github.com/benvanik/WebGL-Inspecto ...
- 两大js移动端调试神器 / 调试工具分享 !
分享大家一个CDN网站:https://www.bootcdn.cn/ eruda 移动端网页调试工具的使用: <script src="https://cdn.bootcdn.net ...
- 分享十个JavaScript在线调试工具
测试Javascript可能是网页开发中最让人忧伤的工作.这里我找一些比较好的工具来帮助大家进行测试工作.这10款是我精选的基于浏览器的JavaScript在线调试工具,希望你们对你们有用. 1.Op ...
- gdb调试工具使用方法分享
刚才看了一个CSDN上分享gdb调试工具使用的教程,讲得非常好,推荐到这里: http://blog.csdn.net/liigo/article/details/582231
- 【4412开发板使用经验分享】迅为4412开发板I2C驱动问题
本文转自迅为论坛:bbs.topeetboard.com 我想写DS3231 的驱动 但是读回的数据老是-6 硬件: 我I2C设备连接的这几个GPIO,看了2.5的手册,接口应该是链接正确的 软件 分 ...
- 分享一款一直在维护的【网络开发运维|通用调试工具】: http请求, websocket,cmd, RSA,DES, 参数签名工具,脚本批量生成工具,google动态口令,端口检测,组件注册,js混淆...
首先发下下载地址:https://files.cnblogs.com/files/taohuadaozhu/ConfigLab.Test.ex.rar 日常开发,运维,跨部门跨公司对接中. 想快速调 ...
- 案例分享:Qt modbus485调试工具(读写Byte、Int、DInt、Real、DReal)(当前v1.3.0)
前言 西门子PLC.台达PLC.法兰克机床等等多年以前玩得比较多,有tcp/ip通讯也有modbus通讯,modbus又分为网络,485,232等. 医疗项目,焊接机器人项目,工控机床项目,数控 ...
- 【前端调试手机页面】分享一款便捷的调试工具--spy-debugger
spy-debugger : 微信调试,各种WebView样式调试.手机浏览器的页面真机调试.便捷的远程调试手机页面.抓包工具,支持:HTTP/HTTPS,无需USB连接设备. github地址: ...
随机推荐
- Git 工作流简介
1.概述 工作流有各式各样的用法,但也正因此使得在实际工作中如何上手使用增加了难度.这篇指南通过总览公司团队中最常用的几种 Git 工作流让大家可以上手使用. 在阅读的过程中请记住,本文中的几种工作流 ...
- 如何获取 topic 主题的列表?
bin/kafka-topics.sh --list --zookeeper localhost:2181
- 接口是否可继承(extends)接口?抽象类是否可实现 (implements)接口?抽象类是否可继承具体类(concrete class)?
接口可以继承接口,而且支持多重继承.抽象类可以实现(implements)接口,抽象类可继承具体类也可以继承抽象类.
- 三、MyCat主要配置介绍
一.配置文件 1.server.xml Mycat的配置文件,设置账号.参数等2.schema.xml Mycat对应的物理数据库和数据库表的配置3.rule.xml Mycat分片(分库分表)规则 ...
- MySQL安装速成指南(ZIP)
MySQL初始化数据库 第一步:将MySQL ZIP压缩包进行解压 第二部:在MySQL主目录创建my.ini文件,并添加以下内容 [client] port=3306 socket=/tmp/mys ...
- 解释 Spring 支持的几种 bean 的作用域?
Spring 框架支持以下五种 bean 的作用域:singleton : bean 在每个 Spring ioc 容器中只有一个实例.prototype:一个 bean 的定义可以有多个实例.req ...
- Atomic 的实现原理
1.直接操作内存,使用Unsafe 这个类 2.使用 getIntVolatile(var1, var2) 获取线程间共享的变量 3.采用CAS的尝试机制(核心所在),代码如下: public fin ...
- 学习heartbeat-01简介
1.Heartbeat介绍 Heartbeat 是一个基于Linux开源的,被广泛使用的高可用集群系统,自1999年开始到现在,发布了众多版本,是目前开源Linux-HA项目最成功的一个例子,在行业内 ...
- java集合类框架的基本接口有哪些
集合类接口指定了一组叫做元素的对象.集合类接口的每一种具体的实现类都可以以他自己的方式对元素进行保存和排序.有的集合允许重复的键,有些不允许. java集合类里面最基本 的接口: Collection ...
- servlet中的HttpServletRequest对象
HttpServletRequest对象表示客户端浏览器发起的请求,当客户端浏览器通过HTTP协议访问服务器时,Tomcat会将HTTP请求中的所有信息解析并封装在HttpServletRequest ...