1818import platform
1919import os .path
2020import shutil
21+ import struct
2122import sys
2223import time
2324from 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
538552def 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+ )
0 commit comments