linux之模拟i2c,i2c-algo-bit

linux 硬件i2c驱动已经很完善了,但是还是有需要模拟i2c的情况。例如:
1、mt7688平台,硬件i2c限制每次只能读取64 bytes。在读取一些大数据的时候,只能多次读取,如此就会显得有些不足。
2、硬件i2c引脚冲突,导致只能用软件模拟

  • 模拟i2c主要需要一下模块
    i2c-core-base
    i2c-gpio
    i2c-algo-bit
    i2c-dev

  • make menuconfig,开启I2C bit-banging interfaces

 .config - Linux/mips 4.14.195 Kernel Configuration
 > Device Drivers > I2C support > I2C Algorithms ─────────────────────────────
  ┌──────────────────────────── I2C Algorithms ────────────────────────────┐
  │  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or      │  
  │  empty submenus ----).  Highlighted letters are hotkeys.  Pressing <Y> │  
  │  includes, <N> excludes, <M> modularizes features.  Press <Esc><Esc>   │  
  │  to exit, <?> for Help, </> for Search.  Legend: [*] built-in  [ ]     │  
  │ ┌────────────────────────────────────────────────────────────────────┐ │  
  │ │    <*> I2C bit-banging interfaces                                  │ │  
  │ │    < > I2C PCF 8584 interfaces                                     │ │  
  │ │    < > I2C PCA 9564 interfaces                                     │ │  
  │ │                                                                    │ │  
  │ │                                                                    │ │  
  │ │                                                                    │ │  
  │ │                                                                    │ │  
  │ │                                                                    │ │  
  │ └────────────────────────────────────────────────────────────────────┘ │  
  ├────────────────────────────────────────────────────────────────────────┤  
  │        <Select>    < Exit >    < Help >    < Save >    < Load >        │  
  └────────────────────────────────────────────────────────────────────────┘  
    
  • 修改设备树,根节点添加如下
	i2c_gpio0: i2c-gpio-0 {
		compatible = "i2c-gpio";
		
		sda-gpios = <&gpio0 5 GPIO_ACTIVE_HIGH>; /* sda */
		scl-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>; /* scl */

		/* can be removed on 4.19 */
		gpios = <&gpio0 5 GPIO_ACTIVE_HIGH /* sda */
				&gpio0 4 GPIO_ACTIVE_HIGH /* scl */
			>;

		// i2c-gpio,sda-open-drain;
		// i2c-gpio,scl-open-drain;
		i2c-gpio,delay-us = <5>;	/* ~100 kHz */
		status = "okay";

		td1030@30 {
			compatible = "td1030";
			reg = <0x30>;
			status = "okay";
		};
	};
通常,sda-open-drain不需要打开,但如果该gpio为开漏模式,则一定要打开才可以正常读取数据。
  • 3、关闭硬件i2c
&i2c {
	status = "disabled"; //okay
	clock-frequency = <100000>;	//100k

	td1030@30 {
		compatible = "td1030";
		reg = <0x30>;
		status = "okay";
	};
};
  • 以上设置好后,内核启动后,可以看对对应的挂载才可以
root@eric:/# dmesg | grep i2c*
[   10.986205] i2c /dev entries driver
[   11.041691] i2c-gpio i2c-gpio-0: using pins 5 (SDA) and 4 (SCL)

  • 查看已经挂载的总线和设备
root@eric:/# ls dev/i2c*
dev/i2c-0
root@eric:/#
root@eric:/#
root@eric:/# ls /sys/bus/i2c/devices/
0-0030  i2c-0
root@eric:/# 
  • i2c-tool 测试,列举总线上挂载的设备
root@eric:/# i2cdetect -l
i2c-0   i2c             i2c-gpio-0                              I2C adapter
root@eric:/# i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: 30 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
root@eric:/#

  • 读取0x30设备所有寄存器的值
root@eric:/# i2cdump -f -y 0 0x30
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
10: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
20: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
30: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
40: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
50: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
80: 00 4d 24 ff ff ff ff ff ff ff ff ff ff ff ff ff    .M$.............
90: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
a0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
b0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
c0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
d0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
  • 读、写指定寄存器的值
root@eric:/# i2cget -f -y 0 0x30 0x55
0xff
root@eric:/#
root@eric:/# i2cset -f -y 0 0x30 0x00 0x23
root@eric:/#

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值