-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio.c
More file actions
156 lines (120 loc) · 3.3 KB
/
gpio.c
File metadata and controls
156 lines (120 loc) · 3.3 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "log.h"
#include "gpio.h"
#define BUFF_SIZE 65
int setup_gpio(uint8_t pin, uint8_t mode)
{
int fd;
char buff[BUFF_SIZE];
ssize_t bytes_written;
//Check if its already open, reuse exported pin
snprintf(buff, BUFF_SIZE, "/sys/class/gpio%d/direction", pin);
if(open(buff, O_RDONLY) < 0){
if(ENOENT == errno){
fd = open("/sys/class/gpio/export", O_WRONLY);
if(fd < 0){
// Log the error here
exit(EXIT_FAILURE);
}
bytes_written = snprintf(buff, BUFF_SIZE, "%d", pin);
if(write(fd, buff, bytes_written) < 0){
// Log the error here
pinmon_log_write("Cannot export gpio pin.");
exit(EXIT_FAILURE);
}
close(fd);
}
}
//Setting mode
sprintf(buff, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(buff, O_WRONLY);
if(fd < 0){
// Log the error here
pinmon_log_write("Cannot open pin direction file.");
exit(EXIT_FAILURE);
}
if(mode == GPIO_OUT){
bytes_written = snprintf(buff, BUFF_SIZE, "out");
}else{
bytes_written = snprintf(buff, BUFF_SIZE, "in");
}
if(write(fd, buff, bytes_written) < 0){
// Log the error here
pinmon_log_write("Cannot set pin direction.");
exit(EXIT_FAILURE);
}
close(fd);
return 0;
}
int cleanup_gpio(uint8_t pin)
{
int fd;
char buff[BUFF_SIZE];
ssize_t bytes_written;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if(fd < 0){
// Log the error here
pinmon_log_write("Cannot open unexport file.");
exit(EXIT_FAILURE);
}
bytes_written = snprintf(buff, BUFF_SIZE, "%d", pin);
if(write(fd, buff, bytes_written) < 0){
// Log the error here
pinmon_log_write("Cannot write to unexport file.");
exit(EXIT_FAILURE);
}
close(fd);
return 0;
}
int gpio_read(uint8_t pin)
{
int fd;
char buff[BUFF_SIZE];
char value_str[3];
snprintf(buff, BUFF_SIZE, "/sys/class/gpio/gpio%d/value", pin);
fd = open(buff, O_RDONLY);
if (fd < 0) {
// Log the error here
pinmon_log_write("Cannot open gpio value file.");
exit(EXIT_FAILURE);
}
if (read(fd, value_str, 3) < 0) {
// Log the error here
pinmon_log_write("Cannot read gpio value file.");
exit(EXIT_FAILURE);
}
close(fd);
return(atoi(value_str));
}
int gpio_write(uint8_t pin, uint8_t value)
{
int fd;
char buff[BUFF_SIZE];
char value_str[3];
snprintf(buff, BUFF_SIZE, "/sys/class/gpio/gpio%d/value", pin);
fd = open(buff, O_WRONLY);
if (fd < 0) {
// Log the error here
pinmon_log_write("Cannot read gpio value file.");
exit(EXIT_FAILURE);
}
if(value == 1){
snprintf(buff, 1, "1");
}else{
snprintf(buff, 1, "0");
}
if (write(fd, buff, 1) < 0) {
// Log the error here
pinmon_log_write("Cannot write to gpio value file.");
exit(EXIT_FAILURE);
}
close(fd);
return 0;
}