Accessing I2C Devices in Linux Print


The Linux kernel provides a device driver for the I2C controller of the i.MX RT1170, enabled in the kernel with the CONFIG_I2C_IMX_LPI2C build-time option. Another kernel configuration option that you will require is CONFIG_I2C_CHARDEV. This option enables the kernel API that allows accessing I2C devices from user-space application code. These kernel configuration options are enabled by default in the rootfs project.

The i.MX RT1170 includes six I2C bus controllers. The parameters of each bus controller are specified in the lpi2c* child nodes properties in the linux/arch/arm/boot/dts/imxrt1170.dtsi file:

..... lpi2c5: lpci2c@40c34000 { compatible = "fsl,imx7ulp-lpi2c"; reg = <0x40c34000 0x4000>; interrupts = <36>; clocks = <&clks IMXRT1170_CLK_LPI2C5>; #address-cells = <1>; #size-cells = <0>; status = "disabled"; }; .....

Assignment of the I2C signals to specific pins is described in the pinctrl_lpi2c* child nodes of the iomuxc_lpsr node in projects/rootfs/rootfs.dts.IMXRT117X_NXPEVK:

& iomuxc_lpsr { ... pinctrl_lpi2c5: lpi2cgrp5 { fsl,pins = < IOMUXC_GPIO_LPSR_05_LPI2C5_SCL (IMX_PAD_SION | \ MXRT1170_PAD_DSE | \ MXRT1170_PAD_PUE | \ MXRT1170_PAD_PUS | \ MXRT1170_PAD_SRE) IOMUXC_GPIO_LPSR_04_LPI2C5_SDA (IMX_PAD_SION | \ MXRT1170_PAD_DSE | \ MXRT1170_PAD_PUE | \ MXRT1170_PAD_PUS | \ MXRT1170_PAD_SRE) >; }; ... };

Finally the rootfs.dts.IMXRT117X_NXPEVK file defines the I2C interface reference with additional properties which enable the appropriate I2C bus controller, connect it to the pins specified:

&lpi2c5 { pinctrl-names = "default"; pinctrl-0 = <&pinctrl_lpi2c5>; status = "okay"; ... };

The Emcraft uClinux distribution includes the Linux run-time tools that can be used to access I2C devices from user space. The following lines in the rootfs.initramfs file add these tools to the target file system:

file /usr/sbin/i2cdump ${INSTALL_ROOT}/A2F/root/usr/sbin/i2cdump 755 0 0 file /usr/sbin/i2cdetect ${INSTALL_ROOT}/A2F/root/usr/sbin/i2cdetect 755 0 0 file /usr/sbin/i2cget ${INSTALL_ROOT}/A2F/root/usr/sbin/i2cget 755 0 0 file /usr/sbin/i2cset ${INSTALL_ROOT}/A2F/root/usr/sbin/i2cset 755 0 0

The functionality described above is enabled by default in the rootfs project. So, just run the rootfs.uImage binary on your target and observe the kernel messages about the I2C bus being enabled:

i2c i2c-0: LPI2C adapter registered

Run the Linux I2C tools to examine I2C devices on your target. For instance, the following command scans the I2C5 bus interface and reports any devices it detects on the bus:

  • On imxrt1170-evk board
  • / # i2cdetect -y 0 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- 1a -- -- -- -- 1f 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --

  • On imxrt1170-evkb board
  • / # i2cdetect -y 0 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- 1a -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- 4a -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --

Refer to the documentation available from the Internet for detailed manuals on the Linux I2C tools, for instance: https://linuxhint.com/i2c-linux-utilities. Sometimes your I2C device is already supported in the Linux kernel, and you want to utilize it in some standard way. To provide such access to the I2C device you need:

  • Enable the appropriate I2C device driver in your Linux kernel configuration;
  • Add information about your I2C device into the appropriate i2c node reference in the rootfs.dts.IMXRT117X_NXPEVK file.

As an example, let's connect an AT24 EEPROM with address 0x58 to the I2C5 bus as follows:

  • SDA of EEPROM is connected to J10.18 on the i.MX RT1170 EVK or EVKB board;
  • SCL of EEPROM is connected to J10.20 on the i.MX RT1170 EVK or EVKB board;
  • VCC of EEPROM is connected to J10.16 on the i.MX RT1170 EVK or EVKB board;
  • GND of EEPROM is connected to J10.14 on the i.MX RT1170 EVK or EVKB board.

After connection provide user with a simple read/write interface to it:

  • Enable the EEPROM driver in the Linux kernel configuration (Device Drivers -> Misc devices -> EEPROM support -> I2C EEPROMs / RAMs / ROMs from most vendors):
  • $ make kmenuconfig

  • Add information about the AT24 EEPROM device into rootfs.dts.IMXRT117X_NXPEVK:
  • &lpi2c5 { ... eeprom0: eeprom0@58 { compatible = "at,24c256"; reg = <0x58>; status = "okay"; }; ... };

  • Build rootfs.uImage:
  • $ make

  • Run the resultant image on the target, and validate access to the EEPROM device via the standard interfaces defined by the kernel drivers:
  • / # echo "Some text written to EEPROM." > /sys/bus/i2c/devices/0-0058/eeprom / # head -n 1 /sys/bus/i2c/devices/0-0058/eeprom Some text written to EEPROM.