summaryrefslogtreecommitdiff
path: root/downloader
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2013-07-02 19:53:22 +0000
committerChristian Pointner <equinox@spreadspace.org>2013-07-02 19:53:22 +0000
commitcb7c6f5bb40b898c89210c52d15a949b4108e13a (patch)
treeb98e7fccd4bd05f8f381bf2fa17fc6ed46c369c2 /downloader
parentset_serial now suports baudrate (diff)
sanity checks for commands
git-svn-id: https://svn.spreadspace.org/pic/trunk@22 a09c6847-51d9-44de-8ef2-e725cf50f3c7
Diffstat (limited to 'downloader')
-rwxr-xr-xdownloader/downloader.py71
1 files changed, 67 insertions, 4 deletions
diff --git a/downloader/downloader.py b/downloader/downloader.py
index cc1c97c..5380b4d 100755
--- a/downloader/downloader.py
+++ b/downloader/downloader.py
@@ -23,13 +23,16 @@
'''spreadspace simple pic downloader.'''
-VERSION = '0.1'
+VERSION_MAJ = 0
+VERSION_MIN = 1
def open_serial(device, baud):
import os
import tty
import termios
+ print "opening %s (%s Baud)" % (device, baud)
+
baudrates = { 50: termios.B50, 75: termios.B75, 110: termios.B110, 134: termios.B134,
150: termios.B150, 200: termios.B200, 300: termios.B300, 600: termios.B600,
1200: termios.B1200, 1800: termios.B1800, 2400: termios.B2400, 4800: termios.B4800,
@@ -61,8 +64,6 @@ def open_serial(device, baud):
def load_hex(file):
- import os
-
fin = file
if fin == '-':
fin = sys.stdin
@@ -82,10 +83,70 @@ def load_hex(file):
return data
+def calc_csum(str):
+ cs = 0
+ for c in str:
+ cs ^= ord(c)
+ return chr(cs)
+
+
+def exec_command(dev, cmd, answer):
+ import struct
+
+ return_codes = { 0: "OK", 1: "invalid command", 2: "bad checksum",
+ 3: "not implemented", 4: "flash write error",
+ 5: "address invalid", 6: "address prohibited" }
+
+ answer = "=cB" + answer + "B"
+ answer_len = struct.calcsize(answer)
+
+ cstr = cmd + calc_csum(cmd)
+ os.write(dev, cstr)
+
+ astr = ""
+ while len(astr) < answer_len:
+ astr += os.read(dev, answer_len - len(astr))
+ data = list(struct.unpack(answer, astr))
+ code = data.pop(0)
+ ret = data.pop(1)
+ checksum = data.pop()
+ if code != cmd[0]:
+ print "ERROR: bootloader returned wrong command code"
+ sys.exit(4)
+ if ret != 0:
+ rstr = "invalid return code"
+ try:
+ rstr = return_codes[ret]
+ except KeyError:
+ pass
+ print "ERROR: bootloader returned %d: %s" % (ret, rstr)
+ sys.exit(4)
+ if 0 != calc_csum(astr):
+ print "ERROR: checksum error"
+ sys.exit(4)
+
+ return data
+
+### commands
+
+def identify(dev):
+ data = exec_command(dev, "i", "BB10sHBB")
+ id = { 'ver_maj': data[0], 'ver_min': data[1], 'name': data[2], 'devid': data[3], 'fss': data[4], 'supported': data[5] }
+
+ if id['ver_maj'] != VERSION_MAJ:
+ print "Incomaptible protocol version, expected: %d, got: %d" % (VERSION_MAJ, id['ver_maj'])
+ sys.exit(4)
+
+ print "connected with Bootloader '%s', (ID=%04X, FSS=%d)" % (id['name'], id['devid'], id['fss'])
+ return id
+
+
+### main
if __name__ == '__main__':
import getopt
import sys
+ import os
from intelhex import IntelHex
@@ -116,7 +177,7 @@ Options:
print(usage)
sys.exit(0)
elif o in ("-v", "--version"):
- print(VERSION)
+ print("Version %d.%d" % (VERSION_MAJ, VERSION_MIN))
sys.exit(0)
elif o in ("--device"):
device = a
@@ -141,3 +202,5 @@ Options:
addrs.sort()
for a in addrs:
print "%05d: %04X" % (a, data[a])
+
+ id = identify(dev)