From 32d39c0533fcfc85f44a2922325b737cca4a75cb Mon Sep 17 00:00:00 2001 From: Mateusz Majchrzycki Date: Wed, 20 Apr 2016 14:23:24 +0200 Subject: [PATCH 1/3] Linear address records replaced with segment addres in output fromatting --- intelhex/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/intelhex/__init__.py b/intelhex/__init__.py index 8af27c3..7425bdf 100644 --- a/intelhex/__init__.py +++ b/intelhex/__init__.py @@ -625,9 +625,9 @@ def write_hex_file(self, f, write_start_addr=True): bin[0] = 2 # reclen bin[1] = 0 # offset msb bin[2] = 0 # offset lsb - bin[3] = 4 # rectyp + bin[3] = 2 # rectyp high_ofs = int(cur_addr>>16) - b = divmod(high_ofs, 256) + b = divmod(cur_addr/16, 256) bin[4] = b[0] # msb of high_ofs bin[5] = b[1] # lsb of high_ofs bin[6] = (-sum(bin)) & 0x0FF # chksum From 3284fbc1fdad98112904d180cd1f7c30152c4635 Mon Sep 17 00:00:00 2001 From: Mateusz Majchrzycki Date: Wed, 20 Apr 2016 15:08:24 +0200 Subject: [PATCH 2/3] Added support for linear and segment addresing modes in output hex files --- intelhex/__init__.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/intelhex/__init__.py b/intelhex/__init__.py index 7425bdf..2c20faa 100644 --- a/intelhex/__init__.py +++ b/intelhex/__init__.py @@ -532,7 +532,7 @@ def __len__(self): """Return count of bytes with real values.""" return len(dict_keys(self._buf)) - def write_hex_file(self, f, write_start_addr=True): + def write_hex_file(self, f, write_start_addr=True, linear_mode=True): """Write data to file f in HEX format. @param f filename or file-like object for writing @@ -625,9 +625,13 @@ def write_hex_file(self, f, write_start_addr=True): bin[0] = 2 # reclen bin[1] = 0 # offset msb bin[2] = 0 # offset lsb - bin[3] = 2 # rectyp high_ofs = int(cur_addr>>16) - b = divmod(cur_addr/16, 256) + if linear_mode: + bin[3] = 4 # rectyp + b = divmod(high_ofs, 256) + else: + bin[3] = 2 # rectyp + b = divmod(cur_addr/16, 256) bin[4] = b[0] # msb of high_ofs bin[5] = b[1] # lsb of high_ofs bin[6] = (-sum(bin)) & 0x0FF # chksum @@ -691,12 +695,16 @@ def write_hex_file(self, f, write_start_addr=True): def tofile(self, fobj, format): """Write data to hex or bin file. Preferred method over tobin or tohex. + "hex" format uses linear addressing mode (04 records) + "shex" format uses segmented addressing mode (02 records instead of 04) @param fobj file name or file-like object - @param format file format ("hex" or "bin") + @param format file format ("hex", "shex" or "bin") """ if format == 'hex': self.write_hex_file(fobj) + elif format == 'shex': + self.write_hex_file(fobj, linear_mode=False) elif format == 'bin': self.tobinfile(fobj) else: From 998a1bd3129d0d941e65574f96b4176b70b02425 Mon Sep 17 00:00:00 2001 From: Mateusz Majchrzycki Date: Thu, 21 Apr 2016 09:07:24 +0200 Subject: [PATCH 3/3] Added unit test for writing segmented hex file (shex format) --- intelhex/test.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/intelhex/test.py b/intelhex/test.py index af8ee83..e226bac 100755 --- a/intelhex/test.py +++ b/intelhex/test.py @@ -331,6 +331,14 @@ :0100000002FD :00000001FF """ + +shex64k = """:020000020000FC +:0100000001FE +:020000021000EC +:0100000002FD +:00000001FF +""" + data64k = {0: 1, 0x10000: 2} @@ -543,6 +551,15 @@ def test_write_hexfile(self): sio.close() self.assertEqualWrittenData(hex_simple, s) + def test_write_shexfile(self): + ih = intelhex.IntelHex(data64k) + # new API: .tofile universal method + sio = StringIO() + ih.tofile(sio, format='shex') + s = sio.getvalue() + sio.close() + self.assertEqualWrittenData(shex64k, s) + def test_write_hex_bug_341051(self): ih = intelhex.IntelHex(StringIO(hex_bug_lp_341051)) sio = StringIO()