This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
products:sbc:common:applications:gpio:irq [2022/06/29 21:51] 127.0.0.1 external edit |
products:sbc:common:applications:gpio:irq [2022/08/07 22:41] (current) olivia [Introduction] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | # IRQ | + | ====== GPIO Interrupts ====== |
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | The page leads you to use GPIO interrupts with the GPIO number test program on Khadas SBC. | ||
+ | |||
+ | <WRAP important > | ||
+ | Here we use '' | ||
+ | </ | ||
+ | |||
+ | ===== Hardware Connection ===== | ||
+ | |||
+ | Connect the physical pins '' | ||
+ | |||
+ | ===== System Configuration ===== | ||
+ | |||
+ | Get '' | ||
+ | |||
+ | Refer to [[/ | ||
+ | |||
+ | Export GPIO | ||
+ | |||
+ | ```shell | ||
+ | $ echo 433 | sudo tee / | ||
+ | ``` | ||
+ | |||
+ | <WRAP info > | ||
+ | Please use '' | ||
+ | Check the [[products: | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Demo Source Code ===== | ||
+ | |||
+ | Get demo source code | ||
+ | |||
+ | ```c gpio_interrupts.c | ||
+ | // SPDX-License-Identifier: | ||
+ | /* | ||
+ | * Copyright (c) 2022 Wesion Technology Co., Ltd. | ||
+ | * | ||
+ | * Author: Yan < | ||
+ | * | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define SYSFS_GPIO_DIR | ||
+ | #define MAX_BUF | ||
+ | |||
+ | int gpio_export(unsigned int gpio){ | ||
+ | |||
+ | int fd, len; | ||
+ | char buf[MAX_BUF]; | ||
+ | |||
+ | fd = open(SYSFS_GPIO_DIR "/ | ||
+ | |||
+ | if (fd < 0) { | ||
+ | fprintf(stderr, | ||
+ | return fd; | ||
+ | } | ||
+ | |||
+ | len = snprintf(buf, | ||
+ | write(fd, buf, len); | ||
+ | close(fd); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | int gpio_unexport(unsigned int gpio){ | ||
+ | |||
+ | int fd, len; | ||
+ | char buf[MAX_BUF]; | ||
+ | |||
+ | fd = open(SYSFS_GPIO_DIR "/ | ||
+ | |||
+ | if (fd < 0) { | ||
+ | fprintf(stderr, | ||
+ | return fd; | ||
+ | } | ||
+ | |||
+ | len = snprintf(buf, | ||
+ | write(fd, buf, len); | ||
+ | close(fd); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | int gpio_set_edge(unsigned int gpio, char *edge){ | ||
+ | |||
+ | int fd, len; | ||
+ | char buf[MAX_BUF]; | ||
+ | |||
+ | len = snprintf(buf, | ||
+ | |||
+ | fd = open(buf, O_WRONLY); | ||
+ | |||
+ | if (fd < 0) { | ||
+ | fprintf(stderr, | ||
+ | return fd; | ||
+ | } | ||
+ | |||
+ | write(fd, edge, strlen(edge)+1); | ||
+ | close(fd); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | int gpio_set_pull(unsigned int gpio, char *pull){ | ||
+ | |||
+ | int fd, len; | ||
+ | char buf[MAX_BUF]; | ||
+ | |||
+ | len = snprintf(buf, | ||
+ | |||
+ | fd = open(buf, O_WRONLY); | ||
+ | |||
+ | if (fd < 0) { | ||
+ | fprintf(stderr, | ||
+ | return fd; | ||
+ | } | ||
+ | |||
+ | write(fd, pull, strlen(pull)+1); | ||
+ | close(fd); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | int main(int argc, char *argv[]) | ||
+ | { | ||
+ | struct pollfd fdset[2]; | ||
+ | int fd, ret, gpio; | ||
+ | char buf[MAX_BUF]; | ||
+ | |||
+ | if (argc < 3 || argc > 4) { | ||
+ | fprintf(stdout, | ||
+ | fflush(stdout); | ||
+ | return | ||
+ | } | ||
+ | |||
+ | gpio = atoi(argv[1]); | ||
+ | if (gpio_export(gpio)) | ||
+ | fprintf(stdout, | ||
+ | fflush(stdout); | ||
+ | return | ||
+ | } | ||
+ | |||
+ | if (gpio_set_edge(gpio, | ||
+ | fprintf(stdout, | ||
+ | fflush(stdout); | ||
+ | return | ||
+ | } | ||
+ | |||
+ | if (argv[3] && gpio_set_pull(gpio, | ||
+ | fprintf(stdout, | ||
+ | fflush(stdout); | ||
+ | return | ||
+ | } | ||
+ | |||
+ | snprintf(buf, | ||
+ | fd = open(buf, O_RDWR); | ||
+ | if (fd < 0) | ||
+ | goto out; | ||
+ | |||
+ | while (1) { | ||
+ | memset(fdset, | ||
+ | fdset[0].fd = STDIN_FILENO; | ||
+ | fdset[0].events = POLLIN; | ||
+ | fdset[1].fd = fd; | ||
+ | fdset[1].events = POLLPRI; | ||
+ | ret = poll(fdset, 2, 3*1000); | ||
+ | |||
+ | if (ret < 0) { | ||
+ | perror(" | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | fprintf(stderr, | ||
+ | |||
+ | if (fdset[1].revents & POLLPRI) { | ||
+ | char c; | ||
+ | (void)read (fd, &c, 1) ; | ||
+ | lseek (fd, 0, SEEK_SET) ; | ||
+ | fprintf(stderr, | ||
+ | } | ||
+ | |||
+ | if (fdset[0].revents & POLLIN) | ||
+ | break; | ||
+ | |||
+ | fflush(stdout); | ||
+ | } | ||
+ | |||
+ | close(fd); | ||
+ | out: | ||
+ | if (gpio_unexport(gpio)) | ||
+ | fprintf(stdout, | ||
+ | fflush(stdout); | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | ``` | ||
+ | Compile the source code | ||
+ | |||
+ | ```shell | ||
+ | $ gcc -o gpio_interrupts gpio_interrupts.c | ||
+ | ``` | ||
+ | |||
+ | ===== Demonstrate ===== | ||
+ | |||
+ | The test code running format is as follows: | ||
+ | |||
+ | ```shell | ||
+ | $ sudo ./ | ||
+ | |||
+ | ``` | ||
+ | |||
+ | Run test: | ||
+ | |||
+ | ```shell | ||
+ | $ sudo ./ | ||
+ | . | ||
+ | GPIO 433 interrupt occurred! | ||
+ | .. | ||
+ | GPIO 433 interrupt occurred! | ||
+ | . | ||
+ | GPIO 433 interrupt occurred! | ||
+ | . | ||
+ | GPIO 433 interrupt occurred! | ||
+ | ``` |