Beispiel #1
0
    def test_jump_to_app(self):
        """ Test that we can jump to the application after programming. """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        if self.verbose: print "--- test_jump_to_app ---"

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                with Bootloader(handler) as piksi_bootloader:
                    if self.verbose: print "Handshaking with bootloader"
                    piksi_bootloader.handshake()
                    if self.verbose: print "Jumping to application"
                    piksi_bootloader.jump_to_app()

                # If we succesfully jump to the application, we should receive
                # Heartbeat messages.
                with Timeout(TIMEOUT_BOOT) as timeout:

                    heartbeat = Heartbeat()
                    handler.add_callback(heartbeat, SBP_MSG_HEARTBEAT)

                    if self.verbose: print "Waiting to receive heartbeat"
                    while not heartbeat.received:
                        time.sleep(0.1)
                    if self.verbose: print "Received hearbeat"

                    handler.remove_callback(heartbeat, SBP_MSG_HEARTBEAT)
Beispiel #2
0
    def test_flash_nap_firmware(self):
        """ Test flashing NAP hexfile. """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        if self.verbose: print "--- test_flash_nap_firmware ---"

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                with Bootloader(handler) as piksi_bootloader:

                    with Timeout(TIMEOUT_BOOT) as timeout:
                        if self.verbose:
                            print "Waiting for bootloader handshake"
                        piksi_bootloader.handshake()
                        if self.verbose: print "Received bootloader handshake"

                    with Flash(handler,
                               flash_type='M25',
                               sbp_version=piksi_bootloader.sbp_version
                               ) as piksi_flash:
                        with Timeout(TIMEOUT_WRITE_NAP) as timeout:
                            if self.verbose:
                                print "Writing firmware to NAP flash"
                                piksi_flash.write_ihx(self.nap_fw,
                                                      sys.stdout,
                                                      mod_print=0x10)
                            else:
                                piksi_flash.write_ihx(self.nap_fw)
Beispiel #3
0
def set_app_mode(handler, verbose=False):
    from piksi_tools.bootload import Bootloader
    """
  Set Piksi into the application firmware, regardless of whether it is
  currently in the application firmware or the bootloader. Will raise a
  piksi_tools.timeout.TimeoutError if Piksi responses appear to have hung.

  Parameters
  ==========
  handler : sbp.client.handler.Handler
    handler to send/receive messages from/to Piksi.
  verbose : bool
    Print more verbose output.

  """

    if verbose: print("Setting device into application mode")

    # Wait until we receive a heartbeat or bootloader handshake so we
    # know what state Piksi is in.
    with Bootloader(handler) as piksi_bootloader:

        heartbeat = Heartbeat()
        handler.add_callback(heartbeat, SBP_MSG_HEARTBEAT)

        if verbose:
            print("Waiting for bootloader handshake or heartbeat from device")
        with Timeout(TIMEOUT_BOOT) as timeout:
            while not heartbeat.received and not piksi_bootloader.handshake_received:
                time.sleep(0.1)

        handler.remove_callback(heartbeat, SBP_MSG_HEARTBEAT)

        # If Piksi is in the application, simply return.
        if heartbeat.received:
            if verbose: print("Received heartbeat")
            return

        # Piksi is in the bootloader, tell Piksi to jump into the application.
        with Timeout(TIMEOUT_BOOT) as timeout:
            if verbose: print("Waiting for bootloader handshake from device")
            piksi_bootloader.handshake()
        piksi_bootloader.jump_to_app()
        if verbose: print("Received handshake")
        if verbose: print("Telling device to jump to application")

        # Wait for Heartbeat to ensure we're in the application firmware.
        heartbeat = Heartbeat()
        handler.add_callback(heartbeat, SBP_MSG_HEARTBEAT)

        if verbose: print("Waiting for heartbeat")
        with Timeout(TIMEOUT_BOOT) as timeout:
            while not heartbeat.received:
                time.sleep(0.1)
        if verbose: print("Received heartbeat")

        handler.remove_callback(heartbeat, SBP_MSG_HEARTBEAT)
    def test_btldr_handshake_wrong_sender_id(self):
        """
    Test setting Piksi into bootloader mode with an incorrect sender ID
    (should fail).
    """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        if self.verbose: print("--- test_btldr_handshake_wrong_sender_id ---")

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                # Make sure device is in the application firmware.
                set_app_mode(handler, self.verbose)

                # Reset Piksi, and attempt to handshake into bootloader mode with an
                # incorrect sender ID.
                if self.verbose: print("Sending reset")
                handler.send(SBP_MSG_RESET, "")

                with Bootloader(handler) as piksi_bootloader:
                    with Timeout(TIMEOUT_BOOT) as timeout:
                        if self.verbose:
                            print(
                                "Waiting for bootloader handshake from device")
                        while not piksi_bootloader.handshake_received:
                            time.sleep(0.1)
                if self.verbose: print("Received handshake")
                if self.verbose:
                    print("Sending handshake with incorrect sender ID")
                handler.send(SBP_MSG_BOOTLOADER_HANDSHAKE_REQ,
                             '\x00',
                             sender=0x41)

                # We should receive a heartbeat if the handshake was unsuccessful.
                with Timeout(TIMEOUT_BOOT) as timeout:

                    heartbeat = Heartbeat()
                    handler.add_callback(heartbeat, SBP_MSG_HEARTBEAT)

                    if self.verbose: print("Waiting to receive heartbeat")
                    while not heartbeat.received:
                        time.sleep(0.1)
                    if self.verbose: print("Received hearbeat")

                    handler.remove_callback(heartbeat, SBP_MSG_HEARTBEAT)
