def read(a): command([0x03] + addr24(a)) while True: ids = struct.unpack("BBB", idcode()) print("Got JEDEC ID: %02x %02x %02x" % ids) if ids[0] not in (0x00, 0xff) and (8 <= ids[2] < 22): break size = 1 << ids[2] print("Flash size is %d bytes" % size) if '-r' in optdict: read(0) chunk = 8 * 1024 with open(optdict['-r'], "wb") as f: for a in range(0, size, chunk): f.write(s.read(chunk)) print("%d/%d KBytes" % (a / 1024, size / 1024)) if '-w' in optdict: write_enable() command([0xc7]) wait_ready() with open(optdict['-w'], "rb") as f: for a in range(0, size, 256): page_program(a, f.read(256)) print("%d/%d KBytes" % (a / 1024, size / 1024)) s.unsel() s.seta(1) s.detach()
def page_program(a, b256): # print('page_program', a) write_enable() command([0x02] + addr24(a)) s.write(b256) wait_ready() def erase_sector(a): # print('erase sector', a) write_enable() command([0xd8] + addr24(a)) wait_ready() ids = struct.unpack("BBB", idcode()) if ids != (0x20, 0xba, 0x16): print("Unexpected JEDEC ID: %02x %02x %02x" % ids) sys.exit(1) bin = open(sys.argv[1], "rb").read() t0 = time.time() for a in range(0, len(bin), 256): if (a & 0xffff) == 0: erase_sector(a) page_program(a, bin[a:a + 256]) print("flash load took %.2f s" % (time.time() - t0)) s.unsel() s.seta(1) # Let the FPGA boot s.detach() # Release the SPI signals