From 052b5f830a9be793c034db638038ab79816aa8f4 Mon Sep 17 00:00:00 2001 From: Theodor M Date: Thu, 29 Aug 2019 21:05:24 +0200 Subject: [PATCH 1/6] Made script executable with python3. Added braces for compatibility with python3, should still be valid python2 which interpretes these braces as a tuple with a single element and simplifies it to that element. Replaced whitespaces with tabs as required by python3, since indentation must be uniform (either whitespaces or tabs). Removed trailing whitespaces. Tested by flashing various STM32 boards with blackmagic firmware using python3.7. --- scripts/dfu.py | 46 +++++++++++++++---------------- scripts/stm32_mem.py | 64 ++++++++++++++++++++++---------------------- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/scripts/dfu.py b/scripts/dfu.py index 5e1e5925..170f2d2a 100644 --- a/scripts/dfu.py +++ b/scripts/dfu.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # # dfu.py: Access USB DFU class devices -# Copyright (C) 2009 Black Sphere Technologies +# Copyright (C) 2009 Black Sphere Technologies # Written by Gareth McMullin # # This program is free software: you can redistribute it and/or modify @@ -17,12 +17,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . - + import usb DFU_DETACH_TIMEOUT = 1000 -# DFU Requests +# DFU Requests DFU_DETACH = 0x00 DFU_DNLOAD = 0x01 DFU_UPLOAD = 0x02 @@ -87,40 +87,40 @@ class dfu_device(object): def release(self): self.handle.releaseInterface() def detach(self, wTimeout=255): - self.handle.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_CLASS | - usb.RECIP_INTERFACE, DFU_DETACH, + self.handle.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_CLASS | + usb.RECIP_INTERFACE, DFU_DETACH, None, value=wTimeout, index=self.index) def download(self, wBlockNum, data): self.handle.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_CLASS | usb.RECIP_INTERFACE, DFU_DNLOAD, - data, value=wBlockNum, index=self.index) + data, value=wBlockNum, index=self.index) def upload(self, wBlockNum, length): - return self.handle.controlMsg(usb.ENDPOINT_IN | + return self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, DFU_UPLOAD, - length, value=wBlockNum, index=self.index) + length, value=wBlockNum, index=self.index) def get_status(self): - buf = self.handle.controlMsg(usb.ENDPOINT_IN | + buf = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, DFU_GETSTATUS, - 6, index=self.index) + 6, index=self.index) return dfu_status(buf) - + def clear_status(self): - self.handle.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_CLASS | - usb.RECIP_INTERFACE, DFU_CLRSTATUS, + self.handle.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_CLASS | + usb.RECIP_INTERFACE, DFU_CLRSTATUS, "", index=0) def get_state(self): - buf = self.handle.controlMsg(usb.ENDPOINT_IN | + buf = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, DFU_GETSTATE, - 1, index=self.index) + 1, index=self.index) return buf[0] def abort(self): - self.handle.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_CLASS | - usb.RECIP_INTERFACE, DFU_ABORT, + self.handle.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_CLASS | + usb.RECIP_INTERFACE, DFU_ABORT, None, index=self.index) @@ -138,7 +138,7 @@ class dfu_device(object): if status.bState == STATE_DFU_IDLE: return True - if ((status.bState == STATE_DFU_DOWNLOAD_SYNC) or + if ((status.bState == STATE_DFU_DOWNLOAD_SYNC) or (status.bState == STATE_DFU_DOWNLOAD_IDLE) or (status.bState == STATE_DFU_MANIFEST_SYNC) or (status.bState == STATE_DFU_UPLOAD_IDLE) or @@ -157,7 +157,7 @@ class dfu_device(object): if ((status.bState == STATE_APP_DETACH) or (status.bState == STATE_DFU_MANIFEST_WAIT_RESET)): - usb.reset(self.handle) + usb.reset(self.handle) return False raise Exception @@ -178,16 +178,16 @@ def finddevs(): if __name__ == "__main__": devs = finddevs() if not devs: - print "No devices found!" + print("No devices found!") exit(-1) for dfu in devs: handle = dfu[0].open() man = handle.getString(dfu[0].iManufacturer, 30) product = handle.getString(dfu[0].iProduct, 30) - print "Device %s: ID %04x:%04x %s - %s" % (dfu[0].filename, - dfu[0].idVendor, dfu[0].idProduct, man, product) - print "%r, %r" % (dfu[1], dfu[2]) + print("Device %s: ID %04x:%04x %s - %s" % (dfu[0].filename, + dfu[0].idVendor, dfu[0].idProduct, man, product)) + print("%r, %r" % (dfu[1], dfu[2])) diff --git a/scripts/stm32_mem.py b/scripts/stm32_mem.py index 805f2481..a9695d39 100755 --- a/scripts/stm32_mem.py +++ b/scripts/stm32_mem.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # # stm32_mem.py: STM32 memory access using USB DFU class -# Copyright (C) 2011 Black Sphere Technologies +# Copyright (C) 2011 Black Sphere Technologies # Copyright (C) 2017 Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de) # Written by Gareth McMullin # @@ -88,7 +88,7 @@ def stm32_scan(args, test): if test == True: return - print "No DFU devices found!" + print("No DFU devices found!") exit(-1) for dev in devs: @@ -101,7 +101,7 @@ def stm32_scan(args, test): continue man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30) - if man == "Black Sphere Technologies": + if man == b"Black Sphere Technologies": bmp = bmp + 1 bmp_devs.append(dev) @@ -109,25 +109,25 @@ def stm32_scan(args, test): if test == True: return - print "No compatible device found\n" + print("No compatible device found\n") exit(-1) if bmp > 1 and not args.serial_target: if test == True: return - print "Found multiple devices:\n" + print("Found multiple devices:\n") for dev in bmp_devs: dfudev = dfu.dfu_device(*dev) man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30) product = dfudev.handle.getString(dfudev.dev.iProduct, 96) serial_no = dfudev.handle.getString(dfudev.dev.iSerialNumber, 30) - print "Device ID:\t %04x:%04x" % (dfudev.dev.idVendor, dfudev.dev.idProduct) - print "Manufacturer:\t %s" % man - print "Product:\t %s" % product - print "Serial:\t\t %s\n" % serial_no + print("Device ID:\t %04x:%04x" % (dfudev.dev.idVendor, dfudev.dev.idProduct)) + print("Manufacturer:\t %s" % man) + print("Product:\t %s" % product) + print("Serial:\t\t %s\n" % serial_no) - print "Select device with serial number!" + print("Select device with serial number!") exit (-1) for dev in bmp_devs: @@ -142,23 +142,23 @@ def stm32_scan(args, test): if man == "Black Sphere Technologies": break - print "Device ID:\t %04x:%04x" % (dfudev.dev.idVendor, dfudev.dev.idProduct) - print "Manufacturer:\t %s" % man - print "Product:\t %s" % product - print "Serial:\t\t %s" % serial_no + print("Device ID:\t %04x:%04x" % (dfudev.dev.idVendor, dfudev.dev.idProduct)) + print("Manufacturer:\t %s" % man) + print("Product:\t %s" % product) + print("Serial:\t\t %s" % serial_no) if args.serial_target and serial_no != args.serial_target: - print "Serial number doesn't match!\n" + print("Serial number doesn't match!\n") exit(-2) return dfudev if __name__ == "__main__": - print - print "USB Device Firmware Upgrade - Host Utility -- version 1.2" - print "Copyright (C) 2011 Black Sphere Technologies" - print "License GPLv3+: GNU GPL version 3 or later " - print + print("-") + print("USB Device Firmware Upgrade - Host Utility -- version 1.2") + print("Copyright (C) 2011 Black Sphere Technologies") + print("License GPLv3+: GNU GPL version 3 or later ") + print("-") parser = argparse.ArgumentParser() parser.add_argument("progfile", help="Binary file to program") @@ -171,12 +171,12 @@ if __name__ == "__main__": state = dfudev.get_state() except: if args.manifest : exit(0) - print "Failed to read device state! Assuming APP_IDLE" + print("Failed to read device state! Assuming APP_IDLE") state = dfu.STATE_APP_IDLE if state == dfu.STATE_APP_IDLE: dfudev.detach() dfudev.release() - print "Invoking DFU Device" + print("Invoking DFU Device") timeout = 0 while True : sleep(0.5) @@ -184,11 +184,11 @@ if __name__ == "__main__": dfudev = stm32_scan(args, True) if dfudev: break if timeout > 5 : - print "Error: DFU device did not appear" + print("Error: DFU device did not appear") exit(-1) if args.manifest : stm32_manifest(dfudev) - print "Invoking Application Device" + print("Invoking Application Device") exit(0) dfudev.make_idle() file = open(args.progfile, "rb") @@ -198,7 +198,7 @@ if __name__ == "__main__": if args.address : start = int(args.address, 0) else : - if "F4" in product: + if b"F4" in product: start = 0x8004000 else: start = 0x8002000 @@ -213,12 +213,12 @@ if __name__ == "__main__": # get evaluated and erase called only once per sector! stm32_erase(dfudev, addr) except: - print "\nErase Timed out\n" + print("\nErase Timed out\n") break try: stm32_set_address(dfudev, addr) except: - print "\nSet Address Timed out\n" + print("\nSet Address Timed out\n") break stm32_write(dfudev, bin[:1024]) bin = bin[1024:] @@ -227,8 +227,8 @@ if __name__ == "__main__": bin = file.read() len = len(bin) addr = start - print - while bin: + print("-") + while bin: try: stm32_set_address(dfudev, addr) data = stm32_read(dfudev) @@ -236,7 +236,7 @@ if __name__ == "__main__": # Abort silent if bootloader does not support upload break print ("Verifying memory at 0x%08X\r" % addr), - stdout.flush() + stdout.flush() if len > 1024 : size = 1024 else : @@ -248,7 +248,7 @@ if __name__ == "__main__": addr += 1024 len -= 1024 if len <= 0 : - print "\nVerified!" + print("\nVerified!") stm32_manifest(dfudev) - print "All operations complete!\n" + print("All operations complete!\n") From d994565f2e95afe55a54ca51035a48b4bd8c8f92 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Sat, 31 Aug 2019 09:57:32 +0200 Subject: [PATCH 2/6] pc-stlinkv2: Fix crash with serial given not matching connected devices. --- src/platforms/pc-stlinkv2/stlinkv2.c | 70 ++++++++++++++-------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/src/platforms/pc-stlinkv2/stlinkv2.c b/src/platforms/pc-stlinkv2/stlinkv2.c index f3de39e4..be691a62 100644 --- a/src/platforms/pc-stlinkv2/stlinkv2.c +++ b/src/platforms/pc-stlinkv2/stlinkv2.c @@ -683,7 +683,7 @@ void stlink_init(int argc, char **argv) goto error; } int i = 0; - bool multiple_devices = false; + int nr_stlinks = 0; while ((dev = devs[i++]) != NULL) { struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); @@ -699,10 +699,6 @@ void stlink_init(int argc, char **argv) DEBUG("STLINKV1 not supported\n"); continue; } - if (Stlink.handle) { - libusb_close(Stlink.handle); - multiple_devices = (serial)? false : true; - } r = libusb_open(dev, &Stlink.handle); if (r == LIBUSB_SUCCESS) { uint8_t data[32]; @@ -736,44 +732,50 @@ void stlink_init(int argc, char **argv) } if (serial && (!strncmp(Stlink.serial, serial, strlen(serial)))) DEBUG("Found "); - if (!serial || (!strncmp(Stlink.serial, serial, strlen(serial)))) { - if (desc.idProduct == PRODUCT_ID_STLINKV2) { - DEBUG("STLINKV20 serial %s\n", Stlink.serial); - Stlink.ver_hw = 20; - Stlink.ep_tx = 2; - } else if (desc.idProduct == PRODUCT_ID_STLINKV21) { - DEBUG("STLINKV21 serial %s\n", Stlink.serial); - Stlink.ver_hw = 21; - Stlink.ep_tx = 1; - } else if (desc.idProduct == PRODUCT_ID_STLINKV21_MSD) { - DEBUG("STLINKV21_MSD serial %s\n", Stlink.serial); - Stlink.ver_hw = 21; - Stlink.ep_tx = 1; - } else if (desc.idProduct == PRODUCT_ID_STLINKV3E) { - DEBUG("STLINKV3E serial %s\n", Stlink.serial); - Stlink.ver_hw = 30; - Stlink.ep_tx = 1; - } else if (desc.idProduct == PRODUCT_ID_STLINKV3) { - DEBUG("STLINKV3 serial %s\n", Stlink.serial); - Stlink.ver_hw = 30; - Stlink.ep_tx = 1; + if (desc.idProduct == PRODUCT_ID_STLINKV2) { + DEBUG("STLINKV20 serial %s\n", Stlink.serial); + Stlink.ver_hw = 20; + Stlink.ep_tx = 2; + } else if (desc.idProduct == PRODUCT_ID_STLINKV21) { + DEBUG("STLINKV21 serial %s\n", Stlink.serial); + Stlink.ver_hw = 21; + Stlink.ep_tx = 1; + } else if (desc.idProduct == PRODUCT_ID_STLINKV21_MSD) { + DEBUG("STLINKV21_MSD serial %s\n", Stlink.serial); + Stlink.ver_hw = 21; + Stlink.ep_tx = 1; + } else if (desc.idProduct == PRODUCT_ID_STLINKV3E) { + DEBUG("STLINKV3E serial %s\n", Stlink.serial); + Stlink.ver_hw = 30; + Stlink.ep_tx = 1; + } else if (desc.idProduct == PRODUCT_ID_STLINKV3) { + DEBUG("STLINKV3 serial %s\n", Stlink.serial); + Stlink.ver_hw = 30; + Stlink.ep_tx = 1; + } else { + DEBUG("Unknown STLINK variant, serial %s\n", Stlink.serial); + } + nr_stlinks++; + if (serial) { + if (!strncmp(Stlink.serial, serial, strlen(serial))) { + break; } else { - DEBUG("Unknown STLINK variant, serial %s\n", Stlink.serial); + libusb_close(Stlink.handle); + Stlink.handle = 0; } } - if (serial && (!strncmp(Stlink.serial, serial, strlen(serial)))) - break; } else { DEBUG("Open failed %s\n", libusb_strerror(r)); } } } - if (multiple_devices) { - DEBUG("Multiple Stlinks. Please specify serial number\n"); - goto error_1; - } if (!Stlink.handle) { - DEBUG("No Stlink device found!\n"); + if (nr_stlinks && serial) + DEBUG("No Stlink with given serial number %s\n", serial); + else if (nr_stlinks > 1) + DEBUG("Multiple Stlinks. Please specify serial number\n"); + else + DEBUG("No Stlink device found!\n"); goto error; } int config; From a01109543e7e2ca3f8c58c74cda420d0f1454da5 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Wed, 28 Aug 2019 16:04:00 +0200 Subject: [PATCH 3/6] pc-stlinkv2: Return failure if STLINK_DEBUG_APIV2_INIT_AP fails. --- src/platforms/pc-stlinkv2/stlinkv2.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/platforms/pc-stlinkv2/stlinkv2.c b/src/platforms/pc-stlinkv2/stlinkv2.c index be691a62..9c13bc00 100644 --- a/src/platforms/pc-stlinkv2/stlinkv2.c +++ b/src/platforms/pc-stlinkv2/stlinkv2.c @@ -1079,8 +1079,7 @@ bool adiv5_ap_setup(int ap) uint8_t data[2]; send_recv_retry(cmd, 16, data, 2); DEBUG_STLINK("Open AP %d\n", ap); - stlink_usb_error_check(data, true); - return true; + return (stlink_usb_error_check(data, true))? false: true; } void adiv5_ap_cleanup(int ap) From aef055bb6f3ca74af15774cad976b2d3b463b1f3 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Wed, 28 Aug 2019 15:43:47 +0200 Subject: [PATCH 4/6] adiv5: If setup AP0 fails, fail immediate. --- src/target/adiv5.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/target/adiv5.c b/src/target/adiv5.c index 0e507c16..66c45a9d 100644 --- a/src/target/adiv5.c +++ b/src/target/adiv5.c @@ -491,7 +491,10 @@ void adiv5_dp_init(ADIv5_DP_t *dp) ap = adiv5_new_ap(dp, i); if (ap == NULL) { adiv5_ap_cleanup(i); - continue; + if (i == 0) + return; + else + continue; } extern void kinetis_mdm_probe(ADIv5_AP_t *); kinetis_mdm_probe(ap); From 0d61106f9005a821b351b9c5f4bba12395851692 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Wed, 28 Aug 2019 17:31:25 +0200 Subject: [PATCH 5/6] pc-stlinkv2: Remove redundant read of CoreID. --- src/platforms/pc-stlinkv2/stlinkv2.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/platforms/pc-stlinkv2/stlinkv2.c b/src/platforms/pc-stlinkv2/stlinkv2.c index 9c13bc00..98e04f64 100644 --- a/src/platforms/pc-stlinkv2/stlinkv2.c +++ b/src/platforms/pc-stlinkv2/stlinkv2.c @@ -900,14 +900,8 @@ int stlink_enter_debug_swd(void) STLINK_DEBUG_ENTER_SWD_NO_RESET}; uint8_t data[2]; DEBUG("Enter SWD\n"); - if (send_recv_retry(cmd, 16, data, 2) != STLINK_ERROR_OK) - return -1; - uint8_t cmd1[16] = {STLINK_DEBUG_COMMAND, - STLINK_DEBUG_READCOREID}; - uint8_t data1[4]; - send_recv(cmd1, 16, data1, 4); - stlink_usb_error_check(data, false); - return 0; + send_recv(cmd, 16, data, 2); + return stlink_usb_error_check(data, true); } int stlink_enter_debug_jtag(void) From d1ee827b4df710a0f693f6df7808cb5d75b2530c Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Wed, 28 Aug 2019 20:45:39 +0200 Subject: [PATCH 6/6] pc-stlinkv2: STLINK_SWD_DP_ERROR seem unrecoverable. Throw exception. --- src/platforms/pc-stlinkv2/stlinkv2.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/platforms/pc-stlinkv2/stlinkv2.c b/src/platforms/pc-stlinkv2/stlinkv2.c index 98e04f64..f7aeb4cc 100644 --- a/src/platforms/pc-stlinkv2/stlinkv2.c +++ b/src/platforms/pc-stlinkv2/stlinkv2.c @@ -440,6 +440,7 @@ static int stlink_usb_error_check(uint8_t *data, bool verbose) case STLINK_SWD_DP_ERROR: if (verbose) DEBUG("STLINK_SWD_DP_ERROR\n"); + raise_exception(EXCEPTION_ERROR, "STLINK_SWD_DP_ERROR"); return STLINK_ERROR_FAIL; case STLINK_SWD_DP_PARITY_ERROR: if (verbose) @@ -487,7 +488,7 @@ static int send_recv_retry(uint8_t *txbuf, size_t txsize, gettimeofday(&now, NULL); timersub(&now, &start, &diff); if ((diff.tv_sec >= 1) || (res != STLINK_ERROR_WAIT)) { - DEBUG_STLINK("Failed: "); + DEBUG("write_retry failed"); return res; } } @@ -510,7 +511,7 @@ static int read_retry(uint8_t *txbuf, size_t txsize, gettimeofday(&now, NULL); timersub(&now, &start, &diff); if ((diff.tv_sec >= 1) || (res != STLINK_ERROR_WAIT)) { - DEBUG_STLINK("Failed: "); + DEBUG("read_retry failed"); return res; } } @@ -534,7 +535,6 @@ static int write_retry(uint8_t *cmdbuf, size_t cmdsize, gettimeofday(&now, NULL); timersub(&now, &start, &diff); if ((diff.tv_sec >= 1) || (res != STLINK_ERROR_WAIT)) { - DEBUG_STLINK("failed"); return res; } }