diff --git a/pslab/cli.py b/pslab/cli.py index 3d63954..5ce0d75 100644 --- a/pslab/cli.py +++ b/pslab/cli.py @@ -18,6 +18,7 @@ import platform import os.path import shutil +import struct import sys import time from itertools import zip_longest @@ -233,12 +234,12 @@ def main(args: argparse.Namespace): install(args) return + handler = SerialHandler(port=args.port) + if args.function == "flash": - flash(args) + flash(handler, args.hexfile) return - handler = SerialHandler(port=args.port) - if args.function == "collect": collect(handler, args) elif args.function == "wave": @@ -524,15 +525,28 @@ def add_install_args(subparser: argparse._SubParsersAction): ) -def flash(args: argparse.Namespace): +def flash(handler: SerialHandler, hexfile: str): """Flash firmware over USB. - Parameters - ---------- - args : :class:`argparse.Namespace` - Parsed arguments. + PSLab must be in bootloader mode. """ - mcbootflash.flash(args) + try: + bootattrs = mcbootflash.get_boot_attrs(handler) + except struct.error: + print("Flashing failed: PSLab is not in bootloader mode.") + + mcbootflash.erase_flash(handler, bootattrs.memory_range, bootattrs.erase_size) + total_bytes, chunks = mcbootflash.chunked(hexfile, bootattrs) + written = 0 + + for chunk in chunks: + mcbootflash.write_flash(handler, chunk) + mcbootflash.checksum(handler, chunk) + written += len(chunk.data) + print(f"{written}/{total_bytes} bytes flashed.", end="\r") + + print("", end="\n") + mcbootflash.self_verify(handler) def add_flash_args(subparser: argparse._SubParsersAction): @@ -543,8 +557,9 @@ def add_flash_args(subparser: argparse._SubParsersAction): subparser : :class:`argparse._SubParsersAction` SubParser to add other arguments related to flash function. """ - parser = mcbootflash.get_parser() - parser.prog = "pslab" - parser.usage = "Flash firmware to PSLab v6." - parser.add_argument("-b", "--baudrate", default=460800, help=argparse.SUPPRESS) - subparser.add_parser("flash", parents=[parser], add_help=False) + flash = subparser.add_parser("flash") + flash.add_argument( + "hexfile", + type=str, + help="an Intel HEX file containing application firmware", + ) diff --git a/pyproject.toml b/pyproject.toml index 3b46fa9..c78ef11 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ dependencies = [ "pyserial", "numpy", "scipy", - "mcbootflash", + "mcbootflash >= 8.0.0", ] classifiers = [ "Programming Language :: Python :: 3",