This compiles with warnings and the value always comes back as 0
#define SW1_NODE DT_ALIAS(sw1)
#if DT_NODE_HAS_STATUS(SW1_NODE, okay)
#define SW1 DT_GPIO_LABEL(SW1_NODE, gpios)
#define SW1_PIN DT_GPIO_PIN(SW1_NODE, gpios)
#define SW1_FLAGS DT_GPIO_FLAGS(SW1_NODE, gpios)
#else
#error "Unsupported board: sw1 devicetree alias is not defined"
#endif
const struct device *sw1 = device_get_binding(SW1);
if (sw1 != NULL) {
printk("Found device \"%s\"\n", SW1);
} else {
printk("No \"%s\" found; is it connected?\n", SW1);
}
gpio_pin_configure(sw1, GPIO_INPUT, GPIO_PULL_DOWN);
while (1) {
int sw1_val = gpio_pin_get(sw1,SW1_PIN);
printk("Switch: %d\n", sw1_val);
k_msleep(1000);
}
The warnings are:
warning: conversion from 'unsigned int' to 'gpio_pin_t' {aka 'unsigned char'} changes value from '256' to '0' [-Woverflow]
43 | #define GPIO_INPUT (1U << 8)
| ^~~~~~~~~
../src/main.c:749:26: note: in expansion of macro 'GPIO_INPUT'
749 | gpio_pin_configure(sw1, GPIO_INPUT, GPIO_PULL_DOWN);
I checked that GPIO_INPUT is correct for gpio_pin_configure in zephyr docs.
Presumably the nrfConnect SDK is the same in this regard?