Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for segment addresing in output hex files #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions intelhex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] = 4 # rectyp
high_ofs = int(cur_addr>>16)
b = divmod(high_ofs, 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
Expand Down Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions intelhex/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@
:0100000002FD
:00000001FF
"""

shex64k = """:020000020000FC
:0100000001FE
:020000021000EC
:0100000002FD
:00000001FF
"""

data64k = {0: 1, 0x10000: 2}


Expand Down Expand Up @@ -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()
Expand Down