Exemple #1
0
    def read_flash(self, start, len):
        crc = CRC()

        print "Reading " + str(len) + " bytes from address " + hex(start)

        data = self.spi_read(start, len)

        crc.process(data)

        local_crc = crc.get_crc()
        dev_crc = self.spi_compute_crc(start, start + len - 1)

        if (local_crc != dev_crc):
            print "CRC validation failed! Expected " + hex(
                local_crc) + ", received " + hex(dev_crc)
            return None

        return data
Exemple #2
0
    def write_segment(self, address, data):
        crc = CRC()
        length = len(data)

        # unprotect the status register
        self.spi_common_command(self.SPI_CMD_WRITE_AFTER_EWSR, 0x01, 0, 1, 0)

        # unprotect the flash
        self.spi_common_command(self.SPI_CMD_WRITE_AFTER_WREN, 0x01, 0, 1, 0)

        if (self._should_program_page(data)):
            # set program size - 1, 255 maximum
            self._i2c_write_reg(0x71, (length - 1) & 0xFF)

            # set the programming address
            self._i2c_write_reg(0x64, (address >> 16) & 0xFF)
            self._i2c_write_reg(0x65, (address >> 8) & 0xFF)
            self._i2c_write_reg(0x66, (address >> 0) & 0xFF)

            # write the content to register 0x70
            # we can only write 16 bytes at a time
            for i in range(0, length, 16):
                if (length - i >= 16):
                    self._i2c_write_bytes(0x70, data[i:i + 16])
                else:
                    self._i2c_write_bytes(0x70, data[i:i + length - i])

            # start programming
            self._i2c_write_reg(0x6F, 0xA0)

        crc.process(data)

        if (not self._wait_write_done()):
            return False

        local_crc = crc.get_crc()
        dev_crc = self.spi_compute_crc(address, address + length - 1)

        if (local_crc != dev_crc):
            print "CRC validation failed! Expected " + hex(
                local_crc) + ", received " + hex(dev_crc)
            return False

        return True