Defining IO pin in zephyr

I am trying to define a simple output pin.

in the DTS file I have:

aliases {
        ledred = &hwledred;
        ledgreen = &hwledgreen;
        memwp = &hwmemwp;
    };

    soc {
        gpio0: gpio@50000000 {
            status = "okay";
        };
    };

    chosen {
        zephyr,sram = &sram0;
        zephyr,flash = &flash0;
        zephyr,code-partition = &slot0_partition;
    };

    leds {
        compatible = "gpio-leds";

        hwledred: led_red {
            gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
        };

        hwledgreen: led_green {
            gpios = <&gpio0 3 GPIO_ACTIVE_LOW>;
        };
    };

    zephyr,user {
        hwmemwp: mew_wp {
            gpios = <&gpio1 15 GPIO_ACTIVE_LOW>;
            label = "mem WP";
        };
    };
The LED definitions are working fine, but I cannot get a way to define the mem_wp pin in the C code.
The following compiles, but the values in the generated structure are meaningless and the program crashes.
#define ZEPHYR_USER_NODE DT_PATH(zephyr_user)
#define WP DT_ALIAS(memwp)
#if !DT_NODE_HAS_STATUS_OKAY(WP)
#error "Unsupported board: WP devicetree alias is not defined"
#endif
const struct gpio_dt_spec memWP = GPIO_DT_SPEC_GET_OR(WP, gpios, {0});
What is the correct way to define and use a simple IO pin?
  • Hi,

    In the zephyr,user node you've written "mew_wp". Shouldn't this also be mem_wp or memwp depending on the alias you decided to use??

    Have you defined this device binding somewhere? I can't see the device binding for any memwp within the SDK

    Kind regards,
    Andreas

  • For the red LEDs I have:
    hwledred linked to ledred and referenced in C as:

    #define LED_RED_NODE DT_ALIAS(ledred)
    static const struct gpio_dt_spec ledRed = GPIO_DT_SPEC_GET(LED_RED_NODE, gpios);

    I expected that the analogous for WP should be:
    hwmemwp linked to memwp and referenced in C as:

    #define WP DT_ALIAS(memwp)
    const struct gpio_dt_spec memWP = GPIO_DT_SPEC_GET_OR(WP, gpios, {0});

    Should work in the same way.
    But I am obviously missing something.
    I have found direct pin control samples in Internet, but when I try them I get a long chain of errors concerning macros calling more macros.
    For this I am asking of a simple example of a DTS defining a pin and the C file referencing it.

  • Hi, 

    Apologies for the response time.

    cmag said:
    I have found direct pin control samples in Internet,

    Could you share a link to this sample? You might be missing some configurations if there's a mismatch between the sample and the SDK version you're using

    Kind regards,
    Andreas

Related