-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpio_utils.c
executable file
·170 lines (148 loc) · 4.11 KB
/
gpio_utils.c
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
/*
Copyright (C) 2020-2021 Vincent Buso <[email protected]>
Copyright (C) 2021 Michel Stempin <[email protected]>
This file is part of the FunKey S GPIO keyboard daemon.
This 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; either
version 2.1 of the License, or (at your option) any later version.
The software 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 the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
*/
/**
* @file gpio_utils.c
* This file contains the GPIO utility functions
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "gpio_utils.h"
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64
/* Export a GPIO in the sysfs pseudo-filesystem */
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof (buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
/* Unexport a GPIO in the sysfs pseudo-filesystem */
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof (buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
/* Set a GPIO direction in the sysfs pseudo-filesystem */
int gpio_set_dir(unsigned int gpio, const char* dir)
{
int fd;
char buf[MAX_BUF];
snprintf(buf, sizeof (buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/direction");
return fd;
}
write(fd, dir, sizeof (dir)+1);
close(fd);
return 0;
}
/* Set a GPIO value in the sysfs pseudo-filesystem */
int gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd;
char buf[MAX_BUF];
snprintf(buf, sizeof (buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-value");
return fd;
}
if (value) {
write(fd, "1", 2);
} else {
write(fd, "0", 2);
}
close(fd);
return 0;
}
/* Get a GPIO value from the sysfs pseudo-filesystem */
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd;
char buf[MAX_BUF];
char ch;
snprintf(buf, sizeof (buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd < 0) {
perror("gpio/get-value");
return fd;
}
read(fd, &ch, 1);
if (ch != '0') {
*value = 1;
} else {
*value = 0;
}
close(fd);
return 0;
}
/* Set a GPIO interrupt edge in the sysfs pseudo-filesystem, must be a string
* among "none", "rising", "falling", or "both"
*/
int gpio_set_edge(unsigned int gpio, const char *edge)
{
int fd;
char buf[MAX_BUF];
snprintf(buf, sizeof (buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-edge");
return fd;
}
write(fd, edge, strlen(edge) + 1);
close(fd);
return 0;
}
/* Open a GPIO pseudo-file from the sysfs pseudo-filesystem */
int gpio_fd_open(unsigned int gpio, unsigned int dir)
{
int fd;
char buf[MAX_BUF];
snprintf(buf, sizeof (buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, dir | O_NONBLOCK );
if (fd < 0) {
perror("gpio/fd_open");
}
return fd;
}
/* Close a GPIO pseudo-file from the sysfs pseudo-filesystem */
int gpio_fd_close(int fd)
{
return close(fd);
}