Use HiSilicon’s built-in i2c tool to access i2c devices

1 Introduction

  The development of i2c device driver under linux is generally divided into two stages. The first stage is driver implementation, and the second stage is debugging. The driver implementation is the correct access to the i2c device, including reading, writing, and control. The second stage is generally used for i2c devices that need to adjust parameters, such as gain, brightness, grayscale, compensation coefficient, etc.; because this type of parameter needs to be associated with the specific application, it needs to be adjusted in real time to meet the best state What effect. For this scenario, if you frequently modify the driver source code to adjust the parameters, it is obviously not an ideal way, the work is tedious and the efficiency is low. At this time, generally use the third-party i2c tool to adjust the parameters through the command terminal until a suitable range or value is confirmed before modifying the parameters of the driver source code. The commonly used third-party i2c tool is i2ctools, which is powerful, easy to transplant, and easy to use. For the use of i2ctools, refer to this article .

  Hisilicon SDK provides an i2c read and write tool, which can easily read and write i2c device registers through the command terminal. Although the i2c tool is not as functional as i2ctools, it is adequate for real-time access to i2c device registers.


2 Use of HiSilicon i2c tools

  The HiSilicon i2c tool only provides two commands, namely read data i2c_readand write data i2c_write.


2.1 i2c_read

Command format:

i2c_read <i2c_num> <device_addr> <reg_addr> <end_reg_addr>  
                   <reg_width> <data_width> <reg_step>  
  • i2c_num, I2c serial number, just fill in numbers, such as 0, 1, 2
  • device_addr, Device address
  • red_addr, The starting register address of the device to be read
  • end_reg_addr, The end address of the device register to be read
  • reg_width, Device register bit width, Byte(1)/Word(2)
  • data_width, Device data bit width, Byte(1)/Word(2)
  • reg_step, Continuous reading mark, 1 means continuous reading, 0 means non-continuous reading, the default is continuous reading mode

2.1.1 Examples

Read a single register

  • i2c bus serial number: i2c-0
  • Device address: 0x60
  • Start register: 0x0
  • End register: 0x0
  • Register bit width: 1Byte
  • Data bit width: 1Byte
  • Linkage mark: non-linkage
# i2c_read 0 0x60 0x0 0x0 0x1 0x1 0x0
*** Board tools : ver0.0.1_20121120 *** 
[debug]: {source/utils/cmdshell.c:167}cmdstr:i2c_read
i2c_num:0x0, dev_addr:0x61; reg_addr:0x 0; reg_addr_end:0x 0; reg_width: 1; data_width: 1; reg_step:0x 1. 

0x0 0xff
[END]

Continuous reading

  • i2c bus serial number: i2c-0
  • Device address: 0x60
  • Start register: 0x0
  • End register: 0x5
  • Register bit width: 1Byte
  • Data bit width: 1Byte
  • Linkage mark: Linkage
# i2c_read 0 0x60 0x0 0x5 0x1 0x1
*** Board tools : ver0.0.1_20121120 *** 
[debug]: {source/utils/cmdshell.c:167}cmdstr:i2c_read
i2c_num:0x0, dev_addr:0x61; reg_addr:0x 0; reg_addr_end:0x 5; reg_width: 1; data_width: 1; reg_step:0x 1. 

0x0 0xff
0x1 0x8
0x2 0x8
0x3 0x8
0x4 0x8
0x5 0x1f
[END]

Incoming wrong device address

# i2c_read 0 0x10 0x0 0x0 0x1 0x1
*** Board tools : ver0.0.1_20121120 *** hi_i2c_wait_rxfifo_notempty->314: 
[debug]: {source/utils/cmdshell.c:167}
cmdstr:i2c_read
i2c_num:0x0, dev_addr:0abort! int_raw_status: 0x750!
x10; reg_addr:0x 0; reg_addr_end:0x 0; r
eg_width: 1; data_width: 1; reg_step:0x hi_i2c_abortprocess->86: 1. 

tx_abrt_src is 1.

CMD_I2C_READ error!

do errro
[END]

2.2 i2c_write

Command format:

i2c_write <i2c_num> <device_addr> <reg_addr> <value> 
				     <reg_width> <data_width>
  • i2c_num, I2c serial number, just fill in numbers, such as 0, 1, 2
  • device_addr, Device address
  • red_addr, The device register address to be written
  • value, The data value to be written
  • reg_width, Device register bit width, Byte(1)/Word(2)
  • data_width, Device data bit width, Byte(1)/Word(2)

2.2.1 Examples

  • i2c bus serial number: i2c-0
  • Device address: 0x60
  • Register to be written: 0x0
  • Data value to be written: 0x55
  • Register bit width: 1Byte
  • Data bit width: 1Byte
# i2c_write 0 0x61 0x0 0x55 0x1 x1
*** Board tools : ver0.0.1_20121120 *** 
[debug]: {source/utils/cmdshell.c:167}cmdstr:i2c_write
dev_addr:0x61; reg_addr:0x 0; reg_value:0x55; reg_width: 1; data_width: 1.
[END]

# i2c_read 0 0x60 0x0 0x0 0x1 0x1
*** Board tools : ver0.0.1_20121120 *** 
[debug]: {source/utils/cmdshell.c:167}cmdstr:i2c_read
i2c_num:0x0, dev_addr:0x60; reg_addr:0x 0; reg_addr_end:0x 0; reg_width: 1; data_width: 1; reg_step:0x 1. 

0x0 0x55
[END]

Guess you like

Origin blog.csdn.net/qq_20553613/article/details/106557221
I2C