diff options
Diffstat (limited to 'software/pic.bootloader/downloader.py')
-rwxr-xr-x | software/pic.bootloader/downloader.py | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/software/pic.bootloader/downloader.py b/software/pic.bootloader/downloader.py index 072ca46..12e9644 100755 --- a/software/pic.bootloader/downloader.py +++ b/software/pic.bootloader/downloader.py @@ -3,7 +3,7 @@ # spreadspace pic utils # # -# Copyright (C) 2011 Christian Pointner <equinox@spreadspace.org> +# Copyright (C) 2011-2013 Christian Pointner <equinox@spreadspace.org> # # This file is part of spreadspace pic utils. # @@ -30,6 +30,7 @@ VERSION_MIN = 1 def load_hex(file): from ihexpic import IHexPic + import os fin = file if fin == '-': @@ -85,10 +86,8 @@ def create_flash_segments(hexdata, fs, fss): ### Interface to Bootloader def open_serial(device, baud): - import os - import tty - import termios import serial + import time print >> sys.stderr, "opening %s (%s Baud)" % (device, baud) @@ -96,6 +95,12 @@ def open_serial(device, baud): dev = serial.Serial(port=device, baudrate=baud, timeout=3) dev.flushInput() dev.flushOutput() + dev.setDTR(True) # send a reset pulse + dev.setBreak(True) # boot into bootloader + time.sleep(0.1) + dev.setDTR(False) + time.sleep(0.01) + dev.setBreak(False) return dev except (ValueError, serial.SerialException), msg: @@ -109,7 +114,6 @@ def calc_csum(str): return cs def exec_command(dev, cmd, param, answer): - import struct return_codes = { 0: "OK", 1: "invalid command", 2: "bad checksum", 3: "not implemented", 4: "flash write error", @@ -166,9 +170,9 @@ def exec_command(dev, cmd, param, answer): ### low level commands def cmd_identify(dev, name): - data = exec_command(dev, 1, '', '<BB3sHHBHBH') + data = exec_command(dev, 1, '', '<BB3sHHBHBBH') 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] } + 'fs': data[4], 'fss': data[5], 'es': data[6], 'mess': data[7], 'cfg': data[8], 'supported': data[9] } if id['ver_maj'] != VERSION_MAJ: print >> sys.stderr, "incompatible protocol version, expected: %d, got: %d" % (VERSION_MAJ, id['ver_maj']) @@ -179,8 +183,8 @@ def cmd_identify(dev, name): print >> sys.stderr, " right device?" sys.exit(4) - print >> sys.stderr, "connected with Bootloader '%s' Version %d.%d,\n (ID=%04X, %d words Flash, FSS=%d, %d bytes EEPROM, MESS=%d)\n" % \ - (id['name'], id['ver_maj'], id['ver_min'], id['devid'], id['fs'], id['fss'], id['es'], id['mess']) + print >> sys.stderr, "connected with Bootloader '%s' Version %d.%d,\n (ID=%04X, %d words Flash, FSS=%d, %d bytes EEPROM, MESS=%d, %d words Config)\n" % \ + (id['name'], id['ver_maj'], id['ver_min'], id['devid'], id['fs'], id['fss'], id['es'], id['mess'], id['cfg']) return id def cmd_boot(dev): @@ -251,6 +255,10 @@ def boot(dev, id, args): print >> sys.stderr, "booting to user code" cmd_boot(dev) +def reset(dev, id, args): + print >> sys.stderr, "reseting MCU" + cmd_reset(dev,id) + def write_flash(dev, id, args): hexdata = load_hex(args[0]) flashsegments = list(create_flash_segments(hexdata, id['fs'], id['fss'])) @@ -304,11 +312,19 @@ def verify_flash(dev, id, args): else: print >> sys.stderr, " *********** verify ok! **********\n" +def read_config(dev, id, args): + nr = int(args[0]) + print >> sys.stderr, "reading configuration word nr %d" % nr + print "0x%04X" % cmd_read_config(dev, id, nr) + + commands = { 'boot': boot, + 'reset': reset, 'write': write_flash, 'read': read_flash, - 'verify': verify_flash + 'verify': verify_flash, + 'read-config': read_config } ### Main @@ -316,21 +332,22 @@ commands = { if __name__ == '__main__': import getopt import sys - import os import struct usage = '''spreadspace simple pic downloader. Usage: python downloader.py [options] command [ command2 [ .. ] ] - You can supply as many commands as you wish. Any command except boot + You can supply as many commands as you wish. Any command except 'boot' may be supplied more than once. The commands will be executed in the - order of appearence in the command line. + order of appearence at command line. Mind that all commands after 'boot' will be ignored because the bootloader - is no longer reachable. If verify detects an error the downloader will - exit with '-1' immediatly, the remaining commands will get ignored. + is no longer reachable. The same may be true after a reset in which case, + depending on the state of BOOTPIN, the user code may get started. + If verify detects an error the downloader will exit with '-1' immediatly and + the remaining commands will get ignored. If you don't specify any command the downloader will connect to the - bootloader print some info and exit. + bootloader print some information and exit. Options: -h, --help this help message. @@ -343,6 +360,8 @@ Commands: --write=<hexfile> write <hexfile> to flash (use '-' for stdin). --verify=<hexfile> compare flash with <hexfile> (use '-' for stdin). --read=<hexfile> read flash and store in <hexfile> (use '-' for stdout). + --read-config=<nr> read the configuration word <nr> and print it on stdout. + --reset reset the MCU (this may start the user code area: BOOTPIN) --boot boot to user code ''' @@ -353,7 +372,7 @@ Commands: try: opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "version", "device=", "baud=", "name=", \ - "write=", "read=", "verify=", "boot" ]) + "write=", "read=", "verify=", "read-config=", "reset", "boot" ]) for o, a in opts: if o in ("-h", "--help"): print >> sys.stderr, usage |