paul@10 | 1 | #!/usr/bin/env python |
paul@10 | 2 | |
paul@19 | 3 | """ |
paul@19 | 4 | A ROM upload utility for the Arduino-Am29F010 interface. |
paul@19 | 5 | |
paul@19 | 6 | Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> |
paul@19 | 7 | |
paul@19 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@19 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@19 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@19 | 11 | version. |
paul@19 | 12 | |
paul@19 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@19 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@19 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@19 | 16 | |
paul@19 | 17 | You should have received a copy of the GNU General Public License along |
paul@19 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@19 | 19 | """ |
paul@19 | 20 | |
paul@10 | 21 | from serial import Serial |
paul@15 | 22 | from time import sleep |
paul@10 | 23 | import sys |
paul@10 | 24 | |
paul@15 | 25 | port = "/dev/ttyUSB0" |
paul@15 | 26 | rate = 115200 |
paul@15 | 27 | |
paul@15 | 28 | def openPort(): |
paul@15 | 29 | global s |
paul@15 | 30 | s = Serial(port, rate) |
paul@15 | 31 | s.setTimeout(5) |
paul@15 | 32 | s.readline() |
paul@15 | 33 | |
paul@15 | 34 | def closePort(): |
paul@15 | 35 | global s |
paul@15 | 36 | s.close() |
paul@15 | 37 | s = None |
paul@15 | 38 | |
paul@15 | 39 | def writeToPort(data): |
paul@15 | 40 | s.write(data) |
paul@15 | 41 | s.flush() |
paul@15 | 42 | print >>sys.stderr, data.rstrip() |
paul@15 | 43 | |
paul@15 | 44 | def readFromPort(): |
paul@15 | 45 | resp = s.readline().rstrip("\r\n") |
paul@15 | 46 | if resp == "?": |
paul@15 | 47 | resp = s.readline().rstrip("\r\n") |
paul@15 | 48 | return resp |
paul@15 | 49 | |
paul@11 | 50 | def console(): |
paul@10 | 51 | try: |
paul@10 | 52 | while 1: |
paul@15 | 53 | cmd = raw_input("> ") |
paul@15 | 54 | if cmd: |
paul@15 | 55 | s.write(cmd + "\n") |
paul@15 | 56 | s.flush() |
paul@11 | 57 | print s.readline().rstrip("\r\n") |
paul@10 | 58 | except EOFError: |
paul@10 | 59 | print "Session closed." |
paul@10 | 60 | |
paul@10 | 61 | def upload(filename, sector): |
paul@10 | 62 | data = open(filename, "rb").read() |
paul@10 | 63 | |
paul@10 | 64 | if len(data) != 0x4000: |
paul@10 | 65 | print >>sys.stderr, "File", filename, "is not", 0x4000, "bytes long." |
paul@10 | 66 | return False |
paul@10 | 67 | |
paul@10 | 68 | print >>sys.stderr, "Erasing sector", sector, "..." |
paul@10 | 69 | |
paul@15 | 70 | writeToPort("S%x\n" % sector) |
paul@15 | 71 | resp = readFromPort() |
paul@10 | 72 | if resp != "S": |
paul@10 | 73 | print >>sys.stderr, "Sector %d erase failed: %s" % (sector, resp) |
paul@10 | 74 | return False |
paul@10 | 75 | |
paul@10 | 76 | print >>sys.stderr, "Uploading", filename, "to sector", sector, "..." |
paul@10 | 77 | |
paul@10 | 78 | i = 0 |
paul@10 | 79 | while i < 0x4000: |
paul@10 | 80 | value = ord(data[i]) |
paul@10 | 81 | addr = (sector << 14) | i |
paul@15 | 82 | |
paul@16 | 83 | writeToPort("P%04x%02x\n" % (addr, value)) |
paul@16 | 84 | resp = readFromPort() |
paul@16 | 85 | if resp != "P": |
paul@16 | 86 | print >>sys.stderr, "Program of location %04x failed: %s" % (addr, resp) |
paul@16 | 87 | return False |
paul@15 | 88 | |
paul@10 | 89 | i += 1 |
paul@10 | 90 | |
paul@10 | 91 | return True |
paul@10 | 92 | |
paul@10 | 93 | if __name__ == "__main__": |
paul@15 | 94 | openPort() |
paul@10 | 95 | |
paul@18 | 96 | try: |
paul@18 | 97 | if "-i" in sys.argv: |
paul@18 | 98 | console() |
paul@18 | 99 | elif len(sys.argv) > 1: |
paul@18 | 100 | if len(sys.argv) > 3 and sys.argv[1] == "-s": |
paul@18 | 101 | base = int(sys.argv[2]) |
paul@18 | 102 | i = 3 |
paul@18 | 103 | else: |
paul@18 | 104 | base = 0 |
paul@18 | 105 | i = 1 |
paul@18 | 106 | for sector, filename in enumerate(sys.argv[i:]): |
paul@18 | 107 | upload(filename, sector + base) |
paul@18 | 108 | sleep(5) # give the device a rest |
paul@16 | 109 | else: |
paul@18 | 110 | print >>sys.stderr, sys.argv[0], "-i | <filename>..." |
paul@18 | 111 | sys.exit(1) |
paul@18 | 112 | finally: |
paul@18 | 113 | closePort() |
paul@10 | 114 | |
paul@10 | 115 | # vim: tabstop=4 expandtab shiftwidth=4 |