From: Jerry Jacobs Date: Mon, 2 May 2016 14:47:23 +0000 (+0200) Subject: st-info: --probe: Due to already claimed stlink one of them was zero. Fixes #401. X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=25deee1055cdea42a4b28597f36e612bec77a2a1;p=fw%2Fstlink st-info: --probe: Due to already claimed stlink one of them was zero. Fixes #401. --- diff --git a/src/st-info.c b/src/st-info.c index cf61bcc..1671468 100644 --- a/src/st-info.c +++ b/src/st-info.c @@ -61,7 +61,7 @@ static void stlink_print_info(stlink_t *sl) } if (params) - printf(" descr: %s\n", params->description); + printf(" descr: %s\n", params->description); } static void stlink_probe(void) @@ -79,8 +79,40 @@ static void stlink_probe(void) stlink_probe_usb_free(&stdevs, size); } -static int print_data(stlink_t *sl, char **av) +static stlink_t *stlink_open_first(void) { + stlink_t* sl = NULL; + sl = stlink_v1_open(0, 1); + if (sl == NULL) + sl = stlink_open_usb(0, 1, NULL); + + return sl; +} + +static int print_data(char **av) +{ + stlink_t* sl = NULL; + + // Probe needs all devices unclaimed + if (strcmp(av[1], "--probe") == 0) { + stlink_probe(); + return 0; + } + + sl = stlink_open_first(); + + if (sl == NULL) { + return -1; + } + + sl->verbose = 0; + + if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) + stlink_exit_dfu_mode(sl); + + if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) + stlink_enter_swd_mode(sl); + if (strcmp(av[1], "--flash") == 0) printf("0x%zx\n", sl->flash_size); else if (strcmp(av[1], "--sram") == 0) @@ -89,8 +121,6 @@ static int print_data(stlink_t *sl, char **av) printf("0x%zx\n", sl->flash_pgsz); else if (strcmp(av[1], "--chipid") == 0) printf("0x%.4x\n", sl->chip_id); - else if (strcmp(av[1], "--probe") == 0) - stlink_probe(); else if (strcmp(av[1], "--serial") == 0) stlink_print_serial(sl, false); else if (strcmp(av[1], "--hla-serial") == 0) @@ -109,50 +139,24 @@ static int print_data(stlink_t *sl, char **av) printf("%s\n", params->description); } - return 0; -} - + if (sl) + { + stlink_exit_debug_mode(sl); + stlink_close(sl); + } -stlink_t* open_sl(void) -{ - stlink_t* sl; - sl = stlink_v1_open(0, 1); - if (sl == NULL) - sl = stlink_open_usb(0, 1, NULL); - return sl; + return 0; } - int main(int ac, char** av) { - stlink_t* sl = NULL; int err = -1; if (ac < 2) { usage(); return -1; } - sl = open_sl(); - - if (sl == NULL) { - return -1; - } - - sl->verbose = 0; - - if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) - stlink_exit_dfu_mode(sl); - - if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) - stlink_enter_swd_mode(sl); - - err = print_data(sl, av); - - if (sl != NULL) - { - stlink_exit_debug_mode(sl); - stlink_close(sl); - } + err = print_data(av); return err; } diff --git a/src/stlink-usb.c b/src/stlink-usb.c index e7e0298..4cdefd7 100644 --- a/src/stlink-usb.c +++ b/src/stlink-usb.c @@ -743,6 +743,10 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16 int devBus =0; int devAddr=0; + /* @TODO: Reading a environment variable in a usb open function is not very nice, this + should be refactored and moved into the CLI tools, and instead of giving USB_BUS:USB_ADDR a real stlink + serial string should be passed to this function. Probably people are using this but this is very odd because + as programmer can change to multiple busses and it is better to detect them based on serial. */ char *device = getenv("STLINK_DEVICE"); if (device) { char *c = strchr(device,':');