diff --git a/mik32_parsers.py b/mik32_parsers.py index 14ace0a..746b69d 100644 --- a/mik32_parsers.py +++ b/mik32_parsers.py @@ -40,11 +40,15 @@ def parse_hex_line(line: str) -> Record: rectype = int(line[7:9], base=16) # Record type field data_bytes_line = line[9:datalen*2 + 9] # Data field crc = int(line[datalen*2 + 9:datalen*2 + 11], base=16) + splitted_by_bytes: List[str] = [] for i in range(datalen): splitted_by_bytes.append(data_bytes_line[i*2:i*2+2]) data_bytes = list(map(lambda x: int(x, base=16), splitted_by_bytes)) + checksum = (datalen + int(line[3:5], base=16) + int(line[5:7], base=16) + rectype + sum(data_bytes)) % 256 + if (checksum + crc) % 256 != 0: + raise Exception("Checksum mismatch in %s" % line) record = Record(RecordType.UNKNOWN, 0, []) @@ -175,11 +179,11 @@ def parse_hex(file: str) -> Dict: def bytes2words(arr: List[int]) -> List[int]: - word = [] + bytes = [] words = [] for byte in arr: - word.append(byte) - if word.__len__() == 4: - words.append(word[0]+2**8*word[1]+2**16*word[2]+2**24*word[3]) - word = [] + bytes.append(byte) + if bytes.__len__() == 4: + words.append(bytes[0]+2**8*bytes[1]+2**16*bytes[2]+2**24*bytes[3]) + bytes = [] return words diff --git a/mik32_upload.py b/mik32_upload.py index bf8acbf..8e805d1 100644 --- a/mik32_upload.py +++ b/mik32_upload.py @@ -37,8 +37,13 @@ def test_connection(): raise Exception("ERROR: no regs found, check MCU connection") -def read_file(filename: str) -> List[Record]: - segments: List[Record] = [] +@dataclass +class Segment: + offset: int + data: List[int] + +def read_file(filename: str) -> List[Segment]: + segments: List[Segment] = [] lines: List[str] = [] file_name, file_extension = os.path.splitext(filename) @@ -52,13 +57,25 @@ def read_file(filename: str) -> List[Record]: else: raise Exception("Unsupported file format: %s" % (file_extension)) + lba: int = 0 # Linear Base Address + expect_address = 0 # Address of the next byte + for line in lines: record: Record = parse_line(line, file_extension) - print(record) if record.type == RecordType.DATA: - pass + drlo: int = record.address # Data Record Load Offset + if segments.__len__() == 0: + expect_address = lba+drlo + segments.append(Segment(offset=expect_address, data=[])) + if expect_address != lba+drlo: + expect_address = lba+drlo + segments.append(Segment(offset=expect_address, data=[])) + + for byte in record.data: + segments[-1].data.append(byte) + expect_address += 1 elif record.type == RecordType.EXTADDR: - pass + lba = record.address elif record.type == RecordType.LINEARSTARTADDR: print("Start Linear Address:", record.address)