gpio-irq-interrupt-10.ino
Configures a GPIO interrupt to detect button press, freeing the processor from constant polling.
Connect a button between the GPIO pin and ground (with internal or external pull-up). Interrupt configuration often requires specific mapping in the Efinity constraint file to link the GPIO to the IRQ line. The interrupt handler must be short and fast.
gpio-irq-interrupt-10.ino
#include <stdio.h>
#include "xhal_gpio.h"
#include "xhal_irq.h"
#define BTN_PIN GPIO_PIN_2
#define IRQ_LINE GPIO_IRQ_LINE_2 // À mapper dans Efinity
volatile int button_pressed = 0;
void gpio_irq_handler(void) {
// Effacer l'interruption
xhal_gpio_clear_irq_status(BTN_PIN);
button_pressed = 1;
}
int main(void) {
xhal_gpio_init();
xhal_gpio_config(BTN_PIN, GPIO_INPUT);
// Configurer l'interruption sur front descendant
xhal_gpio_configure_irq(BTN_PIN, GPIO_IRQ_FALLING);
// Enregistrer le handler
xhal_irq_register(IRQ_LINE, gpio_irq_handler);
xhal_irq_enable(IRQ_LINE);
printf("Waiting for button press...\n");
while(1) {
if (button_pressed) {
printf("Button Pressed!\n");
button_pressed = 0;
}
// WFI pour économie d'énergie (si supporté)
// asm volatile("wfi");
}
}