X-Git-Url: https://defiant.homedns.org/gitweb/?a=blobdiff_plain;f=bootloader.py;h=17cf64f3e486ac44fdffddbd5511a4b63fc1d8b0;hb=refs%2Fheads%2Fmaster;hp=04370cd8b30a6eafc5d3c345a6fbcd2e9ea5730e;hpb=d5c8457c07ed2888798352e565556166d6b1d4a4;p=pyshared.git diff --git a/bootloader.py b/bootloader.py index 04370cd..17cf64f 100755 --- a/bootloader.py +++ b/bootloader.py @@ -14,6 +14,7 @@ CMD_WRITE = 0x3 CMD_ERASE_ALL = 0x5 CMD_JUMP = 0x6 CMD_PAGESIZE = 0x7 +CMD_I2C_ADDR = 0x80 CMD_INFO = 0x99 class bootloader: @@ -22,7 +23,7 @@ class bootloader: self.boot_addr = 0x0 if not self.identify(): - raise "Bootloader not running" + raise Exception("Bootloader not running") self.pagesize = self.get_pagesize() def read_mem(self, addr, num): @@ -36,12 +37,12 @@ class bootloader: self.run_cmd(CMD_ERASE_ALL) def __compare_memarea(self, addr, data): - mem_cmp = self.read_mem(addr, self.pagesize) - if mem_cmp != data: - print "Expected:", data.encode("hex") - print "Got: ", mem_cmp.encode("hex") - raise("Compare mismatch at 0x%x" % addr) - return + mem_cmp = self.read_mem(addr, self.pagesize) + if mem_cmp != data: + print "Expected:", data.encode("hex") + print "Got: ", mem_cmp.encode("hex") + raise Exception("Compare mismatch at 0x%x" % addr) + return def __program_memarea(self, addr, data): self.run_cmd(CMD_WRITE, addr, self.pagesize, data) @@ -62,7 +63,7 @@ class bootloader: count=0 for line in f: if line[0] != ':': - raise("Bad line start character") + raise Exception("Bad line start character") hex = line[1:].replace("\r\n", "") data = hex.decode("hex") num = ord(data[0]) @@ -70,14 +71,14 @@ class bootloader: for c in data: chksum+=ord(c) if chksum % 256 != 0: - raise("Checksum error") + raise Exception("Checksum error") addr, typ, data, chksum = struct.unpack(">HB%ssB" % num, data[1:]) if typ == 0: # Data Record count+=len(data) if next_addr is not None: if next_addr != addr: - raise "Gap in file" + raise Exception("Gap in file") buf_addr = addr-len(buf) buf+=data if len(buf) >= self.pagesize: @@ -87,22 +88,24 @@ class bootloader: else: print "Addr 0x%x" % buf_addr handle(buf_addr, buf[:self.pagesize]) + buf_addr += self.pagesize buf = buf[self.pagesize:] elif typ == 3: # Start Segment Address Record self.boot_addr = int(data.encode("hex"), 16) elif typ == 1: # End of File Record - print "Addr (rest) 0x%x" % buf_addr - buf_addr+=self.pagesize - diff = self.pagesize-len(buf) - buf+=chr(0xff)*diff # fill with 0xff - handle(buf_addr, buf[:self.pagesize]) - if lFirstRow: # was first - buf_addr = lFirstRow[0] - buf = lFirstRow[1] - print "Addr (First) 0x%x" % buf_addr - handle(buf_addr, buf) + if len(buf) > 0: + # Send unhandled data + print "Addr (rest) 0x%x" % buf_addr + diff = self.pagesize-len(buf) + buf+=chr(0xff)*diff # fill with 0xff + handle(buf_addr, buf[:self.pagesize]) + if lFirstRow: # was first + buf_addr = lFirstRow[0] + buf = lFirstRow[1] + print "Addr (First) 0x%x" % buf_addr + handle(buf_addr, buf) else: - raise("Unknown type %d" % typ) + raise Exception("Unknown type %d" % typ) next_addr = addr+num print "Byte count:", count @@ -133,8 +136,6 @@ class bootloader: t1 = time() self.compare(filename) print "Time: %.1fs" % (time() - t1) - print "Jump:" - self.jump(self.boot_addr) def write(self, s): dev = i2c(self.i2c_addr) @@ -164,9 +165,12 @@ class bootloader: s = self.read(1) i = ord(s[0]) if i not in [64, 128]: - raise + raise Exception("Unsupported pagesize") return i + def set_i2c_addr(self, addr): + self.run_cmd(CMD_I2C_ADDR, addr) + def to_bootloader(addr): dev = i2c(addr) @@ -179,13 +183,23 @@ if __name__ == "__main__": usage = "usage: %prog [options] addr [ihex]" parser = OptionParser(usage=usage) parser.add_option("-b", "--start-bootloader", action="store_true", dest="bToBoot", default=False, help="Start Bootloader") + parser.add_option("-j", "--jump", action="store_true", dest="bJump", default=False, help="Jump to Program") + parser.add_option("-s", "--set-i2c-addr", dest="i2c_addr", type="int", help="Set I2C address") (options, args) = parser.parse_args() - if len(args) > 0: + if not args: + print "Missing Address" + else: addr = int(args[0], 16) if options.bToBoot: to_bootloader(addr) sleep(1) loader = bootloader(addr) + if options.i2c_addr is not None: + print "Setting i2c address to 0x%X" % (options.i2c_addr) + loader.set_i2c_addr(options.i2c_addr) if len(args) > 1: loader.load(args[1]) + if options.bJump: + print "Jump to Program" + loader.jump(0x0)