]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0 | |
5 | */ | |
6 | ||
7 | #include "cmd_gpio.h" | |
8 | #include <ctype.h> | |
9 | ||
10 | #include "print-utils.h" | |
11 | #include "getopt-min.h" | |
12 | #include "env.h" | |
13 | #include "gpio.h" | |
14 | //#include "debug.h" | |
15 | ||
16 | ||
17 | static uint_fast8_t pinnames_get(char *pin_names[GPIO_MAX+1]) | |
18 | { | |
19 | static const FLASH char delim1[] = {":= "}; | |
20 | static const FLASH char delim2[] = {", "}; | |
21 | uint_fast8_t width = 0; | |
22 | char *lp; | |
23 | uint_fast8_t i; | |
24 | ||
25 | memset(pin_names, 0, (GPIO_MAX+1) * sizeof(char *)); | |
26 | ||
27 | /* TODO: enters endless loop on wrong parameters */ | |
28 | ||
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 | } | |
41 | } | |
42 | ||
43 | for (i = 0; i < GPIO_MAX; i++) | |
44 | if (strlen(pin_names[i]) > width) | |
45 | width = strlen(pin_names[i]); | |
46 | } | |
47 | } | |
48 | return width; | |
49 | } | |
50 | ||
51 | ||
52 | static size_t xstrlen(char *s) | |
53 | { | |
54 | if (s == NULL) | |
55 | return 0; | |
56 | else | |
57 | return strlen(s); | |
58 | } | |
59 | ||
60 | static const FLASH char * const FLASH pinconf_str[] = { | |
61 | FSTR("?"), | |
62 | FSTR("Input"), | |
63 | FSTR("Pullup"), | |
64 | FSTR("Output"), | |
65 | FSTR("Clock"), | |
66 | }; | |
67 | ||
68 | static const FLASH char * const FLASH pinlevel_str[] = { | |
69 | FSTR("Low"), | |
70 | FSTR("High"), | |
71 | FSTR(""), | |
72 | }; | |
73 | ||
74 | static void print_pin(uint8_t pin, uint_fast8_t multi, char *pin_names[], uint_fast8_t pn_width) | |
75 | { | |
76 | gpiomode_t pinconf; | |
77 | const FLASH char *levelp; | |
78 | long div; | |
79 | ||
80 | pinconf = gpio_config_get(pin); | |
81 | if (pinconf == OUTPUT_TIMER) { | |
82 | div = gpio_clockdiv_get(pin); | |
83 | levelp = pinlevel_str[2]; | |
84 | } else | |
85 | levelp = pinlevel_str[gpio_read(pin)]; | |
86 | ||
87 | if (multi) { | |
88 | printf_P(PSTR("%3d "), pin); | |
89 | if (pn_width) { | |
90 | printf_P(PSTR("%s "), pin_names[pin]); | |
91 | print_blanks(pn_width - xstrlen(pin_names[pin])); | |
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]); | |
103 | my_puts_P(PSTR(", ")); | |
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 | } | |
110 | my_puts_P(PSTR("\n")); | |
111 | } | |
112 | ||
113 | static int_fast8_t pinarg_insert(uint8_t pin, uint_fast8_t count, uint_fast8_t pinarg[]) | |
114 | { | |
115 | uint_fast8_t pos; | |
116 | ||
117 | if (pin >= GPIO_MAX) | |
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 | ||
133 | static 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 | ||
170 | command_ret_t do_gpio(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char *const argv[]) | |
171 | { | |
172 | uint_fast8_t printheader = 1; | |
173 | uint_fast8_t pinarg[GPIO_MAX]; | |
174 | uint_fast8_t pinargc; | |
175 | ||
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 | ||
190 | if (argc == 0) { | |
191 | /* print config of all pins */ | |
192 | for (pinargc = 0; pinargc < GPIO_MAX; pinargc++) | |
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 | ||
203 | ||
204 | if (argc == 0) { | |
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 | ||
210 | if (pinargc == 1) | |
211 | print_pin(pinarg[0], 0, pin_names, pin_names_width); | |
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++) | |
229 | print_pin(pinarg[i], 1, pin_names, pin_names_width); | |
230 | } | |
231 | if (pin_names[GPIO_MAX] != NULL) | |
232 | free(pin_names[GPIO_MAX]); | |
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; | |
242 | gpiomode_t mode = NONE; | |
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: | |
303 | gpio_write(pinarg[i], level); | |
304 | /* fall thru */ | |
305 | case INPUT: | |
306 | case INPUT_PULLUP: | |
307 | gpio_config(pinarg[i], mode); | |
308 | break; | |
309 | case OUTPUT_TIMER: | |
310 | if (gpio_clockdiv_set(pinarg[i], value) < 0) { | |
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 | } |