The phyCORE-i.MX7 SOM supports up to 4x I2C interfaces. This guide provides information on how to view, access, and interact with the I2C interfaces on the phyBOARD-i.MX7 development kit.

I2C devices

  • The 4x I2C interfaces will show up in Linux as the corresponding devices: 

    Hardware Interfacesysfs Path
    I2C1/dev/i2c-0
    I2C2/dev/i2c-1
    I2C3/dev/i2c-2
    I2C4/dev/i2c-3
  • To see what I2C devices are enabled on your phyCORE-i.MX7, run the following command:

    Target (Linux)

    ls /dev/i2c*
    CODE
  • You should see some device files, one of which (device /dev/i2c-0) corresponds to the I2C1 interface on the phyCORE-i.MX7:

    The I2C3 is disabled by default and it is normal to have the device i2c-2 unlisted in sysfs.

I2C1 Devices

  • The I2C1 interface is heavily utilized on the phyCORE-i.MX7 development kit. Devices onboard the SOM such as EEPROM, RTC and PMIC are all connected on this bus. In addition, I2C1 is routed out to the Expansion Board with default jumper configurations set. 
  • Use i2cdetect from Linux to scan the bus for devices:

    Target (Linux)

    i2cdetect -y -r 0
    CODE
  • This command outputs the address of all devices on the I2C1 bus. You should see something similar to the output below: 

    Expected Output

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- UU -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: UU -- -- -- -- -- -- -- 58 -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
    CODE
    • UU indicates that the device connected is tied to a driver. You will be unable to talk to this device via i2c command (i2cset and i2cget).
    • The EEPROM M24C32 has two different addresses (0x50 and 0x58) but one has been reserved in the device tree. 
  • These detected interfaces match with the devices connected to I2C1 on the development kit. 

    InterfaceAddressDescription
    PMIC0x08Power Management IC
    EEPROM0x50

    4kB EEPROM on SOM

    RTC0x68External RTC on SOM

Sensors

This portion of the guide will walk you through the use of a I2C capable sensor. To follow this guide exactly, you will need the SparkFun RedBot Sensor - Accelerometer.


SparkFun RedBot Sensor - Accelerometer

Circuit Diagram

Setup

  • If your accelerometer didn't come with header pins soldered onto it, you will first need to populate these.
  • With your phyCORE-i.MX7 powered off and the power supply removed, connect the sensor to the X11 connector on the PEB-D-RPI Expansion Board by following the circuit diagram above.
  • Power on the phyCORE-i.MX7 and login to Linux as root.
  • In order to find the address of this device, which we connected to the I2C1 bus, enter the following command: 

    Target (Linux)

    i2cdetect -y -r 0
    CODE

    Expected Output

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- UU -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: UU -- -- -- -- -- -- -- 58 -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
    CODE
  • We can see that a new device appeared at the address 0x1d. Remember this address.

  • Open a text editor to write a script: 

    Target (Linux)

    vi ~/bumpDetect.sh
    CODE
  • Enter the following and save the file: 

    Vi Text Editor

    #!/bin/bash
    
    echo Input Sparkfun RedBot-Accelerometer bus:
    read -r bus
    echo Input Sparkfun RedBot-Accelerometer address:
    read -r addy
    
    i2cset -y "$bus" "$addy" 0x2B 0x40		#Reset the accelerometer
    i2cset -y "$bus" "$addy" 0x0E 0x02		#Set dynamic range to 8g from default 2g
    i2cset -y "$bus" "$addy" 0x2A 0x05		#Enable the device
    
    #Constantly check if there is any change in acceleration in the Z axis
    state=$(i2cget -y "$bus" "$addy" 0x05)
    
    while true; do
            temp=$(i2cget -y "$bus" "$addy" 0x05)
            if [ "$state" != "$temp" ];
            then
                    echo Bump!
                    sleep 0.2
                    state=$(i2cget -y "$bus" "$addy" 0x05)
            fi
    done
    CODE

    The vi text editor begins in "Command Mode" and you must first hit the 'i' key in order to enter "Insert Mode". Using the arrow keys to navigate, make the necessary changes and then hit ESC to go back to "Command mode". Now enter ":wq" to write the file and quit.


    Pro Tip: Use the right click on your mouse to paste. This will only work if you are in "Insert Mode" first.

  • Change the permissions in order to execute the script: 

    Target (Linux)

    chmod +x ~/bumpDetect.sh
    CODE
  • Now run the script:

    Target (Linux)

    ~/bumpDetect.sh
    CODE
  • When prompted, enter the bus you connected the device to and the address we found earlier. Both must be given in hexidecimal form! 

    Example Input

    root@imx7d-phyboard-zeta-004:~# ~/bumpDetect.sh
    Input Sparkfun RedBot-Accelerometer bus:
    0x00
    Input Sparkfun RedBot-Accelerometer address:
    0x1d
    CODE
  • Now try tapping or bumping the table

    Because the accelerometer is very sensitive, slight vibrations or running machines nearby can cause the script to register a bump when there isn't one. If the script keeps outputting "Bump!" without you tapping the table, ensure that you are using a still/quiet environment and try taping the sensor directly to the table to prevent any tension in the jumper cables from interfering with readings. 

  • Press Ctrl + C to quit the process.