]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cmd_gpio.c
reset optind before executing command
[z180-stamp.git] / avr / cmd_gpio.c
CommitLineData
35edb766
L
1/*
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
fcf1d5b3 4 * SPDX-License-Identifier: GPL-2.0
35edb766
L
5 */
6
7eecbdac 7#include "cmd_gpio.h"
8f23e84c 8#include <ctype.h>
8f23e84c 9
8f23e84c
L
10#include "print-utils.h"
11#include "getopt-min.h"
12#include "env.h"
05994bd9 13#include "gpio.h"
8f23e84c
L
14//#include "debug.h"
15
16
972e6e9b 17static uint_fast8_t pinnames_get(char *pin_names[GPIO_MAX+1])
8f23e84c
L
18{
19 static const FLASH char delim1[] = {":= "};
20 static const FLASH char delim2[] = {", "};
972e6e9b 21 uint_fast8_t width = 0;
8f23e84c 22 char *lp;
8f23e84c
L
23 uint_fast8_t i;
24
972e6e9b 25 memset(pin_names, 0, (GPIO_MAX+1) * sizeof(char *));
8f23e84c 26
612a6965 27/* TODO: enters endless loop on wrong parameters */
4565be9a 28
972e6e9b
L
29 lp = getenv_str(PSTR(ENV_PINALIAS));
30 if (lp != NULL) {
31 pin_names[GPIO_MAX] = strdup(lp);
32 if (pin_names[GPIO_MAX] != NULL) {
33 char *ptr = strtok_P(pin_names[GPIO_MAX], delim1);
34 while (ptr != NULL) {
35 if (((i = strtoul(ptr, &lp, 10)) < GPIO_MAX) &&
36 lp != ptr &&
37 (ptr = strtok_P(NULL, delim2)) != NULL ) {
38 pin_names[i] = ptr;
39 ptr = strtok_P(NULL, delim1);
40 }
8f23e84c 41 }
8f23e84c 42
972e6e9b
L
43 for (i = 0; i < GPIO_MAX; i++)
44 if (strlen(pin_names[i]) > width)
45 width = strlen(pin_names[i]);
46 }
8f23e84c 47 }
972e6e9b 48 return width;
8f23e84c
L
49}
50
51
52static size_t xstrlen(char *s)
53{
54 if (s == NULL)
55 return 0;
56 else
57 return strlen(s);
58}
59
60static const FLASH char * const FLASH pinconf_str[] = {
61 FSTR("?"),
62 FSTR("Input"),
63 FSTR("Pullup"),
64 FSTR("Output"),
65 FSTR("Clock"),
66 };
67
68static const FLASH char * const FLASH pinlevel_str[] = {
69 FSTR("Low"),
70 FSTR("High"),
71 FSTR(""),
72 };
73
972e6e9b 74static void print_pin(uint8_t pin, uint_fast8_t multi, char *pin_names[], uint_fast8_t pn_width)
8f23e84c 75{
972e6e9b 76 gpiomode_t pinconf;
8f23e84c
L
77 const FLASH char *levelp;
78 long div;
79
05994bd9 80 pinconf = gpio_config_get(pin);
8f23e84c 81 if (pinconf == OUTPUT_TIMER) {
05994bd9 82 div = gpio_clockdiv_get(pin);
8f23e84c
L
83 levelp = pinlevel_str[2];
84 } else
05994bd9 85 levelp = pinlevel_str[gpio_read(pin)];
8f23e84c
L
86
87 if (multi) {
88 printf_P(PSTR("%3d "), pin);
972e6e9b 89 if (pn_width) {
8f23e84c 90 printf_P(PSTR("%s "), pin_names[pin]);
972e6e9b 91 print_blanks(pn_width - xstrlen(pin_names[pin]));
8f23e84c
L
92 }
93 my_puts_P(pinconf_str[pinconf]);
94 print_blanks(7 - strlen_P(pinconf_str[pinconf]));
95 my_puts_P(levelp);
96 print_blanks(5 - strlen_P(levelp));
97 if (pinconf == OUTPUT_TIMER)
98 printf_P(PSTR("%8ld %8ld"),
99 div, F_CPU/div);
100 } else {
101 printf_P(PSTR("%d: \"%s\", "), pin, pin_names[pin] ? pin_names[pin] : 0);
102 my_puts_P(pinconf_str[pinconf]);
972e6e9b 103 my_puts_P(PSTR(", "));
8f23e84c
L
104 my_puts_P(levelp);
105
106 if (pinconf == OUTPUT_TIMER)
107 printf_P(PSTR("divide by %ld (%ldHz)"),
108 div, F_CPU/div);
109 }
972e6e9b 110 my_puts_P(PSTR("\n"));
8f23e84c
L
111}
112
972e6e9b 113static int_fast8_t pinarg_insert(uint8_t pin, uint_fast8_t count, uint_fast8_t pinarg[])
8f23e84c
L
114{
115 uint_fast8_t pos;
116
972e6e9b 117 if (pin >= GPIO_MAX)
8f23e84c
L
118 return -1;
119
120 for (pos = 0; pos < count; pos++) {
121 if (pin == pinarg[pos])
122 return 0;
123 if (pin < pinarg[pos])
124 break;
125 }
126 for (uint_fast8_t i = count-1; i == pos ; i--)
127 pinarg[i+1] = pinarg[i];
128 pinarg[pos] = pin;
129
130 return 1;
131}
132
133static uint_fast8_t pinarg_get(char * arg, uint_fast8_t pinarg[])
134{
135 char *endp;
136 uint_fast8_t pin1;
137 int_fast8_t rc;
138 uint_fast8_t count = 0;
139
140 while (1) {
141 pin1 = strtoul(arg, &endp, 10);
142 if (endp != arg && *endp == '-') {
143 arg = endp+1;
144 uint_fast8_t pin2 = (int) strtoul(arg, &endp, 10);
145 if (pin1 < pin2)
146 for (; pin1 < pin2; pin1++)
147 if ((rc = pinarg_insert(pin1, count, pinarg)) >= 0)
148 count += rc;
149 else
150 return 0;
151 else
152 return 0;
153 }
154 if (endp != arg) {
155 if ((*endp == ',' || *endp == '\0') &&
156 (rc = pinarg_insert(pin1, count, pinarg)) >= 0) {
157 count += rc;
158 if (*endp == '\0')
159 return count;
160 } else
161 return 0;
162 } else
163 return 0;
164
165 arg = endp+1;
166 }
167}
168
169
972e6e9b 170command_ret_t do_gpio(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char *const argv[])
8f23e84c 171{
972e6e9b 172 uint_fast8_t printheader = 1;
05994bd9 173 uint_fast8_t pinarg[GPIO_MAX];
8f23e84c
L
174 uint_fast8_t pinargc;
175
8f23e84c
L
176 int opt;
177 while ((opt = getopt(argc, argv, PSTR("s"))) != -1) {
178 switch (opt) {
179 case 's':
180 printheader = 0;
181 break;
182 default: /* '?' */
183 return CMD_RET_USAGE;
184 }
185 }
186
187 /* remaining arguments */
188 argc -= optind;
189
8f23e84c 190 if (argc == 0) {
972e6e9b 191 /* print config of all pins */
05994bd9 192 for (pinargc = 0; pinargc < GPIO_MAX; pinargc++)
8f23e84c
L
193 pinarg[pinargc] = pinargc;
194 } else {
195 /* get first arg */
196 pinargc = pinarg_get(argv[optind++], pinarg);
197 if (pinargc == 0)
198 return CMD_RET_USAGE;
199 else
200 argc--;
201 }
202
972e6e9b 203
8f23e84c 204 if (argc == 0) {
972e6e9b
L
205 /* no more args, print pin config */
206
207 char *pin_names[GPIO_MAX + 1];
208 uint_fast8_t pin_names_width = pinnames_get(pin_names);
209
8f23e84c 210 if (pinargc == 1)
972e6e9b 211 print_pin(pinarg[0], 0, pin_names, pin_names_width);
8f23e84c
L
212 else {
213 if (printheader) {
214 if (pin_names_width > 0) {
215 if ( strlen("Name") > pin_names_width)
216 pin_names_width = strlen("Name");
217 char s[pin_names_width+1];
218 memset(s, ' ', pin_names_width);
219 s[pin_names_width] = '\0';
220 strncpy_P(s, PSTR("Name"), 4);
221 printf_P(PSTR("Pin %s Config Level Divider Frequency/Hz\n"),s);
222 memset(s, '-', pin_names_width);
223 printf_P(PSTR("----%s-----------------------------------\n"), s);
224 } else
225 printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n"
226 "--------------------------------------\n"));
227 }
228 for (uint_fast8_t i = 0; i < pinargc; i++)
972e6e9b 229 print_pin(pinarg[i], 1, pin_names, pin_names_width);
8f23e84c 230 }
972e6e9b
L
231 if (pin_names[GPIO_MAX] != NULL)
232 free(pin_names[GPIO_MAX]);
8f23e84c
L
233 return CMD_RET_SUCCESS;
234 }
235
236 /* arguments must be in pairs: pins conf */
237 if (argc % 2 != 1)
238 return CMD_RET_USAGE;
239
240 while (argc > 0) {
241 char *endp;
05994bd9 242 gpiomode_t mode = NONE;
8f23e84c
L
243 int level = 0;
244 unsigned long value = 0;
245 uint8_t hz_flag = 0;
246
247 switch (toupper(argv[optind][0])) {
248 case 'H':
249 level = 1;
250 case 'L':
251 mode = OUTPUT;
252 break;
253 case 'P':
254 mode = INPUT_PULLUP;
255 break;
256 case 'I':
257 case 'T':
258 mode = INPUT;
259 break;
260
261 default:
262 value = strtoul(argv[optind], &endp, 10);
263 switch (*endp) {
264 case 'M':
265 value *= 1000;
266 case 'K':
267 value *= 1000;
268 endp++;
269 }
270
271 if (*endp && strcmp_P(endp, PSTR("Hz")) == 0) {
272 hz_flag = 1;
273 endp += 2;
274 }
275
276 if (*endp != '\0') {
277 printf_P(PSTR("invalid parameter: '%s'\n"), argv[optind]);
278 return CMD_RET_USAGE;
279 }
280
281 if (value == 0) {
282 printf_P(PSTR("invalid value: %lu \n"));
283 return CMD_RET_USAGE;
284 }
285
286 if (hz_flag) {
287 if (value > F_CPU / 2) {
288 printf_P(PSTR("Max frequency is: %luHz\n"), F_CPU/2);
289 return CMD_RET_USAGE;
290 }
291 value = F_CPU/value;
292 }
293 mode = OUTPUT_TIMER;
294
295 }
296
297 if (mode == NONE)
298 return CMD_RET_USAGE;
299
300 for (uint_fast8_t i = 0; i < pinargc; i++) {
301 switch (mode) {
302 case OUTPUT:
05994bd9 303 gpio_write(pinarg[i], level);
8f23e84c
L
304 /* fall thru */
305 case INPUT:
306 case INPUT_PULLUP:
05994bd9 307 gpio_config(pinarg[i], mode);
8f23e84c
L
308 break;
309 case OUTPUT_TIMER:
05994bd9 310 if (gpio_clockdiv_set(pinarg[i], value) < 0) {
8f23e84c
L
311 printf_P(PSTR("Setting pin %d to %lu failed.\n"),
312 pinarg[i], value);
313 }
314 break;
315 default:
316 break;
317 }
318 }
319
320 optind++;
321 pinargc = pinarg_get(argv[optind++], pinarg);
322 argc -= 2;
323 }
324
325 return CMD_RET_SUCCESS;
326}