Beispiel #5
0
    def test_program_btldr(self):
        """ Test programming the bootloader once its sector is locked. """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        SECTOR = 0
        ADDRESS = 0x08003FFF

        if self.verbose: print "--- test_program_btldr ---"

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                with Bootloader(handler) as piksi_bootloader:
                    with Timeout(TIMEOUT_BOOT) as timeout:
                        if self.verbose:
                            print "Waiting for bootloader handshake"
                        piksi_bootloader.handshake()
                    with Flash(handler,
                               flash_type='STM',
                               sbp_version=piksi_bootloader.sbp_version
                               ) as piksi_flash:
                        # Make sure the bootloader sector is locked.
                        with Timeout(TIMEOUT_LOCK_SECTOR) as timeout:
                            if self.verbose:
                                print "Locking STM sector:", SECTOR
                            piksi_flash.lock_sector(SECTOR)
                        # Make sure the address to test isn't already programmed.
                        with Timeout(TIMEOUT_READ_STM) as timeout:
                            byte_read = piksi_flash.read(ADDRESS,
                                                         1,
                                                         block=True)
                        self.assertEqual(
                            '\xFF', byte_read,
                            "Address to program is already programmed")
                        # Attempt to write 0x00 to last address of the sector.
                        if self.verbose:
                            print "Attempting to lock STM sector:", SECTOR
                        piksi_flash.program(0x08003FFF, '\x00')
                        with Timeout(TIMEOUT_READ_STM) as timeout:
                            byte_read = piksi_flash.read(0x08003FFF,
                                                         1,
                                                         block=True)
                        self.assertEqual('\xFF', byte_read,
                                         "Bootloader sector was programmed")
def main():
    """
    Get configuration, get driver, build handler, and unlock sectors.
    """
    args = get_args()
    port = args.port[0]
    baud = args.baud[0]

    # Driver with context
    with serial_link.get_driver(use_ftdi=False, port=port,
                                baud=baud) as driver:
        # Handler with context
        with Handler(driver.read, driver.write) as link:
            with Bootloader(link) as piksi_bootloader:
                print(
                    "Waiting for bootloader handshake message from Piksi ...",
                    end=' ')
                sys.stdout.flush()
                try:
                    piksi_bootloader.handshake()
                except KeyboardInterrupt:
                    return
                print("received.")
                print("Piksi Onboard Bootloader Version:",
                      piksi_bootloader.version)
                if piksi_bootloader.sbp_version > (0, 0):
                    print("Piksi Onboard SBP Protocol Version:",
                          piksi_bootloader.sbp_version)

                # Catch all other errors and exit cleanly.
                try:
                    with Flash(link,
                               flash_type="STM",
                               sbp_version=piksi_bootloader.sbp_version
                               ) as piksi_flash:
                        for s in range(0, 12):
                            print("\rUnlocking STM Sector", s, end=' ')
                            sys.stdout.flush()
                            piksi_flash.unlock_sector(s)
                        print()
                except:
                    import traceback
                    traceback.print_exc()
