[SC206E][GPIO] How to Use GPIO Pin as an Interrupt?

Hi everyone,

I am currently using GPIO pin 36 for interrupt detection, but I am using a polling method to check its status.

I want to register a function so that it gets called immediately when an interrupt occurs on the GPIO pin. How can I achieve this?

I have attached my C code for reference. Any guidance would be greatly appreciated!

Thanks in advance.
interrupt.c (2.2 KB)

I have to suggest: use libgpiod library

  1. sudo apt-get install gpiod libgpiod-dev
  2. 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;
}

If you cannot use libgpiod, you can implement pseudo callbacks through multithreading based on the original code: interrupt.c
#include <pthread.h>

// Global callback function pointer
void (*callback)(int) = NULL;

void register_callback(void (*func)(int)) {
callback = func;
}

// Call callback in wait_for_gpio_interrupt
void wait_for_gpio_interrupt() {
// … (original code)
while (1) {
poll(&pfd, 1, -1);
read(fd, buffer, sizeof(buffer));
if (callback) callback(buffer[0] - ‘0’);
}
}

// Example of starting a thread
void* gpio_monitor_thread(void* arg) {
wait_for_gpio_interrupt();
return NULL;
}

int main() {
// Register callback function
register_callback(gpio_interrupt_callback);

// Create thread
pthread_t thread;
pthread_create(&thread, NULL, gpio_monitor_thread, NULL);
pthread_join(thread, NULL);

return 0;
}

@keven.wu-Q

Thank You for response .

I was trying to use a specific pin, but it turns out that the pin is already used in an LDO configuration, which is causing an error.

I want to know how I can configure a single SPI pin with a pull-up resistor, even though it is defined in the DTSI file under SPI configuration.

In other words, if a pin is already defined for SPI in the DTSI file, is it still possible to modify its configuration (e.g., enable a pull-up) based on my specific needs?

Is this possible, and if so, how can it be done?

The document where it shows how a single pin is configured , i am attaching screenshot of that specific part.


Generally speaking, active is to make the pin effective. Then set bias-pull-up; that is, to make the internal resistor pull up.
The pin has been used in the LDO configuration, you can comment or shield the relevant gpio in the pinctrl file.

@keven.wu-Q

Thank You for you quick response .

This is to clarify that …

Does it mean that if I want to change the inbuilt SPI GPIO chip select (CS) pin defined in the device tree, I can replace it with another GPIO pin?

Yes, in theory, but it is recommended that you search whether the gpio number you want to use is used in other files. If it is used, it must be disabled, otherwise the GPIO application will fail.