From f576a4f4785fa75059945b89038cd564cf9a721f Mon Sep 17 00:00:00 2001 From: Sergey Shchelkanov Date: Mon, 29 May 2023 16:00:22 +0300 Subject: [PATCH] WIP test platformio support --- .piopm | 1 + mik32_upload.py | 59 ++++++++++++++++++++++++++++++++++++------------- package.json | 15 +++++++++++++ tclrpc.py | 40 +++++++++++++++++++++++---------- 4 files changed, 88 insertions(+), 27 deletions(-) create mode 100644 .piopm create mode 100644 package.json diff --git a/.piopm b/.piopm new file mode 100644 index 0000000..3deea79 --- /dev/null +++ b/.piopm @@ -0,0 +1 @@ +{"type": "tool", "name": "tool-mik32-uploader", "version": "0.1.0", "spec": {"owner": "platformio", "id": 1235, "name": "tool-mik32-uploader", "requirements": null, "uri": null}} \ No newline at end of file diff --git a/mik32_upload.py b/mik32_upload.py index c2baf7a..7b5fc93 100644 --- a/mik32_upload.py +++ b/mik32_upload.py @@ -61,7 +61,7 @@ mik32v0_sections: List[MemorySection] = [ @dataclass class Segment: offset: int - memory: MemorySection | None + memory: MemorySection or None data: List[int] @@ -74,7 +74,7 @@ def belongs_memory_section(memory_section: MemorySection, offset: int) -> bool: return True -def find_memory_section(offset: int) -> MemorySection | None: +def find_memory_section(offset: int) -> MemorySection or None: for section in mik32v0_sections: if belongs_memory_section(section, offset): return section @@ -126,7 +126,7 @@ def read_file(filename: str) -> List[Segment]: def segment_to_pages(segment: Segment, page_size: int, pages: Dict[int, List[int]]): if segment.memory is None: return - + internal_offset = segment.offset - segment.memory.offset for i, byte in enumerate(segment.data): @@ -136,7 +136,7 @@ def segment_to_pages(segment: Segment, page_size: int, pages: Dict[int, List[int if (page_offset) not in pages.keys(): pages[page_offset] = [0] * page_size - + pages[page_offset][byte_offset - page_offset] = byte @@ -145,11 +145,21 @@ def segments_to_pages(segments: List[Segment], page_size: int) -> Dict[int, List for segment in segments: segment_to_pages(segment, page_size, pages) - + return pages -def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRpc.DEFAULT_PORT, is_resume=True, run_openocd=False, use_quad_spi=False) -> int: +def upload_file( + filename: str, + openocd_path: str, + scripts_path: str, + adapter_speed: str, + host: str = '127.0.0.1', + port: int = OpenOcdTclRpc.DEFAULT_PORT, + is_resume=True, + run_openocd=False, + use_quad_spi=False +) -> int: """ Write ihex or binary file into MIK32 EEPROM or external flash memory @@ -165,6 +175,7 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp result = 0 + print(filename) if not os.path.exists(filename): print("ERROR: File %s does not exist" % filename) exit(1) @@ -181,12 +192,13 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp raise Exception("ERROR: segment with offset %s and length %s overflows section %s" % ( hex(segment.offset), segment.data.__len__(), segment.memory.type.name)) - proc: subprocess.Popen | None = None + proc: subprocess.Popen or None = None if run_openocd: - cmd = shlex.split("%s -s %s -f interface/ftdi/m-link.cfg -f target/mcu32.cfg" % ( - DEFAULT_OPENOCD_EXEC_FILE_PATH, DEFAULT_OPENOCD_SCRIPTS_PATH), posix=False) + cmd = shlex.split("%s -s %s -f interface/ftdi/m-link.cfg -f target/mik32.cfg" % ( + openocd_path, scripts_path), posix=False) + print(cmd) proc = subprocess.Popen( - cmd, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.SW_HIDE) + cmd) with OpenOcdTclRpc() as openocd: pages_eeprom = segments_to_pages(list(filter( @@ -195,12 +207,14 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.SPIFI), segments)), 256) segments_ram = list(filter( lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.RAM), segments)) - + if (pages_eeprom.__len__() > 0): - result |= mik32_eeprom.write_pages(pages_eeprom, openocd, is_resume) + result |= mik32_eeprom.write_pages( + pages_eeprom, openocd, is_resume) if (pages_spifi.__len__() > 0): # print(pages_spifi) - result |= mik32_spifi.write_pages(pages_spifi, openocd, is_resume, use_quad_spi) + result |= mik32_spifi.write_pages( + pages_spifi, openocd, is_resume, use_quad_spi) if (segments_ram.__len__() > 0): mik32_ram.write_segments(segments_ram, openocd, is_resume) result |= 0 @@ -214,6 +228,12 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp def createParser(): parser = argparse.ArgumentParser() parser.add_argument('filepath', nargs='?') + parser.add_argument('--openocd-path', dest='openocd_path', + default=DEFAULT_OPENOCD_EXEC_FILE_PATH) + parser.add_argument('--scripts-path', dest='scripts_path', + default=DEFAULT_OPENOCD_SCRIPTS_PATH) + parser.add_argument('--adapter-speed', dest='adapter_speed', + default=500) parser.add_argument('--run-openocd', dest='run_openocd', action='store_true', default=False) parser.add_argument('--use-quad-spi', dest='use_quad_spi', @@ -234,7 +254,16 @@ if __name__ == '__main__': namespace = parser.parse_args() if namespace.filepath: - upload_file(namespace.filepath, namespace.openocd_host, - namespace.openocd_port, is_resume=(not namespace.keep_halt), run_openocd=namespace.run_openocd, use_quad_spi=namespace.use_quad_spi) + upload_file( + namespace.filepath, + namespace.openocd_path, + namespace.scripts_path, + namespace.adapter_speed, + host=namespace.openocd_host, + port=namespace.openocd_port, + is_resume=(not namespace.keep_halt), + run_openocd=namespace.run_openocd, + use_quad_spi=namespace.use_quad_spi, + ) else: print("Nothing to upload") diff --git a/package.json b/package.json new file mode 100644 index 0000000..1760bd4 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "tool-mik32-uploader", + "version": "0.1.0", + "description": "mik32-uploader", + "keywords": [ + "tools", + "uploader", + "mik32" + ], + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/MikronMIK32/mik32-uploader" + } +} diff --git a/tclrpc.py b/tclrpc.py index 6b58d82..095e8fd 100644 --- a/tclrpc.py +++ b/tclrpc.py @@ -128,8 +128,20 @@ class OpenOcdTclRpc: return self.run(f"capture \"write_memory {address:#0x} {width} {{{data_string}}}\"") def write_word(self, address:int, word:int): - return self.write_memory(address, 32, [word]) + return self.run(f"capture \"mww {address:#0x} {word}\"") + # def read_memory(self, address:int, width:int, count:int): + # """This function provides an efficient way to read the target memory from a Tcl script. + # A Tcl list containing the requested memory elements is returned by this function. + + # address ... target memory address + + # width ... memory access bit size, can be 8, 16, 32 or 64 + + # count ... number of elements to read """ + # data = self.run(f"capture \"read_memory {address:#0x} {width} {count}\"").split(" ") + # return list(map(lambda word: int(word, base=16), data)) + def read_memory(self, address:int, width:int, count:int): """This function provides an efficient way to read the target memory from a Tcl script. A Tcl list containing the requested memory elements is returned by this function. @@ -139,19 +151,23 @@ class OpenOcdTclRpc: width ... memory access bit size, can be 8, 16, 32 or 64 count ... number of elements to read """ - data = self.run(f"capture \"read_memory {address:#0x} {width} {count}\"").split(" ") + data = self.run(f"capture \"mem2array {width} {address:#0x} {count}\"").split(" ") return list(map(lambda word: int(word, base=16), data)) + # def read_word(self, address:int): + # """This function provides an efficient way to read the target memory from a Tcl script. + # A Tcl list containing the requested memory elements is returned by this function. + + # address ... target memory address + + # width ... memory access bit size, can be 8, 16, 32 or 64 + + # count ... number of elements to read """ + # data = self.run(f"capture \"read_memory {address:#0x} 32 1\"").split(" ") + # return int(data[0], base=16) + def read_word(self, address:int): - """This function provides an efficient way to read the target memory from a Tcl script. - A Tcl list containing the requested memory elements is returned by this function. - - address ... target memory address - - width ... memory access bit size, can be 8, 16, 32 or 64 - - count ... number of elements to read """ - data = self.run(f"capture \"read_memory {address:#0x} 32 1\"").split(" ") - return int(data[0], base=16) + data = self.run(f"capture \"mdw {address:#0x}\"").split(" ") + return int(data[1], base=16)