diff --git a/pslab/cli.py b/pslab/cli.py index 5ce0d75..5ba833b 100644 --- a/pslab/cli.py +++ b/pslab/cli.py @@ -237,7 +237,7 @@ def main(args: argparse.Namespace): handler = SerialHandler(port=args.port) if args.function == "flash": - flash(handler, args.hexfile) + flash(pslab.ScienceLab(args.port), args.hexfile) return if args.function == "collect": @@ -525,28 +525,34 @@ def add_install_args(subparser: argparse._SubParsersAction): ) -def flash(handler: SerialHandler, hexfile: str): +def flash(psl: pslab.ScienceLab, hexfile: str): """Flash firmware over USB. PSLab must be in bootloader mode. """ + if psl.interface.baudrate == 1000000: + psl.interface.timeout = 5 + psl.enter_bootloader() + try: - bootattrs = mcbootflash.get_boot_attrs(handler) + bootattrs = mcbootflash.get_boot_attrs(psl) except struct.error: print("Flashing failed: PSLab is not in bootloader mode.") + return - mcbootflash.erase_flash(handler, bootattrs.memory_range, bootattrs.erase_size) + mcbootflash.erase_flash(psl, 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) + mcbootflash.write_flash(psl, chunk) + mcbootflash.checksum(psl, chunk) written += len(chunk.data) print(f"{written}/{total_bytes} bytes flashed.", end="\r") print("", end="\n") - mcbootflash.self_verify(handler) + mcbootflash.self_verify(psl) + mcbootflash.reset(psl) def add_flash_args(subparser: argparse._SubParsersAction): diff --git a/pslab/sciencelab.py b/pslab/sciencelab.py index d4faaf6..eec1170 100644 --- a/pslab/sciencelab.py +++ b/pslab/sciencelab.py @@ -137,6 +137,8 @@ def enter_bootloader(self): time.sleep(boot_lightshow_time / 2) # PIC24 UART RX buffer is four bytes deep; no need to time it perfectly. self.write(CP.Integer.pack(0xDECAFBAD)) + # Wait until lightshow is done to prevent accidentally overwriting magic number. + time.sleep(boot_lightshow_time) def rgb_led(self, colors: List, output: str = "RGB", order: str = "GRB"): """Set shade of a WS2812B RGB LED.