From 9ed3bb53536b9c3fb39fa56b53beb316172ad0fb Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Sat, 11 Jul 2015 17:46:50 +0900 Subject: [PATCH] Fix stlinkv2 for STM32F103xE --- tool/stlinkv2.py | 83 ++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/tool/stlinkv2.py b/tool/stlinkv2.py index bbe188b..0d2523c 100755 --- a/tool/stlinkv2.py +++ b/tool/stlinkv2.py @@ -34,8 +34,8 @@ from colorama import init as colorama_init, Fore, Back, Style CORE_ID_CORTEX_M3=0x1ba01477 CORE_ID_CORTEX_M0=0x0bb11477 -CHIP_ID_STM32F103=0x20036410 - +CHIP_ID_STM32F103xB=0x20036410 +CHIP_ID_STM32F103xE=0x10016414 GPIOA=0x40010800 GPIOB=0x40010C00 @@ -68,7 +68,9 @@ FLASH_CR_STRT= 0x0040 FLASH_CR_LOCK= 0x0080 FLASH_CR_OPTWRE= 0x0200 +FLASH_SIZE_F1_8=0x10000 # 64KiB FLASH_SIZE_F1=0x20000 # 128KiB +FLASH_SIZE_F1_E=0x80000 # 512KiB FLASH_SIZE_F0=0x4000 # 16KiB SPI1= 0x40013000 @@ -477,12 +479,12 @@ class stlinkv2(object): elif mode != 1 and mode != 4: self.exit_from_dfu() new_mode = self.stl_mode() - print "Change ST-Link/V2 mode %04x -> %04x" % (mode, new_mode) + print("Change ST-Link/V2 mode %04x -> %04x" % (mode, new_mode)) self.control_nrst(2) self.enter_swd() s = self.get_status() if s != 0x0080: - print "Status is %04x" % s + print("Status is %04x" % s) self.run() s = self.get_status() if s != 0x0080: @@ -498,13 +500,21 @@ class stlinkv2(object): self.core_id = self.get_core_id() if self.core_id == CORE_ID_CORTEX_M3: self.chip_id = self.read_memory_u32(0xE0042000) + if self.chip_id == CHIP_ID_STM32F103xB: + self.chip_stm32 = True + self.flash_size = FLASH_SIZE_F1 + elif self.chip_id == CHIP_ID_STM32F103xE: + self.chip_stm32 = True + self.flash_size = FLASH_SIZE_F1_E + else: + self.chip_stm32 = False elif self.core_id == CORE_ID_CORTEX_M0: self.chip_id = 0 + self.chip_stm32 = False else: raise ValueError("Unknown core ID", self.core_id) - if self.chip_id == CHIP_ID_STM32F103: + if self.chip_stm32: self.rdp_key = RDP_KEY_F1 - self.flash_size = FLASH_SIZE_F1 self.flash_block_size = FLASH_BLOCK_SIZE_F1 self.require_nrst = False self.external_spi_flash = True @@ -559,15 +569,15 @@ def open_stlinkv2(): return None def help(): - print "stlinkv2.py [-h]: Show this help message" - print "stlinkv2.py [-e]: Erase flash ROM" - print "stlinkv2.py [-u]: Unlock flash ROM" - print "stlinkv2.py [-s]: Show status" - print "stlinkv2.py [-b] [-n] [-r] [-i] FILE: Write content of FILE to flash ROM" - print " -b: Blank check before write (auto erase when not blank)" - print " -n: Don't enable read protection after write" - print " -r: Don't reset after write" - print " -i: Don't test SPI flash" + print("stlinkv2.py [-h]: Show this help message") + print("stlinkv2.py [-e]: Erase flash ROM") + print("stlinkv2.py [-u]: Unlock flash ROM") + print("stlinkv2.py [-s]: Show status") + print("stlinkv2.py [-b] [-n] [-r] [-i] FILE: Write content of FILE to flash ROM") + print(" -b: Blank check before write (auto erase when not blank)") + print(" -n: Don't enable read protection after write") + print(" -r: Don't reset after write") + print(" -i: Don't test SPI flash") def main(show_help, erase_only, no_protect, spi_flash_check, @@ -581,20 +591,15 @@ def main(show_help, erase_only, no_protect, spi_flash_check, if not stl: raise ValueError("No ST-Link/V2 device found.", None) - print "ST-Link/V2 version info: %d %d %d" % stl.version() + print("ST-Link/V2 version info: %d %d %d" % stl.version()) (core_id, chip_id) = stl.start() # FST-01 chip id: 0x20036410 - print "CORE: %08x, CHIP_ID: %08x" % (core_id, chip_id) - print "Flash ROM read protection:", + print("CORE: %08x, CHIP_ID: %08x" % (core_id, chip_id)) protection = stl.protection() - if protection: - print "ON" - else: - print "off" - + print("Flash ROM read protection: " + ("ON" if protection else "off")) option_bytes = stl.option_bytes_read() - print "Option bytes: %08x" % option_bytes + print("Option bytes: %08x" % option_bytes) rdp_key = stl.get_rdp_key() if (option_bytes & 0xff) == rdp_key: ob_protection_enable = False @@ -605,7 +610,7 @@ def main(show_help, erase_only, no_protect, spi_flash_check, stl.enter_debug() status = stl.get_status() if status != 0x0081: - print "Core does not halt, try API V2 halt." + print("Core does not halt, try API V2 halt.") # DCB_DHCSR DBGKEY|C_HALT|C_DEBUGEN stl.write_debug_reg(0xE000EDF0, 0xA05F0003) status = stl.get_status() @@ -616,7 +621,7 @@ def main(show_help, erase_only, no_protect, spi_flash_check, if protection: if status_only: - print "The MCU is now stopped." + print("The MCU is now stopped.") return 0 elif not unlock: raise OperationFailure("Flash ROM is protected") @@ -624,7 +629,7 @@ def main(show_help, erase_only, no_protect, spi_flash_check, if not skip_blank_check: stl.reset_sys() blank = stl.blank_check() - print "Flash ROM blank check: %s" % blank + print("Flash ROM blank check: %s" % blank) else: blank = True if status_only: @@ -633,12 +638,12 @@ def main(show_help, erase_only, no_protect, spi_flash_check, stl.exit_debug() return 0 elif unlock and not ob_protection_enable: - print "No need to unlock. Protection is not enabled." + print("No need to unlock. Protection is not enabled.") return 1 if erase_only: if blank: - print "No need to erase" + print("No need to erase") return 0 stl.setup_gpio() @@ -652,18 +657,18 @@ def main(show_help, erase_only, no_protect, spi_flash_check, stl.usb_disconnect() time.sleep(0.100) stl.finish_gpio() - print "Flash ROM read protection disabled. Reset the board, now." + print("Flash ROM read protection disabled. Reset the board, now.") return 0 if spi_flash_check and stl.has_spi_flash(): stl.spi_flash_init() id = stl.spi_flash_read_id() - print "SPI Flash ROM ID: %06x" % id + print("SPI Flash ROM ID: %06x" % id) if id != 0xbf254a: raise ValueError("bad spi flash ROM ID") if not blank: - print "ERASE ALL" + print("ERASE ALL") stl.reset_sys() stl.flash_erase_all() @@ -675,10 +680,10 @@ def main(show_help, erase_only, no_protect, spi_flash_check, time.sleep(0.100) - print "WRITE" + print("WRITE") stl.flash_write(0x08000000, data) - print "VERIFY" + print("VERIFY") data_received = () size = len(data) off = 0 @@ -693,9 +698,9 @@ def main(show_help, erase_only, no_protect, spi_flash_check, compare(data, data_received) if not no_protect and stl.has_protection(): - print "PROTECT" + print("PROTECT") stl.option_bytes_erase() - print "Flash ROM read protection enabled. Reset the board to enable protection." + print("Flash ROM read protection enabled. Reset the board to enable protection.") if reset_after_successful_write: stl.control_nrst(2) @@ -752,6 +757,8 @@ if __name__ == '__main__': f = open(filename,'rb') data = f.read() f.close() + if len(data) % 1: + raise ValueError("The size of file should be even") sys.argv.pop(1) colorama_init() @@ -761,7 +768,7 @@ if __name__ == '__main__': reset_after_successful_write, skip_blank_check, status_only, unlock, data) if r == 0: - print Fore.WHITE + Back.BLUE + Style.BRIGHT + "SUCCESS" + Style.RESET_ALL + print(Fore.WHITE + Back.BLUE + Style.BRIGHT + "SUCCESS" + Style.RESET_ALL) sys.exit(r) except Exception as e: - print Back.RED + Style.BRIGHT + repr(e) + Style.RESET_ALL + print(Back.RED + Style.BRIGHT + repr(e) + Style.RESET_ALL) -- 2.20.1