Configure a pin from devicetree with gpio_pin_configure_dt
gpio_pin_configure_dt sets a pin's direction and initial level from a gpio_dt_spec, merging the devicetree active-low flag, on Zephyr 4.2.
gpio_pin_configure() takes a device, a pin number and flags, three things the devicetree already knows. The _dt variant takes the struct gpio_dt_spec instead, and it also ORs in the flags written next to the pin in the devicetree, so an active-low LED is configured as active-low without any code.
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/printk.h>
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
int main(void)
{
if (!gpio_is_ready_dt(&led)) {
return -ENODEV;
}
int rc = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
printk("configure rc %d\n", rc);
return 0;
}configure rc 0GPIO_OUTPUT_ACTIVE means "output, driven to the active level", so the LED lights at once whether the board wires it high or low. GPIO_OUTPUT_INACTIVE starts it off. Reading led.dt_flags shows the flags that were merged, which is where GPIO_ACTIVE_LOW comes from. The call returns 0 or a negative error, for example when the controller does not support the requested flags. Always run gpio_is_ready_dt() first. See the GPIO API, version 4.2.