mirror of
https://github.com/MikronMIK32/mik32-uploader.git
synced 2026-01-01 13:37:03 +03:00
reverted old structure, reimpl openocd launching
This commit is contained in:
parent
a244b042d6
commit
999509141f
3
connectopenocd-m-link.bat
Normal file
3
connectopenocd-m-link.bat
Normal 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
|
||||
@ -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<<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_NCYCEP2, 1000)
|
||||
time.sleep(0.1)
|
||||
word_num:int = 0
|
||||
progress:int = 0
|
||||
print("EEPROM writing...")
|
||||
print("[", end="", flush=True)
|
||||
if write_by_word:
|
||||
for word in words:
|
||||
eeprom_write_word(openocd, word_num*4, word)
|
||||
|
||||
openocd.halt()
|
||||
eeprom_sysinit(openocd)
|
||||
eeprom_global_erase(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_NCYCEP1, 100000)
|
||||
openocd.write_word(EEPROM_REGS_NCYCEP2, 1000)
|
||||
time.sleep(0.1)
|
||||
word_num:int = 0
|
||||
progress:int = 0
|
||||
print("EEPROM writing...")
|
||||
print("[", end="", flush=True)
|
||||
if write_by_word:
|
||||
for word in words:
|
||||
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
|
||||
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
|
||||
@ -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):
|
||||
@ -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
|
||||
@ -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")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user