summaryrefslogtreecommitdiff
path: root/downloader/downloader.py
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2013-07-08 00:05:46 +0000
committerChristian Pointner <equinox@spreadspace.org>2013-07-08 00:05:46 +0000
commit7792dab74fce0f5445e78365f41d1728fd1f886f (patch)
tree54734c7a45a6ad349b7bd5f767ff596ea1f09902 /downloader/downloader.py
parentbasic commands work now (diff)
output now in stderr
fixed verify command git-svn-id: https://svn.spreadspace.org/pic/trunk@56 a09c6847-51d9-44de-8ef2-e725cf50f3c7
Diffstat (limited to 'downloader/downloader.py')
-rwxr-xr-xdownloader/downloader.py101
1 files changed, 56 insertions, 45 deletions
diff --git a/downloader/downloader.py b/downloader/downloader.py
index a07e15d..459e0d2 100755
--- a/downloader/downloader.py
+++ b/downloader/downloader.py
@@ -35,7 +35,7 @@ def load_hex(file):
if fin == '-':
fin = sys.stdin
elif not os.path.isfile(fin):
- print "ERROR: File not found: %s" % fin
+ print >> sys.stderr, "ERROR: File not found: %s" % fin
sys.exit(1)
codedata = {}
@@ -86,14 +86,14 @@ def create_flash_segments(codedata, fs, fss):
sa = get_lowest_flash_addr(codedata, fss)
ea = get_highest_flash_addr(codedata, fss)
if ea >= fs:
- print "WARNING: the hex file contains data after end of flash, these words will be ignored"
+ print >> sys.stderr, "WARNING: the hex file contains data after end of flash, these words will be ignored"
ea = fs
img = create_flash_image(codedata, fss, sa, ea)
for i in xrange(0, ea, fss):
slice = tuple(img[1][i:i+fss])
if not all( (elem == 0xFFFF) for elem in slice):
- yield (i, slice)
+ yield (i + img[0], slice)
### Interface to Bootloader
@@ -104,7 +104,7 @@ def open_serial(device, baud):
import termios
import serial
- print "opening %s (%s Baud)" % (device, baud)
+ print >> sys.stderr, "opening %s (%s Baud)" % (device, baud)
try:
dev = serial.Serial(port=device, baudrate=baud, timeout=3)
@@ -113,7 +113,7 @@ def open_serial(device, baud):
return dev
except (ValueError, serial.SerialException), msg:
- print "ERROR: opening serial device: %s" % msg
+ print >> sys.stderr, "ERROR: opening serial device: %s" % msg
sys.exit(3)
def calc_csum(str):
@@ -140,11 +140,11 @@ def exec_command(dev, cmd, param, answer):
astr = bytearray()
astr += dev.read(4)
if len(astr) < 4:
- print "ERROR: timeout while reading response header (expected %d bytes, got %d)" % (4, len(astr))
+ print >> sys.stderr, "ERROR: timeout while reading response header (expected %d bytes, got %d)" % (4, len(astr))
sys.exit(4)
if astr[0] != cstr[0]:
- print "ERROR: bootloader returned wrong command code"
+ print >> sys.stderr, "ERROR: bootloader returned wrong command code"
sys.exit(4)
ret = astr[2]
@@ -154,93 +154,96 @@ def exec_command(dev, cmd, param, answer):
rstr = return_codes[ret]
except KeyError:
pass
- print "ERROR: bootloader returned %d: %s" % (ret, rstr)
+ print >> sys.stderr, "ERROR: bootloader returned %d: %s" % (ret, rstr)
sys.exit(4)
answer_len = astr[1] - 4
if answer_len < struct.calcsize(answer):
- print "ERROR: short answer %d bytes received: expected %s bytes" % (answer_len, struct.calcsize(answer))
+ print >> sys.stderr, "ERROR: short answer %d bytes received: expected %s bytes" % (answer_len, struct.calcsize(answer))
sys.exit(4)
if answer_len > 0:
tmp = bytearray()
tmp += dev.read(answer_len)
if len(tmp) < answer_len:
- print "ERROR: timeout while reading response (expected %d bytes, got %d)" % (answer_len, len(tmp))
+ print >> sys.stderr, "ERROR: timeout while reading response (expected %d bytes, got %d)" % (answer_len, len(tmp))
sys.exit(4)
astr += tmp
if 0 != calc_csum(astr):
- print "ERROR: checksum error"
+ print >> sys.stderr, "ERROR: checksum error"
sys.exit(4)
return struct.unpack_from(answer, astr, 3)
### low level commands
-def identify(dev, name):
+def cmd_identify(dev, name):
data = exec_command(dev, 1, '', '<BB3sHHBHBH')
id = { 'ver_min': data[0], 'ver_maj': data[1], 'name': data[2], 'devid': data[3],
'fs': data[4], 'fss': data[5], 'es': data[6], 'mess': data[7], 'supported': data[8] }
if id['ver_maj'] != VERSION_MAJ:
- print "incompatible protocol version, expected: %d, got: %d" % (VERSION_MAJ, id['ver_maj'])
+ print >> sys.stderr, "incompatible protocol version, expected: %d, got: %d" % (VERSION_MAJ, id['ver_maj'])
sys.exit(4)
if name and id['name'] != name:
- print "ERROR: the bootloaders name '%s' differs from the one supplied via" % id['name']
- print " command line option '%s'. Are sure you are connected to the" % name
- print " right device?"
+ print >> sys.stderr, "ERROR: the bootloaders name '%s' differs from the one supplied via" % id['name']
+ print >> sys.stderr, " command line option '%s'. Are sure you are connected to the" % name
+ print >> sys.stderr, " right device?"
sys.exit(4)
- print "connected with Bootloader '%s' Version %d.%d, (ID=%04X, %d words Flash, FSS=%d, %d bytes EEPROM, MESS=%d)" % \
+ print >> sys.stderr, "connected with Bootloader '%s' Version %d.%d, (ID=%04X, %d words Flash, FSS=%d, %d bytes EEPROM, MESS=%d)" % \
(id['name'], id['ver_maj'], id['ver_min'], id['devid'], id['fs'], id['fss'], id['es'], id['mess'])
return id
-def boot(dev):
+def cmd_boot(dev):
exec_command(dev, 2, '', '<')
-def reset(dev, id):
+def cmd_reset(dev, id):
exec_command(dev, 3, '', '<')
-def read_flash_segment(dev, id, addr):
+def cmd_read_flash_segment(dev, id, addr):
param = struct.pack('<H', addr)
return exec_command(dev, 4, param, '<%dH' % id['fss'])
-def write_flash_segment(dev, id, addr, data):
+def cmd_write_flash_segment(dev, id, addr, data):
param = struct.pack('<H%dH' % id['fss'], addr, *data)
return exec_command(dev, 5, param, '<')
-def read_eeprom(dev, id, addr, len):
+def cmd_read_eeprom(dev, id, addr, len):
param = struct.pack('<HB', addr, len)
return exec_command(dev, 6, param, '<%dB' % len)
-def write_eeprom(dev, id, addr, data):
+def cmd_write_eeprom(dev, id, addr, data):
param = struct.pack('<HB%dB' % len(data), addr, len(data), *data)
return exec_command(dev, 7, param, '<')
-def read_config(dev, id, nr):
+def cmd_read_config(dev, id, nr):
param = struct.pack('<B', nr)
data = exec_command(dev, 8, param, '<H')
return data[0]
-def write_config(dev, id, nr, word):
+def cmd_write_config(dev, id, nr, word):
param = struct.pack('<BH', nr, word)
return exec_command(dev, 9, param, '<')
### commands
+def boot(dev, id, file):
+ cmd_boot(dev)
+
def write_flash(dev, id, file):
codedata = load_hex(file)
for segment in create_flash_segments(codedata, id['fs'], id['fss']):
-# print "%05X: %s" % (segment[0], ''.join('%04X '%i for i in segment[1]))
- write_flash_segment(dev, id, segment[0], segment[1])
+# print >> sys.stderr, "%05X: %s" % (segment[0], ''.join('%04X '%i for i in segment[1]))
+ cmd_write_flash_segment(dev, id, segment[0], segment[1])
def read_flash(dev, id, file):
codedata = {}
for addr in xrange(0, id['fs'], id['fss']):
- data = read_flash_segment(dev, id, addr)
-# print "%05X: %s" % (addr, ''.join('%04X '%i for i in data))
+ data = cmd_read_flash_segment(dev, id, addr)
+# print >> sys.stderr, "%05X: %s" % (addr, ''.join('%04X '%i for i in data))
a = addr
for d in data:
codedata[a] = d
@@ -252,19 +255,26 @@ def verify_flash(dev, id, file):
codedata = load_hex(file)
err = 0
for segment in create_flash_segments(codedata, id['fs'], id['fss']):
- data = read_flash_segment(dev, id, segment[0])
- if segment[1] != data:
- err = 1
-# print "%05X: failed" % (segment[0])
+ flashdata = cmd_read_flash_segment(dev, id, segment[0])
+ for file,flash in zip(segment[1] , flashdata):
+ if flash == 0x3FFF:
+ flash = 0xFFFF
+ if flash != file:
+ err = 1
+ break
+
+ if err !=0:
break
-# else:
-# print "%05X: ok" % (segment[0])
+
if err != 0:
- print "ERROR: verify failed!"
+ print >> sys.stderr, "ERROR: verify failed!"
sys.exit(-1)
-
+ else:
+ print >> sys.stderr, "verify ok!"
+ sys.exit(0)
commands = {
+ 'boot': boot,
'write': write_flash,
'read': read_flash,
'verify': verify_flash
@@ -289,7 +299,7 @@ Arguments:
Options:
-h, --help this help message.
-v, --version version info.
- --cmd=N the command to execute, one out of: "read,write,verify"
+ --cmd=N the command to execute, one out of: "boot,read,write,verify"
--device=N the serial port to use (default: /dev/ttyUSB0).
--baud=N baudrate to use (default: 57600).
--name=N the expected name of the bootloader.
@@ -306,10 +316,10 @@ Options:
for o, a in opts:
if o in ("-h", "--help"):
- print(usage)
+ print >> sys.stderr, usage
sys.exit(0)
elif o in ("-v", "--version"):
- print("Version %d.%d" % (VERSION_MAJ, VERSION_MIN))
+ print >> sys.stderr, "Version %d.%d" % (VERSION_MAJ, VERSION_MIN)
sys.exit(0)
elif o in ("--cmd"):
cmd = a
@@ -327,16 +337,17 @@ Options:
raise getopt.GetoptError('Too many arguments')
if not cmd:
- raise getopt.GetoptError('You have to supply a command (read,write,verify)')
+ raise getopt.GetoptError('You have to supply a command (boot,read,write,verify)')
except getopt.GetoptError, msg:
- print "ERROR: %s" % msg
- print usage
+ print >> sys.stderr, "ERROR: %s" % msg
+ print >> sys.stderr, usage
sys.exit(2)
dev = open_serial(device, baudrate)
- id = identify(dev, name)
+ id = cmd_identify(dev, name)
+
try:
commands[cmd](dev, id, args[0])
except KeyError:
- print "ERROR: unkown command '%s'" % cmd
+ print >> sys.stderr, "ERROR: unkown command '%s'" % cmd