WIP test platformio support

This commit is contained in:
Sergey Shchelkanov 2023-05-29 16:00:22 +03:00
parent 583249b2c2
commit f576a4f478
4 changed files with 88 additions and 27 deletions

1
.piopm Normal file
View 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}}

View File

@ -61,7 +61,7 @@ mik32v0_sections: List[MemorySection] = [
@dataclass
class Segment:
offset: int
memory: MemorySection | None
memory: MemorySection or None
data: List[int]
@ -74,7 +74,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) -> MemorySection or None:
for section in mik32v0_sections:
if belongs_memory_section(section, offset):
return section
@ -149,7 +149,17 @@ def segments_to_pages(segments: List[Segment], page_size: int) -> Dict[int, List
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
@ -165,6 +175,7 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp
result = 0
print(filename)
if not os.path.exists(filename):
print("ERROR: File %s does not exist" % filename)
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" % (
hex(segment.offset), segment.data.__len__(), segment.memory.type.name))
proc: subprocess.Popen | None = None
proc: subprocess.Popen or 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("%s -s %s -f interface/ftdi/m-link.cfg -f target/mik32.cfg" % (
openocd_path, scripts_path), posix=False)
print(cmd)
proc = subprocess.Popen(
cmd, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.SW_HIDE)
cmd)
with OpenOcdTclRpc() as openocd:
pages_eeprom = segments_to_pages(list(filter(
@ -197,10 +209,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))
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
@ -214,6 +228,12 @@ def upload_file(filename: str, host: str = '127.0.0.1', port: int = OpenOcdTclRp
def createParser():
parser = argparse.ArgumentParser()
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',
action='store_true', default=False)
parser.add_argument('--use-quad-spi', dest='use_quad_spi',
@ -234,7 +254,16 @@ 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_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:
print("Nothing to upload")

15
package.json Normal file
View 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"
}
}

View File

@ -128,7 +128,19 @@ class OpenOcdTclRpc:
return self.run(f"capture \"write_memory {address:#0x} {width} {{{data_string}}}\"")
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):
"""This function provides an efficient way to read the target memory from a Tcl script.
@ -139,19 +151,23 @@ class OpenOcdTclRpc:
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(" ")
data = self.run(f"capture \"mem2array {width} {address:#0x} {count}\"").split(" ")
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):
"""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)
data = self.run(f"capture \"mdw {address:#0x}\"").split(" ")
return int(data[1], base=16)