def read_string(self, count = 1): """read_string Read a string of characters Args: count: the number of characters and returns a string if -1 read all characters Returns: string Raises: OlympusCommError: Error in communication """ if self.debug: print "read_string" data = Array('B') if count == -1: data = self.read_all_data() else: data = self.read_raw(count) string = data.tostring() return string
def read_string(self, count=1): """read_string Read a string of characters Args: count: the number of characters and returns a string if -1 read all characters Returns: string Raises: OlympusCommError: Error in communication """ if self.debug: print "read_string" data = Array('B') if count == -1: data = self.read_all_data() else: data = self.read_raw(count) string = data.tostring() return string
def serial_number(self): data = self.raw_data d = Array('B') for i in range(20, 38, 4): d.append(data[i + 2]) d.append(data[i + 3]) d.append(data[i + 0]) d.append(data[i + 1]) return d.tostring()
def serial_number(self): data = self.raw_data d = Array('B') for i in range (20, 38, 4): d.append(data[i + 2]) d.append(data[i + 3]) d.append(data[i + 0]) d.append(data[i + 1]) return d.tostring()
def _ping(self): self.dev.flush() self.dev.write(PING_CMD) self.log.debug("See if the device is there?") #data = Array('B', self.dev.read(4)) data = Array('B', self._read_response()) print "Data: %s" % str(data) if data.tostring()[1] == "S": self.log.debug("Ping Response Successful") return True else: self.log.debug("Ping Response Fail") return False
def test_rw_sector_1(self): from hashlib import sha1 buf = Array('I') # length = 65536 length = len(self.flash) print "length: " + str(length) print "Build Sequence" for address in range (0, length): buf.append(address) print "Swap sequence" buf = buf.byteswap() #print "Erase flash from %08X to %08X" % (0, length) print "Erase all of the flash" self.flash.erase(0, len(self.flash)) bufstr = buf.tostring() dout = Array('B') dout.fromstring(bufstr) self.flash.write(0, bufstr) print "Verify Flash" wmd = sha1() wmd.update(buf.tostring()) refdigest = wmd.hexdigest() print "Read Flash" din = self.flash.read(0, length) print "Dump Flash" print hexdump(din.tostring()) print "Verify Flash" rmd = sha1() rmd.update(din.tostring()) newdigest = rmd.hexdigest() print "Reference: ", refdigest print "Retrieved: ", newdigest try: f = open("din.hex", "w") din.tofile(f) f.close() except IOError, err: print "Error writing to din file"
def test_rw_sector_1(self): from hashlib import sha1 buf = Array('I') # length = 65536 length = len(self.flash) print "length: " + str(length) print "Build Sequence" for address in range(0, length): buf.append(address) print "Swap sequence" buf = buf.byteswap() #print "Erase flash from %08X to %08X" % (0, length) print "Erase all of the flash" self.flash.erase(0, len(self.flash)) bufstr = buf.tostring() dout = Array('B') dout.fromstring(bufstr) self.flash.write(0, bufstr) print "Verify Flash" wmd = sha1() wmd.update(buf.tostring()) refdigest = wmd.hexdigest() print "Read Flash" din = self.flash.read(0, length) print "Dump Flash" print hexdump(din.tostring()) print "Verify Flash" rmd = sha1() rmd.update(din.tostring()) newdigest = rmd.hexdigest() print "Reference: ", refdigest print "Retrieved: ", newdigest try: f = open("din.hex", "w") din.tofile(f) f.close() except IOError, err: print "Error writing to din file"
def _ping(self): self.dev.flush() self.dev.write(PING_CMD) self.log.debug("See if the device is there?") #data = Array('B', self.dev.read(4)) data = Array('B', self._read_response()) print "Data: %s" % str(data) if data.tostring()[1] == "S": self.log.debug("Ping Response Successful") return True else: self.log.debug("Ping Response Fail") return False
def test_flashdevice_long_rw(self): # Fill in the whole flash with a monotonic increasing value, that is # the current flash 32-bit address, then verify the sequence has been # properly read back from hashlib import sha1 buf = Array('I') length = len(self.flash) #length = 4096 print "Build sequence" for address in range(0, length, 4): buf.append(address) # Expect to run on x86 or ARM (little endian), so swap the values # to ease debugging # A cleaner test would verify the host endianess, or use struct module print "Swap sequence" buf.byteswap() print "Erase flash (may take a while...)" self.flash.erase(0, length) # Cannot use buf, as it's an I-array, and SPI expects a B-array bufstr = buf.tostring() print "Write flash", len(bufstr) self.flash.write(0, bufstr) wmd = sha1() wmd.update(buf.tostring()) refdigest = wmd.hexdigest() print "Read flash" data = self.flash.read(0, length) #print "Dump flash" #print hexdump(data.tostring()) print "Verify flash" rmd = sha1() rmd.update(data.tostring()) newdigest = rmd.hexdigest() print "Reference:", refdigest print "Retrieved:", newdigest if refdigest != newdigest: raise AssertionError('Data comparison mismatch')
def _write_program_data(self, address, data): start_address = address buf = Array('B', []) index = 0 #Size is maximum of 4096 finished = False write_len = 0 while True: if len(data[index:]) > 4096: buf = data[index:index + 4096] else: buf = data[index:] #if self.debug: print "Writing: %d bytes to address: 0x%X" % (len(buf), address) try: write_len = self.dev.ctrl_transfer( bmRequestType=0x40, #VRequest, to device, endpoint bRequest=0xA0, #Vendor Spcific write command wValue=0x0000FFFF & address, #Addr Low 16-bit value wIndex=address >> 16, #Addr High 16-bit value data_or_wLength=buf.tostring(), #Data timeout=1000) #Timeout 1 second except usb.core.USBError, err: raise PrometheusUSBError("Error while programming MCU: %s" % str(err)) #Check if there was an error in the transfer if write_len != len(buf): raise PrometheusUSBError( "Write Size != Length of buffer: %d != %d" % (write_len, len(buf))) #Update the index index += write_len address += write_len #Check if we wrote all the data out if index >= len(data): #We're done #if self.debug: print "Sent: %d bytes to address %d" % (len(data), start_address) break
def upload_fpga_image(self, bit_buf): max_size = 512 if self.dev is None: raise USBDeviceError("Device is None") bit_buf_length = len(bit_buf) length_buf = Array('B', [0, 0, 0, 0]) length_buf[3] = (bit_buf_length >> 24) & 0x000000FF length_buf[2] = (bit_buf_length >> 16) & 0x000000FF length_buf[1] = (bit_buf_length >> 8) & 0x000000FF length_buf[0] = (bit_buf_length) & 0x000000FF print "bit buf packets [3] [2] [1] [0]: %X %X %X %X" % (length_buf[3], length_buf[2], length_buf[1], length_buf[0]) print "Length: (Hex): 0x%08X, (Dec): %d" % (bit_buf_length, bit_buf_length) with self.usb_lock: try: self.dev.ctrl_transfer( bmRequestType = 0x40, #VRequest, To the devce, Endpoint bRequest = 0xB2, #FPGA Configuration mode wValue = 0x00, wIndex = 0x00, data_or_wLength = length_buf.tostring(), timeout = 1000) #Timeout = 1 second except usb.core.USBError, err: if err.errno == 110: raise USBDeviceError("Device Timed Out while attempting to send FPGA Config") if err.errno == 5: self.usb_server.update_usb() raise USBDeviceError("Device was disconnected") if err.errno == 16: self.usb_server.update_usb() raise USBDeviceError("Device was disconnected") else: raise USBDeviceError("Unknown USB Device Error: %s" % str(err))
def write_program_data(self, address, data): print "Write data to the device" start_address = address buf = Array('B', []) index = 0 #Size is maximum of 4096 finished = False write_len = 0 while True: if len(data[index:]) > 4096: buf = data[index: index+ 4096] else: buf = data[index:] print "Writing: %d bytes to address: 0x%X" % (len(buf), address) try: write_len = self.dev.ctrl_transfer( bmRequestType = 0x40, #VRequest, to device, endpoint bRequest = 0xA0, #Vendor Spcific write command wValue = 0x0000FFFF & address, #Addr Low 16-bit value wIndex = address >> 16, #Addr High 16-bit value data_or_wLength = buf.tostring(), #Data timeout = 1000) #Timeout 1 second except usb.core.USBError, err: pass #Check if there was an error in the transfer if write_len != len(buf): raise BootFX3Error("Write Size != Length of buffer") #Update the index index += write_len address += write_len #Check if we wrote all the data out if index >= len(data): #We're done print "Sent: %d bytes to address %d" % (len(data), start_address) break
def program_fpga(self, filepath): if not self.is_prometheus_attached(): raise PrometheusUSBError("Prometheus is not attached") self.connect_to_prometheus() f = open(filepath, 'r') buf = Array('B') buf.fromstring(f.read()) f.close() max_size = 512 length = len(buf) flength = float(length) length_buf = Array('B', [0, 0, 0, 0]) length_buf[3] = (length >> 24) & 0xFF length_buf[2] = (length >> 16) & 0xFF length_buf[1] = (length >> 8) & 0xFF length_buf[0] = (length) & 0xFF if self.debug: print "FPGA Image size: 0x%08X (%d)" % (length, length) self._write_mcu_config(CMD_ENTER_FPGA_CONFIG_MODE, length_buf.tostring()) if self.debug: print "Wait for a moment..." time.sleep(0.1) count = 0 while len(buf) > max_size: block = buf[:max_size] try: self.dev.write(0x02, block, timeout=self.timeout) except usb.core.USBError, err: raise PrometheusUSBError(err) if self.debug: sys.stdout.write("\r%%% 3.1f" % (100 * count / flength)) if self.debug: sys.stdout.flush() buf = buf[max_size:] count += max_size
def read_string(self, count=-1): """read_string Read a string of characters Args: count: the number of characters and returns a string if -1 read all characters Returns: string Raises: NysaCommError: Error in communication """ if self.debug: print "read_string: Read count: %d" % count #print "count: %d" % count data = Array('B') if count == -1: data = self.read_all_data() else: data = self.read_raw(count) print "read_string: returned data: %s" % data #byte_data = Array('B') #for i in range (len(data) / 4): # byte_data.append(data[i * 4]) #print "\tread_string: data: %s" % byte_data print "\tread_string: data: %s" % str(data) #string = byte_data.tostring() string = data.tostring() return string
def test_flashdevice_4_long_rw(self): """Long R/W test """ # Max size to perform the test on size = 1 << 20 # Whether to test with random value, or contiguous values to ease debug randomize = True # Fill in the whole flash with a monotonic increasing value, that is # the current flash 32-bit address, then verify the sequence has been # properly read back from hashlib import sha1 # limit the test to 1MiB to keep the test duration short, but performs # test at the end of the flash to verify that high addresses may be # reached length = min(len(self.flash), size) start = len(self.flash) - length print_("Erase %s from flash @ 0x%06x (may take a while...)" % (pretty_size(length), start)) delta = time.time() self.flash.unlock() self.flash.erase(start, length, True) delta = time.time() - delta self._report_bw('Erased', length, delta) if str(self.flash).startswith('SST'): # SST25 flash devices are tremendously slow at writing (one or two # bytes per SPI request MAX...). So keep the test sequence short # enough length = 16 << 10 print_("Build test sequence") if not randomize: buf = Array('I') back = Array('I') for address in range(0, length, 4): buf.append(address) # Expect to run on x86 or ARM (little endian), so swap the values # to ease debugging # A cleaner test would verify the host endianess, or use struct # module buf.byteswap() # Cannot use buf directly, as it's an I-array, # and SPI expects a B-array else: from random import seed seed(0) buf = Array('B') back = Array('B') buf.extend((randint(0, 255) for _ in range(0, length))) bufstr = buf.tostring() print_("Writing %s to flash (may take a while...)" % pretty_size(len(bufstr))) delta = time.time() self.flash.write(start, bufstr) delta = time.time() - delta length = len(bufstr) self._report_bw('Wrote', length, delta) wmd = sha1() wmd.update(buf.tostring()) refdigest = wmd.hexdigest() print_("Reading %s from flash" % pretty_size(length)) delta = time.time() data = self.flash.read(start, length) delta = time.time() - delta self._report_bw('Read', length, delta) # print "Dump flash" # print hexdump(data.tostring()) print_("Verify flash") rmd = sha1() rmd.update(data.tostring()) newdigest = rmd.hexdigest() print_("Reference:", refdigest) print_("Retrieved:", newdigest) if refdigest != newdigest: errcount = 0 back.fromstring(data) for pos in range(len(buf)): if buf[pos] != data[pos]: print_('Invalid byte @ offset 0x%06x: 0x%02x / 0x%02x' % (pos, buf[pos], back[pos])) errcount += 1 # Stop report after 16 errors if errcount >= 32: break raise self.fail('Data comparison mismatch')
def test_flashdevice_4_long_rw(self): """Long R/W test """ # Max size to perform the test on size = 1<<20 # Whether to test with random value, or contiguous values to ease debug randomize = True # Fill in the whole flash with a monotonic increasing value, that is # the current flash 32-bit address, then verify the sequence has been # properly read back from hashlib import sha1 # limit the test to 1MiB to keep the test duration short, but performs # test at the end of the flash to verify that high addresses may be # reached length = min(len(self.flash), size) start = len(self.flash)-length print "Erase %s from flash @ 0x%06x(may take a while...)" % \ (pretty_size(length), start) delta = time.time() self.flash.unlock() self.flash.erase(start, length, True) delta = time.time()-delta self._report_bw('Erased', length, delta) if str(self.flash).startswith('SST'): # SST25 flash devices are tremendously slow at writing (one or two # bytes per SPI request MAX...). So keep the test sequence short # enough length = 16<<10 print "Build test sequence" if not randomize: buf = Array('I') back = Array('I') for address in range(0, length, 4): buf.append(address) # Expect to run on x86 or ARM (little endian), so swap the values # to ease debugging # A cleaner test would verify the host endianess, or use struct # module buf.byteswap() # Cannot use buf directly, as it's an I-array, # and SPI expects a B-array else: from random import seed seed(0) buf = Array('B') back = Array('B') buf.extend((randint(0, 255) for x in range(0, length))) bufstr = buf.tostring() print "Writing %s to flash (may take a while...)" % \ pretty_size(len(bufstr)) delta = time.time() self.flash.write(start, bufstr) delta = time.time()-delta length = len(bufstr) self._report_bw('Wrote', length, delta) wmd = sha1() wmd.update(buf.tostring()) refdigest = wmd.hexdigest() print "Reading %s from flash" % pretty_size(length) delta = time.time() data = self.flash.read(start, length) delta = time.time()-delta self._report_bw('Read', length, delta) #print "Dump flash" #print hexdump(data.tostring()) print "Verify flash" rmd = sha1() rmd.update(data.tostring()) newdigest = rmd.hexdigest() print "Reference:", refdigest print "Retrieved:", newdigest if refdigest != newdigest: errcount = 0 back.fromstring(data) for pos in xrange(len(buf)): if buf[pos] != data[pos]: print 'Invalid byte @ offset 0x%06x: 0x%02x / 0x%02x' % \ (pos, buf[pos], back[pos]) errcount += 1 # Stop report after 16 errors if errcount >= 32: break raise AssertionError('Data comparison mismatch')
def test_flashdevice_4_long_rw(self): """Long R/W test """ # Fill in the whole flash with a monotonic increasing value, that is # the current flash 32-bit address, then verify the sequence has been # properly read back from hashlib import sha1 buf = Array('B') # limit the test to 1MiB to keep the test duration short, but performs # test at the end of the flash to verify that high addresses may be # reached length = min(len(self.flash), 1<<20) start = len(self.flash)-length print "Erase %s from flash (may take a while...)" % \ pretty_size(length) delta = time.time() self.flash.unlock() self.flash.erase(start, length, True) delta = time.time()-delta self._report_bw('Erased', length, delta) if str(self.flash).startswith('SST'): # SST25 flash devices are tremendously slow at writing (one or two # bytes per SPI request MAX...). So keep the test sequence short # enough length = 16<<10 print "Build test sequence" buf.extend((randint(0, 255) for x in range(0, length))) # for address in range(0, length, 4): # buf.append(address) # # Expect to run on x86 or ARM (little endian), so swap the values # # to ease debugging # # A cleaner test would verify the host endianess, or use struct module # print "Swap sequence" # buf.byteswap() # Cannot use buf, as it's an I-array, and SPI expects a B-array bufstr = buf.tostring() print "Writing %s to flash (may take a while...)" % \ pretty_size(len(bufstr)) delta = time.time() self.flash.write(start, bufstr) delta = time.time()-delta length = len(bufstr) self._report_bw('Wrote', length, delta) wmd = sha1() wmd.update(buf.tostring()) refdigest = wmd.hexdigest() print "Reading %s from flash" % pretty_size(length) delta = time.time() data = self.flash.read(start, length) delta = time.time()-delta self._report_bw('Read', length, delta) #print "Dump flash" #print hexdump(data.tostring()) print "Verify flash" rmd = sha1() rmd.update(data.tostring()) newdigest = rmd.hexdigest() print "Reference:", refdigest print "Retrieved:", newdigest if refdigest != newdigest: raise AssertionError('Data comparison mismatch')