Example #1
0
 def setUp(self):
     # FTDI device should be tweak to your actual setup
     self.flash = SerialFlashManager.get_flash_device(0x403, 0x6010, 1)
Example #2
0
class SerialFlashTestCase(unittest.TestCase):

    def setUp(self):
        self.manager = SerialFlashManager(0x403, 0x6010, 1)
        self.flash = self.manager.get_flash_device()

    def tearDown(self):
        del self.flash
        del self.manager

    def test_flashdevice_name(self):
        print "Flash device: %s" % self.flash

    def test_flashdevice_read_bandwidth(self):
        print "Start reading the whole device..."
        delta = time.time()
        data = self.flash.read(0, len(self.flash))
        delta = time.time()-delta
        length = len(data)
        print "%d bytes in %d seconds @ %.1f KB/s" % \
            (length, delta, length/(1024.0*delta))

    def test_flashdevice_small_rw(self):
        self.flash.erase(0x007000, 4096)
        data = self.flash.read(0x007020, 128)
        ref = Array('B', [0xff] * 128)
        self.assertEqual(data, ref)
        string = 'This is a serial SPI flash test'
        ref2 = Array('B', string)
        self.flash.write(0x007020, string)
        data = self.flash.read(0x007020, 128)
        ref2.extend(ref)
        ref2 = ref2[:128]
        self.assertEqual(data, ref2)

    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')
Example #3
0
 def setUp(self):
     self.manager = SerialFlashManager(0x403, 0x6010, 1)
     self.flash = self.manager.get_flash_device()
Example #4
0
class SerialFlashTestCase(unittest.TestCase):

    def setUp(self):
        self.manager = SerialFlashManager(0x403, 0x6010, 1)
        self.flash = self.manager.get_flash_device()

    def tearDown(self):
        del self.flash
        del self.manager

    def test_flashdevice_1_name(self):
        """Retrive device name
        """
        print "Flash device: %s" % self.flash

    def test_flashdevice_2_read_bandwidth(self):
        """Read the whole device to get READ bandwith
        """
        delta = time.time()
        data = self.flash.read(0, len(self.flash))
        delta = time.time()-delta
        length = len(data)
        self._report_bw('Read', length, delta)

    def test_flashdevice_3_small_rw(self):
        """Short R/W test
        """
        self.flash.unlock()
        self.flash.erase(0x007000, 4096)
        data = self.flash.read(0x007020, 128)
        ref = Array('B', [0xff] * 128)
        self.assertEqual(data, ref)
        string = 'This is a serial SPI flash test.'
        ref2 = Array('B', string)
        self.flash.write(0x007020, string)
        data = self.flash.read(0x007020, 128)
        ref2.extend(ref)
        ref2 = ref2[:128]
        self.assertEqual(data, ref2)

    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')

    @classmethod
    def _report_bw(cls, action, length, time):
        if time < 1.0:
            print "%s %s in %d ms @ %s/s" % (action, pretty_size(length),
                1000*time, pretty_size(length/time))
        else:
            print "%s %s in %d seconds @ %s/s" % (action, pretty_size(length),
                time, pretty_size(length/time))