From 70c994914510f9db3ff71373cb96f50ee1669446 Mon Sep 17 00:00:00 2001 From: Leo C Date: Wed, 15 Oct 2014 21:20:41 +0200 Subject: [PATCH] Add pin_alias --- avr/cmd_boot.c | 84 +++++++++++++++++++++++++++++++++++++++++------- avr/main.c | 6 ++-- include/config.h | 7 ++++ 3 files changed, 83 insertions(+), 14 deletions(-) diff --git a/avr/cmd_boot.c b/avr/cmd_boot.c index 26855f1..1213365 100644 --- a/avr/cmd_boot.c +++ b/avr/cmd_boot.c @@ -6,11 +6,13 @@ #include #include #include +#include #include #include #include "command.h" #include "getopt-min.h" +#include "env.h" #include "z80-if.h" #include "pin.h" #include "debug.h" @@ -268,7 +270,41 @@ command_ret_t do_clock2(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[] } #endif -// {INPUT, INPUT_PULLUP, OUTPUT, OUTPUT_TIMER} pinmode_t; + +static const int namestr = PIN_MAX; +static char *pin_names[PIN_MAX+1]; +static uint_least8_t pin_names_width; + +void pinnames_get(void) +{ + static const FLASH char delim1[] = {":= "}; + static const FLASH char delim2[] = {", "}; + 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; + + 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)) < PIN_MAX) && + lp != ptr && + (ptr = strtok_P(NULL, delim2)) != NULL ) { + pin_names[i] = ptr; + ptr = strtok_P(NULL, delim1); + } + } + + for (i = 0; i < PIN_MAX; i++) + if (strlen(pin_names[i]) > pin_names_width) + pin_names_width = strlen(pin_names[i]); + } +} static void print_blanks(uint_fast8_t count) @@ -277,6 +313,14 @@ static void print_blanks(uint_fast8_t count) putchar(' '); } +static int xstrlen(char *s) +{ + if (s == NULL) + return 0; + else + return strlen(s); +} + static const FLASH char * const FLASH pinconf_str[] = { FSTR("?"), FSTR("Input"), @@ -305,16 +349,20 @@ int print_pin(int pin, int multi) levelp = pinlevel_str[pin_read(pin)]; if (multi) { - printf_P(PSTR("%3d "), pin); + printf_P(PSTR("%3d "), pin); + if (pin_names_width) { + printf_P(PSTR("%s "), pin_names[pin]); + print_blanks(pin_names_width - xstrlen(pin_names[pin])); + } my_puts_P(pinconf_str[pinconf]); - print_blanks(8 - strlen_P(pinconf_str[pinconf])); + print_blanks(7 - strlen_P(pinconf_str[pinconf])); my_puts_P(levelp); - print_blanks(6 - strlen_P(levelp)); + print_blanks(5 - strlen_P(levelp)); if (pinconf == OUTPUT_TIMER) - printf_P(PSTR("%7ld %8ld"), + printf_P(PSTR("%8ld %8ld"), div, F_CPU/div); } else { - printf_P(PSTR("Pin %d: "), pin); + 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(levelp); @@ -409,11 +457,13 @@ command_ret_t do_pin(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) /* remaining arguments */ argc -= optind; - if (argc == 0) + pinnames_get(); + + if (argc == 0) { /* print cofig of all pins */ for (pinargc = 0; pinargc < PIN_MAX; pinargc++) pinarg[pinargc] = pinargc; - else { + } else { /* get first arg */ pinargc = pinarg_get(argv[optind++], pinarg); if (pinargc == 0) @@ -427,9 +477,21 @@ command_ret_t do_pin(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) if (pinargc == 1) print_pin(pinarg[0], 0); else { - if (printheader) - printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n" - "-----------------------------------------\n")); + if (printheader) { + if (pin_names_width > 0) { + if ( strlen("Name") > pin_names_width) + pin_names_width = strlen("Name"); + char s[pin_names_width+1]; + memset(s, ' ', pin_names_width); + s[pin_names_width] = '\0'; + strncpy_P(s, PSTR("Name"), 4); + printf_P(PSTR("Pin %s Config Level Divider Frequency/Hz\n"),s); + memset(s, '-', pin_names_width); + printf_P(PSTR("----%s-----------------------------------\n"), s); + } else + printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n" + "--------------------------------------\n")); + } for (int i = 0; i < pinargc; i++) print_pin(pinarg[i], 1); } diff --git a/avr/main.c b/avr/main.c index e7ebd5d..ba8a672 100644 --- a/avr/main.c +++ b/avr/main.c @@ -168,13 +168,13 @@ const char *bootdelay_process(void) char *s; int bootdelay; - bootdelay = (int) getenv_ulong(PSTR("bootdelay"), 10, CONFIG_BOOTDELAY); + bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY); debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); _delay_ms(20); - s = getenv(PSTR("bootcmd")); + s = getenv(PSTR(ENV_BOOTCMD)); stored_bootdelay = bootdelay; return s; } @@ -212,7 +212,7 @@ int main(void) if (reset_reason_is_power_on()) _delay_ms(CONFIG_PWRON_DELAY); - serial_setup(getenv_ulong(PSTR("baudrate"), 10, CONFIG_BAUDRATE)); + serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE)); sei(); #if DEBUG diff --git a/include/config.h b/include/config.h index 4093a67..c0777a8 100644 --- a/include/config.h +++ b/include/config.h @@ -1,6 +1,13 @@ #ifndef CONFIG_H #define CONFIG_H +/* Environment variables */ + +#define ENV_BAUDRATE "baudrate" +#define ENV_BOOTDELAY "bootdelay" +#define ENV_BOOTCMD "bootcmd" +#define ENV_PINALIAS "pin_alias" + #define CONFIG_ENV_SIZE 1600 #define CONFIG_ENV_OFFSET 0 #define CONFIG_ENVVAR_MAX 20 -- 2.39.2