]> defiant.homedns.org Git - pyshared.git/blobdiff - bootloader.py
Bootloader: Allow to set i2c addr
[pyshared.git] / bootloader.py
index a23828710b81a6a9c650ac28d7533a40c8bbc735..60e645a38de61a715e478b6c48cbf07b98f09711 100755 (executable)
@@ -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:
@@ -91,17 +92,19 @@ class bootloader:
                        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
-                               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
@@ -161,9 +164,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)
@@ -177,6 +183,7 @@ if __name__ == "__main__":
        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 not args:
@@ -187,6 +194,9 @@ if __name__ == "__main__":
                        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: