bluelambda
Posts: 3
Joined: Wed Sep 06, 2017 8:24 am

[solved] Overlay for multiple i2c_gpio bitbanging buses

Wed Sep 06, 2017 8:34 am

Hello,

On my system I am currently using a classic device tree DTS settings file for one I2C bitbanging bus:

Code: Select all

// Overlay for i2c_gpio bitbanging host bus.
/dts-v1/;
/plugin/;

/ {
	compatible = "brcm,bcm2708";

	[email protected] {
		target-path = "/";
		__overlay__ {
			i2c_gpio: [email protected] {
				compatible = "i2c-gpio";
				gpios = <&gpio 23 0 /* sda */
					 &gpio 24 0 /* scl */
					>;
				i2c-gpio,delay-us = <2>;        /* ~100 kHz */
				i2c-gpio,timeout-ms = <5000>;  /* 5s */
				#address-cells = <1>;
				#size-cells = <0>;
			};
		};
	};

	[email protected] {
		target-path = "/aliases";
		__overlay__ {
			i2c_gpio = "/[email protected]";
		};
	};

	[email protected] {
		target-path = "/__symbols__";
		__overlay__ {
			i2c_gpio = "/[email protected]";
		};
	};

	__overrides__ {
		i2c_gpio_sda = <&i2c_gpio>,"gpios:4";
		i2c_gpio_scl = <&i2c_gpio>,"gpios:16";
		i2c_gpio_delay_us = <&i2c_gpio>,"i2c-gpio,delay-us:0";
		i2c_gpio_timeout_ms = <&i2c_gpio>,"i2c-gpio,timeout-ms:0";
	};
};
Is it possible to bring modifications to these settings in order to support additionnal I2C bitbanging buses on other GPIOs?

I have looked for this here https://www.raspberrypi.org/documentati ... ce-tree.md but it is not clear for me how I can do.
Maybe someone can help me :)

Best regards.
Last edited by bluelambda on Thu Sep 07, 2017 7:21 am, edited 1 time in total.

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

Re: Overlay for multiple i2c_gpio bitbanging buses

Wed Sep 06, 2017 9:49 am

A (currently undocumented) feature was recently added to the overlay handling that should help to an extent. If you assign to the "reg" property of a node and the node's name includes an "@" then any value after the "@" will be replaced by the assigned value. This feature is already used in the w1-gpio overlay (amongst others), and allows multiple instance of the node to coexist because they have different names.

What won't work for the i2c-gpio overlay is the hacky exporting of the symbols for use by other overlays. There isn't a nice mechanism to allow unique properties to be added to the aliases and __symbols__ node, but as long as you don't need to refer to these buses from other overlays then you should be ok.

All you need to do is add this line to the __overrides__ node:

Code: Select all

bus = <&i2c_gpio>,"reg:0";
and rebuild the overlay:

Code: Select all

dtc [email protected] -I dts -O dtb -o i2c-gpio.dtbo i2c-gpio-overlay.dts

bluelambda
Posts: 3
Joined: Wed Sep 06, 2017 8:24 am

Re: Overlay for multiple i2c_gpio bitbanging buses

Wed Sep 06, 2017 3:36 pm

Great !
Thank you very much for your answer, it really helped me :)

Now I have 2 I2C buses (/dev/i2c-3 and /dev/i2c-4), both running well, by using the following commands:

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
but I am unable to do the same from /boot/config.txt. I am not sure about the syntax to do 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
[...]
It only creates an I2C device for GPIO 16,20, the first line seems overwritten by the second when the file is parsed.
Same thing if I split in several dtoverlay/dtparams lines.

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

Re: Overlay for multiple i2c_gpio bitbanging buses

Wed Sep 06, 2017 8:42 pm

You're right - it's not working in the current firmware. If you revert back to a release in mid-May it should work though:

Code: Select all

sudo rpi-update bb0292107df671bbeff7165518ba4bc5b6be7d12
Note that this will overwrite your patched overlay, so you'll have to make a backup before the update then copy it back afterwards.

I'll figure out what went wrong tomorrow.

bluelambda
Posts: 3
Joined: Wed Sep 06, 2017 8:24 am

Re: Overlay for multiple i2c_gpio bitbanging buses

Thu Sep 07, 2017 7:19 am

Ah OK!
Thank you very much for your answers.
Best regards

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

Re: [solved] Overlay for multiple i2c_gpio bitbanging buses

Thu Sep 07, 2017 4:13 pm

I take it back - it is working fine on the latest firmware. Perhaps the problem is that your firmware is/was too old. If "vcgencmd version" returns a date before "Mar 30 2017" then you would need to update. Any Stretch image should be recent enough.

If you think your firmware is sufficiently new and it isn't working for you, perhaps the problem is that you haven't copied the new overlay with the bus parameter to /boot/overlays. "strings /boot/overlays/i2c-gpio.dtbo | grep reg" will return "reg:0" for the updated overlay and nothing for the original. If that doesn't explain the problem, please add "dtdebug=1" to config.txt, reboot and report back with the output from:

Code: Select all

$ vcgencmd version
$ sudo vcdbg log msg |& grep dt
Once you've confirmed that it is working for you I'll update the overlay in the kernel tree to use the same trick.

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

Re: [solved] Overlay for multiple i2c_gpio bitbanging buses

Mon Feb 19, 2018 8:30 pm

FYI the updated overlay with the bus parameter is now in the rpi-4.14.y tree and will be included in the next firmware release.

Return to “Device Tree”