AIOTNIK-ONE开发板预留了一排GPIO,可以当作普通的IO口来使用。原理图和实物图如下。
以下是GPIO的使用代码示例,实现主控输出GPIO口的高低电压。
/*
*gpio_test.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/time.h>
#include <errno.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/tcp.h>
int open_gpio_port(int gpiogroup, int gpio, char *mode)
{
/*
PA(n) = 0 * 32 + n
PB(n) = 1 * 32 + n
PC(n) = 2 * 32 + n
...
例如:申请 PB(10) = 1 * 32 + 10 = 42
*/
int fd, fd0;
/* gpio0 = 1*32 + 18 */
int gpio0 = gpiogroup*32 + gpio;
char tmp[128];
fd = open("/sys/class/gpio/export", O_WRONLY);
if(fd < 0) {
printf("open /sys/class/gpio/export error !");
return -1;
}
sprintf(tmp, "%d", gpio0);
write(fd, tmp, 2);
close(fd);
sprintf(tmp, "/sys/class/gpio/gpio%d/direction", gpio0);
fd0 = open(tmp, O_RDWR);
if(fd0 < 0) {
printf("open /sys/class/gpio/gpio%d/direction error !", gpio0);
return -1;
}
write(fd0, mode, strlen(mode));
close(fd0);
sprintf(tmp, "/sys/class/gpio/gpio%d/active_low", gpio0);
fd0 = open(tmp, O_RDWR);
if(fd0 < 0) {
printf("open /sys/class/gpio/gpio%d/active_low error !", gpio0);
return -1;
}
write(fd0, "1", 0);
close(fd0);
return 0;
}
int read_gpio_value(int gpiogroup, int gpio, int *value)
{
/*
PA(n) = 0 * 32 + n
PB(n) = 1 * 32 + n
PC(n) = 2 * 32 + n
...
例如:申请 PB(10) = 1 * 32 + 10 = 42
*/
int fd0;
/* gpio0 = 1*32 + 18 */
int gpio0 = gpiogroup*32 + gpio;
char tmp[128];
sprintf(tmp, "/sys/class/gpio/gpio%d/value", gpio0);
fd0 = open(tmp, O_RDWR);
if(fd0 < 0) {
printf("open /sys/class/gpio/gpio%d/value error !", gpio0);
return -1;
}
char buf[2] = {"\0"};
read(fd0, buf, 1);
if(buf[0] == '1')
{
*value = 1;
}
else if(buf[0] == '0'){
*value = 0;
}
close(fd0);
return *value;
}
int write_gpio_value(int gpiogroup, int gpio, int value)
{
/*
PA(n) = 0 * 32 + n
PB(n) = 1 * 32 + n
PC(n) = 2 * 32 + n
...
例如:申请 PB(10) = 1 * 32 + 10 = 42
*/
int fd0;
int gpio0 = gpiogroup*32 + gpio;
char tmp[128];
char on[4] = {0};
sprintf(tmp, "/sys/class/gpio/gpio%d/value", gpio0);
fd0 = open(tmp, O_RDWR);
if(fd0 < 0) {
printf("open /sys/class/gpio/gpio%d/value error !", gpio0);
return -1;
}
sprintf(on,"%d", value);
write(fd0, on, strlen(on));
close(fd0);
return 0;
}
/*
*gpiogroup: gpio组,例如PA,PB,PC
*gpio:gpio序号,如pa14, pb15, pc16
*mode: gpio模式,in:表示设置为输入;out:表示设置为输出
*value:高低电平,0:低电平;1:高电平
*/
int set_gpio_value(int gpiogroup, int gpio, char *mode, int value)
{
int ret = -1;
ret = open_gpio_port(gpiogroup, gpio, mode);
if(ret == -1){
printf("open_gpio_port failed!\n");
return ret;
}
ret = write_gpio_value(gpiogroup, gpio, value);
if(ret == -1){
printf("write_gpio_value failed!\n");
return ret;
}
return 0;
}
int get_gpio_value(int gpiogroup, int gpio, char *mode, int *value)
{
int ret = -1;
ret = open_gpio_port(gpiogroup, gpio, mode);
if(ret == -1){
printf("open_gpio_port failed!\n");
return ret;
}
ret = read_gpio_value(gpiogroup, gpio, value);
if(ret == -1){
printf("read_gpio_value failed!\n");
return ret;
}
return *value;
}
int main()
{
set_gpio_value(2, 14, "out", 1);//将pc14设置为高电平
set_gpio_value(2, 14, "out", 0);//将pc14设置为低电平
set_gpio_value(2, 15, "out", 1);//将pc15设置为高电平
set_gpio_value(2, 16, "out", 1);//将pc16设置为高电平
return 0;
}