Installation and use of i2c debugging artifact i2c-tools under linux

Introduction to i2c-tools

In embedded linux development, sometimes it is necessary to confirm whether the i2c hardware is connected normally, whether the device is working normally, what is the address of the device, etc., here we need to use a tool for testing the I2C bus - i2c-tools.

i2c-tools is an open source tool for debugging i2c. It can obtain the mounted device and device address, and can also read and write I2C device registers. When debugging a new device driver, it is inevitable to modify the registers repeatedly, and then see the result. The traditional method is to modify the driver code register value -> compile -> download -> run -> see the result. This process is time-consuming, and only one bit needs to be changed each time. In this case, i2c-tools is simply a magic tool.

Below we will give a brief introduction to the installation and use of this tool.

i2c-tools official description:

https://i2c.wiki.kernel.org/index.php/I2C_Tools

Tool source code download address:

https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/

i2c-tools tools include:

i2cdetect : used to scan devices on the i2c bus and display addresses.
i2cset : Set the value of a register of the i2c device.
i2cget : Read the value of a register of the i2c device.
i2cdump : Read the value of all registers of an i2c device.
i2ctransfer : Read and write multiple bytes at once.

工具命令介绍:
    i2cdetect:检测i2c芯片
    i2cdump:查看寄存器值
    i2cget:获取单个寄存器值(8位寄存器)
    i2cset:设置单个寄存器值(8位寄存器)
    i2ctransfer:一次传输多字节数据(16位寄存器)
参数介绍:
    -y:禁用交互模式。
    -f:强制访问设备。
    -r:写入后立即读回该值。

Note: The principle of i2c-tools is completed by operating the i2c-× device file in the /dev path, so your kernel must enable the CONFIG_I2C_CHARDEV configuration, otherwise it will report that the node cannot be found.  

On the device, you can also enter through the terminal of the target board: ls /sys/bus/i2c/devices查看i2c设备节点信息。看到的i2c-0, i2c-1 refer to the i2c bus node, and the i2c slave devices are all mounted under the bus node.

tool compilation

Tool source code, use the following command to compile

make CC=arm-linux-gnueabihf-gcc USE_STATIC_LIB=1

USE_STATIC_LIB means to use static compilation. After the compilation is completed, five products i2cdetect, i2cdump, i2cget, i2cset and i2ctransfer will be generated in the tools directory. Copy these executables to the device.

Without the USE_STATIC_LIB compilation option, it will be compiled using dynamic linking. After compiling, you need to copy the libi2c.so.0 dynamic library in the i2c-tools-4.3/lib directory to the /usr/bin directory on the device. Compilation will generate five tools: i2cdetect, i2cset, i2cget, i2cdump, i2ctransfer, which can be used after copying to the development board.

How to use i2c-tools

i2cdetect: Query the i2c bus and peripherals contained in the development board, see the following command:

i2cdetect -l

Probe the i2c bus included with the development board.

i2cdetect -y -r 0

Detect the peripherals mounted on the i2c-0 bus. The parameter -y means cancel the interactive mode and execute the command directly.

i2cdetect -y -r 1

Detect the peripherals mounted on the i2c-1 bus. UU indicates that the device has been occupied by the Linux kernel driver, and the detection program will skip the device. As shown in the figure below, the slave device addresses are 0x0c and 0x1a respectively. UU indicates that the slave device of the device address is occupied by the driver. After the corresponding driver is uninstalled, UU will become data. -- Indicates that the address was detected, but not answered.

 i2cset: Set the value of a single register of the i2c device, see a few commands:

i2cset -y -f 0 0x57 0x00 0x10
i2cset -y -f 0 0x57 0x01 0x11
i2cset -y -f 0 0x57 0x02 0x12
i2cset -y -f 0 0x57 0x03 0x13

The following takes i2cset -y -f 0 0x57 0x00 0x10 as an example for analysis.
Parameter meanings:
-y cancels the interactive mode and directly executes the command
-f forces access to the device.
0 indicates the i2c bus number.
0x57 indicates the i2c device address . 0x00 indicates
the register address.
The written value
i2cget: View the value of a single register of the i2c device, and look at several commands:

i2cget -y -f 0 0x57 0x00

Take i2cget -y -f 0 0x57 0x00 as an example for analysis, parameter meanings:
-y cancels the interactive mode, directly executes the command
-f forces access to the device
0 indicates the i2c bus number
0x57 indicates the i2c device address
0x00 indicates the register address

 i2cdump: View the values ​​of all registers of the i2c device, see the following command:

i2cdump -y -f 1 0x32

Parameter meaning:
-y cancels the interactive process, directly executes the command
-f forces access to the device
1 indicates the i2c bus number
0x32 indicates the i2c device address

other resources

Linux kernel i2c-tools command

A brief introduction to the installation and use of i2c-tools tools - Electronic Paper

https://www.cnblogs.com/ITY996/p/16894380.html

https://www.cnblogs.com/liuwanpeng/p/7346558.html

Use of I2C-tools tools under Linux_i2cdetect_Deep Sea Belt Carp Blog-CSDN Blog 

Guess you like

Origin blog.csdn.net/qq8864/article/details/131953108