mirror of
https://github.com/MikronMIK32/mik32-uploader.git
synced 2026-01-01 21:37:05 +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
|
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
|
||||||
@ -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):
|
||||||
@ -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()
|
|
||||||
spifi_init(openocd)
|
|
||||||
spifi_erase(openocd)
|
|
||||||
print("bin_data_len = ", len(bytes))
|
|
||||||
address = 0
|
|
||||||
|
|
||||||
for address in range(0, len(bytes), 256):
|
openocd.halt()
|
||||||
if ((address + 256) > len(bytes)):
|
spifi_init(openocd)
|
||||||
break
|
spifi_erase(openocd)
|
||||||
print("address = ", address)
|
print("bin_data_len = ", len(bytes))
|
||||||
spifi_write(openocd, address, bytes, 256)
|
address = 0
|
||||||
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
|
return 0
|
||||||
@ -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))
|
||||||
|
|
||||||
if segment_section.type == MemoryType.EEPROM:
|
proc: subprocess.Popen | None = None
|
||||||
result = write_words(bytes2words(segment.data), is_resume)
|
if run_openocd:
|
||||||
elif segment_section.type == MemoryType.SPIFI:
|
cmd = shlex.split("%s -s %s -f interface/ftdi/m-link.cfg -f target/mcu32.cfg" % (
|
||||||
result = spifi_write_file(segment.data, is_resume)
|
DEFAULT_OPENOCD_EXEC_FILE_PATH, DEFAULT_OPENOCD_SCRIPTS_PATH), posix=False)
|
||||||
# elif segment_section.type == MemoryType.RAM:
|
proc = subprocess.Popen(
|
||||||
# write_file(filename, is_resume)
|
cmd, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.SW_HIDE)
|
||||||
# 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 OpenOcdTclRpc() as openocd:
|
||||||
# with subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL) as proc:
|
if segment_section.type == MemoryType.EEPROM:
|
||||||
# if boot_source == "eeprom":
|
result = mik32_eeprom.write_words(bytes2words(
|
||||||
# result = write_words(bytes2words(get_content(filename)))
|
segment.data), openocd, is_resume)
|
||||||
# elif boot_source == "spifi":
|
elif segment_section.type == MemoryType.SPIFI:
|
||||||
# spifi_write_file(get_content(filename))
|
result = mik32_spifi.spifi_write_file(segment.data, openocd, is_resume)
|
||||||
# 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":
|
if run_openocd and proc is not None:
|
||||||
# result = write_words(bytes2words(get_content(filename)), is_resume)
|
proc.kill()
|
||||||
# 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")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user