improve stlinkv2.py

This commit is contained in:
NIIBE Yutaka
2015-07-15 14:01:40 +09:00
parent 3ea5e54eb9
commit a60fe371a4
2 changed files with 43 additions and 31 deletions

View File

@@ -1,3 +1,10 @@
2015-07-15 Niibe Yutaka <gniibe@fsij.org>
* tool/stlinkv2.py (stlinkv2.get_chip_id): New. Detect flash
size, too.
(main): Call stlinkv2.get_chip_id after MCU reset and stop.
Verify read out fix.
2015-07-11 Niibe Yutaka <gniibe@fsij.org> 2015-07-11 Niibe Yutaka <gniibe@fsij.org>
* src/configure (help): Add STM32_PRIMER2 and CQ_STARM. * src/configure (help): Add STM32_PRIMER2 and CQ_STARM.

View File

@@ -36,6 +36,8 @@ CORE_ID_CORTEX_M0=0x0bb11477
CHIP_ID_STM32F103xB=0x20036410 CHIP_ID_STM32F103xB=0x20036410
CHIP_ID_STM32F103xE=0x10016414 CHIP_ID_STM32F103xE=0x10016414
# CHIP_ID_STM32F0 0x20006440
# CHIP_ID_STM32F030?? 0x10006444; FSM-55
GPIOA=0x40010800 GPIOA=0x40010800
GPIOB=0x40010C00 GPIOB=0x40010C00
@@ -68,11 +70,6 @@ FLASH_CR_STRT= 0x0040
FLASH_CR_LOCK= 0x0080 FLASH_CR_LOCK= 0x0080
FLASH_CR_OPTWRE= 0x0200 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 SPI1= 0x40013000
def uint32(v): def uint32(v):
@@ -499,17 +496,8 @@ class stlinkv2(object):
self.core_id = self.get_core_id() self.core_id = self.get_core_id()
if self.core_id == CORE_ID_CORTEX_M3: 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.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: elif self.core_id == CORE_ID_CORTEX_M0:
self.chip_id = 0
self.chip_stm32 = False self.chip_stm32 = False
else: else:
raise ValueError("Unknown core ID", self.core_id) raise ValueError("Unknown core ID", self.core_id)
@@ -521,12 +509,27 @@ class stlinkv2(object):
self.protection_feature = True self.protection_feature = True
else: else:
self.rdp_key = RDP_KEY_F0 self.rdp_key = RDP_KEY_F0
self.flash_size = FLASH_SIZE_F0
self.flash_block_size = FLASH_BLOCK_SIZE_F0 self.flash_block_size = FLASH_BLOCK_SIZE_F0
self.require_nrst = True self.require_nrst = True
self.external_spi_flash = False self.external_spi_flash = False
self.protection_feature = False self.protection_feature = False
return (self.core_id, self.chip_id) return self.core_id
def get_chip_id(self):
if self.chip_stm32:
self.chip_id = self.read_memory_u32(0xE0042000)
self.flash_size = (self.read_memory_u32(0x1ffff7e0) & 0xffff)*1024
if self.chip_id == CHIP_ID_STM32F103xB:
pass
elif self.chip_id == CHIP_ID_STM32F103xE:
pass
else:
raise ValueError("Unknown chip ID", self.chip_id)
else:
self.chip_id = self.read_memory_u32(0x40015800)
self.flash_size = (self.read_memory_u32(0x1ffff7cc) & 0xffff)*1024
print("Flash size: %dKiB" % (self.flash_size/1024))
return self.chip_id
def get_rdp_key(self): def get_rdp_key(self):
return self.rdp_key return self.rdp_key
@@ -592,19 +595,7 @@ def main(show_help, erase_only, no_protect, spi_flash_check,
raise ValueError("No ST-Link/V2 device found.", None) 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() core_id = stl.start()
# FST-01 chip id: 0x20036410
print("CORE: %08x, CHIP_ID: %08x" % (core_id, chip_id))
protection = stl.protection()
print("Flash ROM read protection: " + ("ON" if protection else "off"))
option_bytes = stl.option_bytes_read()
print("Option bytes: %08x" % option_bytes)
rdp_key = stl.get_rdp_key()
if (option_bytes & 0xff) == rdp_key:
ob_protection_enable = False
else:
ob_protection_enable = True
stl.control_nrst(2) stl.control_nrst(2)
stl.enter_debug() stl.enter_debug()
@@ -619,6 +610,20 @@ def main(show_help, erase_only, no_protect, spi_flash_check,
if status != 0x0081: if status != 0x0081:
raise ValueError("Status of core is not halt.", status) raise ValueError("Status of core is not halt.", status)
chip_id = stl.get_chip_id()
# FST-01 chip id: 0x20036410
print("CORE: %08x, CHIP_ID: %08x" % (core_id, chip_id))
protection = stl.protection()
print("Flash ROM read protection: " + ("ON" if protection else "off"))
option_bytes = stl.option_bytes_read()
print("Option bytes: %08x" % option_bytes)
rdp_key = stl.get_rdp_key()
if (option_bytes & 0xff) == rdp_key:
ob_protection_enable = False
else:
ob_protection_enable = True
if protection: if protection:
if status_only: if status_only:
print("The MCU is now stopped.") print("The MCU is now stopped.")
@@ -692,7 +697,7 @@ def main(show_help, erase_only, no_protect, spi_flash_check,
blk_size = 1024 blk_size = 1024
else: else:
blk_size = size blk_size = size
data_received = data_received + stl.read_memory(0x08000000+off, 1024) data_received = data_received + stl.read_memory(0x08000000+off, blk_size)
size = size - blk_size size = size - blk_size
off = off + blk_size off = off + blk_size
compare(data, data_received) compare(data, data_received)