Linux I2C调试工具

Linux I2C tools调试工具

1.源码下载和安装

1.下载

Linux I2C调试工具_第1张图片

2.交叉编译

一般大家都是在开发板上使用i2c-tools工具,简单又方便能够快速的定位问题

1.解压文件

首先先把文件解压出来

tar -vxf i2c-tools-4.3.tar.gz

2.创建编译文件夹

为了不干扰源代码,我们需要创建一个新的文件夹来存放编译出来的工具,

mkdir build

3.修改Makefile

makefile是最重要的文件,我们需要修改交叉编译器和存放编译的目标文件的文件夹

我们需要修改三个地方PREFIX、CC和AR三个变量的参数,PREFIX修改成刚才创建的目标文件夹的绝对路径

CC修改成开发板的交叉编译工具的绝对路径,AR也是如此

# I2C tools for Linux
#
# Copyright (C) 2007-2012  Jean Delvare 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

DESTDIR ?=
PREFIX  = /tool/i2c-tools-4.3/build
bindir  = $(PREFIX)/bin
sbindir = $(PREFIX)/sbin
mandir  = $(PREFIX)/share/man
man3dir = $(mandir)/man3
man8dir = $(mandir)/man8
incdir  = $(PREFIX)/include
libdir  = $(PREFIX)/lib

INSTALL         := install
INSTALL_DATA    := $(INSTALL) -m 644
INSTALL_DIR     := $(INSTALL) -m 755 -d
INSTALL_PROGRAM := $(INSTALL) -m 755
LN              := ln -sf
RM              := rm -f

CC      = /RV1109/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
AR      = /RV1109/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-ar
STRIP   ?= strip

CFLAGS          ?= -O2
# When debugging, use the following instead
#CFLAGS         := -O -g
CFLAGS          += -Wall
SOCFLAGS        := -fpic -D_REENTRANT $(CFLAGS)

BUILD_DYNAMIC_LIB ?= 1
BUILD_STATIC_LIB ?= 1
USE_STATIC_LIB ?= 0

ifeq ($(USE_STATIC_LIB),1)
BUILD_STATIC_LIB := 1
endif

ifeq ($(BUILD_DYNAMIC_LIB),0)
ifeq ($(BUILD_STATIC_LIB),0)
$(error BUILD_DYNAMIC_LIB and BUILD_STATIC_LIB cannot be disabled at the same time)
else
USE_STATIC_LIB := 1
endif
endif

KERNELVERSION   := $(shell uname -r)

.PHONY: all strip clean install uninstall

all:

EXTRA   :=
#EXTRA  += eeprog py-smbus
SRCDIRS := include lib eeprom stub tools $(EXTRA)
include $(SRCDIRS:%=%/Module.mk)

4.编译

在根目录下执行make然后再执行make install,工具就会自动的复制到之前创建的文件下

2.i2c-tools 使用方法

1.i2cdetect

i2cdetect用来搜索i2c某个总线下挂载多少个设备,当你调试新的i2c设备的时候就可以检查硬件是否连接好

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.

i2cdetect -y i2c_bus	搜索i2c_bus下的所有设备
i2cdetect -F i2c_bus	打印当前i2c_bus支持的function
[root@RV1126_RV1109:~]# 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                 no
                        SMBus Block Process Call         no
                        SMBus PEC                        yes
                        I2C Block Write                  yes
                        I2C Block Read                   yes
i2cdetect -l			打印当前设备一共有几组i2c_bus

2.i2cdump

Usage: i2cdump [-f] [-y] [-r first-last] I2CBUS ADDRESS [MODE [BANK [BANKREG]]]
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x03 - 0x77)
  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
    
i2cdump -f -y i2c_bus device_address [mode] 打印当前i2c设备寄存器的值

3.i2cget

Usage: i2cget [-f] [-y] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x03 - 0x77)
  MODE is one of:
    b (read byte data, default)
    w (read word data)
    c (write byte/read byte)
    Append p for SMBus PEC

i2cget -f -y i2c_bus chip_address data_address 	读取i2c设备某个寄存器的值

4.i2cset

Usage: i2cset [-f] [-y] [-m MASK] [-r] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]
  I2CBUS is an integer or an I2C bus name
  ADDRESS is an integer (0x03 - 0x77)
  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
    
   i2cset -f -y 3 0x37 0xfe 0x01 写i2c设备的0xfe地址的值

5.i2ctransfer

Usage: i2ctransfer [-f] [-y] [-v] [-V] 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-

读取i2c2 addres0x14  寄存器0x8047 两个字节
i2ctransfer -f -y 2 w2@0x14 0x80 0x47 r2

往i2c2 addres0x14  寄存器0x8047 写一个字节0x01
i2ctransfer -f -y 2 w3@0x14 0x80 0x47 0x01

你可能感兴趣的:(linux,kernel,linux,运维,服务器)