stardustJerry
Posts: 7
Joined: Mon Apr 04, 2016 11:11 pm

i2c-gpio dtoverlay bus number

Thu Feb 15, 2018 9:31 pm

(I originally posted this in the o/s Raspbian section, but I think it goes here. Sorry about that.)

I need to add 2 additional gpio buses. I'm using the usual i2c for a port expander and also need to read/write two different external eeproms that need to be plugged in on different pins.

Adding a line in config.txt like this only works once and the bus is always #3.
dtoverlay=i2c_gpio,i2c_gpio_sda=27,i2c_gpio_scl=22

After reboot you can see the device using i2cdetect -y -a 3 But adding a second line for another set of pins in config.txt doesn't work.

So, where is that bus number specified and is it possible for me to add another one and perhaps make a separate dtoverlay for it?
I looked at the dts source file for the i2c_gpio overlay on github, but I didn't see anywhere in that file
where the bus number was set.

Thanks for any help.
Jerry

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1616
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: i2c-gpio dtoverlay bus number

Thu Feb 15, 2018 9:57 pm

Can you really not get the three I2C devices (or at least two of them) to share the hardware I2C bus? It would be more efficient.

stardustJerry
Posts: 7
Joined: Mon Apr 04, 2016 11:11 pm

Re: i2c-gpio dtoverlay bus number

Thu Feb 15, 2018 10:36 pm

I don't believe we can put all three devices on the one hardware i2c. The port expander needs to use a higher baud rate than the EEPROMS from what the engineer I am working with tells me. It may be possible to use just the one extra set of GPIO lines for both EEPROMS, but they both use the same ic2 address so I'm not sure how that would work.

If I can define one more additional GPIO i2c that would be a good solution for us. If it is not possible, then we will look for another way.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1616
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: i2c-gpio dtoverlay bus number

Thu Feb 15, 2018 10:47 pm

I'm sure it will be possible - there are many board .dts files with multiple i2c-gpio instances - it is just a question of figuring out the bus numbering and whether we can use one standard overlay repeatedly or whether it must be a custom overlay.

stardustJerry
Posts: 7
Joined: Mon Apr 04, 2016 11:11 pm

Re: i2c-gpio dtoverlay bus number

Thu Feb 15, 2018 11:46 pm

Yes, the bus numbering seems to be the trick and finding out where it gets set. Trying to use the same overlay twice with different pins doesn't work. The second one only takes effect and it always gets bus number 3.

In other words if I put
dtoverlay=i2c-gpio,i2c_gpio_sda=27,i2c_gpio_scl=22
dtoverlay=i2c-gpio,i2c_gpio_sda=05,i2c_gpio_scl=06

into config.txt and reboot, I only get the second one on bus number 3. No others show up when I type i2cdetect -a -l

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1616
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: i2c-gpio dtoverlay bus number

Mon Feb 19, 2018 5:47 pm

Sorry for the delay, but I have a solution for you know. The 4.14 kernel tree now includes a modified i2c-gpio with a new parameter - "bus" - which defaults to zero. You can add multiple instances provided they all have unique bus values. In my test I found the busses (i.e. /dev/i2c-*) were given numbers counting upwards from 3. I'm not sure it is possible to guarantee the mapping, but the first one mentioned appeared as i2c-3 for me - more testing is required.

You can find the updated overlay source here. Compile it with:

Code: Select all

dtc -@ -I dts -O dtb -o i2c-gpio.dtbo i2c-gpio-overlay.dts
and copy it to /boot/overlays.

Use it like this:

Code: Select all

dtoverlay=i2c-gpio,bus=0,i2c_gpio_sda=23,i2c_gpio_scl=24
dtoverlay=i2c-gpio,bus=1,i2c_gpio_sda=16,i2c_gpio_scl=20
Note that "bus=0" is the default and could be omitted.

stardustJerry
Posts: 7
Joined: Mon Apr 04, 2016 11:11 pm

Re: i2c-gpio dtoverlay bus number

Mon Feb 19, 2018 6:19 pm

Wow, thank you so much!

That is just what I needed.

Regards,
Jerry :D

stardustJerry
Posts: 7
Joined: Mon Apr 04, 2016 11:11 pm

Re: i2c-gpio dtoverlay bus number

Mon Feb 19, 2018 8:17 pm

I've been playing around with the new overlay and found that I can use it which is great, but there is an oddity which I don't understand.
if I put lines like this in /boot/config.txt
dtoverlay=hifiberry-dac
dtoverlay=i2s-mmap
dtoverlay=i2c-gpio,bus=3,i2c_gpio_sda=27,i2c_gpio_scl=22,,i2c_gpio_delay_us=625
dtoverlay=i2c-gpio,bus=4,i2c_gpio_sda=05,i2c_gpio_scl=06

Using i2cdetect, The order of the assigned buses is reversed. 3 seems to always get assigned to the second one. Even if I use different numbers that don't include 3, it still assigns 3 to the last one I specify and the first one I specify always gets bus number 4.

I can certainly work with it as it is, but it is a bit of a curiosity.

Cheers,
Jerry

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1616
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: i2c-gpio dtoverlay bus number

Mon Feb 19, 2018 8:21 pm

I tried to make it clear that this is a bit of a grey area. I'll have a look and see if there's a way to force an order (at least), but if it's like MMC/SD interfaces then it's first come first served (in the upstream kernel - we have a patch to force particular instance numbers).

stardustJerry
Posts: 7
Joined: Mon Apr 04, 2016 11:11 pm

Re: i2c-gpio dtoverlay bus number

Mon Feb 19, 2018 10:35 pm

I really do appreciate how you have helped me. I wasn't complaining at all, just noticing the curious way the bus numbers were assigned. It works fine for me.

Cheers,
Jerry

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1616
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: i2c-gpio dtoverlay bus number

Tue Feb 20, 2018 11:23 am

The bus IDs are derived from Device Tree aliases, but the current overlay parameter mechanism isn't flexible enough to be able to generate matching aliases. Instead I've added a small patch to the i2c-gpio driver that allows the "reg" property to be used as the real bus number (unless there is a clash), so your test case will use the expected bus numbering. This patch will be in the next firmware release.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1616
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: i2c-gpio dtoverlay bus number

Tue Feb 20, 2018 9:55 pm

The patched driver and updated overlay are in the current rpi-update release.

Return to “Device Tree”

Who is online

Users browsing this forum: No registered users and 3 guests