diff --git a/board/nm/netbird_v2/board.c b/board/nm/netbird_v2/board.c index 302fdc9a82..d4a8ca1085 100644 --- a/board/nm/netbird_v2/board.c +++ b/board/nm/netbird_v2/board.c @@ -528,6 +528,7 @@ struct shield_command { const char *name; const char *default_shieldcmd; const char *fdtshieldcmd; + void (*init)(void); }; #define SHIELD_COM_IO 0 @@ -539,7 +540,8 @@ static struct shield_command known_shield_commands[] = { "comio", "shield comio mode rs232", "fdt get value serial0 /aliases serial0;" \ - "fdt set $serial0 status okay" + "fdt set $serial0 status okay", + comio_shield_init }, { SHIELD_DUALCAN, @@ -548,7 +550,8 @@ static struct shield_command known_shield_commands[] = { "fdt get value can0 /aliases d_can0;" \ "fdt get value can1 /aliases d_can1;" \ "fdt set $can0 status okay;" \ - "fdt set $can1 status okay;" \ + "fdt set $can1 status okay;", + can_shield_init }, }; @@ -585,8 +588,9 @@ static void shield_config(void) return; } - printf("Shield found: %s\n", cmd->name); + printf("Shield: %s\n", cmd->name); + cmd->init(); shieldcmd = cmd->default_shieldcmd; /* If a shield configuration set by linux take it without bd check, we asume that Linux knows @@ -597,8 +601,6 @@ static void shield_config(void) shieldcmd = shieldcmd_linux; } - printf("Shield command: %s\n", shieldcmd); - setenv("shieldcmd", shieldcmd); set_fdtshieldcmd(cmd->fdtshieldcmd); @@ -606,9 +608,6 @@ static void shield_config(void) static void shield_init(void) { - can_shield_init(); - comio_shield_init(); - shield_config(); } #endif diff --git a/board/nm/netbird_v2/shield.c b/board/nm/netbird_v2/shield.c index 2ee7143240..97f1394a9d 100644 --- a/board/nm/netbird_v2/shield.c +++ b/board/nm/netbird_v2/shield.c @@ -50,13 +50,14 @@ int shield_set_mode(const char* shield_type, int argc, char * const argv[]) return shields[i]->setmode(argv, argc); } } - printf("Shield %s is unknown\n", shield_type); - return -1; + printf("## Error: No %s shield installed\n", shield_type); + /* Do not return error, to not show usage (request by rs) */ + return 0; } static int do_shieldmode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { - if (argc < 3) { + if (argc < 2) { return -1; } diff --git a/board/nm/netbird_v2/shield_comio.c b/board/nm/netbird_v2/shield_comio.c index c66619734a..e9e1135af2 100644 --- a/board/nm/netbird_v2/shield_comio.c +++ b/board/nm/netbird_v2/shield_comio.c @@ -140,14 +140,32 @@ static int configure_shieldmode(int mode) return 0; } -static int get_rs232(const char *mode) +enum mode_nr { + RS232, + RS485, + UNKNOWN +}; + +struct mode { + enum mode_nr nr; + const char* name; + int argc; +}; + +struct mode modes[] = { + {RS232, "rs232", 0}, + {RS485, "rs485", 2} +}; + +static const struct mode *get_mode(const char *mode) { - if (strcmp("rs232", mode) == 0) { - return 1; - } - else { - return 0; + int i; + for (i = 0; i < ARRAY_SIZE(modes); i++) { + if (strcmp(modes[i].name, mode) == 0) { + return &modes[i]; + } } + return NULL; } static int get_termination(const char* termination) @@ -167,6 +185,7 @@ static int get_mode_from_args(char * const argv[], int argc) { int termination = 0; int rs232 = 0; + const struct mode *selected_mode; assert(argc >= 2); @@ -175,18 +194,31 @@ static int get_mode_from_args(char * const argv[], int argc) return -1; } - rs232 = get_rs232(argv[1]); + selected_mode = get_mode(argv[1]); + if (selected_mode == NULL) { + debug("Mode %s not supported\n", argv[1]); + return -1; + } - if (argc > 2) { - if (rs232 || strcmp("termination", argv[2])) { + debug ("Mode %s, index %d, argc %d\n", selected_mode->name, + selected_mode->nr, selected_mode->argc); + + if (selected_mode->argc != argc - 2) { + debug("Invalid argument count for mode %s (should %d is %d)\n", + argv[1], selected_mode->argc, argc - 2); + return -1; + } + + if (selected_mode->nr == RS485) { + if (strcmp("termination", argv[2])) { debug("Invalid arguments, do not configure termination\n"); return -1; } - else { - termination = get_termination(argv[3]); - if (termination < 0) { - return -1; - } + + termination = get_termination(argv[3]); + if (termination < 0) { + debug("Invalid termination %s\n", argv[3]); + return -1; } }