mirror of
https://github.com/MikronMIK32/mik32-uploader.git
synced 2026-01-01 13:37:03 +03:00
refactor upload
This commit is contained in:
parent
41ccfeb7d7
commit
8c3f203dcc
@ -70,10 +70,24 @@ class Segment:
|
|||||||
self.offset = offset
|
self.offset = offset
|
||||||
self.data = data
|
self.data = data
|
||||||
|
|
||||||
|
self._locate_memory_section()
|
||||||
|
|
||||||
|
def _locate_memory_section(self):
|
||||||
for section in mik32v0_sections:
|
for section in mik32v0_sections:
|
||||||
if self._belongs_memory_section(section, offset):
|
if self._belongs_memory_section(section, self.offset):
|
||||||
self.memory = section
|
self.memory = section
|
||||||
|
|
||||||
|
if self.memory is None:
|
||||||
|
raise Exception(
|
||||||
|
f"ERROR: segment with offset {self.offset:#0x} doesn't belong to any section")
|
||||||
|
|
||||||
|
if (self.offset + self.data.__len__()) > (self.memory.offset + self.memory.length):
|
||||||
|
raise Exception(
|
||||||
|
f"ERROR: segment with offset {self.offset:#0x} "
|
||||||
|
f"and length {self.data.__len__()} "
|
||||||
|
f"overflows section {self.memory.type.name}"
|
||||||
|
)
|
||||||
|
|
||||||
def _belongs_memory_section(self, memory_section: MemorySection, offset: int) -> bool:
|
def _belongs_memory_section(self, memory_section: MemorySection, offset: int) -> bool:
|
||||||
if offset < memory_section.offset:
|
if offset < memory_section.offset:
|
||||||
return False
|
return False
|
||||||
@ -160,18 +174,41 @@ def segments_to_pages(segments: List[Segment], page_size: int) -> Dict[int, List
|
|||||||
return pages
|
return pages
|
||||||
|
|
||||||
|
|
||||||
|
def run_openocd(
|
||||||
|
openocd_exec=openocd_exec_path,
|
||||||
|
openocd_scripts=openocd_scripts_path,
|
||||||
|
openocd_interface=openocd_interface_path,
|
||||||
|
openocd_target=openocd_target_path,
|
||||||
|
is_open_console=False
|
||||||
|
) -> subprocess.Popen:
|
||||||
|
cmd = shlex.split(
|
||||||
|
f"{openocd_exec} -s {openocd_scripts} "
|
||||||
|
f"-f {openocd_interface} -f {openocd_target}", posix=False
|
||||||
|
)
|
||||||
|
|
||||||
|
creation_flags = subprocess.SW_HIDE
|
||||||
|
if is_open_console:
|
||||||
|
creation_flags |= subprocess.CREATE_NEW_CONSOLE
|
||||||
|
|
||||||
|
proc = subprocess.Popen(
|
||||||
|
cmd, creationflags=creation_flags)
|
||||||
|
|
||||||
|
return proc
|
||||||
|
|
||||||
|
|
||||||
def upload_file(
|
def upload_file(
|
||||||
filename: str,
|
filename: str,
|
||||||
host: str = '127.0.0.1',
|
host: str = '127.0.0.1',
|
||||||
port: int = OpenOcdTclRpc.DEFAULT_PORT,
|
port: int = OpenOcdTclRpc.DEFAULT_PORT,
|
||||||
is_resume=True,
|
is_resume=True,
|
||||||
run_openocd=False,
|
is_run_openocd=False,
|
||||||
use_quad_spi=False,
|
use_quad_spi=False,
|
||||||
openocd_exec=openocd_exec_path,
|
openocd_exec=openocd_exec_path,
|
||||||
openocd_scripts=openocd_scripts_path,
|
openocd_scripts=openocd_scripts_path,
|
||||||
openocd_interface=openocd_interface_path,
|
openocd_interface=openocd_interface_path,
|
||||||
openocd_target=openocd_target_path,
|
openocd_target=openocd_target_path,
|
||||||
adapter_speed=adapter_default_speed,
|
adapter_speed=adapter_default_speed,
|
||||||
|
is_open_console=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
|
||||||
@ -190,28 +227,10 @@ def upload_file(
|
|||||||
segments: List[Segment] = file.get_segments()
|
segments: List[Segment] = file.get_segments()
|
||||||
# print(segments)
|
# print(segments)
|
||||||
|
|
||||||
for segment in segments:
|
|
||||||
if segment.memory is None:
|
|
||||||
raise Exception(
|
|
||||||
f"ERROR: segment with offset {segment.offset:#0x} doesn't belong to any section")
|
|
||||||
|
|
||||||
if (segment.offset + segment.data.__len__()) > (segment.memory.offset + segment.memory.length):
|
|
||||||
raise Exception(
|
|
||||||
f"ERROR: segment with offset {segment.offset:#0x} "
|
|
||||||
f"and length {segment.data.__len__()} "
|
|
||||||
f"overflows section {segment.memory.type.name}"
|
|
||||||
)
|
|
||||||
|
|
||||||
proc: Union[subprocess.Popen, None] = None
|
proc: Union[subprocess.Popen, None] = None
|
||||||
if run_openocd:
|
if is_run_openocd:
|
||||||
cmd = shlex.split(
|
proc = run_openocd(openocd_exec, openocd_scripts,
|
||||||
f"{openocd_exec} -s {openocd_scripts} "
|
openocd_interface, openocd_target, is_open_console)
|
||||||
f"-f {openocd_interface} -f {openocd_target}", posix=False
|
|
||||||
)
|
|
||||||
# proc = subprocess.Popen(
|
|
||||||
# cmd, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.SW_HIDE)
|
|
||||||
proc = subprocess.Popen(
|
|
||||||
cmd, creationflags= subprocess.SW_HIDE)
|
|
||||||
|
|
||||||
with OpenOcdTclRpc(host, port) as openocd:
|
with OpenOcdTclRpc(host, port) as openocd:
|
||||||
openocd.run(f"adapter speed {adapter_speed}")
|
openocd.run(f"adapter speed {adapter_speed}")
|
||||||
@ -219,11 +238,11 @@ def upload_file(
|
|||||||
openocd.run(f"debug_level 1")
|
openocd.run(f"debug_level 1")
|
||||||
|
|
||||||
pages_eeprom = segments_to_pages(list(filter(
|
pages_eeprom = segments_to_pages(list(filter(
|
||||||
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.EEPROM), segments)), 128)
|
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.EEPROM), segments)
|
||||||
|
), 128)
|
||||||
pages_spifi = segments_to_pages(list(filter(
|
pages_spifi = segments_to_pages(list(filter(
|
||||||
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.SPIFI), segments)), 256)
|
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.SPIFI), segments)
|
||||||
segments_ram = list(filter(
|
), 256)
|
||||||
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.RAM), segments))
|
|
||||||
|
|
||||||
if (pages_eeprom.__len__() > 0):
|
if (pages_eeprom.__len__() > 0):
|
||||||
result |= mik32_eeprom.write_pages(
|
result |= mik32_eeprom.write_pages(
|
||||||
@ -231,11 +250,14 @@ def upload_file(
|
|||||||
if (pages_spifi.__len__() > 0):
|
if (pages_spifi.__len__() > 0):
|
||||||
result |= mik32_spifi.write_pages(
|
result |= mik32_spifi.write_pages(
|
||||||
pages_spifi, openocd, is_resume=is_resume, use_quad_spi=use_quad_spi)
|
pages_spifi, openocd, is_resume=is_resume, use_quad_spi=use_quad_spi)
|
||||||
|
|
||||||
|
segments_ram = list(filter(
|
||||||
|
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.RAM), segments))
|
||||||
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
|
||||||
|
|
||||||
if run_openocd and proc is not None:
|
if proc is not None:
|
||||||
proc.kill()
|
proc.kill()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@ -264,6 +286,8 @@ def createParser():
|
|||||||
'--openocd-interface', dest='openocd_interface', default=openocd_interface_path)
|
'--openocd-interface', dest='openocd_interface', default=openocd_interface_path)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--openocd-target', dest='openocd_target', default=openocd_target_path)
|
'--openocd-target', dest='openocd_target', default=openocd_target_path)
|
||||||
|
parser.add_argument('--open-console', dest='open_console',
|
||||||
|
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
|
||||||
@ -279,13 +303,14 @@ if __name__ == '__main__':
|
|||||||
host=namespace.openocd_host,
|
host=namespace.openocd_host,
|
||||||
port=namespace.openocd_port,
|
port=namespace.openocd_port,
|
||||||
is_resume=(not namespace.keep_halt),
|
is_resume=(not namespace.keep_halt),
|
||||||
run_openocd=namespace.run_openocd,
|
is_run_openocd=namespace.run_openocd,
|
||||||
use_quad_spi=namespace.use_quad_spi,
|
use_quad_spi=namespace.use_quad_spi,
|
||||||
openocd_exec=namespace.openocd_exec,
|
openocd_exec=namespace.openocd_exec,
|
||||||
openocd_scripts=namespace.openocd_scripts,
|
openocd_scripts=namespace.openocd_scripts,
|
||||||
openocd_interface=namespace.openocd_interface,
|
openocd_interface=namespace.openocd_interface,
|
||||||
openocd_target=namespace.openocd_target,
|
openocd_target=namespace.openocd_target,
|
||||||
adapter_speed=namespace.adapter_speed,
|
adapter_speed=namespace.adapter_speed,
|
||||||
|
is_open_console=namespace.open_console,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print("Nothing to upload")
|
print("Nothing to upload")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user