unify read data functions

This commit is contained in:
Sergey Shchelkanov 2023-05-30 15:01:39 +03:00
parent 23da63fa3c
commit 42fb564db9
2 changed files with 32 additions and 27 deletions

View File

@ -286,7 +286,7 @@ def spifi_wait_busy(openocd: OpenOcdTclRpc):
def spifi_chip_erase(openocd: OpenOcdTclRpc): def spifi_chip_erase(openocd: OpenOcdTclRpc):
print("Chip erase...") print("Chip erase...")
# spifi_intrq_clear(openocd) spifi_intrq_clear(openocd)
openocd.write_word(SPIFI_CONFIG_STAT, openocd.read_word( openocd.write_word(SPIFI_CONFIG_STAT, openocd.read_word(
SPIFI_CONFIG_STAT) | SPIFI_CONFIG_STAT_INTRQ_M) SPIFI_CONFIG_STAT) | SPIFI_CONFIG_STAT_INTRQ_M)
openocd.write_word(SPIFI_CONFIG_CMD, (SECTOR_ERASE_COMMAND << SPIFI_CONFIG_CMD_OPCODE_S) | 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): def spifi_sector_erase(openocd: OpenOcdTclRpc, address: int):
print("Erase sector %s..." % hex(address)) print("Erase sector %s..." % hex(address))
openocd.write_word(SPIFI_CONFIG_ADDR, 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( openocd.write_word(SPIFI_CONFIG_STAT, openocd.read_word(
SPIFI_CONFIG_STAT) | SPIFI_CONFIG_STAT_INTRQ_M) SPIFI_CONFIG_STAT) | SPIFI_CONFIG_STAT_INTRQ_M)
openocd.write_word(SPIFI_CONFIG_CMD, (CHIP_ERASE_COMMAND << SPIFI_CONFIG_CMD_OPCODE_S) | 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") spifi_wait_intrq_timeout(openocd, "Timeout executing chip erase command")
def spifi_check_erase(openocd: OpenOcdTclRpc, address: int, byte_count: int): def spifi_read_data(openocd: OpenOcdTclRpc, address: int, byte_count: int) -> List[int]:
print(f"Checking erase from {address:#0x} to {(byte_count-1):#0x}")
read_data: List[int] = [] read_data: List[int] = []
openocd.write_word(SPIFI_CONFIG_ADDR, address) 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) read_data.append(data8)
if config.is_verbose: if config.is_verbose:
print(f"DATA[{i+address}] = {read_data[i]:#0x}") 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): for i in range(byte_count):
if read_data[i] != 0: if read_data[i] != 0:
@ -333,21 +338,11 @@ def spifi_check_erase(openocd: OpenOcdTclRpc, address: int, byte_count: int):
return 0 return 0
def spifi_read_data(openocd: OpenOcdTclRpc, address: int, byte_count: int, bin_data: List[int]) -> int: def spifi_check_program(openocd: OpenOcdTclRpc, address: int, byte_count: int, bin_data: List[int]) -> int:
read_data: List[int] = [] read_data: List[int] = spifi_read_data(openocd, address, byte_count)
openocd.write_word(SPIFI_CONFIG_ADDR, address)
spifi_intrq_clear(openocd) if read_data.__len__ != byte_count:
openocd.write_word(SPIFI_CONFIG_CMD, (READ_DATA_COMMAND << SPIFI_CONFIG_CMD_OPCODE_S) | return 1
(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}")
for i in range(byte_count): for i in range(byte_count):
if read_data[i] != bin_data[i]: 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_write_enable(openocd)
spifi_sector_erase(openocd, sector) spifi_sector_erase(openocd, sector)
spifi_wait_busy(openocd) 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): 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 break
print("address = ", address) print("address = ", address)
spifi_write(openocd, address, bytes, 256) 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 return 1
if (len(bytes) % 256) != 0: if (len(bytes) % 256) != 0:
print( print(
f"address = {address}, +{len(bytes) - address-1}[{address + len(bytes) - address-1}]") f"address = {address}, +{len(bytes) - address-1}[{address + len(bytes) - address-1}]")
spifi_write(openocd, address, bytes, len(bytes) - address) 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 return 1
print("end") print("end")
if is_resume: if is_resume:
@ -513,12 +508,18 @@ def get_segments_list(pages_offsets: List[int], segment_size: int) -> List[int]:
return list(segments) 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 result = 0
openocd.halt() openocd.halt()
spifi_init(openocd) 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 address = 0
spifi_quad_enable(openocd) 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_page_program(openocd, page_offset, page_bytes, 256)
spifi_wait_busy(openocd) 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: if result == 1:
print("Data error") print("Data error")

View File

@ -166,7 +166,8 @@ def upload_file(
port: int = OpenOcdTclRpc.DEFAULT_PORT, port: int = OpenOcdTclRpc.DEFAULT_PORT,
is_resume=True, is_resume=True,
run_openocd=False, run_openocd=False,
use_quad_spi=False use_quad_spi=False,
use_chip_erase=False,
) -> int: ) -> int:
""" """
Write ihex or binary file into MIK32 EEPROM or external flash memory Write ihex or binary file into MIK32 EEPROM or external flash memory
@ -222,7 +223,7 @@ def upload_file(
if (pages_spifi.__len__() > 0): if (pages_spifi.__len__() > 0):
# print(pages_spifi) # print(pages_spifi)
result |= mik32_spifi.write_pages( 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): if (segments_ram.__len__() > 0):
mik32_ram.write_segments(segments_ram, openocd, is_resume) mik32_ram.write_segments(segments_ram, openocd, is_resume)
result |= 0 result |= 0
@ -254,6 +255,8 @@ def createParser():
action='store_true', default=False) action='store_true', default=False)
parser.add_argument('-v', '--verbose', dest='is_verbose', parser.add_argument('-v', '--verbose', dest='is_verbose',
action='store_true', default=False) 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') # parser.add_argument('-b', '--boot-mode', default='undefined')
return parser return parser
@ -275,6 +278,7 @@ if __name__ == '__main__':
is_resume=(not namespace.keep_halt), is_resume=(not namespace.keep_halt),
run_openocd=namespace.run_openocd, run_openocd=namespace.run_openocd,
use_quad_spi=namespace.use_quad_spi, use_quad_spi=namespace.use_quad_spi,
use_chip_erase=namespace.use_chip_erase,
) )
else: else:
print("Nothing to upload") print("Nothing to upload")