1.1 --- a/upload.py Tue Jan 20 22:23:38 2015 +0100
1.2 +++ b/upload.py Tue Jan 20 22:24:19 2015 +0100
1.3 @@ -1,12 +1,41 @@
1.4 #!/usr/bin/env python
1.5
1.6 from serial import Serial
1.7 +from time import sleep
1.8 import sys
1.9
1.10 +port = "/dev/ttyUSB0"
1.11 +rate = 115200
1.12 +
1.13 +def openPort():
1.14 + global s
1.15 + s = Serial(port, rate)
1.16 + s.setTimeout(5)
1.17 + s.readline()
1.18 +
1.19 +def closePort():
1.20 + global s
1.21 + s.close()
1.22 + s = None
1.23 +
1.24 +def writeToPort(data):
1.25 + s.write(data)
1.26 + s.flush()
1.27 + print >>sys.stderr, data.rstrip()
1.28 +
1.29 +def readFromPort():
1.30 + resp = s.readline().rstrip("\r\n")
1.31 + if resp == "?":
1.32 + resp = s.readline().rstrip("\r\n")
1.33 + return resp
1.34 +
1.35 def console():
1.36 try:
1.37 while 1:
1.38 - s.write(raw_input("> ") + "\n")
1.39 + cmd = raw_input("> ")
1.40 + if cmd:
1.41 + s.write(cmd + "\n")
1.42 + s.flush()
1.43 print s.readline().rstrip("\r\n")
1.44 except EOFError:
1.45 print "Session closed."
1.46 @@ -20,8 +49,8 @@
1.47
1.48 print >>sys.stderr, "Erasing sector", sector, "..."
1.49
1.50 - s.write("S%x\n" % sector)
1.51 - resp = s.readline().rstrip("\r\n")
1.52 + writeToPort("S%x\n" % sector)
1.53 + resp = readFromPort()
1.54 if resp != "S":
1.55 print >>sys.stderr, "Sector %d erase failed: %s" % (sector, resp)
1.56 return False
1.57 @@ -32,28 +61,32 @@
1.58 while i < 0x4000:
1.59 value = ord(data[i])
1.60 addr = (sector << 14) | i
1.61 - s.write("P%04x%02x\n" % (addr, value))
1.62 - print >>sys.stderr, "P%04x%02x" % (addr, value)
1.63 - resp = s.readline().rstrip("\r\n")
1.64 - if resp != "P":
1.65 - print >>sys.stderr, "Program of location %04x failed: %s" % (addr, resp)
1.66 - return False
1.67 - #s.write("R%04x\n" % addr)
1.68 - #print >>sys.stderr, "R%04x" % addr
1.69 - #readvalue = s.readline().rstrip("\r\n")
1.70 - #if readvalue != ("%X" % value):
1.71 - # print >>sys.stderr, "Program of location %04x failed: %s read for %s written." % (addr, readvalue, "%X" % value)
1.72 - # return False
1.73 +
1.74 + while 1:
1.75 + writeToPort("P%04x%02x\n" % (addr, value))
1.76 + resp = readFromPort()
1.77 + if resp != "P":
1.78 + print >>sys.stderr, "Program of location %04x failed: %s" % (addr, resp)
1.79 + closePort()
1.80 + sleep(5)
1.81 + openPort()
1.82 + writeToPort("R%04x\n" % addr)
1.83 + resp = readFromPort()
1.84 + if resp == ("%X" % value):
1.85 + break
1.86 + print >>sys.stderr, "Read from location %04x failed: %s" % (addr, resp)
1.87 + closePort()
1.88 + sleep(5)
1.89 + openPort()
1.90 + else:
1.91 + break
1.92 +
1.93 i += 1
1.94
1.95 return True
1.96
1.97 if __name__ == "__main__":
1.98 - port = "/dev/ttyUSB0"
1.99 - rate = 115200
1.100 -
1.101 - s = Serial(port, rate)
1.102 - s.readline()
1.103 + openPort()
1.104
1.105 if "-i" in sys.argv:
1.106 console()