bouton_it.c
Button sw0 (P0.11 on the DK, switch S1) drives the LED through an edge interrupt. The DK user guide insists: the buttons have no external pull-up, so firmware must enable GPIO_PULL_UP or read a stuck-high line.
Hardware
An nRF52833-DK board (Nordic, nRF52833 Cortex-M4F SoC, BLE 5) powered over USB: 4 LEDs on P0.13-P0.16 (active-low) and 4 buttons on P0.11, P0.12, P0.24, P0.25 (active-low, internal pull-up must be enabled). The board alone; the DK switch S1 (or the MDK user button) is enough to test.
Wiring
No wiring. For an external button between a header pin and GND, declare the pull-up in the devicetree overlay.
How it works
gpio_pin_interrupt_configure_dt arms the active (here low) edge; the ISR only toggles the LED — the Zephyr rule is to keep GPIO callbacks as short as possible.
Running it
Zephyr project: west build -b nrf52833dk/nrf52833 then west flash (on-board J-Link); 115200 serial console on the USB UART connector.
Variants
Replace the toggle with a k_sem_give to wake a thread, arm both edges (EDGE_BOTH), or add the DK buttons sw1 to sw3.
bouton_it.c
/* Interruption GPIO sur sw0 : chaque appui bascule la LED0.
* DK : sw0 = bouton S1 sur P0.11, actif bas, PULL-UP INTERNE
* OBLIGATOIRE (aucune résistance externe sur le DK).
* MDK : sw0 = bouton utilisateur. */
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
static const struct gpio_dt_spec btn =
GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios);
static const struct gpio_dt_spec led =
GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
static struct gpio_callback btn_cb_data;
static void sur_appui(const struct device *port,
struct gpio_callback *cb, uint32_t pins)
{
ARG_UNUSED(port); ARG_UNUSED(cb); ARG_UNUSED(pins);
gpio_pin_toggle_dt(&led); /* callback = contexte d'IT : léger */
}
int main(void)
{
gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE);
gpio_pin_configure_dt(&btn, GPIO_PULL_UP);
gpio_pin_interrupt_configure_dt(&btn, GPIO_INT_EDGE_TO_ACTIVE);
gpio_init_callback(&btn_cb_data, sur_appui, BIT(btn.pin));
gpio_add_callback(btn.port, &btn_cb_data);
while (1) {
k_msleep(100); /* le thread main n'a rien à faire */
}
return 0;
}