need simple devicetree overlay example for gpio

I have this as nrf52840dk_nrf52840.overlay in my application directory:

/ {
lightStrings {
compatible = "gpio";
lightString0: lightString_0 {
gpios = <&gpio0 29 GPIO_ACTIVE_HIGH>;
label = "medusa LED 0";
};
};
};
After I do a pristine rebuild I can see "&lightString0" in the vscode plugin under GPIO->&gpio0->Pin 29.  But, during compile with:
const struct gpio_dt_spec blah = GPIO_DT_SPEC_GET(DT_ALIAS(lightString0), gpios);
I get:
...:10:34: error: '__device_dts_ord_DT_N_ALIAS_lightString0_P_gpios_IDX_0_PH_ORD' was not declared in this scope
10 | const struct gpio_dt_spec blah = GPIO_DT_SPEC_GET(DT_ALIAS(lightString0), gpios);
| ^~~~~~~~~~~~~~~~
eventhough
GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
works fine.
Any ideas or pointers to a gpio device tree overlay sample?  There are other examples but I don't really see a simple gpio one.

And, what is:
compatible = "gpio-leds";
Is there simply:
compatible = "gpio";
TIA!
Parents
  • Hi jahess,

    Try to use the code snippet below inside your nrf52840dk_nrf52840.overlay. It makes use of GPIO P0.28.

    /{

       custom_gpios {

          compatible = "gpio-keys";

          cusgpio0: cusgpio_0 {

             gpios = <&gpio0 28 GPIO_ACTIVE_HIGH>;

             label = "My custom GPIO";

          };

       };

       aliases {

       mycusgpio = &cusgpio0;

    };

    Highlighted in red are names which you can change as you prefer. I suggest to leave what remains de-highlighted (in black) as it is. Eventually substitute &gpio0 28 with another pin (i.e. &gpio0 4 or &gpio1 11).

    For what concerns the main program, I am not very keen on DT_SPECS apis. I prefer to enable my gpio pin in the following way:

    #define GPIO028_NODE   DT_ALIAS(mycusgpio)

    #define GPIO028              DT_GPIO_LABEL(GPIO028_NODE, gpios)

    const struct device *dev_gpio0;

    dev_gpio0 = device_get_binding(GPIO028);

    Now you are ready to use GPIO028.

    Note: I omitted any possible sanity check to make things as simple as possible.

    Hope this can help!

    Have a nice day,

    Gianmarco

  • From a comment I made to https://www.youtube.com/watch?v=AMwxj4yUXDQ I learned "the trailing s in latch-en-gpios is required and that the DT... macro support converts any capitals in device tree names to lower case." The last part means device tree names must be lowercase.

    With that, I was able to use:

        GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), lightstring0_gpios)

    Also note that the note name for zephyr_user in the dts overlay is:
        zephyr,user
    Meaning the , gets converted to _ by the macros.
    Kind of strange and hard to find / easy to gloss over zephyr conventions there.
Reply Children
No Data
Related