bouton_it.c

nRF52833-DK (id de boards.json: nrf52833-dk) · Archivo : bouton_it.c · Lenguaje : C · Uso : Basculer la LED par interruption sur le bouton

El botón sw0 (P0.11 en el DK, pulsador S1) maneja el LED por interrupción de flanco. La guía del DK insiste: los botones no tienen pull-up externo, así que el firmware debe activar GPIO_PULL_UP o leer un alto permanente falso.

Hardware

Una placa nRF52833-DK (Nordic, SoC nRF52833 Cortex-M4F, BLE 5) alimentada por USB: 4 LED en P0.13-P0.16 (activos a nivel bajo) y 4 botones en P0.11, P0.12, P0.24, P0.25 (activos a bajo, pull-up interno a activar). Solo la placa; el pulsador S1 del DK (o el botón de usuario del MDK) basta para probar.

Cableado

Sin cableado. Para un botón externo entre un pin del conector y GND, declara el pull-up en el overlay devicetree.

Funcionamiento

gpio_pin_interrupt_configure_dt arma el flanco activo (nivel bajo aquí); la ISR solo alterna el LED: la regla Zephyr es mantener los callbacks GPIO lo más cortos posible.

Ejecución

Proyecto Zephyr: west build -b nrf52833dk/nrf52833 y luego west flash (J-Link embarcado); consola serie 115200 en el conector USB UART.

Variantes

Sustituir el toggle por un k_sem_give para despertar un hilo, armar ambos flancos (EDGE_BOTH) o añadir los botones sw1 a sw3 del DK.

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;
}

← Volver a la ficha