Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically enter bootloader when flashing #223

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading