18
18
import platform
19
19
import os .path
20
20
import shutil
21
+ import struct
21
22
import sys
22
23
import time
23
24
from itertools import zip_longest
@@ -233,12 +234,12 @@ def main(args: argparse.Namespace):
233
234
install (args )
234
235
return
235
236
237
+ handler = SerialHandler (port = args .port )
238
+
236
239
if args .function == "flash" :
237
- flash (args )
240
+ flash (handler , args . hexfile )
238
241
return
239
242
240
- handler = SerialHandler (port = args .port )
241
-
242
243
if args .function == "collect" :
243
244
collect (handler , args )
244
245
elif args .function == "wave" :
@@ -524,15 +525,28 @@ def add_install_args(subparser: argparse._SubParsersAction):
524
525
)
525
526
526
527
527
- def flash (args : argparse . Namespace ):
528
+ def flash (handler : SerialHandler , hexfile : str ):
528
529
"""Flash firmware over USB.
529
530
530
- Parameters
531
- ----------
532
- args : :class:`argparse.Namespace`
533
- Parsed arguments.
531
+ PSLab must be in bootloader mode.
534
532
"""
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 )
536
550
537
551
538
552
def add_flash_args (subparser : argparse ._SubParsersAction ):
@@ -543,8 +557,9 @@ def add_flash_args(subparser: argparse._SubParsersAction):
543
557
subparser : :class:`argparse._SubParsersAction`
544
558
SubParser to add other arguments related to flash function.
545
559
"""
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