mirror of
https://github.com/MikronMIK32/mik32-uploader.git
synced 2026-01-01 13:37:03 +03:00
refactor using f-strings
This commit is contained in:
parent
96c580da27
commit
98c9b842bb
1
.piopm
Normal file
1
.piopm
Normal file
@ -0,0 +1 @@
|
||||
{"type": "tool", "name": "tool-mik32-uploader", "version": "0.1.0", "spec": {"owner": "platformio", "id": 1235, "name": "tool-mik32-uploader", "requirements": null, "uri": null}}
|
||||
@ -1,3 +1,3 @@
|
||||
@echo off
|
||||
openocd\bin\openocd.exe -s openocd/share/openocd/scripts -f interface/ftdi/m-link.cfg -f target/mcu32.cfg
|
||||
openocd\bin\openocd.exe -s openocd/share/openocd/scripts -f interface/ftdi/m-link.cfg -f target/mik32.cfg
|
||||
timeout /t 300
|
||||
|
||||
@ -140,6 +140,7 @@ def eeprom_write_word(openocd: OpenOcdTclRpc, address:int, word:int):
|
||||
time.sleep(0.001)
|
||||
|
||||
def eeprom_write_page(openocd: OpenOcdTclRpc, address:int, data:List[int]):
|
||||
print(f"Writing page {address:#06x}...")
|
||||
openocd.write_word(EEPROM_REGS_EECON, 1 << EEPROM_BWE_S)
|
||||
openocd.write_word(EEPROM_REGS_EEA, address)
|
||||
page_address = address & EEPROM_PAGE_MASK
|
||||
@ -279,7 +280,6 @@ def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc, read_throug
|
||||
print("EEPROM writing...")
|
||||
|
||||
for page_offset in list(pages):
|
||||
print("Writing page %s..." % hex(page_offset))
|
||||
page_words = bytes2words(pages[page_offset])
|
||||
eeprom_write_page(openocd, page_offset, page_words)
|
||||
if read_through_apb:
|
||||
|
||||
@ -332,6 +332,7 @@ def spifi_read_data(openocd: OpenOcdTclRpc, address: int, byte_count: int, bin_d
|
||||
|
||||
|
||||
def spifi_page_program(openocd: OpenOcdTclRpc, ByteAddress: int, data: List[int], byte_count: int):
|
||||
print(f"Writing page {ByteAddress:#010x}...")
|
||||
if byte_count > 256:
|
||||
raise Exception("Byte count more than 256")
|
||||
|
||||
@ -416,6 +417,7 @@ def spifi_write_file(bytes: List[int], openocd: OpenOcdTclRpc, is_resume=True):
|
||||
|
||||
|
||||
def spifi_quad_page_program(openocd: OpenOcdTclRpc, ByteAddress: int, data: List[int], byte_count: int):
|
||||
print(f"Writing page {ByteAddress:#010x}...")
|
||||
if byte_count > 256:
|
||||
raise Exception("Byte count more than 256")
|
||||
|
||||
@ -494,7 +496,6 @@ def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc, is_resume=T
|
||||
spifi_quad_enable(openocd)
|
||||
|
||||
for page_offset in list(pages):
|
||||
print("Writing page %s..." % hex(page_offset))
|
||||
page_bytes = pages[page_offset]
|
||||
|
||||
spifi_write_enable(openocd)
|
||||
|
||||
@ -3,7 +3,7 @@ import argparse
|
||||
import subprocess
|
||||
import os
|
||||
from enum import Enum
|
||||
from typing import List, Dict, NamedTuple
|
||||
from typing import List, Dict, NamedTuple, Union
|
||||
from tclrpc import OpenOcdTclRpc
|
||||
import mik32_eeprom
|
||||
import mik32_spifi
|
||||
@ -20,9 +20,10 @@ from mik32_parsers import *
|
||||
# UNDERLINE = '\033[4m'
|
||||
|
||||
|
||||
DEFAULT_OPENOCD_EXEC_FILE_PATH = os.path.join("openocd", "bin", "openocd.exe")
|
||||
DEFAULT_OPENOCD_SCRIPTS_PATH = os.path.join(
|
||||
"openocd", "share", "openocd", "scripts")
|
||||
openocd_exec_path = os.path.join("openocd", "bin", "openocd.exe")
|
||||
openocd_scripts_path = os.path.join("openocd", "share", "openocd", "scripts")
|
||||
openocd_interface_path = os.path.join("interface", "ftdi", "m-link.cfg")
|
||||
openocd_target_path = os.path.join("target", "mik32.cfg")
|
||||
|
||||
supported_formats = [".hex"]
|
||||
|
||||
@ -30,7 +31,7 @@ supported_formats = [".hex"]
|
||||
def test_connection():
|
||||
output = ""
|
||||
with OpenOcdTclRpc() as openocd:
|
||||
output = openocd.run(f"capture \"reg\"")
|
||||
output = openocd.run("capture \"reg\"")
|
||||
|
||||
if output == "":
|
||||
raise Exception("ERROR: no regs found, check MCU connection")
|
||||
@ -61,7 +62,7 @@ mik32v0_sections: List[MemorySection] = [
|
||||
@dataclass
|
||||
class Segment:
|
||||
offset: int
|
||||
memory: MemorySection | None
|
||||
memory: Union[MemorySection, None]
|
||||
data: List[int]
|
||||
|
||||
|
||||
@ -74,7 +75,7 @@ def belongs_memory_section(memory_section: MemorySection, offset: int) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def find_memory_section(offset: int) -> MemorySection | None:
|
||||
def find_memory_section(offset: int) -> Union[MemorySection, None]:
|
||||
for section in mik32v0_sections:
|
||||
if belongs_memory_section(section, offset):
|
||||
return section
|
||||
@ -96,7 +97,7 @@ def read_file(filename: str) -> List[Segment]:
|
||||
segments.append(
|
||||
Segment(offset=0, memory=find_memory_section(0), data=contents))
|
||||
else:
|
||||
raise Exception("Unsupported file format: %s" % (file_extension))
|
||||
raise Exception(f"Unsupported file format: {file_extension}")
|
||||
|
||||
lba: int = 0 # Linear Base Address
|
||||
expect_address = 0 # Address of the next byte
|
||||
@ -126,7 +127,7 @@ def read_file(filename: str) -> List[Segment]:
|
||||
def segment_to_pages(segment: Segment, page_size: int, pages: Dict[int, List[int]]):
|
||||
if segment.memory is None:
|
||||
return
|
||||
|
||||
|
||||
internal_offset = segment.offset - segment.memory.offset
|
||||
|
||||
for i, byte in enumerate(segment.data):
|
||||
@ -136,7 +137,7 @@ def segment_to_pages(segment: Segment, page_size: int, pages: Dict[int, List[int
|
||||
|
||||
if (page_offset) not in pages.keys():
|
||||
pages[page_offset] = [0] * page_size
|
||||
|
||||
|
||||
pages[page_offset][byte_offset - page_offset] = byte
|
||||
|
||||
|
||||
@ -145,11 +146,18 @@ def segments_to_pages(segments: List[Segment], page_size: int) -> Dict[int, List
|
||||
|
||||
for segment in segments:
|
||||
segment_to_pages(segment, page_size, pages)
|
||||
|
||||
|
||||
return pages
|
||||
|
||||
|
||||
def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRpc.DEFAULT_PORT, is_resume=True, run_openocd=False, use_quad_spi=False) -> int:
|
||||
def upload_file(
|
||||
filename: str,
|
||||
host: str = '127.0.0.1',
|
||||
port: int = OpenOcdTclRpc.DEFAULT_PORT,
|
||||
is_resume=True,
|
||||
run_openocd=False,
|
||||
use_quad_spi=False
|
||||
) -> int:
|
||||
"""
|
||||
Write ihex or binary file into MIK32 EEPROM or external flash memory
|
||||
|
||||
@ -166,7 +174,7 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp
|
||||
result = 0
|
||||
|
||||
if not os.path.exists(filename):
|
||||
print("ERROR: File %s does not exist" % filename)
|
||||
print(f"ERROR: File {filename} does not exist")
|
||||
exit(1)
|
||||
|
||||
segments: List[Segment] = read_file(filename)
|
||||
@ -175,32 +183,39 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp
|
||||
for segment in segments:
|
||||
if segment.memory is None:
|
||||
raise Exception(
|
||||
"ERROR: segment with offset %s doesn't belong to any section" % hex(segment.offset))
|
||||
f"ERROR: segment with offset {segment.offset:#0x} doesn't belong to any section")
|
||||
|
||||
if (segment.offset + segment.data.__len__()) > (segment.memory.offset + segment.memory.length):
|
||||
raise Exception("ERROR: segment with offset %s and length %s overflows section %s" % (
|
||||
hex(segment.offset), segment.data.__len__(), segment.memory.type.name))
|
||||
raise Exception(
|
||||
f"ERROR: segment with offset {segment.offset:#0x} "
|
||||
f"and length {segment.data.__len__()} "
|
||||
f"overflows section {segment.memory.type.name}"
|
||||
)
|
||||
|
||||
proc: subprocess.Popen | None = None
|
||||
proc: Union[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)
|
||||
cmd = shlex.split(
|
||||
f"{openocd_exec_path} -s {openocd_scripts_path} "
|
||||
f"-f {openocd_interface_path} -f {openocd_target_path}", posix=False
|
||||
)
|
||||
proc = subprocess.Popen(
|
||||
cmd, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.SW_HIDE)
|
||||
|
||||
with OpenOcdTclRpc() as openocd:
|
||||
with OpenOcdTclRpc(host, port) as openocd:
|
||||
pages_eeprom = segments_to_pages(list(filter(
|
||||
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.EEPROM), segments)), 128)
|
||||
pages_spifi = segments_to_pages(list(filter(
|
||||
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.SPIFI), segments)), 256)
|
||||
segments_ram = list(filter(
|
||||
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.RAM), segments))
|
||||
|
||||
|
||||
if (pages_eeprom.__len__() > 0):
|
||||
result |= mik32_eeprom.write_pages(pages_eeprom, openocd, is_resume)
|
||||
result |= mik32_eeprom.write_pages(
|
||||
pages_eeprom, openocd, is_resume)
|
||||
if (pages_spifi.__len__() > 0):
|
||||
# print(pages_spifi)
|
||||
result |= mik32_spifi.write_pages(pages_spifi, openocd, is_resume, use_quad_spi)
|
||||
result |= mik32_spifi.write_pages(
|
||||
pages_spifi, openocd, is_resume, use_quad_spi)
|
||||
if (segments_ram.__len__() > 0):
|
||||
mik32_ram.write_segments(segments_ram, openocd, is_resume)
|
||||
result |= 0
|
||||
@ -234,7 +249,13 @@ if __name__ == '__main__':
|
||||
namespace = parser.parse_args()
|
||||
|
||||
if namespace.filepath:
|
||||
upload_file(namespace.filepath, namespace.openocd_host,
|
||||
namespace.openocd_port, is_resume=(not namespace.keep_halt), run_openocd=namespace.run_openocd, use_quad_spi=namespace.use_quad_spi)
|
||||
upload_file(
|
||||
namespace.filepath,
|
||||
namespace.openocd_host,
|
||||
namespace.openocd_port,
|
||||
is_resume=(not namespace.keep_halt),
|
||||
run_openocd=namespace.run_openocd,
|
||||
use_quad_spi=namespace.use_quad_spi
|
||||
)
|
||||
else:
|
||||
print("Nothing to upload")
|
||||
|
||||
15
package.json
Normal file
15
package.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "tool-mik32-uploader",
|
||||
"version": "0.1.0",
|
||||
"description": "mik32-uploader",
|
||||
"keywords": [
|
||||
"tools",
|
||||
"uploader",
|
||||
"mik32"
|
||||
],
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/MikronMIK32/mik32-uploader"
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user