graemef
Posts: 3
Joined: Wed Jun 12, 2019 12:00 am

Rpi Hat -Device Tree

Wed Jun 12, 2019 6:47 am

Hi, I'm in the process of creating a compliant Rpi Hat. I have flashed the Eprom (24LC256) complete with a device tree (below) which I copied from martinlbb very helpful posthttps://lb.raspberrypi.org/forums/viewt ... p?t=108134

Code: Select all

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2708";

    [email protected] {
        target = <&leds>;
        __overlay__ {
            my_led: myled {
                label = "MYLED";
                gpios = <&gpio 18 0>;
                linux,default-trigger = "heartbeat";
            };
        };
    };
};
The Hat works as expected with the LED flashing a heartbeat. The /proc/device-tree/hat directory is created with the product files etc.

What I'd like the Hat's device tree to do is - check the I2C bus 1 on the Rpi and ensure it is enabled, if it isn't already and set 6 different GPIOs as inputs or outputs and set the initial state.
I'm finding it hard to find examples or documentation to help me achieve this.

Also, I don't understand how the I2C bus 0 gets enabled when I plug my Hat into a brand new Pi which would have it disabled by default. Is that something the Hat's device tree should setup as well?

Any help or examples would be much appreciated.

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

Re: Rpi Hat -Device Tree

Wed Jun 12, 2019 7:59 am

The I2C interfaces are described by Device Tree, but the device nodes are disabled by default. The usual RPi way of enabling them for Linux ("dtparam=i2c_arm=on" or "$ sudo dtparam i2c_arm=on") has the effect of changing the "status" property from "disabled" to "okay". DT parameters are essentially mini overlays that are baked into the DTB files, and your HAT overlay can use the same trick - just add another fragment to do it:

Code: Select all

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2708";

    [email protected] {
        target = <&leds>;
        __overlay__ {
            my_led: myled {
                label = "MYLED";
                gpios = <&gpio 18 0>;
                linux,default-trigger = "heartbeat";
            };
        };
    };

    [email protected] {
        target = <&i2c_arm>;
        __overlay__ {
            status = "okay";
        };
    };
};
This will enable I2C1.

You are correct that I2C0 is enabled when a HAT is connected, but only briefly to allow the HAT EEPROM to be read. This happens before the Linux kernel is started, so to all intents and purposes the interface is disabled.

Configuring GPIOs from Device Tree can be more tricky - it depends what you want to use them for (and from), and how timing-critical the configuration is. Linux has a "pinctrl" (pin control) subsystem that allows devices to request that pins are put into certain states before they are initialised; if the initialisation is unsuccessful, the pins are returned to their default state. The mechanism can request that pins be made inputs, outputs or any of the Alt functions, and with chosen pulls, but you can't specify that a pin be an output driving high. Ordinarily the device driver would set the output value, but sometimes people want the pins configured ready for a user-space application to drive them, from the sysfs (/sys/...), gpiolib or one of the many slightly hacky libraries that write directly to the hardware.

If all you want is pre-configuration before an application takes over, device tree doesn't make it easy - you need a device node to hang the pinctrl descriptors from (some overlays use the "leds" node). Fortunately the HAT spec has a special mechanism to do that - the GPIO blob within the HAT EEPROM.

Before I write any more, what does your HAT need GPIOs for?

graemef
Posts: 3
Joined: Wed Jun 12, 2019 12:00 am

Re: Rpi Hat -Device Tree

Wed Jun 12, 2019 9:07 am

PhilE, Thank you for such a comprehensive reply.

The GPIO pins will be connected to relays, switches, and sensors. I see now there is no good reason to set GPIO states via the Hat's DT, it can all be done in the application once the boot is complete. Thank you for adding the DT code to enable the I2C Bus 1, I would never have worked it out.
I wouldn't mind knowing a bit more about the syntax and usage for the Overlay DT, if you can point me towards a resource.

I will test the hat on a fresh SD card (i.e I2C 0 disabled) tomorrow to make sure the hat gets recognised.

Thanks again

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

Re: Rpi Hat -Device Tree

Wed Jun 12, 2019 9:17 am

In terms of documentation, there is (ahem) the guide I wrote many years ago: https://www.raspberrypi.org/documentati ... ce-tree.md

It's a bit long, but it should at least touch on most topics.

graemef
Posts: 3
Joined: Wed Jun 12, 2019 12:00 am

Re: Rpi Hat -Device Tree

Thu Jun 13, 2019 2:40 am

PhilE, I built a fresh SD card, connected the Hat. When the Pi booted, I2C bus1 was enabled, I2C bus0 was disabled. Brilliant.
I will explore DT overlay fragments and get a feel for it. I'm on my way with Pi Hats now. Thanks again

Return to “Device Tree”