gpio.cpp
8.34 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/****************************************************************************
*
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file gpio_test.cpp
* @author Julian Kent <julian@auterion.com>
*
* GPIO read and write tool
*/
#include <px4_platform_common/log.h>
#include <px4_platform_common/module.h>
#include <px4_platform_common/px4_config.h>
#include <nuttx/ioexpander/gpio.h>
#include <fcntl.h>
static void usage(const char *reason);
static int handle_board_ports(bool is_read, int argc, char *argv[]);
static int handle_device_ports(bool is_read, int argc, char *argv[]);
extern "C" __EXPORT int gpio_main(int argc, char *argv[])
{
if (argc < 3) {
usage("not enough arguments");
return -1;
}
const char *command = argv[1];
bool is_read = false;
if (strcmp(command, "read") == 0) {
is_read = true;
} else if (strcmp(command, "write") == 0) {
if (argc < 4) {
usage("not enough arguments");
return -1;
}
} else {
usage("command not read or write");
return -1;
}
if (argv[2][0] == '/') {
return handle_device_ports(is_read, argc, argv);
} else {
return handle_board_ports(is_read, argc, argv);
}
}
int handle_device_ports(bool is_read, int argc, char *argv[])
{
#ifdef CONFIG_DEV_GPIO
const char *device = argv[2];
int fd = open(device, O_RDWR);
if (fd < 0) {
usage("Opening the device failed");
return -1;
}
char *end;
if (is_read) {
gpio_pintype_e pin_type = GPIO_INPUT_PIN;
if (argc >= 4) {
const char *extra = argv[3];
if (strcasecmp(extra, "PULLUP") == 0) {
pin_type = GPIO_INPUT_PIN_PULLUP;
} else if (strcasecmp(extra, "PULLDOWN") == 0) {
pin_type = GPIO_INPUT_PIN_PULLDOWN;
} else {
usage("extra read argument not PULLUP or PULLDOWN");
goto exit_failure;
}
}
int ret = ioctl(fd, GPIOC_SETPINTYPE, pin_type);
if (ret != 0) {
PX4_ERR("ioctl GPIOC_SETPINTYPE failed: %i", ret);
goto exit_failure;
}
bool value;
ret = ioctl(fd, GPIOC_READ, (long)&value);
if (ret != 0) {
PX4_ERR("ioctl GPIOC_READ failed: %i", ret);
goto exit_failure;
}
printf("%d OK\n", (int)value);
} else {
gpio_pintype_e pin_type = GPIO_OUTPUT_PIN;
int32_t value = strtol(argv[3], &end, 10);
if (errno != 0 || *end != '\0' || (value != 0 && value != 1)) {
usage("value not 0 or 1");
goto exit_failure;
}
if (argc >= 5) {
const char *extra = argv[4];
if (strcasecmp(extra, "PUSHPULL") == 0) {
pin_type = GPIO_OUTPUT_PIN;
} else if (strcasecmp(extra, "OPENDRAIN") == 0) {
pin_type = GPIO_OUTPUT_PIN_OPENDRAIN;
} else {
usage("extra write argument not PUSHPULL or OPENDRAIN");
goto exit_failure;
}
}
int ret = ioctl(fd, GPIOC_SETPINTYPE, pin_type);
if (ret != 0) {
PX4_ERR("ioctl GPIOC_SETPINTYPE failed: %i", ret);
goto exit_failure;
}
ret = ioctl(fd, GPIOC_WRITE, value);
if (ret != 0) {
PX4_ERR("ioctl GPIOC_WRITE failed: %i", ret);
goto exit_failure;
}
printf("OK\n");
}
close(fd);
return 0;
exit_failure:
close(fd);
return -1;
#else
usage("not supported");
return -1;
#endif
}
int handle_board_ports(bool is_read, int argc, char *argv[])
{
const char *port_string = argv[2];
char port = port_string[0];
uint32_t mask = is_read ? GPIO_INPUT : GPIO_OUTPUT;
if ('A' <= port && port <= 'K') {
mask |= ((port - 'A') << GPIO_PORT_SHIFT) & GPIO_PORT_MASK;
} else if ('a' <= port && port <= 'k') {
mask |= ((port - 'a') << GPIO_PORT_SHIFT) & GPIO_PORT_MASK;
} else {
usage("invalid port");
return -1;
}
char *end;
int32_t pin = strtol(argv[2] + 1, &end, 10);
if (errno == 0 && *end == '\0' && 0 <= pin && pin <= 15) {
mask |= (pin << GPIO_PIN_SHIFT) & GPIO_PIN_MASK;;
} else {
usage("invalid pin");
return -1;
}
PX4_DEBUG("port=%c, pin=%i", port, pin);
bool matches_default_config = false;
#if defined(PX4_GPIO_INIT_LIST)
const uint32_t default_gpios[] = PX4_GPIO_INIT_LIST;
// check that GPIO matches initialization list for port/pin and input/output
for (uint32_t i = 0; i < arraySize(default_gpios); i++) {
if ((((default_gpios[i] & GPIO_INPUT) == (mask & GPIO_INPUT)) ||
((default_gpios[i] & GPIO_OUTPUT) == (mask & GPIO_OUTPUT)))
&& ((default_gpios[i] & GPIO_PORT_MASK) == (mask & GPIO_PORT_MASK))
&& ((default_gpios[i] & GPIO_PIN_MASK) == (mask & GPIO_PIN_MASK))
) {
matches_default_config = true;
}
}
#endif // PX4_GPIO_INIT_LIST
bool force_apply = strcasecmp(argv[argc - 1], "--force") == 0;
if (!matches_default_config && !force_apply) {
usage("does not match board initialization list, and --force not specified");
return -1;
}
if (is_read) {
if (argc >= 4) {
const char *extra = argv[3];
if (strcasecmp(extra, "PULLUP") == 0) {
mask |= GPIO_PULLUP;
} else if (strcasecmp(extra, "PULLDOWN") == 0) {
mask |= GPIO_PULLDOWN;
} else if (argc == 4 && !force_apply) {
usage("extra read argument not PULLUP or PULLDOWN");
return -1;
}
}
px4_arch_configgpio(mask);
int value = px4_arch_gpioread(mask);
printf("%d OK\n", value);
} else {
int32_t value = strtol(argv[3], &end, 10);
if (errno != 0 || *end != '\0' || (value != 0 && value != 1)) {
usage("value not 0 or 1");
return -1;
}
if (argc >= 5) {
const char *extra = argv[4];
if (strcasecmp(extra, "PUSHPULL") == 0) {
mask |= GPIO_PUSHPULL;
} else if (strcasecmp(extra, "OPENDRAIN") == 0) {
mask |= GPIO_OPENDRAIN;
} else if (argc == 5 && !force_apply) {
usage("extra write argument not PUSHPULL or OPENDRAIN");
return -1;
}
}
px4_arch_configgpio(mask);
px4_arch_gpiowrite(mask, value);
printf("OK\n");
}
return 0;
}
void usage(const char *reason)
{
printf("FAIL\n");
if (reason != nullptr) {
PX4_WARN("%s", reason);
}
PRINT_MODULE_DESCRIPTION(
R"DESCR_STR(
### Description
This command is used to read and write GPIOs
gpio read <PORT><PIN>/<DEVICE> [PULLDOWN|PULLUP] [--force]
gpio write <PORT><PIN>/<DEVICE> <VALUE> [PUSHPULL|OPENDRAIN] [--force]
### Examples
Read the value on port H pin 4 configured as pullup, and it is high
$ gpio read H4 PULLUP
1 OK
Set the output value on Port E pin 7 to high
$ gpio write E7 1 --force
Set the output value on device /dev/gpin1 to high
$ gpio write /dev/gpin1 1
)DESCR_STR");
PRINT_MODULE_USAGE_NAME_SIMPLE("gpio", "command");
PRINT_MODULE_USAGE_COMMAND("read");
PRINT_MODULE_USAGE_ARG("<PORT><PIN>/<DEVICE>", "GPIO port and pin or device", false);
PRINT_MODULE_USAGE_ARG("PULLDOWN|PULLUP", "Pulldown/Pullup", true);
PRINT_MODULE_USAGE_ARG("--force", "Force (ignore board gpio list)", true);
PRINT_MODULE_USAGE_COMMAND("write");
PRINT_MODULE_USAGE_ARG("<PORT> <PIN>", "GPIO port and pin", false);
PRINT_MODULE_USAGE_ARG("<VALUE>", "Value to write", false);
PRINT_MODULE_USAGE_ARG("PUSHPULL|OPENDRAIN", "Pushpull/Opendrain", true);
PRINT_MODULE_USAGE_ARG("--force", "Force (ignore board gpio list)", true);
}