Re: Unable to get GPIO controller handle from gpio node using DT_GPIO_CTLR


Bolivar, Marti
 

Hi Jason,

On Wed, Mar 16 2022, Jason Bens via lists zephyrproject org wrote:
I'd like to get the GPIO controller handle, and figured I could do it with something like the following:
static const struct device *gpio_dev =
DT_GPIO_CTLR(DT_NODELABEL(button_sel_role), gpios);
DT_GPIO_CTLR returns a node identifier:

https://docs.zephyrproject.org/latest/reference/devicetree/api.html#c.DT_GPIO_CTLR

And node identifiers are not values:

https://docs.zephyrproject.org/latest/guides/dts/api-usage.html#node-identifiers-are-not-values

You can't do anything useful with a node identifier at runtime -- the
devicetree has "vanished" at that point (see
https://www.youtube.com/watch?v=sWaxQyIgEBY for what I mean by
"vanished").

You need to convert the node identifier to a device pointer with
DEVICE_DT_GET:

static const struct device *gpio_dev =
DEVICE_DT_GET(DT_GPIO_CTLR(DT_NODELABEL(button_sel_role), gpios));

Hope this helps,
Martí

Join users@lists.zephyrproject.org to automatically receive all group messages.