| 1 | #!/usr/bin/python3 |
| 2 | import argparse |
| 3 | from libiceblink import ICE40Board, M25P10 |
| 4 | |
| 5 | def main(): |
| 6 | ap = argparse.ArgumentParser() |
| 7 | ap.add_argument("-e", "--erase", action="store_true") |
| 8 | ap.add_argument("-v", "--verbose", action="store_true") |
| 9 | ap.add_argument("-w", "--write", type=argparse.FileType("rb")) |
| 10 | args = ap.parse_args() |
| 11 | |
| 12 | board = ICE40Board() |
| 13 | |
| 14 | if args.verbose: |
| 15 | print("Found iCE40 board serial: %s" % board.get_serial()) |
| 16 | |
| 17 | |
| 18 | sp = board.get_spi_port(0) |
| 19 | |
| 20 | with board.get_gpio() as gpio: |
| 21 | # Force the FPGA into reset so we may drive the IOs |
| 22 | gpio.ice40SetReset(True) |
| 23 | |
| 24 | with board.get_spi_port(0) as sp: |
| 25 | sp.setSpeed(50000000) |
| 26 | sp.setMode() |
| 27 | |
| 28 | flash = M25P10(sp.io) |
| 29 | |
| 30 | flash.wakeup() |
| 31 | |
| 32 | # Verify that we're talking to the part we think we are |
| 33 | assert flash.getID() == b'\x20\x20\x11' |
| 34 | |
| 35 | # Now, do the actions |
| 36 | if args.erase: |
| 37 | if args.verbose: |
| 38 | print("Erasing flash...") |
| 39 | flash.chipErase() |
| 40 | if args.verbose: |
| 41 | print("") |
| 42 | |
| 43 | if args.write: |
| 44 | data = args.write.read() |
| 45 | |
| 46 | if args.verbose: |
| 47 | print("Writing image...") |
| 48 | |
| 49 | for addr in range(0, len(data), 256): |
| 50 | buf = data[addr:addr+256] |
| 51 | flash.pageProgram(addr, buf) |
| 52 | |
| 53 | if args.verbose: |
| 54 | print("Verifying written image...") |
| 55 | # Now verify |
| 56 | buf = flash.read(0, len(data)) |
| 57 | assert len(buf) == len(data) |
| 58 | |
| 59 | nvfailures = 0 |
| 60 | for i,(a,b) in enumerate(zip(buf, data)): |
| 61 | if a!=b: |
| 62 | print ("verification failure at %06x: %02x != %02x" % |
| 63 | (i,a,b)) |
| 64 | nvfailures += 1 |
| 65 | |
| 66 | if nvfailures == 5: |
| 67 | print("Too many verification failures, bailing") |
| 68 | break |
| 69 | |
| 70 | # Release the FPGA reset |
| 71 | gpio.ice40SetReset(False) |
| 72 | |
| 73 | |
| 74 | if __name__ == "__main__": |
| 75 | main() |