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
|
@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
|
timeout /t 300
|
||||||
|
|||||||
@ -140,6 +140,7 @@ def eeprom_write_word(openocd: OpenOcdTclRpc, address:int, word:int):
|
|||||||
time.sleep(0.001)
|
time.sleep(0.001)
|
||||||
|
|
||||||
def eeprom_write_page(openocd: OpenOcdTclRpc, address:int, data:List[int]):
|
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_EECON, 1 << EEPROM_BWE_S)
|
||||||
openocd.write_word(EEPROM_REGS_EEA, address)
|
openocd.write_word(EEPROM_REGS_EEA, address)
|
||||||
page_address = address & EEPROM_PAGE_MASK
|
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...")
|
print("EEPROM writing...")
|
||||||
|
|
||||||
for page_offset in list(pages):
|
for page_offset in list(pages):
|
||||||
print("Writing page %s..." % hex(page_offset))
|
|
||||||
page_words = bytes2words(pages[page_offset])
|
page_words = bytes2words(pages[page_offset])
|
||||||
eeprom_write_page(openocd, page_offset, page_words)
|
eeprom_write_page(openocd, page_offset, page_words)
|
||||||
if read_through_apb:
|
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):
|
def spifi_page_program(openocd: OpenOcdTclRpc, ByteAddress: int, data: List[int], byte_count: int):
|
||||||
|
print(f"Writing page {ByteAddress:#010x}...")
|
||||||
if byte_count > 256:
|
if byte_count > 256:
|
||||||
raise Exception("Byte count more than 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):
|
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:
|
if byte_count > 256:
|
||||||
raise Exception("Byte count more than 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)
|
spifi_quad_enable(openocd)
|
||||||
|
|
||||||
for page_offset in list(pages):
|
for page_offset in list(pages):
|
||||||
print("Writing page %s..." % hex(page_offset))
|
|
||||||
page_bytes = pages[page_offset]
|
page_bytes = pages[page_offset]
|
||||||
|
|
||||||
spifi_write_enable(openocd)
|
spifi_write_enable(openocd)
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import argparse
|
|||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import List, Dict, NamedTuple
|
from typing import List, Dict, NamedTuple, Union
|
||||||
from tclrpc import OpenOcdTclRpc
|
from tclrpc import OpenOcdTclRpc
|
||||||
import mik32_eeprom
|
import mik32_eeprom
|
||||||
import mik32_spifi
|
import mik32_spifi
|
||||||
@ -20,9 +20,10 @@ from mik32_parsers import *
|
|||||||
# UNDERLINE = '\033[4m'
|
# UNDERLINE = '\033[4m'
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_OPENOCD_EXEC_FILE_PATH = os.path.join("openocd", "bin", "openocd.exe")
|
openocd_exec_path = os.path.join("openocd", "bin", "openocd.exe")
|
||||||
DEFAULT_OPENOCD_SCRIPTS_PATH = os.path.join(
|
openocd_scripts_path = os.path.join("openocd", "share", "openocd", "scripts")
|
||||||
"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"]
|
supported_formats = [".hex"]
|
||||||
|
|
||||||
@ -30,7 +31,7 @@ supported_formats = [".hex"]
|
|||||||
def test_connection():
|
def test_connection():
|
||||||
output = ""
|
output = ""
|
||||||
with OpenOcdTclRpc() as openocd:
|
with OpenOcdTclRpc() as openocd:
|
||||||
output = openocd.run(f"capture \"reg\"")
|
output = openocd.run("capture \"reg\"")
|
||||||
|
|
||||||
if output == "":
|
if output == "":
|
||||||
raise Exception("ERROR: no regs found, check MCU connection")
|
raise Exception("ERROR: no regs found, check MCU connection")
|
||||||
@ -61,7 +62,7 @@ mik32v0_sections: List[MemorySection] = [
|
|||||||
@dataclass
|
@dataclass
|
||||||
class Segment:
|
class Segment:
|
||||||
offset: int
|
offset: int
|
||||||
memory: MemorySection | None
|
memory: Union[MemorySection, None]
|
||||||
data: List[int]
|
data: List[int]
|
||||||
|
|
||||||
|
|
||||||
@ -74,7 +75,7 @@ def belongs_memory_section(memory_section: MemorySection, offset: int) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def find_memory_section(offset: int) -> MemorySection | None:
|
def find_memory_section(offset: int) -> Union[MemorySection, None]:
|
||||||
for section in mik32v0_sections:
|
for section in mik32v0_sections:
|
||||||
if belongs_memory_section(section, offset):
|
if belongs_memory_section(section, offset):
|
||||||
return section
|
return section
|
||||||
@ -96,7 +97,7 @@ def read_file(filename: str) -> List[Segment]:
|
|||||||
segments.append(
|
segments.append(
|
||||||
Segment(offset=0, memory=find_memory_section(0), data=contents))
|
Segment(offset=0, memory=find_memory_section(0), data=contents))
|
||||||
else:
|
else:
|
||||||
raise Exception("Unsupported file format: %s" % (file_extension))
|
raise Exception(f"Unsupported file format: {file_extension}")
|
||||||
|
|
||||||
lba: int = 0 # Linear Base Address
|
lba: int = 0 # Linear Base Address
|
||||||
expect_address = 0 # Address of the next byte
|
expect_address = 0 # Address of the next byte
|
||||||
@ -149,7 +150,14 @@ def segments_to_pages(segments: List[Segment], page_size: int) -> Dict[int, List
|
|||||||
return 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
|
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
|
result = 0
|
||||||
|
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
print("ERROR: File %s does not exist" % filename)
|
print(f"ERROR: File {filename} does not exist")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
segments: List[Segment] = read_file(filename)
|
segments: List[Segment] = read_file(filename)
|
||||||
@ -175,20 +183,25 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp
|
|||||||
for segment in segments:
|
for segment in segments:
|
||||||
if segment.memory is None:
|
if segment.memory is None:
|
||||||
raise Exception(
|
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):
|
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" % (
|
raise Exception(
|
||||||
hex(segment.offset), segment.data.__len__(), segment.memory.type.name))
|
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:
|
if run_openocd:
|
||||||
cmd = shlex.split("%s -s %s -f interface/ftdi/m-link.cfg -f target/mcu32.cfg" % (
|
cmd = shlex.split(
|
||||||
DEFAULT_OPENOCD_EXEC_FILE_PATH, DEFAULT_OPENOCD_SCRIPTS_PATH), posix=False)
|
f"{openocd_exec_path} -s {openocd_scripts_path} "
|
||||||
|
f"-f {openocd_interface_path} -f {openocd_target_path}", posix=False
|
||||||
|
)
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
cmd, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.SW_HIDE)
|
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(
|
pages_eeprom = segments_to_pages(list(filter(
|
||||||
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.EEPROM), segments)), 128)
|
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.EEPROM), segments)), 128)
|
||||||
pages_spifi = segments_to_pages(list(filter(
|
pages_spifi = segments_to_pages(list(filter(
|
||||||
@ -197,10 +210,12 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp
|
|||||||
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.RAM), segments))
|
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.RAM), segments))
|
||||||
|
|
||||||
if (pages_eeprom.__len__() > 0):
|
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):
|
if (pages_spifi.__len__() > 0):
|
||||||
# print(pages_spifi)
|
# 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):
|
if (segments_ram.__len__() > 0):
|
||||||
mik32_ram.write_segments(segments_ram, openocd, is_resume)
|
mik32_ram.write_segments(segments_ram, openocd, is_resume)
|
||||||
result |= 0
|
result |= 0
|
||||||
@ -234,7 +249,13 @@ if __name__ == '__main__':
|
|||||||
namespace = parser.parse_args()
|
namespace = parser.parse_args()
|
||||||
|
|
||||||
if namespace.filepath:
|
if namespace.filepath:
|
||||||
upload_file(namespace.filepath, namespace.openocd_host,
|
upload_file(
|
||||||
namespace.openocd_port, is_resume=(not namespace.keep_halt), run_openocd=namespace.run_openocd, use_quad_spi=namespace.use_quad_spi)
|
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:
|
else:
|
||||||
print("Nothing to upload")
|
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