reverted old structure, reimpl openocd launching

This commit is contained in:
Sergey Shchelkanov 2023-05-17 12:18:38 +03:00
parent a244b042d6
commit 999509141f
6 changed files with 105 additions and 114 deletions

View File

@ -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

View File

@ -1,7 +1,7 @@
from typing import List from typing import List
import time import time
from .tclrpc import TclException from tclrpc import TclException
from .tclrpc import OpenOcdTclRpc from tclrpc import OpenOcdTclRpc
# -------------------------- # --------------------------
# PM register offset # PM register offset
@ -193,7 +193,7 @@ def eeprom_check_data_ahb_lite(openocd: OpenOcdTclRpc, words: List[int]) -> int:
return 0 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 Write words in MIK32 EEPROM through APB bus
@ -207,7 +207,7 @@ def write_words(words: List[int], write_by_word = False, read_through_apb = Fals
@return: return 0 if successful, 1 if failed @return: return 0 if successful, 1 if failed
""" """
print(f"Write {len(words*4)} bytes") print(f"Write {len(words*4)} bytes")
with OpenOcdTclRpc() as openocd:
openocd.halt() openocd.halt()
eeprom_sysinit(openocd) eeprom_sysinit(openocd)
eeprom_global_erase(openocd) eeprom_global_erase(openocd)
@ -253,6 +253,7 @@ def write_words(words: List[int], write_by_word = False, read_through_apb = Fals
result = eeprom_check_data_ahb_lite(openocd, words) result = eeprom_check_data_ahb_lite(openocd, words)
if is_resume: if is_resume:
openocd.resume(0) openocd.resume(0)
if result == 0: if result == 0:
print("EEPROM write file done!") print("EEPROM write file done!")
return result return result

View File

@ -1,5 +1,5 @@
from .tclrpc import TclException from tclrpc import TclException
from .tclrpc import OpenOcdTclRpc from tclrpc import OpenOcdTclRpc
from pathlib import Path from pathlib import Path
def write_file(filename, is_resume=True): def write_file(filename, is_resume=True):

View File

@ -1,7 +1,7 @@
from typing import List from typing import List
import time import time
from .tclrpc import TclException from tclrpc import TclException
from .tclrpc import OpenOcdTclRpc from tclrpc import OpenOcdTclRpc
# -------------------------- # --------------------------
# PM register offset # PM register offset
@ -424,7 +424,7 @@ def spifi_write(openocd: OpenOcdTclRpc, address: int, data: List[int], data_len:
print("written") 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 Write bytes in MIK32 External SPIFI Flash memory
@ -435,7 +435,7 @@ def spifi_write_file(bytes: List[int], is_resume=True):
""" """
# print(bytes) # print(bytes)
print(f"Write {len(bytes)} bytes") print(f"Write {len(bytes)} bytes")
with OpenOcdTclRpc() as openocd:
openocd.halt() openocd.halt()
spifi_init(openocd) spifi_init(openocd)
spifi_erase(openocd) spifi_erase(openocd)
@ -459,4 +459,5 @@ def spifi_write_file(bytes: List[int], is_resume=True):
print("end") print("end")
if is_resume: if is_resume:
openocd.resume(0) openocd.resume(0)
return 0 return 0

View File

@ -1,14 +1,13 @@
import shlex import shlex
import argparse import argparse
import sys
import subprocess import subprocess
import os import os
from enum import Enum from enum import Enum
from typing import List, NamedTuple from typing import List, NamedTuple
from drivers.tclrpc import OpenOcdTclRpc from tclrpc import OpenOcdTclRpc
from drivers.mik32_eeprom import * import mik32_eeprom
from drivers.mik32_spifi import * import mik32_spifi
from drivers.mik32_ram import * import mik32_ram
from mik32_parsers import * from mik32_parsers import *
@ -113,7 +112,7 @@ def read_file(filename: str) -> List[Segment]:
return segments 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 Write ihex or binary file into MIK32 EEPROM or external flash memory
@ -149,39 +148,22 @@ def upload_file(filename: str, is_resume=True) -> int:
raise Exception("ERROR: segment with offset %s and length %s overflows section %s" % ( raise Exception("ERROR: segment with offset %s and length %s overflows section %s" % (
hex(segment.offset), segment.data.__len__(), segment_section.type.name)) hex(segment.offset), segment.data.__len__(), segment_section.type.name))
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)
with OpenOcdTclRpc() as openocd:
if segment_section.type == MemoryType.EEPROM: if segment_section.type == MemoryType.EEPROM:
result = write_words(bytes2words(segment.data), is_resume) result = mik32_eeprom.write_words(bytes2words(
segment.data), openocd, is_resume)
elif segment_section.type == MemoryType.SPIFI: elif segment_section.type == MemoryType.SPIFI:
result = spifi_write_file(segment.data, is_resume) result = mik32_spifi.spifi_write_file(segment.data, openocd, 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) if run_openocd and proc is not None:
# with subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL) as proc: proc.kill()
# 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()
# 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
return result return result
@ -189,6 +171,10 @@ def upload_file(filename: str, is_resume=True) -> int:
def createParser(): def createParser():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('filepath', nargs='?') 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') # parser.add_argument('-b', '--boot-mode', default='undefined')
return parser return parser
@ -199,6 +185,6 @@ if __name__ == '__main__':
namespace = parser.parse_args() namespace = parser.parse_args()
if namespace.filepath: if namespace.filepath:
upload_file(namespace.filepath) upload_file(namespace.filepath, namespace.openocd_host, namespace.openocd_port, run_openocd=namespace.run_openocd)
else: else:
print("Nothing to upload") print("Nothing to upload")