Posts: 16
Joined: Sun Mar 08, 2020 9:09 am

How to force specific I2C Bus number to similar devices?

Thu Mar 12, 2020 7:17 am

I need to use multiple USB>I2C adapters ... /digispark (Flashed myself) in a smart-home project.
Sadly when I plug in 1-1 adapter the I2C Bus numbers are reordered automatically, which can generate big problems.

Code: Select all

[19:46:17] openhabian@openhab:~$ sudo i2cdetect -l
i2c-1   i2c             bcm2835 I2C adapter                     I2C adapter
i2c-8   i2c             i2c-tiny-usb at bus 001 device 014      I2C adapter
i2c-7   i2c             i2c-tiny-usb at bus 001 device 012      I2C adapter

How could I set a FIXED bus number for 1-1 device?

After reading 250+ topics here my guess is:
1. Recompiling each I2C-Tiny-USB uniquely with different name? "i2c-tiny-usb-51", "i2c-tiny-usb-52",
(But won't there be a problem with auto-driver recognition?)

2. I Guess I have to add a line to config.txt like this? :idea:

Code: Select all

... Sorry I'm too new to all this, and confused by all these overwelming amount of infos. Any help is appreciated! :)

Posts: 16
Joined: Sun Mar 08, 2020 9:09 am

Re: How to force specific I2C Bus number to similar devices?

Fri Mar 20, 2020 8:26 pm

The AtTiny85 USB>I2C Firmware has a bug!

After RPi reboot >> the device is not recognized. (It hangs.)
You have to manually unplug and re-plug the board to make it work again.

User avatar
Posts: 7168
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: How to force specific I2C Bus number to similar devices?

Sat Mar 21, 2020 12:07 am

I'm not going to wade through that source code partly because my C/C++ skils are not up to it but...

The USB spec already allows for this sort of thing. All devices must have a VID* and PID** and may additionaly have a serial number. Where the USB master has multiple devices with the same VID and PID it's supposed to use the serial number to distinguish between them. If they're the same it should use the physical address derived from the bus, port, device etc. However it often depends on the driver too.

I suspect whoever wrote that USB2I2C code didn't allow for using multiple device on the same USB master and it either doesn't provide a serial number, provides the same serial number on every flashed unit, or does provied a serial number but the linux side driver ignores it.

You've a few choices:
  • Modify the micro controller code so that each device has a unique serial number and passes it to the USB master then write udev rules so a given serial number always gets the same bus ID. This may involve modifying the linux side driver.
  • Always keep a given device in a given USB socket. Write udev rules to assign bus ID based on bus.port.device IDs in the USB stack.
  • Give up on these devices and use the Pi's onboard I2C interfaces. Models prior to 4B have two but one is reserved for the HAT ID eprom during boot and the Pi camera afterwards (if you're using one). The 4B, I believe has six, same caveat regarding camera and ID pins.

    Have a read of ... t/overlays, specifically the i2c3 through i2c6 overlays.
*VID: Vendor ID
**PID: Product ID

DIsclaimer: I am not an expert on the inner workings of USB or udev. This is just stuff I've picked up over the years.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help?

Return to “Beginners”