实际项目的调试中,往往需要快速修改驱动芯片(只针对IIC通讯)中对应寄存器的值,传统的方式一般是编译驱动 -> 烧录固件 -> 测试,而这样的方式往往很繁琐。这里介绍使用i2c-tools快速修改驱动芯片的寄存器方式1
使用工具的前提:
本文以TI音频芯片TAS5754驱动为例 , 内核版本为: Linux buildroot 4.9.68 , 前一篇文章[Linux驱动炼成记] 04-功放TAS5754,已经说了一些设备地址的配置,参数等等。
i2c-tools这个工具的源码是开放的,可以修改源码包中的Makefile编译成不通平台的可执行文件, 下载地址: 这里
这里为了快速构建直接使用buildroot 中自带的/buildroot/package/i2c-tools
编译
罗列出设备上共有的i2c总线
# i2cdetect -l
i2c-1 i2c Meson I2C adapter I2C adapter
i2c-0 i2c Meson I2C adapter I2C adapter
查看i2c0上挂载的设备
# i2cdetect -r -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- UU --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
参数含义:
-y Disable interactive(交互) mode
-r Use smbus read byte commands for probing
可以看到i2c0总线上上挂载了一个从机地址为0x4e设备,正好对应内核dts中
tas575x_37: tas575x_37@37 {
compatible = "ti,tas575x"
reg = <0x4e>; 对应这里的设备地址
status = "okay";
mute_pin = <&gpio_ao GPIOAO_3 0>;
mic_pin = <&gpio_ao GPIOAO_6 0>;
};
继续看一下i2c1总线上挂载的设备:
# i2cdetect -r -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
可以看到i2c1总线上的设备地址为0x44,正好对应dts中isl29018光感的配置
isl29018@44 {
compatible = "isil,isl29035";
reg = <0x44>;
//interrupt-parent = <&gpio>;
//interrupts = <TEGRA_GPIO(A, 20) IRQ_TYPE_LEVEL_LOW>;
};
罗列出设备上寄存器的值
# i2cdump -f -y 0 0x4e
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 00 00 00 00 01 11 00 00 00 00 00 01 7c 00 00 00 ....??.....?|...
10: 00 00 00 10 00 00 00 00 00 00 80 00 00 00 00 04 ...?......?....?
20: 00 00 00 01 00 00 f3 04 00 00 22 01 00 00 00 00 ...?..??.."?....
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 44 44 22 .............ll"
40: 02 04 14 05 00 00 00 00 55 00 00 00 00 00 00 00 ????....U.......
50: 00 00 00 00 00 00 00 00 81 11 00 36 00 40 00 00 ........??.6.@..
60: 01 10 00 00 00 01 0f 03 07 10 ff d3 33 00 02 02 ??...?????.?3.??
70: 81 81 03 00 04 00 85 21 00 00 00 00 00 00 00 00 ???.?.?!........
80: 00 00 00 00 01 11 00 00 00 00 00 01 7c 00 00 00 ....??.....?|...
90: 00 00 00 10 00 00 00 00 00 00 80 00 00 00 00 04 ...?......?....?
a0: 00 00 00 01 00 00 f3 04 00 00 22 01 00 00 00 00 ...?..??.."?....
b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 6c 6c 22 .............ll"
c0: 02 04 14 05 00 00 00 00 55 00 00 00 00 00 00 00 ????....U.......
d0: 00 00 00 00 00 00 00 00 81 11 00 36 00 40 00 00 ........??.6.@..
e0: 01 10 00 00 00 01 0f 03 07 10 ff d3 33 00 02 02 ??...?????.?3.??
f0: 81 81 03 00 04 00 85 21 00 00 00 00 00 00 00 00 ???.?.?!........
参数含义
-f Force access
-y Disable interactive mode
使用i2cdump就可以罗列处所有寄存器的值,这里以0x3d 0x3e为例,可以看出0x3d和0x3e寄存器对应的值为0x44, 然后我们去驱动中看一下是否可以对应上:
#define TAS575X_CH_B_DIG_VOL 61 -> 十六进制 0x3d
#define TAS575X_CH_A_DIG_VOL 62 -> 十六进制 0x3e
static int tas575x_probe(struct snd_soc_codec *codec)
{
/* set stanby mode */
snd_soc_write(codec, TAS575X_STANDBY, 0x10);
/* reset */
snd_soc_write(codec, TAS575X_RESET, 0x01);
/* set for DAC path */
snd_soc_write(codec, TAS575X_DATA_PATH, 0x22);
//0x3d,0x3e对应的就是这里,可以发现驱动中就是写的0x44的值
snd_soc_write(codec, TAS575X_CH_B_DIG_VOL, 0x44);
snd_soc_write(codec, TAS575X_CH_A_DIG_VOL, 0x44);
/* exit stanby mode */
snd_soc_write(codec, TAS575X_STANDBY, 0x0);
return 0;
}
修改寄存器的值
# i2cset -y <bus> <device> <register> <value>
devices : 代表设备的从机地址
# i2cset -y 0 0x4e 0x3d 0x6c
i2cset: can't set address to 0x4e: Device or resource busy
提示设备处于忙状态,这个时候需要添加-f参数,强制修改
# i2cset -y -f 0 0x4e 0x3d 0x6c
可以看到0x3d值的已经变为6c
# i2cdump -y -f 0 0x4e
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 00 00 00 00 01 11 00 00 00 00 00 01 7c 00 00 00 ....??.....?|...
10: 00 00 00 10 00 00 00 00 00 00 80 00 00 00 00 04 ...?......?....?
20: 00 00 00 01 00 00 f3 04 00 00 22 01 00 00 00 00 ...?..??.."?....
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 6c 44 22 .............lD"
使用过程中有疑问的可以留言或关注我的公众号程序手艺人留言