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)
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()
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")
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()
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()
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)