From f4795ab9a64ada93b78bafd976e4bd78f7b76286 Mon Sep 17 00:00:00 2001 From: Colin Beighley Date: Wed, 9 Jan 2013 13:47:37 -0800 Subject: [PATCH] Cleaning up flashing stuff and making printing prettier --- scripts/flash.py | 116 ++++++++++++++++++++++++++++-------------- scripts/fpga_flash.py | 57 +++++++++------------ 2 files changed, 102 insertions(+), 71 deletions(-) diff --git a/scripts/flash.py b/scripts/flash.py index ddaea9ac..e4524e98 100644 --- a/scripts/flash.py +++ b/scripts/flash.py @@ -10,7 +10,7 @@ SECTORSIZE = 64*1024 FLASHSIZE = 1024*1024 #max number of flash operations to have pending in STM -PENDING_COMMANDS_LIMIT = 20 +PENDING_COMMANDS_LIMIT = 5 def roundup_multiple(x, multiple): return x if x % multiple == 0 else x + multiple - x % multiple @@ -51,41 +51,41 @@ def _flash_read_callback(self, data): self._read_callbacks_received += 1 addr = None length = None - try: - addr = struct.unpack('>I','\x00' + data[0:3])[0] - self.rd_cb_addrs.append(addr) - length = struct.unpack('B',data[3])[0] - self.rd_cb_lens.append(length) - self.rd_cb_data += list(struct.unpack(str(self.rd_cb_lens[-1]) + 'B',data[4:])) -# self.rd_cb_data += list(struct.unpack('20B',data[4:])) - if length != 16: - print "read_callbacks_received = ", self._read_callbacks_received - print ' addr =', addr - print ' length =', length - print ' len of data =', len(data) - print ' data =', [ord(i) for i in data] - print ' last_data =', [ord(i) for i in self.last_data] - print ' hex(addr) =', hex(addr) - print ' flash operations left =', self.flash_operations_left() - sys.exit() - except Exception: - print "read_callbacks_received = ", self._read_callbacks_received - print ' addr =', addr - print ' length =', length - print ' len of data =', len(data) - print ' data =', [ord(i) for i in data] - print ' last_data =', [ord(i) for i in self.last_data] - print ' hex(addr) =', hex(addr) - sys.exit() - if self._read_callbacks_received % 0x1000 == 0: - print "read_callbacks_received = ", self._read_callbacks_received - print ' addr =', addr - print ' length =', length - print ' len of data =', len(data) - print ' data =', [ord(i) for i in data] - print ' last_data =', [ord(i) for i in self.last_data] - print ' hex(addr) =', hex(addr) - self.last_data = data +# try: + addr = struct.unpack('>I','\x00' + data[0:3])[0] + self.rd_cb_addrs.append(addr) + length = struct.unpack('B',data[3])[0] + self.rd_cb_lens.append(length) + self.rd_cb_data += list(struct.unpack(str(self.rd_cb_lens[-1]) + 'B',data[4:])) +## self.rd_cb_data += list(struct.unpack('20B',data[4:])) +# if length != 16: +# print "read_callbacks_received = ", self._read_callbacks_received +# print ' addr =', addr +# print ' length =', length +# print ' len of data =', len(data) +# print ' data =', [ord(i) for i in data] +# print ' last_data =', [ord(i) for i in self.last_data] +# print ' hex(addr) =', hex(addr) +# print ' flash operations left =', self.flash_operations_left() +# sys.exit() +# except Exception: +# print "read_callbacks_received = ", self._read_callbacks_received +# print ' addr =', addr +# print ' length =', length +# print ' len of data =', len(data) +# print ' data =', [ord(i) for i in data] +# print ' last_data =', [ord(i) for i in self.last_data] +# print ' hex(addr) =', hex(addr) +# sys.exit() +# if self._read_callbacks_received % 0x1000 == 0: +# print "read_callbacks_received = ", self._read_callbacks_received +# print ' addr =', addr +# print ' length =', length +# print ' len of data =', len(data) +# print ' data =', [ord(i) for i in data] +# print ' last_data =', [ord(i) for i in self.last_data] +# print ' hex(addr) =', hex(addr) +# self.last_data = data def _acquire_flash(self): self._flash_ready.wait() @@ -99,6 +99,8 @@ def stop(self): def run(self): while not self._wants_to_exit: + while self.flash_operations_left() < 0: + raise Exception('self.flash_operations_left() returns less than 0') if (len(self._command_queue) > 0) and ((self._commands_sent - self._done_callbacks_received - self._read_callbacks_received) < PENDING_COMMANDS_LIMIT): cmd, args = self._command_queue[0] self._command_queue = self._command_queue[1:] @@ -152,15 +154,53 @@ def _write(self, addr, data): self._commands_sent += 1 self.link.send_message(0xF0, msg_header+data) -# def write_ihx(self, filename): -# self._schedule_command('_write_ihx', (filename,)) def write_ihx(self, filename): ihx = IntelHex(filename) + t1 = time.time() min_sector = rounddown_multiple(ihx.minaddr(), SECTORSIZE) max_sector = roundup_multiple(ihx.maxaddr(), SECTORSIZE) + + #Write hex file to flash + print "Writing hex to flash..." for addr in range(min_sector, max_sector, SECTORSIZE): self.erase_sector(addr) min_page = rounddown_multiple(ihx.minaddr(), 128) max_page = roundup_multiple(ihx.maxaddr(), 128) for addr in range(min_page, max_page, 128): self.write(addr, ihx.tobinstr(start=addr, size=128)) + ops_left = self.flash_operations_left() + while ops_left != 0: + print "\r ", + print "\rFlash operations left =", ops_left, + sys.stdout.flush() + time.sleep(0.2) + ops_left = self.flash_operations_left() + print "\r ", + print "\rFlash operations left =", ops_left + t2 = time.time() + print "Finished writing hex to flash, took %.2f seconds" % (t2 - t1) + + #Read bytes back from flash + print "Reading hex back from flash..." + self.read(0,ihx.maxaddr()) + ops_left = self.flash_operations_left() + while ops_left != 0: + print "\r ", + print "\rFlash operations left =", ops_left, + sys.stdout.flush() + time.sleep(0.2) + ops_left = self.flash_operations_left() + print "\r ", + print "\rFlash operations left =", ops_left + t3 = time.time() + print "Finished reading hex from flash, took %.2f seconds" % (t3 - t2) + + #Check that bytes read back from flash match hex file + print "Verifying that bytes read back from flash match hex file..." + self.read_cb_sanity_check() + if self.rd_cb_data != list(ihx.tobinarray(start=ihx.minaddr(), size=ihx.maxaddr()-ihx.minaddr())): + raise Exception('Data read from flash does not match configuration file') + print "Data from flash matches hex file, update successful" + + #Finished, report execution time + print "Total time was %.2f seconds" %(t3 - t1) diff --git a/scripts/fpga_flash.py b/scripts/fpga_flash.py index 8862c743..ab5de7d2 100755 --- a/scripts/fpga_flash.py +++ b/scripts/fpga_flash.py @@ -19,12 +19,13 @@ serial_port = args.port[0] flash_file = args.configuration_file[0] -#Check that ending address of hex file is greater than 16 bytes from -#the end of the flash - we use that space for the FPGA DNA hash +#Check that ending address of hex file is not in the last sector of the flash ihx = IntelHex(flash_file) print "Checking to make sure hex file's maximum address is not in last sector" -print " Maximum addresss =", hex(ihx.maxaddr()) -assert ihx.maxaddr() < (flash.FLASHSIZE-flash.SECTORSIZE), "Highest address in hexfile in in last sector" +print "We don't want to erase the device's authentication hash" +print " First address of flash's last sector =", hex(flash.FLASHSIZE-flash.SECTORSIZE) +print " Maximum address of hex file =", hex(ihx.maxaddr()) +assert ihx.maxaddr() < (flash.FLASHSIZE-flash.SECTORSIZE), "Maximum address in hex file is in last sector" #Create SerialLink and Flash objects to write to FPGA's flash print "Creating serial link and adding print callback..." @@ -35,42 +36,32 @@ piksi_flash.start() #Write configuration file to FPGA's flash -print "Writing hex file to FPGA's flash..." piksi_flash.write_ihx(flash_file) -#Wait for flash operations to finish -t1 = time.time() -try: - while piksi_flash.flash_operations_left() > 0: - time.sleep(0.1) -except KeyboardInterrupt: - pass -print "Finished writing hex file to device's flash, took %.2f seconds" % (time.time() - t1) - #Read back the configuration from the flash in order to validate -print "piksi_flash._read_callbacks_received =", piksi_flash._read_callbacks_received -print "Reading configuration from flash" -piksi_flash.read(0,ihx.maxaddr()) -while piksi_flash.flash_operations_left() > 0: - try: - time.sleep(0.1) - if link.unhandled_bytes > 0: - print "unhandled bytes =", link.unhandled_bytes - except KeyboardInterrupt: - break -print "piksi_flash._read_callbacks_received =", piksi_flash._read_callbacks_received -print "unhandled bytes received by serial link =", link.unhandled_bytes -print "piksi_flash.rd_cb_addrs[-1] =", hex(piksi_flash.rd_cb_addrs[-1]) -print "piksi_flash.rd_cb_addrs[-1] + piksi_flash.rd_cb_lens[-1] =", hex(piksi_flash.rd_cb_addrs[-1]+piksi_flash.rd_cb_lens[-1]) +#print "piksi_flash._read_callbacks_received =", piksi_flash._read_callbacks_received +#print "Reading configuration from flash" +#piksi_flash.read(0,ihx.maxaddr()) +#while piksi_flash.flash_operations_left() != 0: +# try: +# time.sleep(0.5) +# if link.unhandled_bytes > 0: +# print "unhandled bytes =", link.unhandled_bytes +# except KeyboardInterrupt: +# break +#print "piksi_flash._read_callbacks_received =", piksi_flash._read_callbacks_received +#print "unhandled bytes received by serial link =", link.unhandled_bytes +#print "piksi_flash.rd_cb_addrs[-1] =", hex(piksi_flash.rd_cb_addrs[-1]) +#print "piksi_flash.rd_cb_addrs[-1] + piksi_flash.rd_cb_lens[-1] =", hex(piksi_flash.rd_cb_addrs[-1]+piksi_flash.rd_cb_lens[-1]) #Make sure the starting address and length of each callback we received are continuous -piksi_flash.read_cb_sanity_check() -print "Sanity check passed" +#piksi_flash.read_cb_sanity_check() +#print "Sanity check passed" #Compare the data read out of the flash with the data in the hex file -if piksi_flash.rd_cb_data != list(ihx.tobinarray(start=ihx.minaddr(), size=ihx.maxaddr()-ihx.minaddr())): - raise Exception('Data read from flash does not match configuration file') -print "Data read from flash matches configuration file" +#if piksi_flash.rd_cb_data != list(ihx.tobinarray(start=ihx.minaddr(), size=ihx.maxaddr()-ihx.minaddr())): +# raise Exception('Data read from flash does not match configuration file') +#print "Data read from flash matches configuration file" #Clean up before exiting piksi_flash.stop()