linux应用开发 —— GPIO控制_linux 应用控制gpio

int gpio_set_edge(int gpio, edge_t edge);
int gpio_edge_none(int gpio);
int gpio_edge_rising(int gpio);
int gpio_edge_falling(int gpio);
int gpio_edge_both(int gpio);

int gpio_set_direction(int gpio, gpio_dir_t dir);
int gpio_direction_input(int gpio);
int gpio_direction_output(int gpio);

void gpio_set_value(int gpio, int value);
int gpio_get_value(int gpio);

#endif /* __GPIO_H__ */


**gpio.c:**



#include “gpio.h”
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

/*
gpio 编号计算
index = GPIOn_IOx = (n-1)*32 + x
例如蜂鸣器使用的引脚编号为:index = GPIO1_19 = (1-1)*32 +19 = 19。
又例如GPIO4_IO20的编号为:index = GPIO4_IO20 = (4-1)*32+20=116。
*/
int gpio_export(int gpio)
{
int fd;
char gpio_path[100] = {0};

/* GPIO目录路径 */
sprintf(gpio_path, “/sys/class/gpio/gpio%d”, gpio);
/* 判断GPIO目录是否存在 */
if (access(gpio_path, F_OK) == 0) return 0;

if (0 > (fd = open("/sys/class/gpio/export", O_WRONLY))) 
{ 
    perror("open error"); 
    return -1;
} 

/\* 导出 gpio \*/
char gpio_str[10] = {0};
sprintf(gpio_str, "%d", gpio);
int len = strlen(gpio_str);
if (len != write(fd, gpio_str, len))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值