Linuxシミュレーションi2c、i2c-algo-bit

Linuxハードウェアi2cドライバーは非常に完全ですが、i2cをシミュレートする必要がある状況がまだあります。次に例を示し
ます。1。mt7688プラットフォームでは、ハードウェアi2c制限は毎回64バイトしか読み取ることができません。ビッグデータを読み取る場合、複数回しか読み取れないため、多少不十分です。
2.ソフトウェアでのみシミュレートできるハードウェアi2cピンの競合

  • i2cのシミュレーションには、主に次のモジュールが必要です
    。i2c-core-
    basei2c-
    gpio i2c-algo-bit
    i2c-dev

  • menuconfigを作成し、启I2Cビットバンギングインターフェイスを開きます

 .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-ツールテスト、バスにマウントされたデバイスのリスト
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:/#

おすすめ

転載: blog.csdn.net/pyt1234567890/article/details/112990777