Beispiel #7
0
    def test_btldr_handshake(self):
        """ Test setting Piksi into bootloader mode. """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        if self.verbose: print "--- test_btldr_handshake ---"

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                with Bootloader(handler) as piksi_bootloader:
                    # If the Piksi bootloader successfully received our handshake, we
                    # should be able to receive handshakes from it indefinitely. Test
                    # this a few times.
                    if self.verbose: print "Setting Piksi into bootloader mode"
                    with Timeout(TIMEOUT_BOOT) as timeout:
                        piksi_bootloader.handshake()
                    if self.verbose:
                        print "Testing bootloader handshake replies"
                    for i in range(10):
                        time.sleep(1)
                        with Timeout(TIMEOUT_BOOT) as timeout:
                            piksi_bootloader.handshake()
Beispiel #8
0
    def test_flash_stm_firmware(self):
        """ Test flashing STM hexfile. """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        if self.verbose: print "--- test_flash_stm_firmware ---"

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                # Wait until we receive a heartbeat or bootloader handshake so we
                # know what state Piksi is in.
                with Bootloader(handler) as piksi_bootloader:

                    with Timeout(TIMEOUT_BOOT) as timeout:
                        if self.verbose:
                            print "Waiting for bootloader handshake"
                        piksi_bootloader.handshake()
                        if self.verbose: print "Received bootloader handshake"

                    with Flash(handler,
                               flash_type="STM",
                               sbp_version=piksi_bootloader.sbp_version
                               ) as piksi_flash:
                        # Erase entire STM flash (except bootloader).
                        if self.verbose: print "Erasing STM"
                        with Timeout(TIMEOUT_ERASE_STM) as timeout:
                            for s in range(1, 12):
                                piksi_flash.erase_sector(s)
                        # Write STM firmware.
                        with Timeout(TIMEOUT_PROGRAM_STM) as timeout:
                            if self.verbose:
                                if self.verbose: print "Programming STM"
                                piksi_flash.write_ihx(self.stm_fw,
                                                      sys.stdout,
                                                      0x10,
                                                      erase=False)
                            else:
                                piksi_flash.write_ihx(self.stm_fw, erase=False)
    def test_flashing_wrong_sender_id(self):
        """ Test flashing using an incorrect sender ID (should fail). """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        SECTOR = 1
        SENDER_ID = 0x41

        if self.verbose: print("--- test_flashing_wrong_sender_id ---")

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                # Verify that flash erase times out when using incorrect sender ID.
                with Bootloader(handler) as piksi_bootloader:
                    with Timeout(TIMEOUT_BOOT) as timeout:
                        print("Handshaking with bootloader")
                        piksi_bootloader.handshake()
                    with Flash(handler,
                               flash_type='STM',
                               sbp_version=piksi_bootloader.sbp_version
                               ) as piksi_flash:
                        try:
                            with Timeout(TIMEOUT_ERASE_SECTOR) as timeout:
                                if self.verbose:
                                    print(
                                        "Attempting to erase sector with incorrect sender ID"
                                    )
                                msg_buf = struct.pack(
                                    "BB", piksi_flash.flash_type_byte, SECTOR)
                                handler.send(SBP_MSG_FLASH_ERASE,
                                             msg_buf,
                                             sender=SENDER_ID)
                                handler.wait(SBP_MSG_FLASH_DONE,
                                             TIMEOUT_ERASE_SECTOR + 1)
                                raise Exception(
                                    "Should have timed out but didn't")
                        except TimeoutError:
                            if self.verbose: print("Timed out as expected")
