I2C Multiple adapter confusion

Problem

A raspberry pi 3 running a Balena OS variant balenalib/rpi-raspbian generates two i2c adapters. When you use the i2c-tools package to analyze the bus via i2cdetect -y 1, you can find devices on port 1. If you then perform i2cdetect -y 3 on the 3rd port, the same devices appeared. Then running i2cdetect -y 1 on the 1st port again is slow and yields 0 devices. I am super confused, this does not occur on the official raspberry pi OS since there is only one i2c adapter on port 1 which is thebcm2835 interface.

Steps


FROM balenalib/rpi-raspbian
RUN apt-get update && apt-get install -y i2c-tools
RUN modprobe i2c-dev
# ...

Start the container.

ssh into the running container

i2cdetect -l
i2c-3   i2c             ffffffff.i2c                            I2C adapter
i2c-1   i2c             bcm2835 I2C adapter                     I2C adapter

When you specify port 1 first:

root@41e310d:/usr/src/app# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: 10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: 20 21 22 23 -- -- -- -- -- -- -- -- -- -- -- -- 
30: 30 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- 44 -- -- -- 48 -- -- -- -- -- -- -- 
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- 63 64 -- -- -- 68 -- 6a -- -- -- 6e -- 
70: -- -- -- -- -- -- -- --    

Then port 3:

root@41e310d:/usr/src/app# i2cdetect -y 3
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: 10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: 20 21 22 23 -- -- -- -- -- -- -- -- -- -- -- -- 
30: 30 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- 44 -- -- -- 48 -- -- -- -- -- -- -- 
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- 63 64 -- -- -- 68 -- 6a -- -- -- 6e -- 
70: -- -- -- -- -- -- -- --            

Then go back to port 1, i2cdetect is slow and every device has disappeared

root@41e310d:/usr/src/app# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

Note I tried this on a Pi 4 as well:

root@e8d3c87:/usr/src/app# i2cdetect -l
i2c-1   i2c             bcm2835 (i2c@7e804000)                  I2C adapter
i2c-11  i2c             ffffffff00000002.i2c                    I2C adapter

And i2c-detect -y 1 resulted in 0 devices from the get go but the expected devices appeared on port 11. This again is very different behavior than the original Raspberry pi OS

Hello @evylez

Welcome to the forums!

Can you please confirm which version of balenaOS you are using? Note, i’m refering to the image os you downloaded from the dashboard most probably and not the docker base image(balenalib/rpi-raspbian)?

I will try to reproduce the issue.

Cheers

Hey

I have not been able to reproduce this. This is the output from a privileged container

root@12fb630:/usr/src/app# i2cdetect -l
i2c-1   i2c             bcm2835 (i2c@7e804000)                  I2C adapter
root@12fb630:/usr/src/app# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- 4c -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

Could this be because you are running modprobe i2c-dev during the docker image build phase?

I was able to use i2c-detect without modprobe i2c-dev. If you want to run it at runtime, you should also install kmod since balenalib/rpi-raspbian base image doesn’t ship the modprobe command.

You can also simplify your dockerfile as follows:

FROM balenalib/rpi-raspbian

RUN install_packages i2c-tools 
...

You can read more about the install_packages utility here https://www.balena.io/docs/reference/base-images/base-images/#installing-packages

Another ques: Do you have any dtoverlay configured that might be changing the bus number etc?

Thank you Rahul for taking a look at this. The OS version for the pi 3 was 2.51.1+rev1 and 2.60.1+rev1. The modprobe command might have been a relic from when this project was started about two years ago(when you guys were still called resin). However you all might want to update your documentation . I will check the dtoverlay and try a container without the modprobe. What OS version did you test on?

Here the dtoverlay I see in our fleet "i2c-gpio,i2c_gpio_delay_us=14,i2c_gpio_sda=2,i2c_gpio_scl=3"

Oh wow. Removing those did the trick. Thank you Rahul. Device Tree Overlay

Ran into a another problem though. My project uses a mix of devices from Raspberry Pi Zero W to Pi 3 B+. So for the pi zero when I removed the dtoverlays:

i2cdetect -y 1
Error: Could not open file `/dev/i2c-1' or `/dev/i2c/1': No such file or directory
root@1cc7279:/usr/src/app# i2cdetect -l
root@1cc7279:/usr/src/app# 

None of the i2c adapters are generated. So the Pi Zero’s may need defined dtoverlays but the newer Pi versions do not.

Hi @evylez, sounds like you need a different dtoverlay setting depending on the device type. I guess that the default ones defined works fine for some devices but not for Pi Zero. Can you try overwriting dtoverlay setting on the device configuration page and see if it helps?