This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Proper use of DTS files to describe GPIOs in Zephyr?

I started off my project with the Zigbee light_bulb sample, and I added some new functionality that needs to manipulate various GPIOs.  These GPIOs are neither buttons/keys nor LEDs.  I added the definitions to $PROJECT_DIR/boards/nrf52840dongle_nrf52840.overlay with compatible = "gpio-keys" .  I see the following issues:

  1. They aren't actually keys/buttons.  This DTS file works fine in practice, but it feels hacky. Is there a more appropriate compatible= string to be using?  https://devzone.nordicsemi.com/f/nordic-q-a/85362/problem-using-nrf-connect-sdk-devicetree-gpio-definitions-in-code/360543#360543 suggests adding a custom DTS binding; is this what Nordic recommends?  Or is there a better "generic gpio" compatible= string I could use?
  2. When I build this project from the command line using west / ninja it compiles just fine.  When I load it into Segger and try to build through the IDE, Segger doesn't find my .overlay file unless I copy it into the project root directory (one level up from boards/ ).  What could cause this discrepancy -- are there Segger-specific build settings that need to be kept in sync with the rest of the project?
  3. I see examples like this:


    Most of the samples seem to define an alias for e.g. gpiocus0 and then reference it using DT_ALIAS() in the C code.  Is there a way to avoid all of this indirection and just reference gpiocus_0 from C?  I don't like having to have 2-3 subtly-different names pointing to the same node.  I'd prefer to get rid of the labels and aliases if possible.
  4. It might be helpful to add a Device Tree tag in this forum.
Parents
  • Hello

    1. The compatible property is there to help the build system find the right binding for the hardware node. If you only want to define a basic gpio input, then the "gpio-keys" binding is the most basic binding you'll get and works just fine. Otherwise, there are many bindings defined in dts/bindings/gpio/ in your Zephyr install, among them "nordic, nrf-gpio", maybe that could work for your application? I would recommend having a look at these binding define files to get an idea of how they differ. You could also define your own binding, but unless you want very specific functionality that isn't available in the already defined ones, I don't think it will be necessary.

    2. This sounds strange to me, the build system should be able to detect overlay files in the boards/ directory. You could try to set the CMake variable DTC_OVERLAY_FILE manually to contain your overlay file as described here.

    3. Yes, there are many helper functions in Zephyr's Devicetree API that you can use to obtain the node identifier. For your node you could for example use DT_NODELABEL(gpiocus0). You can also input the label property directly into the device_get_binding function, in your case device_get_binding("Custom gpio 28") I believe. The aliases are commonly used just to make the code more readable.

    4. Yes this is a good point, many newcomers to Zephyr seem to have trouble with the devicetree.

    Best regards,

    Einar

  • For #2: the build system normally does detect overlays under boards/, but that doesn't work under Segger.  Maybe I'm importing the project incorrectly?

    For #3: is there a way I can omit the "gpiocus0" label entirely and just reference "gpiocus_0" from the C code?  It's annoying having two slightly different names pointing to the same node -- an unnecessary layer of abstraction (at least in my use case).

  • 2. hm, the only thing I can think of is that Segger might define DTC_OVERLAY_FILE for you, since the build system won't keep looking for an overlay file if this variable is defined. As I said I would try to add your overlay file to DTC_OVERLAY_FILE.

    3. I don't think there's a straight forward way to use "gpiocus_0" instead of "gpiocus0" to access your node, but you can look through the Devicetree API to see if you find a function that suits your needs. As I said you can input the label property, which you can name whatever you want, directly into the device_get_binding function if you find that cleaner.

    -Einar

Reply
  • 2. hm, the only thing I can think of is that Segger might define DTC_OVERLAY_FILE for you, since the build system won't keep looking for an overlay file if this variable is defined. As I said I would try to add your overlay file to DTC_OVERLAY_FILE.

    3. I don't think there's a straight forward way to use "gpiocus_0" instead of "gpiocus0" to access your node, but you can look through the Devicetree API to see if you find a function that suits your needs. As I said you can input the label property, which you can name whatever you want, directly into the device_get_binding function if you find that cleaner.

    -Einar

Children
No Data
Related