-
Notifications
You must be signed in to change notification settings - Fork 2k
/
main.c
351 lines (295 loc) · 8.69 KB
/
main.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright (C) 2014,2017 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup tests
* @defgroup tests_periph_gpio_exp
* @{
*
* @file
* @brief Test application for GPIO peripheral drivers
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "irq.h"
#include "shell.h"
#include "benchmark.h"
#include "periph/gpio.h"
#define BENCH_RUNS_DEFAULT (1000UL * 1000)
/**
* BENCHMARK_FUNC disables the interrupts and does not work correctly on
* some platforms, e.g. ATmega. On other platforms, such as STM32, the
* benchmark test gives the same results with interrupts not disabled.
* Therefore a modified version is used here that does not disable the
* interrupts.
*/
#define MY_BENCHMARK_FUNC(name, runs, func) \
{ \
uint32_t _benchmark_time = xtimer_now_usec(); \
for (unsigned long i = 0; i < runs; i++) { \
func; \
} \
_benchmark_time = (xtimer_now_usec() - _benchmark_time);\
benchmark_print_time(_benchmark_time, runs, name); \
}
#ifdef MODULE_PERIPH_GPIO_IRQ
static void cb(void *arg)
{
printf("INT: external interrupt from GPIO_PIN(%i, %i)\n",
(int)arg >> 8, (int)arg & 0xff);
}
#endif
static int str2port(char *port)
{
if (strncmp(port, "exp", 3) == 0) {
return GPIO_EXP_PORT + atoi(&port[3]);
}
else {
return atoi(port);
}
}
static int init_pin(int argc, char **argv, gpio_mode_t mode)
{
int po, pi;
if (argc < 3) {
printf("usage: %s <port> <pin>\n", argv[0]);
return 1;
}
po = str2port(argv[1]);
pi = atoi(argv[2]);
if (gpio_init(GPIO_PIN(po, pi), mode) < 0) {
printf("Error to initialize GPIO_PIN(%i, %i)\n", po, pi);
return 1;
}
return 0;
}
static int init_out(int argc, char **argv)
{
return init_pin(argc, argv, GPIO_OUT);
}
static int init_in(int argc, char **argv)
{
return init_pin(argc, argv, GPIO_IN);
}
static int init_in_pu(int argc, char **argv)
{
return init_pin(argc, argv, GPIO_IN_PU);
}
static int init_in_pd(int argc, char **argv)
{
return init_pin(argc, argv, GPIO_IN_PD);
}
static int init_od(int argc, char **argv)
{
return init_pin(argc, argv, GPIO_OD);
}
static int init_od_pu(int argc, char **argv)
{
return init_pin(argc, argv, GPIO_OD_PU);
}
#ifdef MODULE_PERIPH_GPIO_IRQ
static int init_int(int argc, char **argv)
{
int po, pi;
gpio_mode_t mode = GPIO_IN;
gpio_flank_t flank;
int fl;
if (argc < 4) {
printf("usage: %s <port> <pin> <flank> [pull_config]\n", argv[0]);
puts("\tflank:\n"
"\t0: falling\n"
"\t1: rising\n"
"\t2: both\n"
"\tpull_config:\n"
"\t0: no pull resistor (default)\n"
"\t1: pull up\n"
"\t2: pull down");
return 1;
}
po = str2port(argv[1]);
pi = atoi(argv[2]);
fl = atoi(argv[3]);
switch (fl) {
case 0:
flank = GPIO_FALLING;
break;
case 1:
flank = GPIO_RISING;
break;
case 2:
flank = GPIO_BOTH;
break;
default:
puts("error: invalid value for active flank");
return 1;
}
if (argc >= 5) {
int pr = atoi(argv[4]);
switch (pr) {
case 0:
mode = GPIO_IN;
break;
case 1:
mode = GPIO_IN_PU;
break;
case 2:
mode = GPIO_IN_PD;
break;
default:
puts("error: invalid pull resistor option");
return 1;
}
}
if (gpio_init_int(GPIO_PIN(po, pi), mode, flank, cb, (void *)((po << 8) + pi)) < 0) {
printf("error: init_int of GPIO_PIN(%i, %i) failed\n", po, pi);
return 1;
}
printf("GPIO_PIN(%i, %i) successfully initialized as ext int\n", po, pi);
return 0;
}
static int enable_int(int argc, char **argv)
{
int po, pi;
int status;
if (argc < 4) {
printf("usage: %s <port> <pin> <status>\n", argv[0]);
puts("\tstatus:\n"
"\t0: disable\n"
"\t1: enable\n");
return 1;
}
po = str2port(argv[1]);
pi = atoi(argv[2]);
status = atoi(argv[3]);
switch (status) {
case 0:
puts("disabling GPIO interrupt");
gpio_irq_disable(GPIO_PIN(po, pi));
break;
case 1:
puts("enabling GPIO interrupt");
gpio_irq_enable(GPIO_PIN(po, pi));
break;
default:
puts("error: invalid status");
return 1;
}
return 0;
}
#endif
static int read(int argc, char **argv)
{
int port, pin;
if (argc < 3) {
printf("usage: %s <port> <pin>\n", argv[0]);
return 1;
}
port = str2port(argv[1]);
pin = atoi(argv[2]);
if (gpio_read(GPIO_PIN(port, pin))) {
printf("GPIO_PIN(%i, %i) is HIGH\n", port, pin);
}
else {
printf("GPIO_PIN(%i, %i) is LOW\n", port, pin);
}
return 0;
}
static int set(int argc, char **argv)
{
if (argc < 3) {
printf("usage: %s <port> <pin>\n", argv[0]);
return 1;
}
gpio_set(GPIO_PIN(str2port(argv[1]), atoi(argv[2])));
return 0;
}
static int clear(int argc, char **argv)
{
if (argc < 3) {
printf("usage: %s <port> <pin>\n", argv[0]);
return 1;
}
gpio_clear(GPIO_PIN(str2port(argv[1]), atoi(argv[2])));
return 0;
}
static int toggle(int argc, char **argv)
{
if (argc < 3) {
printf("usage: %s <port> <pin>\n", argv[0]);
return 1;
}
gpio_toggle(GPIO_PIN(str2port(argv[1]), atoi(argv[2])));
return 0;
}
static int writep(int argc, char **argv)
{
if (argc < 4) {
printf("usage: %s <port> <pin> <value>\n", argv[0]);
return 1;
}
gpio_write(GPIO_PIN(str2port(argv[1]), atoi(argv[2])), atoi(argv[3]));
return 0;
}
static int bench(int argc, char **argv)
{
if (argc < 3) {
printf("usage: %s <port> <pin> [# of runs]\n", argv[0]);
return 1;
}
gpio_t pin = GPIO_PIN(str2port(argv[1]), atoi(argv[2]));
unsigned long runs = BENCH_RUNS_DEFAULT;
if (argc > 3) {
runs = (unsigned long)atol(argv[3]);
}
puts("\nGPIO driver run-time performance benchmark\n");
MY_BENCHMARK_FUNC("nop loop", runs, __asm__ volatile("nop"));
MY_BENCHMARK_FUNC("gpio_set", runs, gpio_set(pin));
MY_BENCHMARK_FUNC("gpio_clear", runs, gpio_clear(pin));
MY_BENCHMARK_FUNC("gpio_toggle", runs, gpio_toggle(pin));
MY_BENCHMARK_FUNC("gpio_read", runs, (void)gpio_read(pin));
MY_BENCHMARK_FUNC("gpio_write", runs, gpio_write(pin, 1));
puts("\n --- DONE ---");
return 0;
}
static const shell_command_t shell_commands[] = {
{ "init_out", "init as output (push-pull mode)", init_out },
{ "init_in", "init as input w/o pull resistor", init_in },
{ "init_in_pu", "init as input with pull-up", init_in_pu },
{ "init_in_pd", "init as input with pull-down", init_in_pd },
{ "init_od", "init as output (open-drain without pull resistor)", init_od },
{ "init_od_pu", "init as output (open-drain with pull-up)", init_od_pu },
#ifdef MODULE_PERIPH_GPIO_IRQ
{ "init_int", "init as external INT w/o pull resistor", init_int },
{ "enable_int", "enable or disable gpio interrupt", enable_int },
#endif
{ "read", "read pin status", read },
{ "set", "set pin to HIGH", set },
{ "clear", "set pin to LOW", clear },
{ "toggle", "toggle pin", toggle },
{ "write", "write pin", writep },
{ "bench", "run a set of predefined benchmarks", bench },
{ NULL, NULL, NULL }
};
int main(void)
{
puts("GPIO peripheral driver test\n");
puts("In this test, pins are specified by integer port and pin numbers.\n"
"So if your platform has a pin PA01, it will be port=0 and pin=1,\n"
"PC14 would be port=2 and pin=14 etc. When using exp0, exp1, ...\n"
"for the port, the virtual GPIO expander ports are addressed.\n\n"
"NOTE: make sure the values you use exist on your platform! The\n"
" behavior for not existing ports/pins is not defined!");
/* start the shell */
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}