kopia lustrzana https://github.com/stlink-org/stlink
Added parameter to specify one stlink v2 of many
This adds a parameter to the function stlink_open_usb and to the binary st-flash to specify one of multiple connected stlinks. As the identifier the iSerial of the stlink is used. If no serial is given the function and binary behave as before.pull/354/head
rodzic
f290f65c74
commit
06fd9c4b70
38
flash/main.c
38
flash/main.c
|
@ -17,6 +17,7 @@ struct opts
|
|||
{
|
||||
enum st_cmds cmd;
|
||||
const char* devname;
|
||||
char *serial;
|
||||
const char* filename;
|
||||
stm32_addr_t addr;
|
||||
size_t size;
|
||||
|
@ -26,11 +27,11 @@ struct opts
|
|||
|
||||
static void usage(void)
|
||||
{
|
||||
puts("stlinkv1 command line: ./st-flash [--debug] [--reset] {read|write} /dev/sgX path addr <size>");
|
||||
puts("stlinkv1 command line: ./st-flash [--debug] [--reset] [--serial <iSerial>] {read|write} /dev/sgX path addr <size>");
|
||||
puts("stlinkv1 command line: ./st-flash [--debug] /dev/sgX erase");
|
||||
puts("stlinkv2 command line: ./st-flash [--debug] [--reset] {read|write} path addr <size>");
|
||||
puts("stlinkv2 command line: ./st-flash [--debug] erase");
|
||||
puts(" use hex format for addr and <size>");
|
||||
puts("stlinkv2 command line: ./st-flash [--debug] [--reset] [--serial <iSerial>] {read|write} path addr <size>");
|
||||
puts("stlinkv2 command line: ./st-flash [--debug] [--serial <iSerial>] erase");
|
||||
puts(" use hex format for addr, <iSerial> and <size>");
|
||||
}
|
||||
|
||||
static int get_opts(struct opts* o, int ac, char** av)
|
||||
|
@ -64,6 +65,31 @@ static int get_opts(struct opts* o, int ac, char** av)
|
|||
o->reset = 0;
|
||||
}
|
||||
|
||||
if (strcmp(av[0], "--serial") == 0)
|
||||
{
|
||||
ac--;
|
||||
av++;
|
||||
int i=strlen(av[0]);
|
||||
if(i%2 != 0){
|
||||
puts("no valid hex value, length must be multiple of 2\n");
|
||||
return -1;
|
||||
}
|
||||
int j=0;
|
||||
while(i>=0 && j<=13){
|
||||
char buffer[3]={0};
|
||||
memcpy(buffer,&av[0][i],2);
|
||||
o->serial[12-j] = (char)strtol((const char*)buffer,NULL, 16);
|
||||
j++;
|
||||
i-=2;
|
||||
}
|
||||
ac--;
|
||||
av++;
|
||||
}
|
||||
else
|
||||
{
|
||||
o->serial = NULL;
|
||||
}
|
||||
|
||||
if (ac < 1) return -1;
|
||||
|
||||
/* stlinkv2 */
|
||||
|
@ -123,6 +149,8 @@ int main(int ac, char** av)
|
|||
{
|
||||
stlink_t* sl = NULL;
|
||||
struct opts o;
|
||||
char serial_buffer[13] = {0};
|
||||
o.serial = serial_buffer;
|
||||
int err = -1;
|
||||
|
||||
o.size = 0;
|
||||
|
@ -141,7 +169,7 @@ int main(int ac, char** av)
|
|||
}
|
||||
else /* stlinkv2 */
|
||||
{
|
||||
sl = stlink_open_usb(o.log_level, 1);
|
||||
sl = stlink_open_usb(o.log_level, 1, o.serial);
|
||||
if (sl == NULL) goto on_error;
|
||||
sl->verbose = o.log_level;
|
||||
}
|
||||
|
|
|
@ -173,7 +173,7 @@ int main(int argc, char** argv) {
|
|||
parse_options(argc, argv, &state);
|
||||
switch (state.stlink_version) {
|
||||
case 2:
|
||||
sl = stlink_open_usb(state.logging_level, 0);
|
||||
sl = stlink_open_usb(state.logging_level, 0, NULL);
|
||||
if(sl == NULL) return 1;
|
||||
break;
|
||||
case 1:
|
||||
|
|
|
@ -530,7 +530,7 @@ connect_button_cb (GtkWidget *widget, gpointer data)
|
|||
/* try version 1 then version 2 */
|
||||
gui->sl = stlink_v1_open(0, 1);
|
||||
if (gui->sl == NULL) {
|
||||
gui->sl = stlink_open_usb(0, 1);
|
||||
gui->sl = stlink_open_usb(0, 1, NULL);
|
||||
}
|
||||
if (gui->sl == NULL) {
|
||||
stlink_gui_set_info_error_message (gui, "Failed to connect to STLink."); return;
|
||||
|
|
|
@ -51,7 +51,7 @@ stlink_t* open_sl(void)
|
|||
stlink_t* sl;
|
||||
sl = stlink_v1_open(0, 1);
|
||||
if (sl == NULL)
|
||||
sl = stlink_open_usb(0, 1);
|
||||
sl = stlink_open_usb(0, 1, NULL);
|
||||
return sl;
|
||||
}
|
||||
|
||||
|
|
|
@ -251,7 +251,7 @@ int main(int ac, char** av) {
|
|||
|
||||
sig_init();
|
||||
|
||||
sl = stlink_open_usb(10, 1);
|
||||
sl = stlink_open_usb(10, 1, NULL);
|
||||
if (sl != NULL) {
|
||||
printf("ST-Linky proof-of-concept terminal :: Created by Necromant for lulz\n");
|
||||
stlink_version(sl);
|
||||
|
|
|
@ -728,7 +728,7 @@ stlink_backend_t _stlink_usb_backend = {
|
|||
};
|
||||
|
||||
|
||||
stlink_t* stlink_open_usb(const int verbose, int reset) {
|
||||
stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial) {
|
||||
stlink_t* sl = NULL;
|
||||
struct stlink_libusb* slu = NULL;
|
||||
int error = -1;
|
||||
|
@ -747,7 +747,6 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
|
|||
sl->backend_data = slu;
|
||||
|
||||
sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
|
||||
|
||||
if (libusb_init(&(slu->libusb_ctx))) {
|
||||
WLOG("failed to init libusb context, wrong version of libraries?\n");
|
||||
goto on_error;
|
||||
|
@ -776,7 +775,22 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
|
|||
if (desc.idVendor!=USB_ST_VID) continue;
|
||||
if (devBus && devAddr)
|
||||
if ((libusb_get_bus_number(list[cnt])!=devBus) || (libusb_get_device_address(list[cnt])!=devAddr)) continue;
|
||||
if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ) break;
|
||||
if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ){
|
||||
if ((p_usb_iserial != NULL)){
|
||||
unsigned char buffer[13];
|
||||
struct libusb_device_handle* handle;
|
||||
libusb_open(list[cnt], &handle);
|
||||
libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buffer, 13);
|
||||
libusb_close(handle);
|
||||
if (memcmp(p_usb_iserial,&buffer,12) == 0){
|
||||
break;
|
||||
}else{
|
||||
continue;
|
||||
}
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (desc.idProduct == USB_STLINK_PID) {
|
||||
slu->protocoll = 1;
|
||||
break;
|
||||
|
|
|
@ -30,7 +30,7 @@ extern "C" {
|
|||
unsigned int cmd_len;
|
||||
};
|
||||
|
||||
stlink_t* stlink_open_usb(const int verbose, int reset);
|
||||
stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -10,7 +10,7 @@ int main(int ac, char** av) {
|
|||
ac = ac;
|
||||
av = av;
|
||||
|
||||
sl = stlink_open_usb(10, 1);
|
||||
sl = stlink_open_usb(10, 1, NULL);
|
||||
if (sl != NULL) {
|
||||
printf("-- version\n");
|
||||
stlink_version(sl);
|
||||
|
|
Ładowanie…
Reference in New Issue