From 999509141f017e697232e62ec51703d89bacba1a Mon Sep 17 00:00:00 2001 From: Sergey Shchelkanov Date: Wed, 17 May 2023 12:18:38 +0300 Subject: [PATCH] reverted old structure, reimpl openocd launching --- connectopenocd-m-link.bat | 3 + drivers/mik32_eeprom.py => mik32_eeprom.py | 97 +++++++++++----------- drivers/mik32_ram.py => mik32_ram.py | 4 +- drivers/mik32_spifi.py => mik32_spifi.py | 51 ++++++------ mik32_upload.py | 64 ++++++-------- drivers/tclrpc.py => tclrpc.py | 0 6 files changed, 105 insertions(+), 114 deletions(-) create mode 100644 connectopenocd-m-link.bat rename drivers/mik32_eeprom.py => mik32_eeprom.py (77%) rename drivers/mik32_ram.py => mik32_ram.py (81%) rename drivers/mik32_spifi.py => mik32_spifi.py (94%) rename drivers/tclrpc.py => tclrpc.py (100%) diff --git a/connectopenocd-m-link.bat b/connectopenocd-m-link.bat new file mode 100644 index 0000000..ed1e51d --- /dev/null +++ b/connectopenocd-m-link.bat @@ -0,0 +1,3 @@ +@echo off +openocd\bin\openocd.exe -s openocd/share/openocd/scripts -f interface/ftdi/m-link.cfg -f target/mcu32.cfg +timeout /t 300 diff --git a/drivers/mik32_eeprom.py b/mik32_eeprom.py similarity index 77% rename from drivers/mik32_eeprom.py rename to mik32_eeprom.py index 6b17780..95d4818 100644 --- a/drivers/mik32_eeprom.py +++ b/mik32_eeprom.py @@ -1,7 +1,7 @@ from typing import List import time -from .tclrpc import TclException -from .tclrpc import OpenOcdTclRpc +from tclrpc import TclException +from tclrpc import OpenOcdTclRpc # -------------------------- # PM register offset @@ -193,7 +193,7 @@ def eeprom_check_data_ahb_lite(openocd: OpenOcdTclRpc, words: List[int]) -> int: return 0 -def write_words(words: List[int], 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 @@ -207,52 +207,53 @@ def write_words(words: List[int], write_by_word = False, read_through_apb = Fals @return: return 0 if successful, 1 if failed """ print(f"Write {len(words*4)} bytes") - with OpenOcdTclRpc() as openocd: - openocd.halt() - eeprom_sysinit(openocd) - eeprom_global_erase(openocd) - # eeprom_global_erase_check(openocd) - openocd.write_word(EEPROM_REGS_NCYCRL, 1< progress: + print("#"*(curr_progress - progress), end="", flush=True) + progress = curr_progress + else: + page = [] + page_num = 0 + page_size = 32 + while word_num < len(words): + if word_num < page_size*(page_num+1): + page.append(words[word_num]) word_num += 1 - curr_progress = int((word_num * 50) / len(words)) - if curr_progress > progress: - print("#"*(curr_progress - progress), end="", flush=True) - progress = curr_progress - else: - page = [] - page_num = 0 - page_size = 32 - while word_num < len(words): - if word_num < page_size*(page_num+1): - page.append(words[word_num]) - word_num += 1 - else: - # print(list(map(lambda word: f"{word:#0x}", page))) - eeprom_write_page(openocd, page_num*page_size*4, page) - page_num += 1 - page.clear() - curr_progress = int((word_num * 50) / len(words)) - if curr_progress > progress: - print("#"*(curr_progress - progress), end="", flush=True) - progress = curr_progress - eeprom_write_page(openocd, page_num*page_size*4, page) - print("]") - if read_through_apb: - result = eeprom_check_data_apb(openocd, words) - else: - result = eeprom_check_data_ahb_lite(openocd, words) - if is_resume: - openocd.resume(0) + else: + # print(list(map(lambda word: f"{word:#0x}", page))) + eeprom_write_page(openocd, page_num*page_size*4, page) + page_num += 1 + page.clear() + curr_progress = int((word_num * 50) / len(words)) + if curr_progress > progress: + print("#"*(curr_progress - progress), end="", flush=True) + progress = curr_progress + eeprom_write_page(openocd, page_num*page_size*4, page) + print("]") + if read_through_apb: + result = eeprom_check_data_apb(openocd, words) + else: + result = eeprom_check_data_ahb_lite(openocd, words) + if is_resume: + openocd.resume(0) + if result == 0: print("EEPROM write file done!") return result diff --git a/drivers/mik32_ram.py b/mik32_ram.py similarity index 81% rename from drivers/mik32_ram.py rename to mik32_ram.py index e2c7299..4237470 100644 --- a/drivers/mik32_ram.py +++ b/mik32_ram.py @@ -1,5 +1,5 @@ -from .tclrpc import TclException -from .tclrpc import OpenOcdTclRpc +from tclrpc import TclException +from tclrpc import OpenOcdTclRpc from pathlib import Path def write_file(filename, is_resume=True): diff --git a/drivers/mik32_spifi.py b/mik32_spifi.py similarity index 94% rename from drivers/mik32_spifi.py rename to mik32_spifi.py index 214b675..6d189fa 100644 --- a/drivers/mik32_spifi.py +++ b/mik32_spifi.py @@ -1,7 +1,7 @@ from typing import List import time -from .tclrpc import TclException -from .tclrpc import OpenOcdTclRpc +from tclrpc import TclException +from tclrpc import OpenOcdTclRpc # -------------------------- # PM register offset @@ -424,7 +424,7 @@ def spifi_write(openocd: OpenOcdTclRpc, address: int, data: List[int], data_len: print("written") -def spifi_write_file(bytes: List[int], is_resume=True): +def spifi_write_file(bytes: List[int], openocd: OpenOcdTclRpc, is_resume=True): """ Write bytes in MIK32 External SPIFI Flash memory @@ -435,28 +435,29 @@ def spifi_write_file(bytes: List[int], is_resume=True): """ # print(bytes) print(f"Write {len(bytes)} bytes") - with OpenOcdTclRpc() as openocd: - openocd.halt() - spifi_init(openocd) - spifi_erase(openocd) - print("bin_data_len = ", len(bytes)) - address = 0 + + openocd.halt() + spifi_init(openocd) + spifi_erase(openocd) + print("bin_data_len = ", len(bytes)) + address = 0 - for address in range(0, len(bytes), 256): - if ((address + 256) > len(bytes)): - break - print("address = ", address) - spifi_write(openocd, address, bytes, 256) - if spifi_read_data(openocd, address, 256, bytes) == 1: - return 1 + for address in range(0, len(bytes), 256): + if ((address + 256) > len(bytes)): + break + print("address = ", address) + spifi_write(openocd, address, bytes, 256) + if spifi_read_data(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: - return 1 - print("end") - if is_resume: - openocd.resume(0) + 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: + return 1 + print("end") + if is_resume: + openocd.resume(0) + return 0 diff --git a/mik32_upload.py b/mik32_upload.py index 2e33b71..9c88ce3 100644 --- a/mik32_upload.py +++ b/mik32_upload.py @@ -1,14 +1,13 @@ import shlex import argparse -import sys import subprocess import os from enum import Enum from typing import List, NamedTuple -from drivers.tclrpc import OpenOcdTclRpc -from drivers.mik32_eeprom import * -from drivers.mik32_spifi import * -from drivers.mik32_ram import * +from tclrpc import OpenOcdTclRpc +import mik32_eeprom +import mik32_spifi +import mik32_ram from mik32_parsers import * @@ -113,7 +112,7 @@ def read_file(filename: str) -> List[Segment]: return segments -def upload_file(filename: str, is_resume=True) -> int: +def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRpc.DEFAULT_PORT, is_resume=True, run_openocd=False) -> int: """ Write ihex or binary file into MIK32 EEPROM or external flash memory @@ -148,40 +147,23 @@ def upload_file(filename: str, is_resume=True) -> int: if (segment.offset + segment.data.__len__()) > (segment_section.offset + segment_section.length): raise Exception("ERROR: segment with offset %s and length %s overflows section %s" % ( hex(segment.offset), segment.data.__len__(), segment_section.type.name)) - - if segment_section.type == MemoryType.EEPROM: - result = write_words(bytes2words(segment.data), is_resume) - elif segment_section.type == MemoryType.SPIFI: - result = spifi_write_file(segment.data, is_resume) - # elif segment_section.type == MemoryType.RAM: - # write_file(filename, is_resume) - # result = write_words(bytes2words(segment.data), is_resume) - # 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) - # with subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL) as proc: - # if boot_source == "eeprom": - # result = write_words(bytes2words(get_content(filename))) - # elif boot_source == "spifi": - # spifi_write_file(get_content(filename)) - # result = 0 # TODO - # elif boot_source == "ram": - # write_file(filename) - # result = 0 # TODO - # else: - # raise Exception("Unsupported boot source, use eeprom or spifi") - # result = 1 - # proc.kill() + proc: subprocess.Popen | 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) + proc = subprocess.Popen( + cmd, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.SW_HIDE) - # if boot_source == "eeprom": - # result = write_words(bytes2words(get_content(filename)), is_resume) - # elif boot_source == "spifi": - # result = spifi_write_file(get_content(filename), is_resume) - # elif boot_source == "ram": - # write_file(filename, is_resume) - # result = 0 # TODO - # else: - # raise Exception("Unsupported boot source, use eeprom or spifi") - # result = 1 + with OpenOcdTclRpc() as openocd: + if segment_section.type == MemoryType.EEPROM: + result = mik32_eeprom.write_words(bytes2words( + segment.data), openocd, is_resume) + elif segment_section.type == MemoryType.SPIFI: + result = mik32_spifi.spifi_write_file(segment.data, openocd, is_resume) + + if run_openocd and proc is not None: + proc.kill() return result @@ -189,6 +171,10 @@ def upload_file(filename: str, is_resume=True) -> int: def createParser(): parser = argparse.ArgumentParser() parser.add_argument('filepath', nargs='?') + parser.add_argument('--run-openocd', dest='run_openocd', + action='store_true', default=False) + parser.add_argument('--openocd-host', dest='openocd_host', default='127.0.0.1') + parser.add_argument('--openocd-port', dest='openocd_port', default=OpenOcdTclRpc.DEFAULT_PORT) # parser.add_argument('-b', '--boot-mode', default='undefined') return parser @@ -199,6 +185,6 @@ if __name__ == '__main__': namespace = parser.parse_args() if namespace.filepath: - upload_file(namespace.filepath) + upload_file(namespace.filepath, namespace.openocd_host, namespace.openocd_port, run_openocd=namespace.run_openocd) else: print("Nothing to upload") diff --git a/drivers/tclrpc.py b/tclrpc.py similarity index 100% rename from drivers/tclrpc.py rename to tclrpc.py