linux i2c-tools使用方法

本文主要对i2c-tools工具中的i2cdetect 、i2cdump、i2cget 、   i2cset操作使用进行说明。

一、i2cdetect 命令

1.1 description

Detect i2c devices.

-a      All addresses 
-F      Show functionality
-l      List all buses
-r      Probe with SMBus Read Byte
-y      Answer "yes" to confirmation prompts (for script use)

1.2 usage

语法:

i2cdetect [-y] [-a] [-q|-r] i2cbus [first last]

1.3 Example

查看当前所有i2c总线:

i2cdetect -l
i2c-3   i2c             qt-i2c                                  I2C Adapter
i2c-10  i2c             qt-i2c                                  I2C Adapter
i2c-1   i2c             qt-i2c                                  I2C Adapter
i2c-8   i2c             qt-i2c                                  I2C Adapter
i2c-6   i2c             qt-i2c                                  I2C Adapter
i2c-4   i2c             qt-i2c                                  I2C Adapter
i2c-11  i2c             qt-i2c                                  I2C Adapter
i2c-2   i2c             qt-i2c                                  I2C Adapter
i2c-0   i2c             qt-i2c                                  I2C Adapter
i2c-9   i2c             qt-i2c                                  I2C Adapter
i2c-7   i2c             qt-i2c                                  I2C Adapter
i2c-5   i2c             qt-i2c                                  I2C Adapter

返回值说明:
​
‘–’:表示该地址被检测,但没有芯片应答;
​
‘UU’:表示该地址当前由内核驱动程序使用;
​
‘**’:**表示以十六进制表示的设备地址编号.
扫描bus设备:
扫描i2cbus 0上的设备
i2cdetect -y  0

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

二、i2cget命令

2.1 description

usage: i2cget [-fy] BUS CHIP ADDR

Read an i2c register.

-f      Force access to busy devices
-y      Answer "yes" to confirmation prompts (for script use)

2.2 usage

i2cget -f -y

b (read byte data, default)
w (read word data)
i (read I2C block data)

语法:

i2cget [-f] [-y] i2cbus chip-address [data-address [mode]]

2.3 Example

读取i2c 0总线上 从设备是0x68地址 的0x00寄存器的内容

i2cget  -f -y 0 0x68 0x00
0x51

or sometimes :

i2cget -f -y 2 0x1a 0x0 bs

三、i2cset命令

3.1 description

usage: i2cset [-fy] BUS CHIP ADDR VALUE... MODE

Write an i2c register. MODE is b for byte, w for 16-bit word, i for I2C block.

-f      Force access to busy devices
-y      Answer "yes" to confirmation prompts (for script use)

3.2 usage

语法:

i2cset [-f] [-y] [-m mask] [-r] i2cbus chip-address data-address [value] … [mode]

3.3 Example

将i2c 2总线上 0x1a从设备 0x0寄存器的值设置为0xff

i2cset -f -y 2 0x1a 0x0 0xff b

四、i2cdump命令

4.1 description

usage: i2cdump [-fy] BUS CHIP

Dump i2c registers.

-f      Force access to busy devices
-y      Answer "yes" to confirmation prompts (for script use)

4.2 usage

语法:

i2cdump [-f] [-r first-last] [-y] i2cbus chip-address [mode [bank [bankreg]]]
参数y:关闭人机交互模式;

4.3 Example

读取bus 0总线上0x1d 从设备上的全部寄存器的值

i2cdump -y 0 0x1d

五、i2ctransfer

有时需要传输多位数据,可以用到此命令

i2ctransfer -y -f 1 w3@0x51 $reg_addr $data_high $data_low

i2ctransfer 命令执行了一个 I2C 传输操作(总线为i2c bus 3),向地址为 0x51 的设备发送了一个写入操作。w3 表示写入 3 个数据字节。第一个字节是寄存器地址 reg_addr,第二个字节是高位数据 data_high,第三个字节是低位数据 data_low.

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a2591748032-随心所记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值