mirror of
https://github.com/MikronMIK32/mik32-uploader.git
synced 2026-01-01 13:37:03 +03:00
WIP test platformio support
This commit is contained in:
parent
583249b2c2
commit
f576a4f478
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}}
|
||||||
@ -61,7 +61,7 @@ mik32v0_sections: List[MemorySection] = [
|
|||||||
@dataclass
|
@dataclass
|
||||||
class Segment:
|
class Segment:
|
||||||
offset: int
|
offset: int
|
||||||
memory: MemorySection | None
|
memory: MemorySection or None
|
||||||
data: List[int]
|
data: List[int]
|
||||||
|
|
||||||
|
|
||||||
@ -74,7 +74,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) -> MemorySection or 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
|
||||||
@ -126,7 +126,7 @@ def read_file(filename: str) -> List[Segment]:
|
|||||||
def segment_to_pages(segment: Segment, page_size: int, pages: Dict[int, List[int]]):
|
def segment_to_pages(segment: Segment, page_size: int, pages: Dict[int, List[int]]):
|
||||||
if segment.memory is None:
|
if segment.memory is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
internal_offset = segment.offset - segment.memory.offset
|
internal_offset = segment.offset - segment.memory.offset
|
||||||
|
|
||||||
for i, byte in enumerate(segment.data):
|
for i, byte in enumerate(segment.data):
|
||||||
@ -136,7 +136,7 @@ def segment_to_pages(segment: Segment, page_size: int, pages: Dict[int, List[int
|
|||||||
|
|
||||||
if (page_offset) not in pages.keys():
|
if (page_offset) not in pages.keys():
|
||||||
pages[page_offset] = [0] * page_size
|
pages[page_offset] = [0] * page_size
|
||||||
|
|
||||||
pages[page_offset][byte_offset - page_offset] = byte
|
pages[page_offset][byte_offset - page_offset] = byte
|
||||||
|
|
||||||
|
|
||||||
@ -145,11 +145,21 @@ def segments_to_pages(segments: List[Segment], page_size: int) -> Dict[int, List
|
|||||||
|
|
||||||
for segment in segments:
|
for segment in segments:
|
||||||
segment_to_pages(segment, page_size, pages)
|
segment_to_pages(segment, page_size, pages)
|
||||||
|
|
||||||
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,
|
||||||
|
openocd_path: str,
|
||||||
|
scripts_path: str,
|
||||||
|
adapter_speed: 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
|
||||||
|
|
||||||
@ -165,6 +175,7 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp
|
|||||||
|
|
||||||
result = 0
|
result = 0
|
||||||
|
|
||||||
|
print(filename)
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
print("ERROR: File %s does not exist" % filename)
|
print("ERROR: File %s does not exist" % filename)
|
||||||
exit(1)
|
exit(1)
|
||||||
@ -181,12 +192,13 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp
|
|||||||
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.memory.type.name))
|
hex(segment.offset), segment.data.__len__(), segment.memory.type.name))
|
||||||
|
|
||||||
proc: subprocess.Popen | None = None
|
proc: subprocess.Popen or 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("%s -s %s -f interface/ftdi/m-link.cfg -f target/mik32.cfg" % (
|
||||||
DEFAULT_OPENOCD_EXEC_FILE_PATH, DEFAULT_OPENOCD_SCRIPTS_PATH), posix=False)
|
openocd_path, scripts_path), posix=False)
|
||||||
|
print(cmd)
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
cmd, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.SW_HIDE)
|
cmd)
|
||||||
|
|
||||||
with OpenOcdTclRpc() as openocd:
|
with OpenOcdTclRpc() as openocd:
|
||||||
pages_eeprom = segments_to_pages(list(filter(
|
pages_eeprom = segments_to_pages(list(filter(
|
||||||
@ -195,12 +207,14 @@ 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.SPIFI), segments)), 256)
|
lambda segment: (segment.memory is not None) and (segment.memory.type == MemoryType.SPIFI), segments)), 256)
|
||||||
segments_ram = list(filter(
|
segments_ram = list(filter(
|
||||||
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
|
||||||
@ -214,6 +228,12 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp
|
|||||||
def createParser():
|
def createParser():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filepath', nargs='?')
|
parser.add_argument('filepath', nargs='?')
|
||||||
|
parser.add_argument('--openocd-path', dest='openocd_path',
|
||||||
|
default=DEFAULT_OPENOCD_EXEC_FILE_PATH)
|
||||||
|
parser.add_argument('--scripts-path', dest='scripts_path',
|
||||||
|
default=DEFAULT_OPENOCD_SCRIPTS_PATH)
|
||||||
|
parser.add_argument('--adapter-speed', dest='adapter_speed',
|
||||||
|
default=500)
|
||||||
parser.add_argument('--run-openocd', dest='run_openocd',
|
parser.add_argument('--run-openocd', dest='run_openocd',
|
||||||
action='store_true', default=False)
|
action='store_true', default=False)
|
||||||
parser.add_argument('--use-quad-spi', dest='use_quad_spi',
|
parser.add_argument('--use-quad-spi', dest='use_quad_spi',
|
||||||
@ -234,7 +254,16 @@ 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_path,
|
||||||
|
namespace.scripts_path,
|
||||||
|
namespace.adapter_speed,
|
||||||
|
host=namespace.openocd_host,
|
||||||
|
port=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"
|
||||||
|
}
|
||||||
|
}
|
||||||
40
tclrpc.py
40
tclrpc.py
@ -128,8 +128,20 @@ class OpenOcdTclRpc:
|
|||||||
return self.run(f"capture \"write_memory {address:#0x} {width} {{{data_string}}}\"")
|
return self.run(f"capture \"write_memory {address:#0x} {width} {{{data_string}}}\"")
|
||||||
|
|
||||||
def write_word(self, address:int, word:int):
|
def write_word(self, address:int, word:int):
|
||||||
return self.write_memory(address, 32, [word])
|
return self.run(f"capture \"mww {address:#0x} {word}\"")
|
||||||
|
|
||||||
|
# def read_memory(self, address:int, width:int, count:int):
|
||||||
|
# """This function provides an efficient way to read the target memory from a Tcl script.
|
||||||
|
# A Tcl list containing the requested memory elements is returned by this function.
|
||||||
|
|
||||||
|
# address ... target memory address
|
||||||
|
|
||||||
|
# width ... memory access bit size, can be 8, 16, 32 or 64
|
||||||
|
|
||||||
|
# count ... number of elements to read """
|
||||||
|
# data = self.run(f"capture \"read_memory {address:#0x} {width} {count}\"").split(" ")
|
||||||
|
# return list(map(lambda word: int(word, base=16), data))
|
||||||
|
|
||||||
def read_memory(self, address:int, width:int, count:int):
|
def read_memory(self, address:int, width:int, count:int):
|
||||||
"""This function provides an efficient way to read the target memory from a Tcl script.
|
"""This function provides an efficient way to read the target memory from a Tcl script.
|
||||||
A Tcl list containing the requested memory elements is returned by this function.
|
A Tcl list containing the requested memory elements is returned by this function.
|
||||||
@ -139,19 +151,23 @@ class OpenOcdTclRpc:
|
|||||||
width ... memory access bit size, can be 8, 16, 32 or 64
|
width ... memory access bit size, can be 8, 16, 32 or 64
|
||||||
|
|
||||||
count ... number of elements to read """
|
count ... number of elements to read """
|
||||||
data = self.run(f"capture \"read_memory {address:#0x} {width} {count}\"").split(" ")
|
data = self.run(f"capture \"mem2array {width} {address:#0x} {count}\"").split(" ")
|
||||||
return list(map(lambda word: int(word, base=16), data))
|
return list(map(lambda word: int(word, base=16), data))
|
||||||
|
|
||||||
|
# def read_word(self, address:int):
|
||||||
|
# """This function provides an efficient way to read the target memory from a Tcl script.
|
||||||
|
# A Tcl list containing the requested memory elements is returned by this function.
|
||||||
|
|
||||||
|
# address ... target memory address
|
||||||
|
|
||||||
|
# width ... memory access bit size, can be 8, 16, 32 or 64
|
||||||
|
|
||||||
|
# count ... number of elements to read """
|
||||||
|
# data = self.run(f"capture \"read_memory {address:#0x} 32 1\"").split(" ")
|
||||||
|
# return int(data[0], base=16)
|
||||||
|
|
||||||
def read_word(self, address:int):
|
def read_word(self, address:int):
|
||||||
"""This function provides an efficient way to read the target memory from a Tcl script.
|
data = self.run(f"capture \"mdw {address:#0x}\"").split(" ")
|
||||||
A Tcl list containing the requested memory elements is returned by this function.
|
return int(data[1], base=16)
|
||||||
|
|
||||||
address ... target memory address
|
|
||||||
|
|
||||||
width ... memory access bit size, can be 8, 16, 32 or 64
|
|
||||||
|
|
||||||
count ... number of elements to read """
|
|
||||||
data = self.run(f"capture \"read_memory {address:#0x} 32 1\"").split(" ")
|
|
||||||
return int(data[0], base=16)
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user