I have to suggest: use libgpiod library
- sudo apt-get install gpiod libgpiod-dev
- Use libgpiod to implement interrupt callback
The code is as follows: interrupt.c
#define GPIO_CHIP_NAME “gpiochip0” // GPIO controller name (adjust according to the actual system)
#define GPIO_PIN 36 // GPIO pin number (physical pin number)
static volatile int running = 1;
// Custom callback function (called when an interrupt occurs)
void gpio_interrupt_callback(int value) {
printf(“GPIO interrupt triggered! Current value: %d\n”, value);
}
// Signal processing function (for graceful exit)
void signal_handler(int sig) {
running = 0;
}
int main() {
struct gpiod_chip *chip;
struct gpiod_line *line;
int ret;
// Register signal processing
signal(SIGINT, signal_handler);
// Open GPIO controller
chip = gpiod_chip_open_by_name(GPIO_CHIP_NAME);
if (!chip) {
perror(“Unable to open GPIO controller”);
return EXIT_FAILURE;
}
// Get GPIO pin
line = gpiod_chip_get_line(chip, GPIO_PIN);
if (!line) {
perror(“Unable to get GPIO pin”);
gpiod_chip_close(chip);
return EXIT_FAILURE;
}
// Configure as input and set edge detection (double edge)
ret = gpiod_line_request_rising_edge_events(line, “gpio-interrupt”);
if (ret < 0) {
perror(“Unable to configure GPIO interrupt”);
gpiod_chip_close(chip);
return EXIT_FAILURE;
}
printf(“Waiting for GPIO %d interrupt…\n”, GPIO_PIN);
// Loop waiting for interrupt
while (running) {
// Block waiting for event (timeout is set to -1 to indicate infinite waiting)
ret = gpiod_line_event_wait(line, NULL, -1);
if (ret < 0) {
perror(“Waiting for event failed”);
break;
} else if (ret == 1) {
// Read current GPIO value
int value = gpiod_line_get_value(line);
// Call callback function
gpio_interrupt_callback(value);
}
}
// Clean up resources
gpiod_line_release(line);
gpiod_chip_close(chip);
return EXIT_SUCCESS;
}