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,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 @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)
# eeprom_global_erase_check(openocd) # eeprom_global_erase_check(openocd)
openocd.write_word(EEPROM_REGS_NCYCRL, 1<<EEPROM_N_LD_S | 3<<EEPROM_N_R_1_S | 1<<EEPROM_N_R_2_S) openocd.write_word(EEPROM_REGS_NCYCRL, 1<<EEPROM_N_LD_S | 3<<EEPROM_N_R_1_S | 1<<EEPROM_N_R_2_S)
openocd.write_word(EEPROM_REGS_NCYCEP1, 100000) openocd.write_word(EEPROM_REGS_NCYCEP1, 100000)
openocd.write_word(EEPROM_REGS_NCYCEP2, 1000) openocd.write_word(EEPROM_REGS_NCYCEP2, 1000)
time.sleep(0.1) time.sleep(0.1)
word_num:int = 0 word_num:int = 0
progress:int = 0 progress:int = 0
print("EEPROM writing...") print("EEPROM writing...")
print("[", end="", flush=True) print("[", end="", flush=True)
if write_by_word: if write_by_word:
for word in words: for word in words:
eeprom_write_word(openocd, word_num*4, word) eeprom_write_word(openocd, word_num*4, word)
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 word_num += 1
curr_progress = int((word_num * 50) / len(words)) else:
if curr_progress > progress: # print(list(map(lambda word: f"{word:#0x}", page)))
print("#"*(curr_progress - progress), end="", flush=True) eeprom_write_page(openocd, page_num*page_size*4, page)
progress = curr_progress page_num += 1
else: page.clear()
page = [] curr_progress = int((word_num * 50) / len(words))
page_num = 0 if curr_progress > progress:
page_size = 32 print("#"*(curr_progress - progress), end="", flush=True)
while word_num < len(words): progress = curr_progress
if word_num < page_size*(page_num+1): eeprom_write_page(openocd, page_num*page_size*4, page)
page.append(words[word_num]) print("]")
word_num += 1 if read_through_apb:
else: result = eeprom_check_data_apb(openocd, words)
# print(list(map(lambda word: f"{word:#0x}", page))) else:
eeprom_write_page(openocd, page_num*page_size*4, page) result = eeprom_check_data_ahb_lite(openocd, words)
page_num += 1 if is_resume:
page.clear() openocd.resume(0)
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: 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,28 +435,29 @@ 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)
print("bin_data_len = ", len(bytes)) print("bin_data_len = ", len(bytes))
address = 0 address = 0
for address in range(0, len(bytes), 256): for address in range(0, len(bytes), 256):
if ((address + 256) > len(bytes)): if ((address + 256) > len(bytes)):
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_read_data(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_read_data(openocd, address, len(bytes) - address, bytes) == 1:
return 1 return 1
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
@ -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): 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" % ( 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))
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) proc: subprocess.Popen | None = None
# with subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL) as proc: if run_openocd:
# if boot_source == "eeprom": cmd = shlex.split("%s -s %s -f interface/ftdi/m-link.cfg -f target/mcu32.cfg" % (
# result = write_words(bytes2words(get_content(filename))) DEFAULT_OPENOCD_EXEC_FILE_PATH, DEFAULT_OPENOCD_SCRIPTS_PATH), posix=False)
# elif boot_source == "spifi": proc = subprocess.Popen(
# spifi_write_file(get_content(filename)) cmd, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.SW_HIDE)
# 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": with OpenOcdTclRpc() as openocd:
# result = write_words(bytes2words(get_content(filename)), is_resume) if segment_section.type == MemoryType.EEPROM:
# elif boot_source == "spifi": result = mik32_eeprom.write_words(bytes2words(
# result = spifi_write_file(get_content(filename), is_resume) segment.data), openocd, is_resume)
# elif boot_source == "ram": elif segment_section.type == MemoryType.SPIFI:
# write_file(filename, is_resume) result = mik32_spifi.spifi_write_file(segment.data, openocd, is_resume)
# result = 0 # TODO
# else: if run_openocd and proc is not None:
# raise Exception("Unsupported boot source, use eeprom or spifi") proc.kill()
# 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")