Skip to content

Commit eab8516

Browse files
committed
Automatically enter bootloader when flashing
1 parent 55ef822 commit eab8516

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

pslab/cli.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def main(args: argparse.Namespace):
237237
handler = SerialHandler(port=args.port)
238238

239239
if args.function == "flash":
240-
flash(handler, args.hexfile)
240+
flash(pslab.ScienceLab(args.port), args.hexfile)
241241
return
242242

243243
if args.function == "collect":
@@ -525,28 +525,34 @@ def add_install_args(subparser: argparse._SubParsersAction):
525525
)
526526

527527

528-
def flash(handler: SerialHandler, hexfile: str):
528+
def flash(psl: pslab.ScienceLab, hexfile: str):
529529
"""Flash firmware over USB.
530530
531531
PSLab must be in bootloader mode.
532532
"""
533+
if psl.interface.baudrate == 1000000:
534+
psl.interface.timeout = 5
535+
psl.enter_bootloader()
536+
533537
try:
534-
bootattrs = mcbootflash.get_boot_attrs(handler)
538+
bootattrs = mcbootflash.get_boot_attrs(psl)
535539
except struct.error:
536540
print("Flashing failed: PSLab is not in bootloader mode.")
541+
return
537542

538-
mcbootflash.erase_flash(handler, bootattrs.memory_range, bootattrs.erase_size)
543+
mcbootflash.erase_flash(psl, bootattrs.memory_range, bootattrs.erase_size)
539544
total_bytes, chunks = mcbootflash.chunked(hexfile, bootattrs)
540545
written = 0
541546

542547
for chunk in chunks:
543-
mcbootflash.write_flash(handler, chunk)
544-
mcbootflash.checksum(handler, chunk)
548+
mcbootflash.write_flash(psl, chunk)
549+
mcbootflash.checksum(psl, chunk)
545550
written += len(chunk.data)
546551
print(f"{written}/{total_bytes} bytes flashed.", end="\r")
547552

548553
print("", end="\n")
549-
mcbootflash.self_verify(handler)
554+
mcbootflash.self_verify(psl)
555+
mcbootflash.reset(psl)
550556

551557

552558
def add_flash_args(subparser: argparse._SubParsersAction):

pslab/sciencelab.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ def enter_bootloader(self):
137137
time.sleep(boot_lightshow_time / 2)
138138
# PIC24 UART RX buffer is four bytes deep; no need to time it perfectly.
139139
self.write(CP.Integer.pack(0xDECAFBAD))
140+
# Wait until lightshow is done to prevent accidentally overwriting magic number.
141+
time.sleep(boot_lightshow_time)
140142

141143
def rgb_led(self, colors: List, output: str = "RGB", order: str = "GRB"):
142144
"""Set shade of a WS2812B RGB LED.

0 commit comments

Comments
 (0)