Skip to content

Commit 0a9350b

Browse files
committed
Update mcbootflash
1 parent 70963c6 commit 0a9350b

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

pslab/cli.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import platform
1919
import os.path
2020
import shutil
21+
import struct
2122
import sys
2223
import time
2324
from itertools import zip_longest
@@ -233,12 +234,12 @@ def main(args: argparse.Namespace):
233234
install(args)
234235
return
235236

237+
handler = SerialHandler(port=args.port)
238+
236239
if args.function == "flash":
237-
flash(args)
240+
flash(handler, args.hexfile)
238241
return
239242

240-
handler = SerialHandler(port=args.port)
241-
242243
if args.function == "collect":
243244
collect(handler, args)
244245
elif args.function == "wave":
@@ -524,15 +525,28 @@ def add_install_args(subparser: argparse._SubParsersAction):
524525
)
525526

526527

527-
def flash(args: argparse.Namespace):
528+
def flash(handler: SerialHandler, hexfile: str):
528529
"""Flash firmware over USB.
529530
530-
Parameters
531-
----------
532-
args : :class:`argparse.Namespace`
533-
Parsed arguments.
531+
PSLab must be in bootloader mode.
534532
"""
535-
mcbootflash.flash(args)
533+
try:
534+
bootattrs = mcbootflash.get_boot_attrs(handler)
535+
except struct.error:
536+
print("Flashing failed: PSLab is not in bootloader mode.")
537+
538+
mcbootflash.erase_flash(handler, bootattrs.memory_range, bootattrs.erase_size)
539+
total_bytes, chunks = mcbootflash.chunked(hexfile, bootattrs)
540+
written = 0
541+
542+
for chunk in chunks:
543+
mcbootflash.write_flash(handler, chunk)
544+
mcbootflash.checksum(handler, chunk)
545+
written += len(chunk.data)
546+
print(f"{written}/{total_bytes} bytes flashed.", end="\r")
547+
548+
print("", end="\n")
549+
mcbootflash.self_verify(handler)
536550

537551

538552
def add_flash_args(subparser: argparse._SubParsersAction):
@@ -543,8 +557,9 @@ def add_flash_args(subparser: argparse._SubParsersAction):
543557
subparser : :class:`argparse._SubParsersAction`
544558
SubParser to add other arguments related to flash function.
545559
"""
546-
parser = mcbootflash.get_parser()
547-
parser.prog = "pslab"
548-
parser.usage = "Flash firmware to PSLab v6."
549-
parser.add_argument("-b", "--baudrate", default=460800, help=argparse.SUPPRESS)
550-
subparser.add_parser("flash", parents=[parser], add_help=False)
560+
flash = subparser.add_parser("flash")
561+
flash.add_argument(
562+
"hexfile",
563+
type=str,
564+
help="an Intel HEX file containing application firmware",
565+
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies = [
1313
"pyserial",
1414
"numpy",
1515
"scipy",
16-
"mcbootflash",
16+
"mcbootflash >= 8.0.0",
1717
]
1818
classifiers = [
1919
"Programming Language :: Python :: 3",

0 commit comments

Comments
 (0)