]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_gpio.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
10 #include "print-utils.h"
11 #include "getopt-min.h"
18 static const int namestr
= GPIO_MAX
;
19 static char *pin_names
[GPIO_MAX
+1];
20 static uint_least8_t pin_names_width
;
22 static void pinnames_get(void)
24 static const FLASH
char delim1
[] = {":= "};
25 static const FLASH
char delim2
[] = {", "};
30 if (pin_names
[namestr
] != NULL
)
31 free(pin_names
[namestr
]);
32 memset(pin_names
, 0, sizeof(pin_names
));
35 /* TODO: enters endless loop on wrong parameters */
37 if ((lp
= getenv_str(PSTR(ENV_PINALIAS
))) != NULL
) {
38 pin_names
[namestr
] = strdup(lp
);
39 ptr
= strtok_P(pin_names
[namestr
], delim1
);
41 if (((i
= strtoul(ptr
, &lp
, 10)) < GPIO_MAX
) &&
43 (ptr
= strtok_P(NULL
, delim2
)) != NULL
) {
45 ptr
= strtok_P(NULL
, delim1
);
49 for (i
= 0; i
< GPIO_MAX
; i
++)
50 if (strlen(pin_names
[i
]) > pin_names_width
)
51 pin_names_width
= strlen(pin_names
[i
]);
56 static size_t xstrlen(char *s
)
64 static const FLASH
char * const FLASH pinconf_str
[] = {
72 static const FLASH
char * const FLASH pinlevel_str
[] = {
78 static int print_pin(int pin
, int multi
)
81 const FLASH
char *levelp
;
84 pinconf
= gpio_config_get(pin
);
85 if (pinconf
== OUTPUT_TIMER
) {
86 div
= gpio_clockdiv_get(pin
);
87 levelp
= pinlevel_str
[2];
89 levelp
= pinlevel_str
[gpio_read(pin
)];
92 printf_P(PSTR("%3d "), pin
);
93 if (pin_names_width
) {
94 printf_P(PSTR("%s "), pin_names
[pin
]);
95 print_blanks(pin_names_width
- xstrlen(pin_names
[pin
]));
97 my_puts_P(pinconf_str
[pinconf
]);
98 print_blanks(7 - strlen_P(pinconf_str
[pinconf
]));
100 print_blanks(5 - strlen_P(levelp
));
101 if (pinconf
== OUTPUT_TIMER
)
102 printf_P(PSTR("%8ld %8ld"),
105 printf_P(PSTR("%d: \"%s\", "), pin
, pin_names
[pin
] ? pin_names
[pin
] : 0);
106 my_puts_P(pinconf_str
[pinconf
]);
107 printf_P(PSTR(", "));
110 if (pinconf
== OUTPUT_TIMER
)
111 printf_P(PSTR("divide by %ld (%ldHz)"),
114 printf_P(PSTR("\n"));
119 static int_fast8_t pinarg_insert(int pin
, uint_fast8_t count
, uint_fast8_t pinarg
[])
123 if (pin
< 0 || pin
>= GPIO_MAX
)
126 for (pos
= 0; pos
< count
; pos
++) {
127 if (pin
== pinarg
[pos
])
129 if (pin
< pinarg
[pos
])
132 for (uint_fast8_t i
= count
-1; i
== pos
; i
--)
133 pinarg
[i
+1] = pinarg
[i
];
139 static uint_fast8_t pinarg_get(char * arg
, uint_fast8_t pinarg
[])
144 uint_fast8_t count
= 0;
147 pin1
= strtoul(arg
, &endp
, 10);
148 if (endp
!= arg
&& *endp
== '-') {
150 uint_fast8_t pin2
= (int) strtoul(arg
, &endp
, 10);
152 for (; pin1
< pin2
; pin1
++)
153 if ((rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0)
161 if ((*endp
== ',' || *endp
== '\0') &&
162 (rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0) {
176 command_ret_t
do_gpio(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char *const argv
[])
178 char printheader
= 1;
179 uint_fast8_t pinarg
[GPIO_MAX
];
180 uint_fast8_t pinargc
;
182 (void) cmdtp
; (void) flag
;
188 while ((opt
= getopt(argc
, argv
, PSTR("s"))) != -1) {
194 return CMD_RET_USAGE
;
198 /* remaining arguments */
204 /* print cofig of all pins */
205 for (pinargc
= 0; pinargc
< GPIO_MAX
; pinargc
++)
206 pinarg
[pinargc
] = pinargc
;
209 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
211 return CMD_RET_USAGE
;
217 /* no more args, print config */
219 print_pin(pinarg
[0], 0);
222 if (pin_names_width
> 0) {
223 if ( strlen("Name") > pin_names_width
)
224 pin_names_width
= strlen("Name");
225 char s
[pin_names_width
+1];
226 memset(s
, ' ', pin_names_width
);
227 s
[pin_names_width
] = '\0';
228 strncpy_P(s
, PSTR("Name"), 4);
229 printf_P(PSTR("Pin %s Config Level Divider Frequency/Hz\n"),s
);
230 memset(s
, '-', pin_names_width
);
231 printf_P(PSTR("----%s-----------------------------------\n"), s
);
233 printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n"
234 "--------------------------------------\n"));
236 for (uint_fast8_t i
= 0; i
< pinargc
; i
++)
237 print_pin(pinarg
[i
], 1);
239 return CMD_RET_SUCCESS
;
242 /* arguments must be in pairs: pins conf */
244 return CMD_RET_USAGE
;
248 gpiomode_t mode
= NONE
;
250 unsigned long value
= 0;
253 switch (toupper(argv
[optind
][0])) {
268 value
= strtoul(argv
[optind
], &endp
, 10);
277 if (*endp
&& strcmp_P(endp
, PSTR("Hz")) == 0) {
283 printf_P(PSTR("invalid parameter: '%s'\n"), argv
[optind
]);
284 return CMD_RET_USAGE
;
288 printf_P(PSTR("invalid value: %lu \n"));
289 return CMD_RET_USAGE
;
293 if (value
> F_CPU
/ 2) {
294 printf_P(PSTR("Max frequency is: %luHz\n"), F_CPU
/2);
295 return CMD_RET_USAGE
;
304 return CMD_RET_USAGE
;
306 for (uint_fast8_t i
= 0; i
< pinargc
; i
++) {
309 gpio_write(pinarg
[i
], level
);
313 gpio_config(pinarg
[i
], mode
);
316 if (gpio_clockdiv_set(pinarg
[i
], value
) < 0) {
317 printf_P(PSTR("Setting pin %d to %lu failed.\n"),
327 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
331 return CMD_RET_SUCCESS
;