ppelleti
Posts: 8
Joined: Sun Jun 11, 2017 11:00 pm

beginner question: enabling I2C in Device Tree

Thu Mar 28, 2019 6:28 pm

I have built a simple HAT which has a couple of GPIO expanders on the I2C bus. I'm trying to figure out what to put in the EEPROM, but the device tree in particular is hard to figure out.

I'm going to access the GPIO expanders from userspace via /dev/i2c-1, so what I need is for /dev/i2c-1 to be enabled when the Pi boots up with the HAT attached, since by default /dev/i2c-1 is not enabled.

I think I have a pretty good idea of what to put in the non-devtree part of the EEPROM: I just need to put this in eeprom_settings.txt:

Code: Select all

setgpio  2     ALT0     DEFAULT
setgpio  3     ALT0     DEFAULT
and that should take care of enabling I2C in hardware. But, in order to get /dev/i2c-1 to show up, I believe I would also need to enable the I2C driver, and I assume that's where the Device Tree comes in.

I've been looking through the documentation linked from https://github.com/raspberrypi/hats/blo ... e-guide.md, but the documentation seems vast and I'm having trouble finding the information I need.

The closest I've come is https://www.raspberrypi.org/documentati ... ce-tree.md, which suggests that I just need to say:

Code: Select all

dtparam=i2c_arm=on
But I haven't yet figured out where I need to put this line, and how to get from there to the device tree file that I feed to eepmake.

I appreciate any help in figuring this out. Thanks!

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

Re: beginner question: enabling I2C in Device Tree

Fri Mar 29, 2019 5:42 pm

The "dtparam=i2c_arm=on" in config.txt causes the firmware to manipulate the loaded Device Tree and enable i2c1 (on all but the oldest Pis which would use i2c0). This is implemented like this:

Code: Select all

		i2c_arm = <&i2c_arm>,"status";
This says that the i2c_arm parameter targets the "status" property of the node with the label "i2c_arm". Because the firmware knows that "status" is special, a true value (non-zero integer, "on", "y", etc.) is magically transformed into "okay", while false becomes "disabled".

You need an overlay that does the same thing. Fortunately, all the I2C overlays I know already include this feature in some form or other, and it looks like this:

Code: Select all

/dts-v1/;
/plugin/;

/ {
	compatible = "brcm,bcm2708";

	[email protected] {
		target = <&i2c_arm>;
		__overlay__ {
			#address-cells = <1>;
			#size-cells = <0>;
			status = "okay";
		};
	};
};
Put this in a file called "i2c-on-overlay.dts", then compile it with:

Code: Select all

$ dtc [email protected] -I dts -O dtb -o i2c-on.dtbo i2c-on-overlay.dts
You will see a warning about "no reg property", but this is nothing to worry about - the dtc utility just doesn't make allowances for the overlay syntax.

You can decompile the overlay with:

Code: Select all

$ fdtdump i2c-on.dtbo
Now copy it to /boot/overlays, put "dtoverlay=i2c-on" in config.txt and reboot - /dev/i2c-1 should exist.

This .dtbo file is what you need to pass to eepmake to build the overlay into the EEPROM.

ppelleti
Posts: 8
Joined: Sun Jun 11, 2017 11:00 pm

Re: beginner question: enabling I2C in Device Tree

Sat Mar 30, 2019 5:02 pm

Thanks! I will give that a try.

One more question: how can I set the baud rate for the i2c? (The equivalent of "i2c_arm_baudrate=400000" in config.txt)

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

Re: beginner question: enabling I2C in Device Tree

Sun Mar 31, 2019 1:58 pm

The "i2c_arm_baudrate" parameter sets another property of the i2c_arm/i2c1 node - this time it's an integer. To the overlay you have already you just need to add:

Code: Select all

			clock-frequency = <400000>;
Place it somewhere inside the __overlay__ section.

Before you program your EEPROMs, there is another option to consider - you can put the name of the overlay in the EEPROM instead of the contents, and then either get it included in the standard firmware distributions or supply it along with the HATs.

Return to “Device Tree”