Non-static GPIO initialization #drivers


W Kicinski
 
Edited

In the Zephyr GPIO API, it appears there are only static initializers for struct gpio_dt_spec objects from the device tree.

To initialize the GPIO in a module context structure with the static structures I've resorted to this:

#define A_SIGNAL_NODE DT_ALIAS(sig_a)
#define B_SIGNAL_NODE DT_ALIAS(sig_b)

void module_init(struct module_ctx *context)
{
    struct gpio_dt_spec a_init = GPIO_DT_SPEC_GET_OR(A_SIGNAL_NODE, gpios,{0});
    struct gpio_dt_spec b_init = GPIO_DT_SPEC_GET_OR(B_SIGNAL_NODE, gpios,{0});

    context->signal_a_pin = a_init;
    context->signal_b_pin = b_init;

 
   <... etc>
}

This seems... strange.  Is there a better way to do this?