pc-stlinkv2: Print serial number of V20 devices readable.

This commit is contained in:
Uwe Bonnes 2019-07-19 20:53:03 +02:00
parent 6323409fec
commit 1b3acf3b8a
1 changed files with 30 additions and 7 deletions

View File

@ -33,6 +33,7 @@
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <ctype.h>
#include <sys/time.h> #include <sys/time.h>
#if !defined(timersub) #if !defined(timersub)
@ -206,7 +207,7 @@ typedef struct {
uint16_t vid; uint16_t vid;
uint16_t pid; uint16_t pid;
uint8_t transport_mode; uint8_t transport_mode;
uint8_t serial[32]; char serial[32];
uint8_t dap_select; uint8_t dap_select;
uint8_t ep_tx; uint8_t ep_tx;
uint8_t ver_hw; /* 20, 21 or 31 deciphered from USB PID.*/ uint8_t ver_hw; /* 20, 21 or 31 deciphered from USB PID.*/
@ -719,16 +720,38 @@ void stlink_init(int argc, char **argv)
} }
r = libusb_open(dev, &Stlink.handle); r = libusb_open(dev, &Stlink.handle);
if (r == LIBUSB_SUCCESS) { if (r == LIBUSB_SUCCESS) {
uint8_t data[32];
uint16_t lang;
libusb_get_string_descriptor(
Stlink.handle, 0, 0, data, sizeof(data));
lang = data[2] << 8 | data[3];
unsigned char sernum[32];
if (desc.iSerialNumber) { if (desc.iSerialNumber) {
r = libusb_get_string_descriptor_ascii r = libusb_get_string_descriptor
(Stlink.handle,desc.iSerialNumber, Stlink.serial, (Stlink.handle, desc.iSerialNumber, lang,
sizeof(Stlink.serial)); sernum, sizeof(sernum));
} else { } else {
DEBUG("No serial number\n"); DEBUG("No serial number\n");
} }
if (serial && (!strncmp((char*)Stlink.serial, serial, strlen(serial)))) /* Older devices have hex values instead of ascii
* in the serial string. Recode eventually!*/
bool readable = true;
uint16_t *p = (uint16_t *)sernum;
for (p += 1; *p; p++) {
bool isr = isalnum(*p);
readable &= isr;
}
char *s = Stlink.serial;
p = (uint16_t *)sernum;
for (p += 1; *p; p++, s++) {
if (readable)
*s = *p;
else
snprintf(s, 3, "%02x", *p & 0xff);
}
if (serial && (!strncmp(Stlink.serial, serial, strlen(serial))))
DEBUG("Found "); DEBUG("Found ");
if (!serial || (!strncmp((char*)Stlink.serial, serial, strlen(serial)))) { if (!serial || (!strncmp(Stlink.serial, serial, strlen(serial)))) {
if (desc.idProduct == PRODUCT_ID_STLINKV2) { if (desc.idProduct == PRODUCT_ID_STLINKV2) {
DEBUG("STLINKV20 serial %s\n", Stlink.serial); DEBUG("STLINKV20 serial %s\n", Stlink.serial);
Stlink.ver_hw = 20; Stlink.ver_hw = 20;
@ -753,7 +776,7 @@ void stlink_init(int argc, char **argv)
DEBUG("Unknown STLINK variant, serial %s\n", Stlink.serial); DEBUG("Unknown STLINK variant, serial %s\n", Stlink.serial);
} }
} }
if (serial && (!strncmp((char*)Stlink.serial, serial, strlen(serial)))) if (serial && (!strncmp(Stlink.serial, serial, strlen(serial))))
break; break;
} else { } else {
DEBUG("Open failed %s\n", libusb_strerror(r)); DEBUG("Open failed %s\n", libusb_strerror(r));