diff --git a/selfdrive/pandad/pandad.py b/selfdrive/pandad/pandad.py index 2400f8cd6..87719de02 100755 --- a/selfdrive/pandad/pandad.py +++ b/selfdrive/pandad/pandad.py @@ -79,19 +79,32 @@ def main() -> None: while not do_exit: try: cloudlog.event("pandad.flash_and_connect", count=count) - if (count % 2) == 0: + if count == 0: HARDWARE.reset_internal_panda() - else: - HARDWARE.recover_internal_panda() count += 1 - # Flash all Pandas in DFU mode - for serial in PandaDFU.list(): - cloudlog.info(f"Panda in DFU mode found, flashing recovery {serial}") - PandaDFU(serial).recover() - time.sleep(1) + # The internal panda takes a few seconds to boot its app after a reset. + # Wait for it to come back in normal mode before deciding to flash. + panda_serials: list[str] = [] + for _ in range(16): + panda_serials = Panda.list() + if len(panda_serials) == 1: + try: + with Panda(panda_serials[0]) as p: + if not p.bootstub: + break + except Exception: + pass + time.sleep(0.5) + + if len(panda_serials) == 0: + # Flash all Pandas in DFU mode + for serial in PandaDFU.list(): + cloudlog.info(f"Panda in DFU mode found, flashing recovery {serial}") + PandaDFU(serial).recover() + time.sleep(1) + panda_serials = Panda.list() - panda_serials = Panda.list() if len(panda_serials): assert len(panda_serials) == 1 cloudlog.info(f"{len(panda_serials)} panda found, connecting - {panda_serials}")