diff options
author | Christian Pointner <equinox@spreadspace.org> | 2013-07-07 23:23:03 +0000 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2013-07-07 23:23:03 +0000 |
commit | f69b7ef1f8b03609ad09453e484760ef7cc48b9e (patch) | |
tree | b2e8b52fbb0c5fdd36b9383a548636c9728e5f10 | |
parent | identify now checks if the downloader is connected to the right device (diff) |
basic commands work now
git-svn-id: https://svn.spreadspace.org/pic/trunk@55 a09c6847-51d9-44de-8ef2-e725cf50f3c7
-rwxr-xr-x | downloader/downloader.py | 81 |
1 files changed, 73 insertions, 8 deletions
diff --git a/downloader/downloader.py b/downloader/downloader.py index 48e6836..a07e15d 100755 --- a/downloader/downloader.py +++ b/downloader/downloader.py @@ -50,6 +50,23 @@ def load_hex(file): return codedata +def write_hex(file, codedata): + from intelhex import IntelHex + + fin = file + if fin == '-': + fin = sys.stdout + + hexdata = {} + for a in codedata: + hexdata[2*a] = codedata[a] & 0xFF + hexdata[2*a+1] = codedata[a] >> 8 + + ih = IntelHex() + ih.fromdict(hexdata) + ih.write_hex_file(fin) + + def get_lowest_flash_addr(codedata, fss): lowest_code_addr = sorted(codedata.keys())[0] return lowest_code_addr - (lowest_code_addr%fss) @@ -74,7 +91,7 @@ def create_flash_segments(codedata, fs, fss): img = create_flash_image(codedata, fss, sa, ea) for i in xrange(0, ea, fss): - slice = img[1][i:i+fss] + slice = tuple(img[1][i:i+fss]) if not all( (elem == 0xFFFF) for elem in slice): yield (i, slice) @@ -160,7 +177,7 @@ def exec_command(dev, cmd, param, answer): return struct.unpack_from(answer, astr, 3) -### Commands +### low level commands def identify(dev, name): data = exec_command(dev, 1, '', '<BB3sHHBHBH') @@ -212,6 +229,47 @@ def write_config(dev, id, nr, word): return exec_command(dev, 9, param, '<') +### commands +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]) + +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)) + a = addr + for d in data: + codedata[a] = d + a += 1 + + write_hex(file, codedata) + +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]) + break +# else: +# print "%05X: ok" % (segment[0]) + if err != 0: + print "ERROR: verify failed!" + sys.exit(-1) + + +commands = { + 'write': write_flash, + 'read': read_flash, + 'verify': verify_flash +} + ### Main if __name__ == '__main__': @@ -231,6 +289,7 @@ Arguments: Options: -h, --help this help message. -v, --version version info. + --cmd=N the command to execute, one out of: "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. @@ -239,10 +298,11 @@ Options: device = "/dev/ttyUSB0" baudrate = 57600 name = None + cmd = None try: opts, args = getopt.getopt(sys.argv[1:], "hv", - ["help", "version", "device=", "baud=", "name="]) + ["help", "version", "cmd=", "device=", "baud=", "name="]) for o, a in opts: if o in ("-h", "--help"): @@ -251,6 +311,8 @@ Options: elif o in ("-v", "--version"): print("Version %d.%d" % (VERSION_MAJ, VERSION_MIN)) sys.exit(0) + elif o in ("--cmd"): + cmd = a elif o in ("--device"): device = a elif o in ("--baud"): @@ -259,19 +321,22 @@ Options: name = a if not args: - raise getopt.GetoptError('Input file is not specified') + raise getopt.GetoptError('Hex file is not specified') if len(args) > 1: raise getopt.GetoptError('Too many arguments') + if not cmd: + raise getopt.GetoptError('You have to supply a command (read,write,verify)') + except getopt.GetoptError, msg: print "ERROR: %s" % msg print usage sys.exit(2) dev = open_serial(device, baudrate) - codedata = load_hex(args[0]) - id = identify(dev, name) - for segment in create_flash_segments(codedata, id['fs'], id['fss']): - print "%05X: %s" % (segment[0], ''.join('%04X'%i for i in segment[1])) + try: + commands[cmd](dev, id, args[0]) + except KeyError: + print "ERROR: unkown command '%s'" % cmd |