This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
products:sbc:common:applications:gpio:irq [2022/07/04 02:47] frank [System Configuration] fixup error |
products:sbc:common:applications:gpio:irq [2022/08/07 22:41] (current) olivia [Introduction] |
||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ===== Introduction ===== | ===== Introduction ===== | ||
| - | This document mainly introduces how to use GPIO interrupts on Khadas SBC. You will know how to use GPIO interrupts with the GPIO number test program. | + | The page leads you to use GPIO interrupts with the GPIO number test program |
| <WRAP important > | <WRAP important > | ||
| - | Use '' | + | Here we use '' |
| </ | </ | ||
| - | |||
| ===== Hardware Connection ===== | ===== Hardware Connection ===== | ||
| - | Connect the physical pins (PIN20 and PIN15) using a DuPont line. | + | Connect the physical pins '' |
| ===== System Configuration ===== | ===== System Configuration ===== | ||
| - | * Get '' | + | Get '' |
| - | Refer [[/ | + | |
| - | * Export GPIO | + | Refer to [[/ |
| - | ```sh | + | |
| + | Export GPIO | ||
| + | |||
| + | ```shell | ||
| $ echo 433 | sudo tee / | $ echo 433 | sudo tee / | ||
| ``` | ``` | ||
| <WRAP info > | <WRAP info > | ||
| - | Please use gpio readall to check the status of GPIOH_6, if it is not shown as a normal GPIO, you need to remove uart3 from overlays in the / | + | Please use '' |
| - | Check the [[/products/sbc/vim4/ | + | Check the [[products:sbc:vim4: |
| </ | </ | ||
| Line 32: | Line 33: | ||
| ===== Demo Source Code ===== | ===== Demo Source Code ===== | ||
| - | * Get demo source code | + | Get demo source code |
| - | ```sh | + | ```c gpio_interrupts.c |
| - | $ wget https://dl.khadas.com/development/ | + | // SPDX-License-Identifier: |
| - | ``` | + | /* |
| + | * Copyright (c) 2022 Wesion Technology Co., Ltd. | ||
| + | * | ||
| + | * Author: Yan < | ||
| + | * | ||
| + | */ | ||
| - | * Compile the source code | + | #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 | ||
| - | ```sh | + | ```shell |
| $ gcc -o gpio_interrupts gpio_interrupts.c | $ gcc -o gpio_interrupts gpio_interrupts.c | ||
| ``` | ``` | ||
| Line 48: | Line 218: | ||
| The test code running format is as follows: | The test code running format is as follows: | ||
| - | ```sh | + | ```shell |
| $ sudo ./ | $ sudo ./ | ||
| Line 55: | Line 225: | ||
| Run test: | Run test: | ||
| - | ```sh | + | ```shell |
| $ sudo ./ | $ sudo ./ | ||
| . | . | ||