Skip to content

Commit

Permalink
Automatically enter bootloader when flashing
Browse files Browse the repository at this point in the history
  • Loading branch information
bessman committed Nov 23, 2023
1 parent 55ef822 commit ad377cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 13 additions & 7 deletions pslab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions pslab/sciencelab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ad377cf

Please sign in to comment.