zl程序教程

您现在的位置是:首页 >  其他

当前栏目

14——使用GPIO操作I2C设备_IMX6ULL

2023-04-18 14:42:18 时间

资料下载

coding无法使用浏览器打开,必须用git工具下载:

git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git

视频观看

百问网驱动大全

使用GPIO操作I2C设备_IMX6ULL

参考资料:

  • i2c_spec.pdf
  • Linux文档
    • Linux-5.4Documentationdevicetreeindingsi2ci2c-gpio.yaml
    • Linux-4.9.88Documentationdevicetreeindingsi2ci2c-gpio.txt
  • Linux驱动源码
    • Linux-5.4driversi2cussesi2c-gpio.c
    • Linux-4.9.88driversi2cussesi2c-gpio.c
  • 扩展板原理图:imx6ull_extend_v10.pdf
  • 本节对应的代码:
    • doc_and_source_for_driversIMX6ULLsource4_I2C7_i2c_gpio_dts_imx6ull

1. 硬件连接

  • IMX6ULL:把I2C模块接到GPIO

2. 根据原理图编写设备树

2.1 原理图

2.2 编写设备树

i2c_gpio_100ask {
	compatible = "i2c-gpio";
	gpios = <&gpio4 20 0 /* sda */
		     &gpio4 21 0 /* scl */
		    >;
	i2c-gpio,delay-us = <5>;	/* ~100 kHz */
	#address-cells = <1>;
	#size-cells = <0>;
};

把上述代码,放入arch/arm/boot/dts/100ask_imx6ull-14x14.dts的根节点下面。

3. 确认内核已经配置了I2C-GPIO

查看内核目录下的.config,如果未设置CONFIG_I2C_GPIO,上机实验时需要配置内核、编译I2C-GPIO驱动。

4. 上机实验

4.1 设置工具链

4.2 编译、替换设备树

4.3 编译I2C-GPIO驱动

1. 配置内核

在IMX6ULL内核源码目录下执行make menuconfig命令,如下配置内核:

Device Drivers  --->
    I2C support  --->
        I2C Hardware Bus support  --->
            <M> GPIO-based bitbanging I2C      // 输入M,编译为模块        
2. 编译模块

设置工具链后,在内核目录下执行:

make modules   // 得到 drivers/i2c/busses/i2c-gpio.ko

5. 测试

在开发板上执行:

[root@100ask:~]# i2cdetect -l    // 加载i2c-gpio.ko前只看到2条I2C BUS
i2c-1   i2c             21a4000.i2c                             I2C adapter
i2c-0   i2c             21a0000.i2c                             I2C adapter
[root@100ask:~]#
[root@100ask:~]# insmod /mnt/i2c-gpio.ko   
[   45.067602] i2c-gpio i2c_gpio_100ask: using pins 116 (SDA) and 117 (SCL)
[root@100ask:~]# i2cdetect -l     // 加载i2c-gpio.ko后看到3条I2C BUS
i2c-1   i2c             21a4000.i2c                             I2C adapter
i2c-4   i2c             i2c_gpio_100ask                         I2C adapter
i2c-0   i2c             21a0000.i2c                             I2C adapter
[root@100ask:~]#
[root@100ask:~]# i2cdetect -y 4     // 检测到0x50的设备
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
[root@100ask:~]#
[root@100ask:~]# i2cset -f -y 4 0x50 0 0x55   // 往0地址写入0x55
[root@100ask:~]# i2cget -f -y 4 0x50 0        // 读0地址
0x55