chiark / gitweb /
Updated all lineends for py files to unix style.
[cura.git] / Cura / avr_isp / intelHex.py
1 import io
2
3 def readHex(filename):
4         data = []
5         extraAddr = 0
6         f = io.open(filename, "r")
7         for line in f:
8                 line = line.strip()
9                 if line[0] != ':':
10                         raise Exception("Hex file has a line not starting with ':'")
11                 recLen = int(line[1:3], 16)
12                 addr = int(line[3:7], 16) + extraAddr
13                 recType = int(line[7:9], 16)
14                 if len(line) != recLen * 2 + 11:
15                         raise Exception("Error in hex file: " + line)
16                 checkSum = 0
17                 for i in xrange(0, recLen + 5):
18                         checkSum += int(line[i*2+1:i*2+3], 16)
19                 checkSum &= 0xFF
20                 if checkSum != 0:
21                         raise Exception("Checksum error in hex file: " + line)
22                 
23                 if recType == 0:#Data record
24                         while len(data) < addr + recLen:
25                                 data.append(0)
26                         for i in xrange(0, recLen):
27                                 data[addr + i] = int(line[i*2+9:i*2+11], 16)
28                 elif recType == 1:      #End Of File record
29                         pass
30                 elif recType == 2:      #Extended Segment Address Record
31                         extraAddr = int(line[9:13], 16) * 16
32                 else:
33                         print(recType, recLen, addr, checkSum, line)
34         f.close()
35         return data