-
Notifications
You must be signed in to change notification settings - Fork 5
/
sysfs-gpio.c
248 lines (209 loc) · 6.14 KB
/
sysfs-gpio.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* rf-ctrl - A command-line tool to control 433MHz OOK based devices
* SYSFS GPIO-based 433 MHz RF Transmitter driver
*
* Copyright (C) 2018 Jean-Christophe Rona <jc@rona.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "rf-ctrl.h"
#define HARDWARE_NAME "SYSFS GPIO"
#define GPIO_SYSFS_ROOT "/sys/class/gpio"
#define GPIO_SYSFS_EXPORT "export" // At root level
#define GPIO_SYSFS_UNEXPORT "unexport" // At root level
#define GPIO_SYSFS_DIRECTION "direction" // At root/gpio<num> level
#define GPIO_SYSFS_VALUE "value" // At root/gpio<num> level
static uint16_t gpio_num;
static int sysfs_gpio_probe(void) {
/* This driver cannot be auto-detected */
return -1;
}
static int sysfs_gpio_init(struct rf_hardware_params *params) {
FILE *f;
char path[256];
if (!(params->provided_params & PARAM_GPIO)) {
fprintf(stderr, "%s: GPIO parameter missing !\n", HARDWARE_NAME);
return -1;
}
gpio_num = params->gpio;
printf("%s: Using GPIO %u\n", HARDWARE_NAME, gpio_num);
/* Export the GPIO used by the transmitter */
f = fopen(GPIO_SYSFS_ROOT"/"GPIO_SYSFS_EXPORT, "w");
if (f == NULL) {
fprintf(stderr, "%s: Unable to open %s !\n", HARDWARE_NAME, GPIO_SYSFS_ROOT"/"GPIO_SYSFS_EXPORT);
return -1;
}
fprintf(f, "%u\n", gpio_num);
fclose(f);
/* Set the direction of the GPIO as output */
snprintf(path, 256, "%s/gpio%u/%s", GPIO_SYSFS_ROOT, gpio_num, GPIO_SYSFS_DIRECTION);
f = fopen(path, "w");
if (f == NULL) {
fprintf(stderr, "%s: Unable to open %s !\n", HARDWARE_NAME, path);
return -1;
}
fprintf(f, "out\n");
fclose(f);
return 0;
}
static void sysfs_gpio_close(void) {
FILE *f;
/* Release the GPIO used by the transmitter */
f = fopen(GPIO_SYSFS_ROOT"/"GPIO_SYSFS_UNEXPORT, "w");
if (f == NULL) {
fprintf(stderr, "%s: Unable to open %s !\n", HARDWARE_NAME, GPIO_SYSFS_ROOT"/"GPIO_SYSFS_UNEXPORT);
return;
}
fprintf(f, "%u\n", gpio_num);
fclose(f);
}
static int sysfs_gpio_send_cmd(struct timing_config *config, uint8_t *frame_data, uint16_t bit_count) {
int fd;
char path[256];
uint16_t i, j;
char old_bit, new_bit;
snprintf(path, 256, "%s/gpio%u/%s", GPIO_SYSFS_ROOT, gpio_num, GPIO_SYSFS_VALUE);
fd = open(path, O_WRONLY| O_SYNC);
if (fd < 0) {
fprintf(stderr, "%s: Unable to open %s (%s) !\n", HARDWARE_NAME, path, strerror(errno));
return fd;
}
for (j = 0; j < config->frame_count; j++) {
switch (config->bit_fmt) {
case RF_BIT_FMT_HL:
if (config->start_bit_h_time > 0) {
write(fd, "1", 1);
usleep(config->start_bit_h_time);
}
if (config->start_bit_l_time > 0) {
write(fd, "0", 1);
usleep(config->start_bit_l_time);
}
break;
case RF_BIT_FMT_LH:
if (config->start_bit_l_time > 0) {
write(fd, "0", 1);
usleep(config->start_bit_l_time);
}
if (config->start_bit_h_time > 0) {
write(fd, "1", 1);
usleep(config->start_bit_h_time);
}
break;
}
/* Toggle the GPIO bit by bit */
old_bit = 'N';
for (i = 0; i < bit_count; i++) {
new_bit = (frame_data[i/8] & (1 << (7 - (i % 8)))) ? '1' : '0';
switch (config->bit_fmt) {
case RF_BIT_FMT_HL:
if (new_bit == '1') {
if (config->data_bit1_h_time > 0) {
write(fd, "1", 1);
usleep(config->data_bit1_h_time);
}
if (config->data_bit1_l_time > 0) {
write(fd, "0", 1);
usleep(config->data_bit1_l_time);
}
} else {
if (config->data_bit0_h_time > 0) {
write(fd, "1", 1);
usleep(config->data_bit0_h_time);
}
if (config->data_bit0_l_time > 0) {
write(fd, "0", 1);
usleep(config->data_bit0_l_time);
}
}
break;
case RF_BIT_FMT_LH:
if (new_bit == '1') {
if (config->data_bit1_l_time > 0) {
write(fd, "0", 1);
usleep(config->data_bit1_l_time);
}
if (config->data_bit1_h_time > 0) {
write(fd, "1", 1);
usleep(config->data_bit1_h_time);
}
} else {
if (config->data_bit0_l_time > 0) {
write(fd, "0", 1);
usleep(config->data_bit0_l_time);
}
if (config->data_bit0_h_time > 0) {
write(fd, "1", 1);
usleep(config->data_bit0_h_time);
}
}
break;
case RF_BIT_FMT_RAW:
if (new_bit != old_bit) {
old_bit = new_bit;
write(fd, &new_bit, 1);
}
usleep(config->base_time);
break;
}
}
switch (config->bit_fmt) {
case RF_BIT_FMT_HL:
if (config->end_bit_h_time > 0) {
write(fd, "1", 1);
usleep(config->end_bit_h_time);
}
if (config->end_bit_l_time > 0) {
write(fd, "0", 1);
usleep(config->end_bit_l_time);
}
break;
case RF_BIT_FMT_LH:
if (config->end_bit_l_time > 0) {
write(fd, "0", 1);
usleep(config->end_bit_l_time);
}
if (config->end_bit_h_time > 0) {
write(fd, "1", 1);
usleep(config->end_bit_h_time);
}
break;
}
}
/* Make sure to turn off the transmitter */
write(fd, "0", 1);
close(fd);
return 0;
}
struct rf_hardware_driver sysfs_gpio_driver = {
.name = HARDWARE_NAME,
.cmd_name = "sysfs-gpio",
.long_name = "SYSFS GPIO-based 433 MHz RF Transmitter",
.supported_bit_fmts = (1 << RF_BIT_FMT_HL) | (1 << RF_BIT_FMT_LH) | (1 << RF_BIT_FMT_RAW),
.needed_hw_params = PARAM_GPIO,
.probe = &sysfs_gpio_probe,
.init = &sysfs_gpio_init,
.close = &sysfs_gpio_close,
.send_cmd = &sysfs_gpio_send_cmd,
};