From ed6054f5491326e6c505af371c462cbe2a332cae Mon Sep 17 00:00:00 2001 From: Sergey Shchelkanov Date: Thu, 1 Jun 2023 15:24:44 +0300 Subject: [PATCH] add progress info --- mik32_eeprom.py | 101 ++++++++++++++++++++++++++++-------------------- mik32_spifi.py | 48 +++++++++++++---------- 2 files changed, 87 insertions(+), 62 deletions(-) diff --git a/mik32_eeprom.py b/mik32_eeprom.py index e5b488f..be259b8 100644 --- a/mik32_eeprom.py +++ b/mik32_eeprom.py @@ -19,7 +19,7 @@ PM_Clk_APB_P_Set_OFFSET = 0x1C PM_CLOCK_CPU_S = 0 PM_CLOCK_CPU_M = (1 << PM_CLOCK_CPU_S) PM_CLOCK_EEPROM_S = 1 -PM_CLOCK_EEPROM_M = (1 << PM_CLOCK_EEPROM_S) +PM_CLOCK_EEPROM_M = (1 << PM_CLOCK_EEPROM_S) PM_CLOCK_RAM_S = 2 PM_CLOCK_RAM_M = (1 << PM_CLOCK_RAM_S) PM_CLOCK_SPIFI_S = 3 @@ -89,8 +89,9 @@ EEPROM_BEH_GLOB = 3 EEPROM_PAGE_MASK = 0x1F80 + def eeprom_sysinit(openocd: OpenOcdTclRpc): - print("MCU clock init...") + print("MCU clock init...", flush=True) openocd.write_word(WU_BASE_ADDRESS + WU_Clocks_OFFSET, 0x202) openocd.write_word(PM_BASE_ADDRESS + PM_Clk_APB_P_Set_OFFSET, 0xffffffff) @@ -99,7 +100,7 @@ def eeprom_sysinit(openocd: OpenOcdTclRpc): def eeprom_global_erase(openocd: OpenOcdTclRpc): - print("EEPROM global erase...") + print("EEPROM global erase...", flush=True) with OpenOcdTclRpc() as openocd: openocd.write_word(EEPROM_REGS_NCYCRL, 1 << EEPROM_N_LD_S | 3 << EEPROM_N_R_1_S | 1 << EEPROM_N_R_2_S) @@ -118,9 +119,10 @@ def eeprom_global_erase(openocd: OpenOcdTclRpc): (EEPROM_OP_ER << EEPROM_OP_S) | (EEPROM_BEH_GLOB << EEPROM_WRBEH_S) )) + def eeprom_global_erase_check(openocd: OpenOcdTclRpc): - print("EEPROM global erase check through APB...") - print(" Read Data at ...") + print("EEPROM global erase check through APB...", flush=True) + print(" Read Data at ...", flush=True) ex_value = 0x00000000 openocd.write_word(EEPROM_REGS_EEA, 0x00000000) for i in range(0, 64): @@ -128,19 +130,22 @@ def eeprom_global_erase_check(openocd: OpenOcdTclRpc): for j in range(0, 32): value = openocd.read_memory(EEPROM_REGS_EEDAT, 32, 1)[0] if ex_value != value: - print(f"Unexpect value at Row {i}, Word {j}, expect {ex_value:#0x}, {value:#0x}") + print( + f"Unexpect value at Row {i}, Word {j}, expect {ex_value:#0x}, {value:#0x}", flush=True) -def eeprom_write_word(openocd: OpenOcdTclRpc, address:int, word:int): +def eeprom_write_word(openocd: OpenOcdTclRpc, address: int, word: int): openocd.write_word(EEPROM_REGS_EECON, 1 << EEPROM_BWE_S) openocd.write_word(EEPROM_REGS_EEA, address) # buffer load openocd.write_word(EEPROM_REGS_EEDAT, word) - openocd.write_word(EEPROM_REGS_EECON, (1 << EEPROM_EX_S) | (1 << EEPROM_BWE_S) | (EEPROM_OP_PR << EEPROM_OP_S)) + openocd.write_word(EEPROM_REGS_EECON, (1 << EEPROM_EX_S) | ( + 1 << EEPROM_BWE_S) | (EEPROM_OP_PR << EEPROM_OP_S)) time.sleep(0.001) -def eeprom_write_page(openocd: OpenOcdTclRpc, address:int, data:List[int]): - print(f"Writing page {address:#06x}...") + +def eeprom_write_page(openocd: OpenOcdTclRpc, address: int, data: List[int], progress: str = ""): + print(f"Writing page {address:#06x}... {progress}", flush=True) openocd.write_word(EEPROM_REGS_EECON, 1 << EEPROM_BWE_S) openocd.write_word(EEPROM_REGS_EEA, address) page_address = address & EEPROM_PAGE_MASK @@ -148,23 +153,26 @@ def eeprom_write_page(openocd: OpenOcdTclRpc, address:int, data:List[int]): # buffer load for word in data: if ((address + n) & EEPROM_PAGE_MASK) != page_address: - raise Exception("ERROR: word outside page!") + raise Exception("ERROR: word outside page!", flush=True) openocd.write_word(EEPROM_REGS_EEDAT, word) - openocd.write_word(EEPROM_REGS_EECON, (1 << EEPROM_EX_S) | (1 << EEPROM_BWE_S) | (EEPROM_OP_PR << EEPROM_OP_S)) + openocd.write_word(EEPROM_REGS_EECON, (1 << EEPROM_EX_S) | ( + 1 << EEPROM_BWE_S) | (EEPROM_OP_PR << EEPROM_OP_S)) time.sleep(0.001) + def eeprom_check_data_apb(openocd: OpenOcdTclRpc, words: List[int], offset: int, print_progress=True) -> int: if print_progress: - print("EEPROM check through APB...") + print("EEPROM check through APB...", flush=True) openocd.write_word(EEPROM_REGS_EEA, offset) word_num = 0 progress = 0 if print_progress: print("[", end="", flush=True) for word in words: - value:int = openocd.read_word(EEPROM_REGS_EEDAT) + value: int = openocd.read_word(EEPROM_REGS_EEDAT) if words[word_num] != value: - print(f"Unexpect value at {word_num} word, expect {word:#0x}, get {value:#0x}") + print( + f"Unexpect value at {word_num} word, expect {word:#0x}, get {value:#0x}", flush=True) return 1 word_num += 1 curr_progress = int((word_num * 50) / len(words)) @@ -172,35 +180,37 @@ def eeprom_check_data_apb(openocd: OpenOcdTclRpc, words: List[int], offset: int, print("#"*(curr_progress - progress), end="", flush=True) progress = curr_progress if print_progress: - print("]") - print("EEPROM check through APB done!") + print("]", flush=True) + print("EEPROM check through APB done!", flush=True) return 0 + def eeprom_check_data_ahb_lite(openocd: OpenOcdTclRpc, words: List[int], offset: int, print_progress=True) -> int: if print_progress: - print("EEPROM check through AHB-Lite...") + print("EEPROM check through AHB-Lite...", flush=True) mem_array = openocd.read_memory(0x01000000 + offset, 32, len(words)) if len(words) != len(mem_array): - raise Exception("Wrong number of words in read_memory output!") + raise Exception( + "Wrong number of words in read_memory output!", flush=True) progress = 0 if print_progress: print("[", end="", flush=True) for word_num in range(len(words)): if words[word_num] != mem_array[word_num]: - print(f"Unexpect value at {word_num} word, expect {words[word_num]:#0x}, \ - get {mem_array[word_num]:#0x}") + print(f"Unexpect value at {word_num} word, expect {words[word_num]:#0x}, " + f"get {mem_array[word_num]:#0x}", flush=True) return 1 curr_progress = int((word_num * 50) / len(words)) if print_progress and (curr_progress > progress): print("#"*(curr_progress - progress), end="", flush=True) progress = curr_progress if print_progress: - print("]") - print("EEPROM check through APB done!") + print("]", flush=True) + print("EEPROM check through APB done!", flush=True) return 0 -def write_words(words: List[int], openocd: OpenOcdTclRpc, write_by_word = False, read_through_apb = False, is_resume=True) -> int: +def write_words(words: List[int], openocd: OpenOcdTclRpc, write_by_word=False, read_through_apb=False, is_resume=True) -> int: """ Write words in MIK32 EEPROM through APB bus @@ -213,19 +223,20 @@ def write_words(words: List[int], openocd: OpenOcdTclRpc, write_by_word = False, @return: return 0 if successful, 1 if failed """ - print(f"Write {len(words*4)} bytes") + print(f"Write {len(words*4)} bytes", flush=True) openocd.halt() eeprom_sysinit(openocd) eeprom_global_erase(openocd) # eeprom_global_erase_check(openocd) - openocd.write_word(EEPROM_REGS_NCYCRL, 1< int: +def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc, read_through_apb=False, is_resume=True) -> int: result = 0 openocd.halt() eeprom_sysinit(openocd) eeprom_global_erase(openocd) # eeprom_global_erase_check(openocd) - openocd.write_word(EEPROM_REGS_NCYCRL, 1< 256: raise Exception("Byte count more than 256") @@ -389,7 +391,7 @@ def spifi_write(openocd: OpenOcdTclRpc, address: int, data: List[int], data_len: def spifi_write_file(bytes: List[int], openocd: OpenOcdTclRpc, is_resume=True): # print(bytes) print(f"Write {len(bytes)} bytes") - + openocd.halt() spifi_init(openocd) spifi_erase(openocd) @@ -413,12 +415,12 @@ def spifi_write_file(bytes: List[int], openocd: OpenOcdTclRpc, is_resume=True): print("end") if is_resume: openocd.resume(0) - + return 0 -def spifi_quad_page_program(openocd: OpenOcdTclRpc, ByteAddress: int, data: List[int], byte_count: int): - print(f"Writing page {ByteAddress:#010x}...") +def spifi_quad_page_program(openocd: OpenOcdTclRpc, ByteAddress: int, data: List[int], byte_count: int, progress: str = ""): + print(f"Writing page {ByteAddress:#010x}... {progress}", flush=True) if byte_count > 256: raise Exception("Byte count more than 256") @@ -474,7 +476,8 @@ def spifi_quad_disable(openocd): (1 << SPIFI_CONFIG_CMD_DOUT_S) | (0 << SPIFI_CONFIG_CMD_POLL_S) | (1 << SPIFI_CONFIG_CMD_DATALEN_S)) - openocd.write_memory(SPIFI_CONFIG_DATA32, 8, [sreg2 & (~SREG2_QUAD_ENABLE_M)]) + openocd.write_memory(SPIFI_CONFIG_DATA32, 8, [ + sreg2 & (~SREG2_QUAD_ENABLE_M)]) spifi_wait_busy(openocd) @@ -488,30 +491,35 @@ def get_segments_list(pages_offsets: List[int], segment_size: int) -> List[int]: def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc, is_resume=True, use_quad_spi=False, use_chip_erase=False): result = 0 - + openocd.halt() spifi_init(openocd) if use_chip_erase: spifi_erase(openocd, EraseType.CHIP_ERASE) else: - spifi_erase(openocd, EraseType.SECTOR_ERASE, get_segments_list(list(pages), 4*1024)) + spifi_erase(openocd, EraseType.SECTOR_ERASE, + get_segments_list(list(pages), 4*1024)) address = 0 spifi_quad_disable(openocd) # spifi_quad_enable(openocd) - for page_offset in list(pages): + pages_offsets = list(pages) + + for index, page_offset in enumerate(pages_offsets): page_bytes = pages[page_offset] spifi_write_enable(openocd) if (use_quad_spi): - spifi_quad_page_program(openocd, page_offset, page_bytes, 256) + spifi_quad_page_program( + openocd, page_offset, page_bytes, 256, f"{(index*100)//pages_offsets.__len__()}%") else: - spifi_page_program(openocd, page_offset, page_bytes, 256) + spifi_page_program(openocd, page_offset, page_bytes, + 256, f"{(index*100)//pages_offsets.__len__()}%") spifi_wait_busy(openocd) result = spifi_read_data(openocd, page_offset, 256, page_bytes) - + if result == 1: print("Data error") return result @@ -520,5 +528,5 @@ def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc, is_resume=T if is_resume: openocd.resume(0) - + return 0