diff options
author | Christian Pointner <equinox@spreadspace.org> | 2013-07-04 22:28:25 +0000 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2013-07-04 22:28:25 +0000 |
commit | 5b75aff608197bd4b229e89274988ebdfb522a80 (patch) | |
tree | 8e433e1d8bab8eec8d6d4f991f9250bb64db1ea8 /downloader/downloader.py | |
parent | cleanups (diff) |
fixed issues with short answers
bootloader now has working stubs for all commands
git-svn-id: https://svn.spreadspace.org/pic/trunk@36 a09c6847-51d9-44de-8ef2-e725cf50f3c7
Diffstat (limited to 'downloader/downloader.py')
-rwxr-xr-x | downloader/downloader.py | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/downloader/downloader.py b/downloader/downloader.py index 00b50a4..3a66f3b 100755 --- a/downloader/downloader.py +++ b/downloader/downloader.py @@ -89,7 +89,7 @@ def open_serial(device, baud): 9600: termios.B9600, 19200: termios.B19200, 38400: termios.B38400, 57600: termios.B57600, 115200: termios.B115200, 230400: termios.B230400 } - baudreate = termios.B19200 + baudreate = termios.B57600 try: baudrate = baudrates[int(baud)] except (KeyError, ValueError): @@ -123,17 +123,15 @@ def exec_command(dev, cmd, answer): return_codes = { 0: "OK", 1: "invalid command", 2: "bad checksum", 3: "not implemented", 4: "flash write error", - 5: "address invalid", 6: "address prohibited" } - - answer = '<' + answer + 'B' - answer_len = struct.calcsize(answer) + 2 + 5: "address invalid", 6: "address prohibited", + 7: "value out of bounds" } cstr = cmd + chr(calc_csum(cmd)) os.write(dev, cstr) astr = b'' - while len(astr) < 2: - astr += os.read(dev, 2 - len(astr)) + while len(astr) < 3: + astr += os.read(dev, 3 - len(astr)) if astr[0] != cmd[0]: print "ERROR: bootloader returned wrong command code" @@ -149,6 +147,7 @@ def exec_command(dev, cmd, answer): print "ERROR: bootloader returned %d: %s" % (ret, rstr) sys.exit(4) + answer_len = struct.calcsize(answer) + len(astr) while len(astr) < answer_len: astr += os.read(dev, answer_len - len(astr)) @@ -161,7 +160,7 @@ def exec_command(dev, cmd, answer): ### Commands def identify(dev): - data = exec_command(dev, 'i', 'BB10sHBHH') + data = exec_command(dev, 'i', '<BB10sHBHH') id = { 'ver_min': data[0], 'ver_maj': data[1], 'name': data[2], 'devid': data[3], 'fss': data[4], 'mess': data[5], 'supported': data[6] } @@ -176,35 +175,35 @@ def identify(dev): return id def boot(dev): - exec_command(dev, 'b', '') + exec_command(dev, 'b', '<') def reset(dev, id): - exec_command(dev, 'r', '') + exec_command(dev, 'r', '<') def read_flash_segment(dev, id, addr): cmd = struct.pack('<cH', 'f', addr) - return exec_command(dev, cmd, '%dH' % id['fss']) + return exec_command(dev, cmd, '<%dH' % id['fss']) def write_flash_segment(dev, id, addr, data): cmd = struct.pack('<cH%dH' % id['fss'], 'F', addr, *data) - return exec_command(dev, cmd, '') + return exec_command(dev, cmd, '<') def read_eeprom(dev, id, addr, len): cmd = struct.pack('<cHH', 'e', addr, len) - return exec_command(dev, cmd, '%dH' % len) + return exec_command(dev, cmd, '<%dB' % len) -def write_eeprom(dev, id, addr, len, data): - cmd = struct.pack('<cHH%dH' % len, 'E', addr, len, *data) - return exec_command(dev, cmd, '') +def write_eeprom(dev, id, addr, data): + cmd = struct.pack('<cHH%dB' % len(data), 'E', addr, len(data), *data) + return exec_command(dev, cmd, '<') def read_config(dev, id, nr): cmd = struct.pack('<cB', 'c', nr) - data = exec_command(dev, cmd, 'H') + data = exec_command(dev, cmd, '<H') return data[0] def write_config(dev, id, nr, word): cmd = struct.pack('<cBH', 'C', nr, word) - return exec_command(dev, cmd, '') + return exec_command(dev, cmd, '<') ### Main @@ -227,11 +226,11 @@ Options: -h, --help this help message. -v, --version version info. --device=N the serial port to use (default: /dev/ttyUSB0). - --baud=N baudrate to use (default: 19200). + --baud=N baudrate to use (default: 57600). ''' device = "/dev/ttyUSB0" - baudrate = 19200 + baudrate = 57600 try: opts, args = getopt.getopt(sys.argv[1:], "hv", @@ -264,5 +263,5 @@ Options: codedata = load_hex(args[0]) id = identify(dev) - for segment in create_flash_segments(codedata, id['fss']): - print "%05X: %s" % (segment[0], ''.join('%04X'%i for i in segment[1])) + # for segment in create_flash_segments(codedata, id['fss']): + # print "%05X: %s" % (segment[0], ''.join('%04X'%i for i in segment[1])) |