| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include "commGPIO.h"
- int gpio_export(int pin)
- {
- char buffer[64];
- int len;
- int fd;
-
- fd = open("/sys/class/gpio/export", O_WRONLY);
- if (fd < 0) {
- MSG("Failed to open export for writing!");
- return(-1);
- }
-
- len = snprintf(buffer, sizeof(buffer), "%d", pin);
- if (write(fd, buffer, len) < 0) {
- MSG("Failed to export gpio!");
- return -1;
- }
-
- close(fd);
- return 0;
- }
- int gpio_unexport(int pin)
- {
- char buffer[64];
- int len;
- int fd;
-
- fd = open("/sys/class/gpio/unexport", O_WRONLY);
- if (fd < 0) {
- MSG("Failed to open unexport for writing!");
- return -1;
- }
-
- len = snprintf(buffer, sizeof(buffer), "%d", pin);
- if (write(fd, buffer, len) < 0) {
- MSG("Failed to unexport gpio!");
- return -1;
- }
-
- close(fd);
- return 0;
- }
- //dir: 0-->IN, 1-->OUT
- int gpio_direction(int pin, int dir)
- {
- static const char dir_str[] = "in\0out";
- char path[64];
- int fd;
-
- snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
- fd = open(path, O_WRONLY);
- if (fd < 0) {
- MSG("Failed to open gpio direction for writing!");
- return -1;
- }
-
- if (write(fd, &dir_str[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0) {
- MSG("Failed to set direction!");
- return -1;
- }
-
- close(fd);
- return 0;
- }
- //value: 0-->LOW, 1-->HIGH
- int gpio_write(int pin, int value)
- {
- static const char values_str[] = "01";
- char path[64];
- int fd;
-
- snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
- fd = open(path, O_WRONLY);
- if (fd < 0) {
- MSG("Failed to open gpio value for writing!");
- return -1;
- }
-
- if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0) {
- MSG("Failed to write value!");
- return -1;
- }
-
- close(fd);
- return 0;
- }
- int gpio_read(int pin)
- {
- char path[64];
- char value_str[3];
- int fd;
-
- snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
- fd = open(path, O_RDONLY);
- if (fd < 0) {
- MSG("Failed to open gpio value for reading!");
- return -1;
- }
-
- if (read(fd, value_str, 3) < 0) {
- MSG("Failed to read value!");
- return -1;
- }
-
- close(fd);
- return (atoi(value_str));
- }
- // none表示引脚为输入,不是中断引脚
- // rising表示引脚为中断输入,上升沿触发
- // falling表示引脚为中断输入,下降沿触发
- // both表示引脚为中断输入,边沿触发
- // 0-->none, 1-->rising, 2-->falling, 3-->both
- int gpio_edge(int pin, int edge)
- {
- const char dir_str[] = "none\0rising\0falling\0both";
- int ptr;
- char path[64];
- int fd;
- switch(edge){
- case 0:
- ptr = 0;
- break;
- case 1:
- ptr = 5;
- break;
- case 2:
- ptr = 12;
- break;
- case 3:
- ptr = 20;
- break;
- default:
- ptr = 0;
- break;
- }
-
- snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);
- fd = open(path, O_WRONLY);
- if (fd < 0) {
- MSG("Failed to open gpio edge for writing!");
- return -1;
- }
-
- if (write(fd, &dir_str[ptr], strlen(&dir_str[ptr])) < 0) {
- MSG("Failed to set edge!");
- return -1;
- }
-
- close(fd);
- return 0;
- }
- //实现中断检测函数
- //GPIO1_17
- int main_test()
- {
- int gpio_fd, ret;
- struct pollfd fds[1];
- char buff[10];
- unsigned char cnt = 0;
- //LED引脚初始化
- gpio_export(115);
- gpio_direction(115, 1);
- gpio_write(115, 0);
- //按键引脚初始化
- gpio_export(49);
- gpio_direction(49, 0);
- gpio_edge(49,1);
- gpio_fd = open("/sys/class/gpio/gpio49/value",O_RDONLY);
- if(gpio_fd < 0){
- MSG("Failed to open value!");
- return -1;
- }
- fds[0].fd = gpio_fd;
- fds[0].events = POLLPRI;
- ret = read(gpio_fd,buff,10);
- if( ret == -1 )
- MSG("read");
- while(1){
- ret = poll(fds,1,0);
- if( ret == -1 )
- MSG("poll\n");
- if( fds[0].revents & POLLPRI){
- ret = lseek(gpio_fd,0,SEEK_SET);
- if( ret == -1 )
- MSG("lseek");
- ret = read(gpio_fd,buff,10);
- if( ret == -1 )
- MSG("read");
- gpio_write(115, cnt++%2);
- }
- usleep(100000);
- }
- return 0;
- }
|