Beispiel #10
0
    def test_erase_btldr(self):
        """ Test erasing the bootloader once its sector is locked. """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        SECTOR = 0

        if self.verbose: print("--- test_erase_btldr ---")

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                with Bootloader(handler) as piksi_bootloader:
                    with Timeout(TIMEOUT_BOOT) as timeout:
                        if self.verbose:
                            print("Waiting for bootloader handshake")
                        piksi_bootloader.handshake()
                    with Flash(handler,
                               flash_type='STM',
                               sbp_version=piksi_bootloader.sbp_version
                               ) as piksi_flash:
                        # Make sure the bootloader sector is locked.
                        with Timeout(TIMEOUT_LOCK_SECTOR) as timeout:
                            if self.verbose:
                                print("Locking STM sector:", SECTOR)
                            piksi_flash.lock_sector(SECTOR)
                        # Attempt to erase the sector.
                        with Timeout(TIMEOUT_ERASE_SECTOR) as timeout:
                            if self.verbose:
                                print("Attempting to erase STM sector:",
                                      SECTOR)
                            piksi_flash.erase_sector(SECTOR, warn=False)
                        # If the sector was successfully erased, we should timeout here
                        # as the bootloader will stop sending handshakes.
                        with Timeout(TIMEOUT_BOOT) as timeout:
                            if self.verbose:
                                print("Waiting for bootloader handshake")
                            piksi_bootloader.handshake()
Beispiel #11
0
    def test_get_versions(self):
        """ Get Piksi bootloader/firmware/NAP version from device. """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        if self.verbose: print("--- test_get_versions ---")

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                with Bootloader(handler) as piksi_bootloader:

                    # Get bootloader version, print, and jump to application firmware.
                    with Timeout(TIMEOUT_BOOT) as timeout:
                        if self.verbose:
                            print("Waiting for bootloader handshake")
                        piksi_bootloader.handshake()
                    piksi_bootloader.jump_to_app()
                    print("Piksi Bootloader Version:",
                          piksi_bootloader.version)

                # Wait for heartbeat, get settings, print firmware/NAP versions.
                heartbeat = Heartbeat()
                handler.add_callback(heartbeat, SBP_MSG_HEARTBEAT)
                if self.verbose: print("Waiting to receive heartbeat")
                while not heartbeat.received:
                    time.sleep(0.1)
                if self.verbose: print("Received hearbeat")
                handler.remove_callback(heartbeat, SBP_MSG_HEARTBEAT)

                if self.verbose: print("Getting Piksi settings")
                settings = self.get_piksi_settings(handler)

                if self.verbose:                    print("Piksi Firmware Version:", \
                     settings['system_info']['firmware_version'])

                if self.verbose:                    print("Piksi NAP Version:", \
                     settings['system_info']['nap_version'])
Beispiel #12
0
def setup_piksi(handler, stm_fw, nap_fw, verbose=False):
    """
  Set Piksi into a known state (STM / NAP firmware). Erases entire STM flash
  (except for bootloader sector). Requires Piksi have a valid STM firmware
  sending heartbeat messages and with the reset callback registered. Will raise
  a timeout.TimeoutError if Piksi responses appear to have hung.

  Parameters
  ==========
  handler : sbp.client.handler.Handler
    handler to send/receive SBP messages to/from Piksi through.
  stm_fw : intelhex.IntelHex
    firmware to program Piksi STM with.
  nap_fw : intelhex.IntelHex
    firmware to program Piksi NAP with.
  verbose : bool
    Print more verbose output.

  """

    # Wait until we receive a heartbeat or bootloader handshake so we
    # know what state Piksi is in.
    with Bootloader(handler) as piksi_bootloader:

        heartbeat = Heartbeat()
        handler.add_callback(heartbeat, SBP_MSG_HEARTBEAT)

        # Throw an exception if a heartbeat or handshake
        # is not received for 5 seconds.
        with Timeout(TIMEOUT_BOOT) as timeout:
            if verbose: print "Waiting for Heartbeat or Bootloader Handshake"
            while not heartbeat.received and not piksi_bootloader.handshake_received:
                time.sleep(0.1)
        # If Piksi is in the application, reset it into the bootloader.
        if heartbeat.received:
            if verbose: print "Received Heartbeat, resetting Piksi"
            handler.send(SBP_MSG_RESET, "")

        handler.remove_callback(heartbeat, SBP_MSG_HEARTBEAT)

        with Timeout(TIMEOUT_BOOT) as timeout:
            piksi_bootloader.handshake()
        bootloader_version = piksi_bootloader.version
        if verbose: print "Received bootloader handshake"

        with Flash(handler,
                   flash_type="STM",
                   sbp_version=piksi_bootloader.sbp_version) as piksi_flash:
            # Erase entire STM flash (except bootloader).
            if verbose: print "Erasing STM"
            with Timeout(TIMEOUT_ERASE_STM) as timeout:
                for s in range(1, 12):
                    piksi_flash.erase_sector(s)
            # Write STM firmware.
            with Timeout(TIMEOUT_PROGRAM_STM) as timeout:
                if verbose:
                    if verbose: print "Programming STM"
                    piksi_flash.write_ihx(stm_fw,
                                          sys.stdout,
                                          0x10,
                                          erase=False)
                else:
                    piksi_flash.write_ihx(stm_fw, erase=False)

        with Flash(handler,
                   flash_type="M25",
                   sbp_version=piksi_bootloader.sbp_version) as piksi_flash:
            # Write NAP hexfile.
            with Timeout(TIMEOUT_WRITE_NAP) as timeout:
                if verbose:
                    if verbose: print "Programming NAP"
                    piksi_flash.write_ihx(nap_fw, sys.stdout, 0x10)
                else:
                    piksi_flash.write_ihx(nap_fw)

        # Jump to the application firmware.
        if verbose: print "Jumping to application"
        piksi_bootloader.jump_to_app()
