#include #include #include #include #include #include #include #if !DT_NODE_HAS_STATUS(DT_ALIAS(sw0), okay) #error "Board does not define alias sw0. Add a boad overlay" #endif const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios); static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios); bool led_on; static struct gpio_callback button_cb; static void button_isr(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins) { ARG_UNUSED(port); ARG_UNUSED(pins); ARG_UNUSED(cb); // Do something int v = gpio_pin_get_dt(&button); if(v >= 0) { printk("[IRQ] Button state: %s\r\n", v ? "ACTIVE" : "INVACTIVE"); } led_on = !led_on; gpio_pin_set_dt(&led, (int)led_on); } int main(void) { int ret; if (!device_is_ready(led.port) || !device_is_ready(button.port)) { printk("GPIO device not ready\n"); return 0; } ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE); if (ret != 0) { printk("Failed to configure LED: %d\n", ret); return 0; } ret = gpio_pin_configure_dt(&button, GPIO_INPUT); if (ret != 0) { printk("Failed to configure button: %d\n", ret); return 0; } // gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_BOTH) ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE); if (ret != 0) { printk("Failed to configure interrupt: %d\n", ret); return 0; } // Set up callback and register it gpio_init_callback(&button_cb, button_isr, BIT(button.pin)); gpio_add_callback(button.port, &button_cb); printk("\r\n[EXTI] Button pin interrupt is ready. Press button\r\n"); while (1) { k_sleep(K_FOREVER); } return 0; }