X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/142cb1002bb66e7cc183a8265f44c8d56ec54a9b..52ef24e4020adcb773adcb0d82b1c2c385610461:/avr/cmd_gpio.c diff --git a/avr/cmd_gpio.c b/avr/cmd_gpio.c index f448e36..549654f 100644 --- a/avr/cmd_gpio.c +++ b/avr/cmd_gpio.c @@ -1,9 +1,12 @@ -#include "common.h" -#include -#include +/* + * (C) Copyright 2014 Leo C. + * + * SPDX-License-Identifier: GPL-2.0 + */ + +#include "cmd_gpio.h" #include -#include "command.h" #include "print-utils.h" #include "getopt-min.h" #include "env.h" @@ -11,42 +14,38 @@ //#include "debug.h" - -static const int namestr = GPIO_MAX; -static char *pin_names[GPIO_MAX+1]; -static uint_least8_t pin_names_width; - -static void pinnames_get(void) +static uint_fast8_t pinnames_get(char *pin_names[GPIO_MAX+1]) { static const FLASH char delim1[] = {":= "}; static const FLASH char delim2[] = {", "}; + uint_fast8_t width = 0; char *lp; - char *ptr; uint_fast8_t i; - if (pin_names[namestr] != NULL) - free(pin_names[namestr]); - memset(pin_names, 0, sizeof(pin_names)); - pin_names_width = 0; - -/* TODO: enters endless loop on wron parameters */ - - if ((lp = getenv(PSTR(ENV_PINALIAS))) != NULL) { - pin_names[namestr] = strdup(lp); - ptr = strtok_P(pin_names[namestr], delim1); - while (ptr != NULL) { - if (((i = strtoul(ptr, &lp, 10)) < GPIO_MAX) && - lp != ptr && - (ptr = strtok_P(NULL, delim2)) != NULL ) { - pin_names[i] = ptr; - ptr = strtok_P(NULL, delim1); + memset(pin_names, 0, (GPIO_MAX+1) * sizeof(char *)); + +/* TODO: enters endless loop on wrong parameters */ + + lp = getenv_str(PSTR(ENV_PINALIAS)); + if (lp != NULL) { + pin_names[GPIO_MAX] = strdup(lp); + if (pin_names[GPIO_MAX] != NULL) { + char *ptr = strtok_P(pin_names[GPIO_MAX], delim1); + while (ptr != NULL) { + if (((i = strtoul(ptr, &lp, 10)) < GPIO_MAX) && + lp != ptr && + (ptr = strtok_P(NULL, delim2)) != NULL ) { + pin_names[i] = ptr; + ptr = strtok_P(NULL, delim1); + } } - } - for (i = 0; i < GPIO_MAX; i++) - if (strlen(pin_names[i]) > pin_names_width) - pin_names_width = strlen(pin_names[i]); + for (i = 0; i < GPIO_MAX; i++) + if (strlen(pin_names[i]) > width) + width = strlen(pin_names[i]); + } } + return width; } @@ -72,9 +71,9 @@ static const FLASH char * const FLASH pinlevel_str[] = { FSTR(""), }; -static int print_pin(int pin, int multi) +static void print_pin(uint8_t pin, uint_fast8_t multi, char *pin_names[], uint_fast8_t pn_width) { - int pinconf; + gpiomode_t pinconf; const FLASH char *levelp; long div; @@ -87,9 +86,9 @@ static int print_pin(int pin, int multi) if (multi) { printf_P(PSTR("%3d "), pin); - if (pin_names_width) { + if (pn_width) { printf_P(PSTR("%s "), pin_names[pin]); - print_blanks(pin_names_width - xstrlen(pin_names[pin])); + print_blanks(pn_width - xstrlen(pin_names[pin])); } my_puts_P(pinconf_str[pinconf]); print_blanks(7 - strlen_P(pinconf_str[pinconf])); @@ -101,23 +100,21 @@ static int print_pin(int pin, int multi) } else { printf_P(PSTR("%d: \"%s\", "), pin, pin_names[pin] ? pin_names[pin] : 0); my_puts_P(pinconf_str[pinconf]); - printf_P(PSTR(", ")); + my_puts_P(PSTR(", ")); my_puts_P(levelp); if (pinconf == OUTPUT_TIMER) printf_P(PSTR("divide by %ld (%ldHz)"), div, F_CPU/div); } - printf_P(PSTR("\n")); - - return 0; + my_puts_P(PSTR("\n")); } -static int_fast8_t pinarg_insert(int pin, uint_fast8_t count, uint_fast8_t pinarg[]) +static int_fast8_t pinarg_insert(uint8_t pin, uint_fast8_t count, uint_fast8_t pinarg[]) { uint_fast8_t pos; - if (pin < 0 || pin >= GPIO_MAX) + if (pin >= GPIO_MAX) return -1; for (pos = 0; pos < count; pos++) { @@ -170,17 +167,12 @@ static uint_fast8_t pinarg_get(char * arg, uint_fast8_t pinarg[]) } -command_ret_t do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +command_ret_t do_gpio(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char *const argv[]) { - char printheader = 1; + uint_fast8_t printheader = 1; uint_fast8_t pinarg[GPIO_MAX]; uint_fast8_t pinargc; - (void) cmdtp; (void) flag; - - /* reset getopt() */ - optind = 1; - int opt; while ((opt = getopt(argc, argv, PSTR("s"))) != -1) { switch (opt) { @@ -195,10 +187,8 @@ command_ret_t do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) /* remaining arguments */ argc -= optind; - pinnames_get(); - if (argc == 0) { - /* print cofig of all pins */ + /* print config of all pins */ for (pinargc = 0; pinargc < GPIO_MAX; pinargc++) pinarg[pinargc] = pinargc; } else { @@ -210,10 +200,15 @@ command_ret_t do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) argc--; } + if (argc == 0) { - /* no more args, print config */ + /* no more args, print pin config */ + + char *pin_names[GPIO_MAX + 1]; + uint_fast8_t pin_names_width = pinnames_get(pin_names); + if (pinargc == 1) - print_pin(pinarg[0], 0); + print_pin(pinarg[0], 0, pin_names, pin_names_width); else { if (printheader) { if (pin_names_width > 0) { @@ -231,8 +226,10 @@ command_ret_t do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) "--------------------------------------\n")); } for (uint_fast8_t i = 0; i < pinargc; i++) - print_pin(pinarg[i], 1); + print_pin(pinarg[i], 1, pin_names, pin_names_width); } + if (pin_names[GPIO_MAX] != NULL) + free(pin_names[GPIO_MAX]); return CMD_RET_SUCCESS; }