diff --git a/mik32_spifi.py b/mik32_spifi.py index cca07f2..b5b9d11 100644 --- a/mik32_spifi.py +++ b/mik32_spifi.py @@ -286,7 +286,7 @@ def spifi_wait_busy(openocd: OpenOcdTclRpc): def spifi_chip_erase(openocd: OpenOcdTclRpc): print("Chip erase...") - # spifi_intrq_clear(openocd) + spifi_intrq_clear(openocd) openocd.write_word(SPIFI_CONFIG_STAT, openocd.read_word( SPIFI_CONFIG_STAT) | SPIFI_CONFIG_STAT_INTRQ_M) openocd.write_word(SPIFI_CONFIG_CMD, (SECTOR_ERASE_COMMAND << SPIFI_CONFIG_CMD_OPCODE_S) | @@ -298,7 +298,7 @@ def spifi_chip_erase(openocd: OpenOcdTclRpc): def spifi_sector_erase(openocd: OpenOcdTclRpc, address: int): print("Erase sector %s..." % hex(address)) openocd.write_word(SPIFI_CONFIG_ADDR, address) - # spifi_intrq_clear(openocd) + spifi_intrq_clear(openocd) openocd.write_word(SPIFI_CONFIG_STAT, openocd.read_word( SPIFI_CONFIG_STAT) | SPIFI_CONFIG_STAT_INTRQ_M) openocd.write_word(SPIFI_CONFIG_CMD, (CHIP_ERASE_COMMAND << SPIFI_CONFIG_CMD_OPCODE_S) | @@ -307,9 +307,7 @@ def spifi_sector_erase(openocd: OpenOcdTclRpc, address: int): spifi_wait_intrq_timeout(openocd, "Timeout executing chip erase command") -def spifi_check_erase(openocd: OpenOcdTclRpc, address: int, byte_count: int): - print(f"Checking erase from {address:#0x} to {(byte_count-1):#0x}") - +def spifi_read_data(openocd: OpenOcdTclRpc, address: int, byte_count: int) -> List[int]: read_data: List[int] = [] openocd.write_word(SPIFI_CONFIG_ADDR, address) @@ -324,6 +322,13 @@ def spifi_check_erase(openocd: OpenOcdTclRpc, address: int, byte_count: int): read_data.append(data8) if config.is_verbose: print(f"DATA[{i+address}] = {read_data[i]:#0x}") + + return read_data + + +def spifi_check_erase(openocd: OpenOcdTclRpc, address: int, byte_count: int): + print(f"Checking erase from {address:#0x} to {(byte_count-1):#0x}") + read_data: List[int] = spifi_read_data(openocd, address, byte_count) for i in range(byte_count): if read_data[i] != 0: @@ -333,21 +338,11 @@ def spifi_check_erase(openocd: OpenOcdTclRpc, address: int, byte_count: int): return 0 -def spifi_read_data(openocd: OpenOcdTclRpc, address: int, byte_count: int, bin_data: List[int]) -> int: - read_data: List[int] = [] - openocd.write_word(SPIFI_CONFIG_ADDR, address) +def spifi_check_program(openocd: OpenOcdTclRpc, address: int, byte_count: int, bin_data: List[int]) -> int: + read_data: List[int] = spifi_read_data(openocd, address, byte_count) - spifi_intrq_clear(openocd) - openocd.write_word(SPIFI_CONFIG_CMD, (READ_DATA_COMMAND << SPIFI_CONFIG_CMD_OPCODE_S) | - (SPIFI_CONFIG_CMD_FRAMEFORM_OPCODE_3ADDR << SPIFI_CONFIG_CMD_FRAMEFORM_S) | - (SPIFI_CONFIG_CMD_FIELDFORM_ALL_SERIAL << SPIFI_CONFIG_CMD_FIELDFORM_S) | - (byte_count << SPIFI_CONFIG_CMD_DATALEN_S)) - # spifi_wait_intrq_timeout(openocd, "Timeout executing read data command") - for i in range(byte_count): - data8 = openocd.read_memory(SPIFI_CONFIG_DATA32, 8, 1)[0] - read_data.append(data8) - if config.is_verbose: - print(f"DATA[{i+address}] = {read_data[i]:#0x}") + if read_data.__len__ != byte_count: + return 1 for i in range(byte_count): if read_data[i] != bin_data[i]: @@ -399,7 +394,7 @@ def spifi_erase(openocd, erase_type: EraseType = EraseType.CHIP_ERASE, sectors: spifi_write_enable(openocd) spifi_sector_erase(openocd, sector) spifi_wait_busy(openocd) - spifi_check_erase(openocd, 0, 4096) + spifi_check_erase(openocd, sector, sector + 4096) def spifi_write(openocd: OpenOcdTclRpc, address: int, data: List[int], data_len: int): @@ -428,14 +423,14 @@ def spifi_write_file(bytes: List[int], openocd: OpenOcdTclRpc, is_resume=True): break print("address = ", address) spifi_write(openocd, address, bytes, 256) - if spifi_read_data(openocd, address, 256, bytes) == 1: + if spifi_check_program(openocd, address, 256, bytes) == 1: return 1 if (len(bytes) % 256) != 0: print( f"address = {address}, +{len(bytes) - address-1}[{address + len(bytes) - address-1}]") spifi_write(openocd, address, bytes, len(bytes) - address) - if spifi_read_data(openocd, address, len(bytes) - address, bytes) == 1: + if spifi_check_program(openocd, address, len(bytes) - address, bytes) == 1: return 1 print("end") if is_resume: @@ -513,12 +508,18 @@ def get_segments_list(pages_offsets: List[int], segment_size: int) -> List[int]: return list(segments) -def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc, is_resume=True, use_quad_spi=False): +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) - spifi_erase(openocd, EraseType.SECTOR_ERASE, get_segments_list(list(pages), 4*1024)) + + if use_chip_erase: + erase_type = EraseType.CHIP_ERASE + else: + erase_type = EraseType.SECTOR_ERASE + + spifi_erase(openocd, erase_type, get_segments_list(list(pages), 4*1024)) address = 0 spifi_quad_enable(openocd) @@ -533,7 +534,7 @@ def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc, is_resume=T spifi_page_program(openocd, page_offset, page_bytes, 256) spifi_wait_busy(openocd) - result = spifi_read_data(openocd, page_offset, 256, page_bytes) + result = spifi_check_program(openocd, page_offset, 256, page_bytes) if result == 1: print("Data error") diff --git a/mik32_upload.py b/mik32_upload.py index 7af6789..9fa67d7 100644 --- a/mik32_upload.py +++ b/mik32_upload.py @@ -166,7 +166,8 @@ def upload_file( port: int = OpenOcdTclRpc.DEFAULT_PORT, is_resume=True, run_openocd=False, - use_quad_spi=False + use_quad_spi=False, + use_chip_erase=False, ) -> int: """ Write ihex or binary file into MIK32 EEPROM or external flash memory @@ -222,7 +223,7 @@ def upload_file( if (pages_spifi.__len__() > 0): # print(pages_spifi) result |= mik32_spifi.write_pages( - pages_spifi, openocd, is_resume, use_quad_spi) + pages_spifi, openocd, is_resume, use_quad_spi, use_chip_erase) if (segments_ram.__len__() > 0): mik32_ram.write_segments(segments_ram, openocd, is_resume) result |= 0 @@ -254,6 +255,8 @@ def createParser(): action='store_true', default=False) parser.add_argument('-v', '--verbose', dest='is_verbose', action='store_true', default=False) + parser.add_argument('--use-chip-erase', dest='use_chip_erase', + action='store_true', default=False) # parser.add_argument('-b', '--boot-mode', default='undefined') return parser @@ -275,6 +278,7 @@ if __name__ == '__main__': is_resume=(not namespace.keep_halt), run_openocd=namespace.run_openocd, use_quad_spi=namespace.use_quad_spi, + use_chip_erase=namespace.use_chip_erase, ) else: print("Nothing to upload")