Beispiel #13
0
    def test_sector_lock_unlock(self):
        """ Test if we can lock / unlock sectors. """
        unittest.skipIf(self.skip_single, 'Skipping single Piksi tests')

        SECTOR = 1
        ADDRESS = 0x08004000

        if self.verbose: print "--- test_sector_lock_unlock ---"

        with serial_link.get_driver(use_ftdi=False, port=self.port1) as driver:
            with Handler(driver.read, driver.write) as handler:

                with Bootloader(handler) as piksi_bootloader:
                    with Timeout(TIMEOUT_BOOT) as timeout:
                        if self.verbose:
                            print "Waiting for bootloader handshake"
                        piksi_bootloader.handshake()
                    if self.verbose: print "Handshaked with bootloader"

                    with Flash(handler,
                               flash_type='STM',
                               sbp_version=piksi_bootloader.sbp_version
                               ) as piksi_flash:

                        try:
                            # Erase the sector, lock it, and attempt to write to it.
                            with Timeout(TIMEOUT_ERASE_SECTOR) as timeout:
                                if self.verbose:
                                    print "Erasing STM sector:", SECTOR
                                piksi_flash.erase_sector(SECTOR)

                            with Timeout(TIMEOUT_LOCK_SECTOR) as timeout:
                                if self.verbose:
                                    print "Locking STM sector:", SECTOR
                                piksi_flash.lock_sector(SECTOR)
                            if self.verbose:
                                print "Attempting to program address:", hex(
                                    ADDRESS)
                            piksi_flash.program(ADDRESS, '\x00')
                            with Timeout(TIMEOUT_READ_STM) as timeout:
                                byte_read = piksi_flash.read(ADDRESS,
                                                             1,
                                                             block=True)
                            self.assertEqual('\xFF', byte_read, \
                                             "Address was programmed")
                            if self.verbose: print "Program failed as expected"

                            # Unlock the sector, and attempt to write to it.
                            with Timeout(TIMEOUT_LOCK_SECTOR) as timeout:
                                if self.verbose:
                                    print "Unlocking STM sector:", SECTOR
                                piksi_flash.unlock_sector(SECTOR)
                            if self.verbose:
                                print "Attempting to program address:", hex(
                                    ADDRESS)
                            piksi_flash.program(ADDRESS, '\x00')
                            with Timeout(TIMEOUT_READ_STM) as timeout:
                                byte_read = piksi_flash.read(ADDRESS,
                                                             1,
                                                             block=True)
                            self.assertEqual('\x00', byte_read, \
                                             "Address was not programmed")
                            if self.verbose:
                                print "Program was successful as expected"

                        except Exception:
                            # If all else fails, make sure we unlock
                            # the sector before leaving this test.
                            with Timeout(TIMEOUT_LOCK_SECTOR) as timeout:
                                if self.verbose:
                                    print "Had exception, unlocking STM sector:", SECTOR
                                piksi_flash.unlock_sector(SECTOR)
                            raise

                        # Clean up - write valid STM firmware over sector that was erased.
                        with Timeout(TIMEOUT_WRITE_STM) as timeout:
                            if self.verbose:
                                print "Cleaning up, writing firmware to STM flash"
                                piksi_flash.write_ihx(self.stm_fw,
                                                      sys.stdout,
                                                      mod_print=0x10)
                            else:
                                piksi_flash.write_ihx(self.stm_fw)