-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinHeader.h
139 lines (124 loc) · 3.23 KB
/
pinHeader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*************************************************************************
> File Name: digital.c
> Author: Guoger
> Mail: 505917699@qq.com
> Created Time: Fri Jan 1 00:14:17 2015
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#define GPIO_MODE_DIR "/sys/devices/virtual/misc/gpio/mode/"
#define GPIO_PIN_DIR "/sys/devices/virtual/misc/gpio/pin/"
#define GPIO_IF_PREFIX "gpio"
#define INPUT 0
#define OUTPUT 1
#define LOW 0
#define HIGH 1
#define MAX_GPIO_NUM 23
int gpio_mode_fd[MAX_GPIO_NUM+1];
int gpio_pin_fd[MAX_GPIO_NUM+1];
extern int err;
int digitalRead(uint8_t pin){
int fd = -1;
char buf[4];
int ret = -1;
char path[1024];
memset((void*)buf, 0, sizeof(buf));
memset(path, 0, sizeof(path));
sprintf(path, "%s%s%d", GPIO_PIN_DIR, GPIO_IF_PREFIX, pin);
// fprintf(stdout, "%s%s%d", GPIO_PIN_DIR, GPIO_IF_PREFIX, pin);
gpio_pin_fd[pin] = open(path, O_RDWR);
lseek(gpio_pin_fd[pin], 0, SEEK_SET);
ret = read(gpio_pin_fd[pin], buf, sizeof(buf));
close(gpio_pin_fd[pin]);
if(ret <= 0)
fprintf(stderr, "read gpio%d failied\n", pin);
ret = buf[0] - '0';
return ret;
}
void digitalWrite(uint8_t pin, uint8_t value){
int fd = -1;
int ret = -1;
char path[1024];
char *mesg = NULL;
memset(path, 0 , sizeof(path));
sprintf(path, "%s%s%d", GPIO_PIN_DIR, GPIO_IF_PREFIX, pin);
// fprintf(stdout, "%s%s%d\n", GPIO_PIN_DIR, GPIO_IF_PREFIX, pin);
gpio_pin_fd[pin] = open(path, O_RDWR);
if(gpio_pin_fd[pin] < 0){
// fprintf(stderr, "OPEN GPIO%d Fail\n",pin);
fprintf(stdout, "%s%s%d\n", GPIO_PIN_DIR, GPIO_IF_PREFIX, pin);
fprintf(stdout, "err = %d\n", err);
mesg = strerror(err);
printf("%s\n", mesg);
exit(-1);
}
lseek(gpio_pin_fd[pin], 0, SEEK_SET);
if(value == HIGH){
ret = write(gpio_pin_fd[pin], "1", 1);
if(ret < 0)
fprintf(stderr,"Set GPIO%d High Fail\n",pin);
}
else{
ret = write(gpio_pin_fd[pin], "0", 1);
if(ret < 0)
fprintf(stderr, "Set GPIO%d LOW Fail\n", pin);
}
if(gpio_pin_fd[pin]){
close(gpio_pin_fd[pin]);
}
}
void pinMode(uint8_t pin, uint8_t mode){
int fd = -1;
int ret = -1;
unsigned long val = pin;
char path[1024];
switch(pin){
case 3:
case 9:
case 10:
case 11:
fd = open("/dev/pwmtimer", O_RDONLY);
if(fd < 0)
printf("open pwm device fail\n");
ret = ioctl(fd, 0x102, &val);
if(ret < 0)
printf("can't set pwmtimer stop\n");
if(fd)
close(fd);
break;
default:
break;
}
switch (mode){
case INPUT:
memset(path, 0 , sizeof(path));
sprintf(path, "%s%s%d", GPIO_MODE_DIR, GPIO_IF_PREFIX, pin);
gpio_mode_fd[pin] = open(path, O_RDWR);
if(gpio_mode_fd[pin] < 0){
fprintf(stderr, "open %s failed\n", path);
return;
}
write(gpio_mode_fd[pin], "0", 1);
break;
case OUTPUT:
memset(path, 0 , sizeof(path));
sprintf(path, "%s%s%d", GPIO_MODE_DIR, GPIO_IF_PREFIX, pin);
gpio_mode_fd[pin] = open(path, O_RDWR);
if(gpio_mode_fd[pin] < 0){
fprintf(stderr, "open %s failed\n", path);
return;
}
write(gpio_mode_fd[pin], "1", 1);
break;
default:
break;
}
if(gpio_mode_fd[pin])
close(gpio_mode_fd[pin]);
}