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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
/*
* The Rin Library – gpio module
*
* Copyright (C) 2016 Gediminas Jakutis
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "rin/gpio.h"
#include "gpio_private.h"
unsigned int rin_gpio_open(const unsigned int num, const enum rin_gpiotype type)
{
struct stat st;
struct timespec grace = { 0, 1 * 1000 * 1000 }; /* 1 ms */
char buf[48] = { 0 };
int fd;
unsigned int timeout;
init();
if (type == rin_gpio_closed || !rin_gpio_pin_exists(num) || gpio[num].type != rin_gpio_closed) {
return 1;
}
fd = open("/sys/class/gpio/export", O_WRONLY | O_DSYNC);
sprintf(buf, "%u", num);
write(fd, buf, strlen(buf));
close(fd);
sprintf(buf, "/sys/class/gpio/gpio%u/direction", num);
timeout = 500; /* 500 ms, if it takes longer, something ain't right. */
while (stat(buf, &st)) {
nanosleep(&grace, NULL);
if (!(--timeout)) {
return 1;
}
}
fd = open(buf, O_WRONLY | O_DSYNC);
if (type == rin_gpio_in) {
write(fd, "in", 2);
} else {
write(fd, "out", 3);
}
close(fd);
gpio[num].type = type;
sprintf(buf, "/sys/class/gpio/gpio%u/value", num);
if (type == rin_gpio_in) {
gpio[num].fd = open(buf, O_RDONLY);
} else {
gpio[num].fd = open(buf, O_WRONLY);
}
return 0;
}
unsigned int rin_gpio_close(const unsigned int num)
{
char buf[32] = { 0 };
struct stat st;
int fd;
init();
sprintf(buf, "/sys/class/gpio/gpio%u", num);
if (stat(buf, &st)) {
return 0;
}
close(gpio[num].fd);
gpio[num].fd = -1;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
sprintf(buf, "%u", num);
write(fd, buf, strlen(buf));
close(fd);
gpio[num].type = rin_gpio_closed;
return 0;
}
unsigned int rin_gpio_setval(const unsigned int num, const unsigned int val)
{
char buf[8] = { 0 };
init();
if (!rin_gpio_pin_exists(num) || gpio[num].type != rin_gpio_out) {
return 1;
}
sprintf(buf, "%u", val);
lseek(gpio[num].fd, 0, SEEK_SET);
write(gpio[num].fd, buf, strlen(buf));
return 0;
}
unsigned int rin_gpio_readval(const unsigned int num, unsigned int * const val)
{
init();
if (!rin_gpio_pin_exists(num) || gpio[num].type != rin_gpio_in) {
exit(EXIT_FAILURE);
}
lseek(gpio[num].fd, 0, SEEK_SET);
read(gpio[num].fd, val, 1);
if (*val >= '0') {
*val -= '0';
}
return 0;
}
unsigned int rin_gpio_writesequence(const struct rin_gpio_dataset * const dataset)
{
unsigned char *data;
size_t i;
if (!dataset || !dataset->data) {
return 1;
}
data = dataset->data;
for (i = 0; i < dataset->len; ++i) {
unsigned char a;
unsigned char bit;
a = data[i / CHAR_BIT];
bit = !(!(a & (1 << (i % CHAR_BIT))));
rin_gpio_setval(dataset->signalpin, bit);
rin_gpio_pulse(dataset->clockpin, dataset->pulseduration);
if (dataset->delay) {
nanosleep(dataset->delay, NULL);
}
}
return 0;
}
unsigned int rin_gpio_on(const unsigned int num)
{
return rin_gpio_setval(1, num);
}
unsigned int rin_gpio_off(const unsigned int num)
{
return rin_gpio_setval(0, num);
}
unsigned int rin_gpio_pulse(const unsigned int num, const struct timespec * const pulselen)
{
unsigned int ret;
ret = rin_gpio_on(num);
if (ret) {
return ret;
}
if (pulselen) {
nanosleep(pulselen, NULL);
}
ret = rin_gpio_off(num);
return ret;
}
/*
* right now is rpi V1 specific, should be rewritten when/if we support
* other platforms.
*/
unsigned int rin_gpio_pin_exists(const unsigned int pin)
{
switch (pin) {
case 2:
case 3:
case 4:
case 7:
case 8:
case 9:
case 10:
case 11:
case 14:
case 15:
case 17:
case 18:
case 22:
case 23:
case 24:
case 25:
return 1;
default:
return 0;
}
}
/*
* in case we ever support hardware other than the V1 Raspberry,
* we need this to get the pin usable pin count
*/
unsigned int rin_gpio_get_pincount(void)
{
return 16;
}
static void init(void)
{
size_t i;
static unsigned int firstrun = 1;
if (firstrun) {
firstrun = 0;
for (i = 0; i < gpio_count; ++i) {
gpio[i].num = i;
gpio[i].type = rin_gpio_closed;
gpio[i].fd = -1;
}
}
}
|