From 38d964e7d613c6c3d4c691fd79cdb46e1c61004e Mon Sep 17 00:00:00 2001 From: Jean-Christian de Rivaz <84882+jcdr@users.noreply.github> Date: Thu, 8 Apr 2021 23:15:58 +0200 Subject: [PATCH 1/4] Add --vid= and --pid= for libusb --- teensy_loader_cli.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/teensy_loader_cli.c b/teensy_loader_cli.c index 6e78f1c..99c46bc 100644 --- a/teensy_loader_cli.c +++ b/teensy_loader_cli.c @@ -37,7 +37,7 @@ void usage(const char *err) { if(err != NULL) fprintf(stderr, "%s\n\n", err); fprintf(stderr, - "Usage: teensy_loader_cli --mcu= [-w] [-h] [-n] [-b] [-v] \n" + "Usage: teensy_loader_cli --mcu= [--vid=] [--pid=] [-w] [-h] [-n] [-b] [-v] \n" "\t-w : Wait for device to appear\n" "\t-r : Use hard reboot if device not online\n" "\t-s : Use soft reboot if device not online (Teensy 3.x & 4.x)\n" @@ -78,6 +78,8 @@ int reboot_after_programming = 1; int verbose = 0; int boot_only = 0; int code_size = 0, block_size = 0; +int opt_vid = 0x16C0; +int opt_pid = 0x0483; const char *filename=NULL; @@ -334,7 +336,7 @@ int soft_reboot(void) { usb_dev_handle *serial_handle = NULL; - serial_handle = open_usb_device(0x16C0, 0x0483); + serial_handle = open_usb_device(opt_vid, opt_pid); if (!serial_handle) { char *error = usb_strerror(); printf("Error opening USB device: %s\n", error); @@ -1168,6 +1170,8 @@ void parse_options(int argc, char **argv) if(strcasecmp(name, "help") == 0) usage(NULL); else if(strcasecmp(name, "mcu") == 0) read_mcu(val); else if(strcasecmp(name, "list-mcus") == 0) list_mcus(); + else if(strcasecmp(name, "vid") == 0) opt_vid = strtoul(val, NULL, 16); + else if(strcasecmp(name, "pid") == 0) opt_pid = strtoul(val, NULL, 16); else { fprintf(stderr, "Unknown option \"%s\"\n\n", arg); usage(NULL); From e85d77e6f88d8b693ab459147301bd9f58846b25 Mon Sep 17 00:00:00 2001 From: Jean-Christian de Rivaz <84882+jcdr@users.noreply.github> Date: Thu, 8 Apr 2021 23:16:16 +0200 Subject: [PATCH 2/4] Add --serial= for libusb --- teensy_loader_cli.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/teensy_loader_cli.c b/teensy_loader_cli.c index 99c46bc..e8bbbf4 100644 --- a/teensy_loader_cli.c +++ b/teensy_loader_cli.c @@ -37,7 +37,7 @@ void usage(const char *err) { if(err != NULL) fprintf(stderr, "%s\n\n", err); fprintf(stderr, - "Usage: teensy_loader_cli --mcu= [--vid=] [--pid=] [-w] [-h] [-n] [-b] [-v] \n" + "Usage: teensy_loader_cli --mcu= [--vid=] [--pid=] [--serial=] [-w] [-h] [-n] [-b] [-v] \n" "\t-w : Wait for device to appear\n" "\t-r : Use hard reboot if device not online\n" "\t-s : Use soft reboot if device not online (Teensy 3.x & 4.x)\n" @@ -80,6 +80,7 @@ int boot_only = 0; int code_size = 0, block_size = 0; int opt_vid = 0x16C0; int opt_pid = 0x0483; +char *opt_serial = NULL; const char *filename=NULL; @@ -229,13 +230,14 @@ int main(int argc, char **argv) // http://libusb.sourceforge.net/doc/index.html #include -usb_dev_handle * open_usb_device(int vid, int pid) +usb_dev_handle * open_usb_device(int vid, int pid, char *serial) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *h; char buf[128]; int r; + char string[256]; usb_init(); usb_find_busses(); @@ -255,6 +257,18 @@ usb_dev_handle * open_usb_device(int vid, int pid) printf_verbose("Found device but unable to open\n"); continue; } + if (serial) { + if (dev->descriptor.iSerialNumber) { + int ret = usb_get_string_simple(h, dev->descriptor.iSerialNumber, string, sizeof(string)); + if (ret > 0) { + printf("Serial Number: %s\n", string); + if (strcmp(serial, string)) { + usb_close(h); + continue; + } + } + } + } #ifdef LIBUSB_HAS_GET_DRIVER_NP r = usb_get_driver_np(h, 0, buf, sizeof(buf)); if (r >= 0) { @@ -289,7 +303,7 @@ static usb_dev_handle *libusb_teensy_handle = NULL; int teensy_open(void) { teensy_close(); - libusb_teensy_handle = open_usb_device(0x16C0, 0x0478); + libusb_teensy_handle = open_usb_device(0x16C0, 0x0478, NULL); if (libusb_teensy_handle) return 1; return 0; } @@ -323,7 +337,7 @@ int hard_reboot(void) usb_dev_handle *rebootor; int r; - rebootor = open_usb_device(0x16C0, 0x0477); + rebootor = open_usb_device(0x16C0, 0x0477, NULL); if (!rebootor) return 0; r = usb_control_msg(rebootor, 0x21, 9, 0x0200, 0, "reboot", 6, 100); usb_release_interface(rebootor, 0); @@ -336,7 +350,7 @@ int soft_reboot(void) { usb_dev_handle *serial_handle = NULL; - serial_handle = open_usb_device(opt_vid, opt_pid); + serial_handle = open_usb_device(opt_vid, opt_pid, opt_serial); if (!serial_handle) { char *error = usb_strerror(); printf("Error opening USB device: %s\n", error); @@ -1172,6 +1186,7 @@ void parse_options(int argc, char **argv) else if(strcasecmp(name, "list-mcus") == 0) list_mcus(); else if(strcasecmp(name, "vid") == 0) opt_vid = strtoul(val, NULL, 16); else if(strcasecmp(name, "pid") == 0) opt_pid = strtoul(val, NULL, 16); + else if(strcasecmp(name, "serial") == 0) opt_serial = val; else { fprintf(stderr, "Unknown option \"%s\"\n\n", arg); usage(NULL); From 1ca27741523543b5d775a5afa38a5d3dfc2dab3c Mon Sep 17 00:00:00 2001 From: Jean-Christian de Rivaz <84882+jcdr@users.noreply.github.com> Date: Thu, 8 Apr 2021 23:27:39 +0200 Subject: [PATCH 3/4] Add options vid, pid, and serial to README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 12c61b2..deedd96 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Teensy Loader - Command Line Version# +# Teensy Loader - Command Line Version with options to set VID, PID, and serial number The Teensy Loader is available in a command line version for advanced users who want to automate programming, typically using a Makefile. For most uses, the graphical version in Automatic Mode is much easier. @@ -61,6 +61,12 @@ Optional command line parameters: `-v` : Verbose output. Normally teensy_loader_cli prints only error messages if any operation fails. This enables verbose output, which can help with troubleshooting, or simply show you more status information. +`--vid=` : (only with libusb) Specify the Vendor ID to match for sending the soft reset. + +`--pid=` : (only with libusb) Specify the Product ID to match for sending the soft reset. + +`--serial=` : (only with libusb) Specify the serial number to match for sending the soft reset. + ## System Specific Setup Linux requires UDEV rules for non-root users. From 91051192331d4fa3daf7121a41db0935ddae4b76 Mon Sep 17 00:00:00 2001 From: Jean-Christian de Rivaz <84882+jcdr@users.noreply.github> Date: Sun, 11 Apr 2021 03:30:44 +0200 Subject: [PATCH 4/4] Overrides on all plateforms default hard and soft vid and pid with options --vid and --pid. --- teensy_loader_cli.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/teensy_loader_cli.c b/teensy_loader_cli.c index e8bbbf4..d669a4e 100644 --- a/teensy_loader_cli.c +++ b/teensy_loader_cli.c @@ -78,8 +78,10 @@ int reboot_after_programming = 1; int verbose = 0; int boot_only = 0; int code_size = 0, block_size = 0; -int opt_vid = 0x16C0; -int opt_pid = 0x0483; +int hard_vid = 0x16C0; +int hard_pid = 0x0477; +int soft_vid = 0x16C0; +int soft_pid = 0x0483; char *opt_serial = NULL; const char *filename=NULL; @@ -337,7 +339,7 @@ int hard_reboot(void) usb_dev_handle *rebootor; int r; - rebootor = open_usb_device(0x16C0, 0x0477, NULL); + rebootor = open_usb_device(hard_vid, hard_pid, NULL); if (!rebootor) return 0; r = usb_control_msg(rebootor, 0x21, 9, 0x0200, 0, "reboot", 6, 100); usb_release_interface(rebootor, 0); @@ -350,7 +352,7 @@ int soft_reboot(void) { usb_dev_handle *serial_handle = NULL; - serial_handle = open_usb_device(opt_vid, opt_pid, opt_serial); + serial_handle = open_usb_device(soft_vid, soft_pid, opt_serial); if (!serial_handle) { char *error = usb_strerror(); printf("Error opening USB device: %s\n", error); @@ -524,7 +526,7 @@ int hard_reboot(void) HANDLE rebootor; int r; - rebootor = open_usb_device(0x16C0, 0x0477); + rebootor = open_usb_device(hard_vid, hard_pid); if (!rebootor) return 0; r = write_usb_device(rebootor, "reboot", 6, 100); CloseHandle(rebootor); @@ -720,7 +722,7 @@ int hard_reboot(void) IOHIDDeviceRef rebootor; IOReturn ret; - rebootor = open_usb_device(0x16C0, 0x0477); + rebootor = open_usb_device(hard_vid, hard_pid); if (!rebootor) return 0; ret = IOHIDDeviceSetReport(rebootor, kIOHIDReportTypeOutput, 0, (uint8_t *)("reboot"), 6); @@ -827,7 +829,7 @@ int hard_reboot(void) { int r, rebootor_fd; - rebootor_fd = open_usb_device(0x16C0, 0x0477); + rebootor_fd = open_usb_device(hard_vid, hard_pid); if (rebootor_fd < 0) return 0; r = write(rebootor_fd, "reboot", 6); delay(0.1); @@ -1184,8 +1186,8 @@ void parse_options(int argc, char **argv) if(strcasecmp(name, "help") == 0) usage(NULL); else if(strcasecmp(name, "mcu") == 0) read_mcu(val); else if(strcasecmp(name, "list-mcus") == 0) list_mcus(); - else if(strcasecmp(name, "vid") == 0) opt_vid = strtoul(val, NULL, 16); - else if(strcasecmp(name, "pid") == 0) opt_pid = strtoul(val, NULL, 16); + else if(strcasecmp(name, "vid") == 0) hard_vid = soft_vid = strtoul(val, NULL, 16); + else if(strcasecmp(name, "pid") == 0) hard_pid = soft_pid = strtoul(val, NULL, 16); else if(strcasecmp(name, "serial") == 0) opt_serial = val; else { fprintf(stderr, "Unknown option \"%s\"\n\n", arg);