From 37e797d651db1802dfdbd7a1f5dd455110fd3c0d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 10 Jul 2013 23:02:50 +0000 Subject: improved command line options fixed bug at progress bar git-svn-id: https://svn.spreadspace.org/pic/trunk@72 a09c6847-51d9-44de-8ef2-e725cf50f3c7 --- bootloader/downloader.py | 99 +++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 44 deletions(-) diff --git a/bootloader/downloader.py b/bootloader/downloader.py index 8ca2b1b..175b03a 100755 --- a/bootloader/downloader.py +++ b/bootloader/downloader.py @@ -179,7 +179,7 @@ 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)" % \ + 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']) return id @@ -215,25 +215,28 @@ def cmd_write_config(dev, id, nr, word): return exec_command(dev, 9, param, '<') -### commands +### utils + class progressbar(object): - def __init__(self, total, name): + def __init__(self, total, name, size=50): self._name = name - self._cnt = 0 + self._cnt = 0.0 self._total = float(total) self._point = total / 100.0 - self._increment = total / 50.0 + self._size = size + self._increment = total / float(self._size) self._update() def _update(self): - sys.stderr.write("\r %13s" % self._name) - sys.stderr.write(" [" + "=" * int(self._cnt / self._increment) + " " * int((self._total - self._cnt)/ self._increment) + "]") -# sys.stderr.write(" " + str(self._cnt / self._point) + "%") - sys.stderr.write(" ( %d / %d )" % (self._cnt, self._total)) + sys.stderr.write('\r %13s' % self._name) + val = int(self._cnt / self._increment) + sys.stderr.write(' [' + '=' * val + ' ' * (self._size-val) + ']') + sys.stderr.write(' {:3.0f}'.format((self._cnt / self._point)) + '%') +# sys.stderr.write(' ( %d / %d )' % (self._cnt, self._total)) sys.stderr.flush() def increment(self): - self._cnt += 1 + self._cnt += 1.0 self._update() def end(self): @@ -242,14 +245,15 @@ class progressbar(object): sys.stderr.write("\n") +### commands -def boot(dev, id, file): +def boot(dev, id, args): cmd_boot(dev) -def write_flash(dev, id, file): - hexdata = load_hex(file) +def write_flash(dev, id, args): + hexdata = load_hex(args[0]) flashsegments = list(create_flash_segments(hexdata, id['fs'], id['fss'])) - print >> sys.stderr, "" + print >> sys.stderr, "writing to flash from '%s'" % args[0] bar = progressbar(len(flashsegments), "write flash") for segment in flashsegments: cmd_write_flash_segment(dev, id, segment[0], segment[1]) @@ -257,9 +261,9 @@ def write_flash(dev, id, file): bar.end() print >> sys.stderr, "" -def read_flash(dev, id, file): +def read_flash(dev, id, args): codedata = {} - print >> sys.stderr, "" + print >> sys.stderr, "reading flash to '%s'" % args[0] bar = progressbar(float(id['fs'])/float(id['fss']), "read flash") for addr in xrange(0, id['fs'], id['fss']): data = cmd_read_flash_segment(dev, id, addr) @@ -270,14 +274,14 @@ def read_flash(dev, id, file): codedata[a] = d a += 1 bar.end() - write_hex(file, codedata) + write_hex(args[0], codedata) print >> sys.stderr, "" -def verify_flash(dev, id, file): - hexdata = load_hex(file) +def verify_flash(dev, id, args): + hexdata = load_hex(args[0]) err = 0 flashsegments = list(create_flash_segments(hexdata, id['fs'], id['fss'])) - print >> sys.stderr, "" + print >> sys.stderr, "comparing flash with '%s'" % args[0] bar = progressbar(len(flashsegments), "verify flash") for segment in flashsegments: flashdata = cmd_read_flash_segment(dev, id, segment[0]) @@ -294,11 +298,10 @@ def verify_flash(dev, id, file): bar.end() print >> sys.stderr, "" if err != 0: - print >> sys.stderr, " ********* verify failed! ********" + print >> sys.stderr, " ********* verify failed! ******** .. exiting" sys.exit(-1) else: - print >> sys.stderr, " *********** verify ok! **********" - sys.exit(0) + print >> sys.stderr, " *********** verify ok! **********\n" commands = { 'boot': boot, @@ -317,30 +320,39 @@ if __name__ == '__main__': usage = '''spreadspace simple pic downloader. Usage: - python downloader.py [options] INFILE + python downloader.py [options] command [ command2 [ .. ] ] -Arguments: - INFILE name of hex file for downloading. - Use '-' for reading from stdin. + 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. + 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. + If you don't specify any command the downloader will connect to the + bootloader print some info and exit. Options: -h, --help this help message. -v, --version version info. - --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. + +Commands: + --write= write to flash (use '-' for stdin). + --verify= compare flash with (use '-' for stdin). + --read= read flash and store in (use '-' for stdout). + --boot boot to user code ''' device = "/dev/ttyUSB0" baudrate = 57600 name = None - cmd = None + cmds = [] try: - opts, args = getopt.getopt(sys.argv[1:], "hv", - ["help", "version", "cmd=", "device=", "baud=", "name="]) - + opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "version", "device=", "baud=", "name=", \ + "write=", "read=", "verify=", "boot" ]) for o, a in opts: if o in ("-h", "--help"): print >> sys.stderr, usage @@ -348,23 +360,21 @@ Options: elif o in ("-v", "--version"): print >> sys.stderr, "Version %d.%d" % (VERSION_MAJ, VERSION_MIN) sys.exit(0) - elif o in ("--cmd"): - cmd = a - elif o in ("--device"): + elif o == "--device": device = a - elif o in ("--baud"): + elif o == "--baud": baudrate = a - elif o in ("--name"): + elif o == "--name": name = a - - if not args: - raise getopt.GetoptError('Hex file is not specified') + else: + cmds.append({ 'name': o[2:], 'args': a.split(':') }); if len(args) > 1: raise getopt.GetoptError('Too many arguments') - if not cmd: - raise getopt.GetoptError('You have to supply a command (boot,read,write,verify)') + if len(cmds) == 0: + print "WARNING: no commands specified" + sys.exit except getopt.GetoptError, msg: print >> sys.stderr, "ERROR: %s" % msg @@ -375,6 +385,7 @@ Options: id = cmd_identify(dev, name) try: - commands[cmd](dev, id, args[0]) + for cmd in cmds: + commands[cmd['name']](dev, id, cmd['args']) except KeyError: - print >> sys.stderr, "ERROR: unkown command '%s'" % cmd + print >> sys.stderr, "ERROR: unkown command '%s'" % cmd['name'] -- cgit v1.2.3