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):
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)
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])
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:
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
s = self.read(1)
i = ord(s[0])
if i not in [64, 128]:
- raise
+ raise Exception("Unsupported pagesize")
return i