Software (i2c-gpio) With Balena + Rasbperry Pi CM4

Hello! I’m trying to use the Software I2C (i2c-gpio) interface instead of the hardware i2c interface on a raspberry pi cm4 to work with a sensor that needs clock stretching support.

I added the following. configuration variables that should have enabled it I believe, but I can’t see the interface using i2cdetect.

Does anyone have experience with this that could point me in the right direction?

dtparam=i2c_arm=off
dtparam=i2c=off
dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3

@alanb128 or @chrisys – Do either of you have any experience with i2c-gpio perhaps? Ever attempted to use it in projects?

And looking at that linked post @keenanjohnson, that means you have the device on Physical Pins 3 and 5, correct?

Correct! Though I have tried it with different GPIO pins and I don’t see a different result.

Hi,

I just tested this on my Raspberry Pi 3 and 4.
For the Raspberry Pi 3, your setup worked perfectly, for the Raspberry Pi 4, I had to add dtparam="i2c-gpio=on".

I also noticed there is a difference in numbering.
With the Raspberry Pi 3, my I2C device was numbered 3, with the Raspberry Pi 4, it was numbered 11.

Hope this helps.

Oh perhaps my mistake was just setting “i2c-gpio” instead of “i2c-gpio=on”. I’ll try this on my end once the Balena API issues this morning are sorted out.

Just to confirm @TJvV, your full configuration with the Raspberry Pi 4 was as shown below?

dtparam=i2c_arm=off
dtparam=i2c=off
dtoverlay=i2c-gpio=on,i2c_gpio_sda=2,i2c_gpio_scl=3

Hi,

You are close, the configuration I used was:

BALENA_HOST_CONFIG_dtparam = "i2c_arm=off","spi=on","audio=on","i2c-gpio=on"
BALENA_HOST_CONFIG_dtoverlay = "vc4-fkms-v3d","i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3"

which boils down to:

dtparam=i2c_arm=off
dtparam=i2c-gpio=on
dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3

to enable it.

Ah I see! Thanks so much! I’ll test that shortly!

@TJvV were you able to verify via:

i2cdetect -l

I tried the configuration above on my raspberry pi cm4, but to no avail.

root@5582468:~# cat /mnt/boot/config.txt 
dtoverlay=dwc2,dr_mode=host,i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3
dtparam=i2c_arm=off
dtparam=i2c-gpio=on
arm_64bit=1
avoid_warnings=1
disable_splash=1
gpu_mem=16
root@5582468:~#

Still no i2c devices available in the container:

root@5582468:/usr/src# i2cdetect -l
root@5582468:/usr/src#

I’m assembling a CM4 + IO board to try to reproduce and troubleshoot. I’ll follow up here soon…

Thanks so much @alanb128! Let me know how I can support your testing and really appreciate any insights you have!

I’m running with balenaOS 2.85.2+rev3 fryi @alanb128. If you’d like me to give you access to a test device of mine here I can do that as well!

I granted support access to device id: 5582468ac31e723a6def4580c131f69a if you want to poke at a device @alanb128 !

Hi,

i2cdetect -l is how I arrived at the number 11 for my software device.
For reference, I tested it with balenaOS 2.83.10+rev1 in development mode, device type raspberrypi4-64.

Thanks @alanb128 and @TJvV for the assist here, much appreciated! :partying_face:

It took me a bit longer than expected to get it online! As soon as I get a chance to test I’ll post an update. (Probably tomorrow)

Thank you so much @alanb128 ! If I can get this working it will really help out my open source project! There are lots of people that will be quite thankful!

As I mentioned, feel free to poke around at the device I linked above as well!

You’re welcome @keenanjohnson! In my setup, I have a CM4 (104016) in the IO board with balenaOS 2.85.2+rev3 in development mode, device type Raspberry Pi CM4 IO Board. In the dashboard, I changed the “Define DT overlays” to the following value: "vc4-fkms-v3d","i2c-gpio","i2c_gpio_sda=2","i2c_gpio_scl=3"
Running i2cdetect -l gives:

i2c-1   i2c             bcm2835 (i2c@7e804000)                  I2C adapter
i2c-11  i2c             ffffffff00000002.i2c                    I2C adapter

Is that what you would expect to see? I’m not sure how to confirm that software I2C is being utilized - the documentation seems a bit sketchy. (I’m learning a bit about this as I go along!) I do have a BMP280 on pins 3 and 5 that seems to be detected either way.

Thank you for your help @alanb128!

So this is interesting because I believe (I’m not an expert) that the second interface (i2c-11) is a software i2c interface that bit-bangs the GPIOs to create an I2C bus while the first interface (i2c-1) is the hardware periphial.

What is weird is that the default i2c bus is on the same pins you’ve defined for the software bus (i2c-11) pins 2 and 3, so I’m not sure what that means haha. That’s why I was trying to disable the hardware bus via my dt_params above. I’m assuming you haven’t set any of those?

What is the vc4-fkms-v3d? Is that related to the I2C? Is seems to be something about the video drivers from my googling?

You mentioned that you were able to see a device @alanb128, were you able to figure out which bus the device was using?

I was able to replicate this on my device. Interesting, I wasn’t able to do it via custom configuration variables (BALENA_HOST_CONFIG_dtoverlay), but had to select the “Define DT overlays" thing in the Balena web UI.

Anyway, my configuration is as such:

Define DT parameters = "i2c_arm=off","i2c-gpio=on"
Define DT overlays = "i2c-gpio","i2c_gpio_sda=2","i2c_gpio_scl=3"

This allows me to see the i2c interface as interface 11, but unfortunately, I can’t see any of my devices. Digging into this further, I see that DMESG reports it has started the i2c drive on different pins than I specified above. I’m not sure why that would be, but perhaps you have some insight @alanb128 ?

root@5582468:/usr/src# i2cdetect -l
i2c-11  i2c             ffffffff00000002.i2c                    I2C adapter
root@5582468:/usr/src# i2cdetect -y 11
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
root@5582468:/usr/src# dmesg | grep i2c
[    9.589999] i2c-gpio ffffffff00000002.i2c: using lines 23 (SDA) and 24 (SCL)
[   13.292367] i2c /dev entries driver