From be691f3e7478d5e8a95a228f5fc0b4fb41c0bc68 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 2 Jul 2013 22:59:18 +0000 Subject: functions to generate flash segments git-svn-id: https://svn.spreadspace.org/pic/trunk@23 a09c6847-51d9-44de-8ef2-e725cf50f3c7 --- downloader/downloader.py | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) (limited to 'downloader/downloader.py') diff --git a/downloader/downloader.py b/downloader/downloader.py index 5380b4d..c6cb3ed 100755 --- a/downloader/downloader.py +++ b/downloader/downloader.py @@ -71,17 +71,41 @@ def load_hex(file): print "ERROR: File not found: %s" % fin sys.exit(1) - data = {} + codedata = {} ih = IntelHex(fin) for a in ih.addresses(): - if a/2 not in data.keys(): - data[a/2] = 0 + if a/2 not in codedata.keys(): + codedata[a/2] = 0 if a%2 == 0: - data[a/2] += ih[a] + codedata[a/2] += ih[a] else: - data[a/2] += (ih[a] << 8) + codedata[a/2] += (ih[a] << 8) + + return codedata + +def get_lowest_flash_addr(codedata, fss): + lowest_code_addr = sorted(codedata.keys())[0] + return lowest_code_addr - (lowest_code_addr%fss) + +def get_highest_flash_addr(codedata, fss): + highest_code_addr = sorted(codedata.keys())[-1] + return highest_code_addr + (fss - highest_code_addr%fss) + +def create_flash_image(codedata, fss, sa, ea): + img = ( sa, [0xFFFF]*(ea-sa) ) + for a,d in codedata.items(): + img[1][a-sa] = d + return img + +def create_flash_segments(codedata, fss): + sa = get_lowest_flash_addr(codedata, fss) + ea = get_highest_flash_addr(codedata, fss) + img = create_flash_image(codedata, fss, sa, ea) + for i in xrange(0, ea, fss): + slice = img[1][i:i+fss] + if not all( (elem == 0xFFFF) for elem in slice): + yield (i, slice) - return data def calc_csum(str): cs = 0 @@ -89,7 +113,6 @@ def calc_csum(str): cs ^= ord(c) return chr(cs) - def exec_command(dev, cmd, answer): import struct @@ -196,11 +219,8 @@ Options: sys.exit(2) dev = open_serial(device, baudrate) - data = load_hex(args[0]) - - addrs = data.keys() - addrs.sort() - for a in addrs: - print "%05d: %04X" % (a, data[a]) - + 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])) -- cgit v1.2.3