Skip to content

Commit 7a5dae3

Browse files
committed
Merge pull request #10 from https://github.com/erki1993/intelhex.
2 parents 2e0fc7e + 736705e commit 7a5dae3

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

intelhex/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ def _get_eol_textfile(eolstyle, platform):
544544
raise ValueError("wrong eolstyle %s" % repr(eolstyle))
545545
_get_eol_textfile = staticmethod(_get_eol_textfile)
546546

547-
def write_hex_file(self, f, write_start_addr=True, eolstyle='native'):
547+
def write_hex_file(self, f, write_start_addr=True, eolstyle='native', byte_count=16):
548548
"""Write data to file f in HEX format.
549549
550550
@param f filename or file-like object for writing
@@ -555,7 +555,10 @@ def write_hex_file(self, f, write_start_addr=True, eolstyle='native'):
555555
@param eolstyle can be used to force CRLF line-endings
556556
for output file on different platforms.
557557
Supported eol styles: 'native', 'CRLF'.
558+
@param byte_count number of bytes in the data field
558559
"""
560+
if byte_count > 255 or byte_count < 1:
561+
raise ValueError("wrong byte_count " + str(byte_count))
559562
fwrite = getattr(f, "write", None)
560563
if fwrite:
561564
fobj = f
@@ -656,7 +659,7 @@ def write_hex_file(self, f, write_start_addr=True, eolstyle='native'):
656659
# produce one record
657660
low_addr = cur_addr & 0x0FFFF
658661
# chain_len off by 1
659-
chain_len = min(15, 65535-low_addr, maxaddr-cur_addr)
662+
chain_len = min(byte_count - 1, 65535-low_addr, maxaddr-cur_addr)
660663

661664
# search continuous chain
662665
stop_addr = cur_addr + chain_len

intelhex/test.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,77 @@ def test_sripts_hexdiff_version(self):
16591659
def test_sripts_hexmerge_version(self):
16601660
self.versionChecker('%s scripts/hexmerge.py --version')
16611661

1662+
1663+
class TestWriteHexFileByteCount(unittest.TestCase):
1664+
1665+
def setUp(self):
1666+
self.f = StringIO(hex8)
1667+
1668+
def tearDown(self):
1669+
self.f.close()
1670+
del self.f
1671+
1672+
def test_write_hex_file_bad_byte_count(self):
1673+
ih = intelhex.IntelHex(self.f)
1674+
sio = StringIO()
1675+
with self.assertRaises(ValueError):
1676+
ih.write_hex_file(sio, byte_count=0)
1677+
with self.assertRaises(ValueError):
1678+
ih.write_hex_file(sio, byte_count=-1)
1679+
with self.assertRaises(ValueError):
1680+
ih.write_hex_file(sio, byte_count=256)
1681+
1682+
def test_write_hex_file_byte_count_1(self):
1683+
ih = intelhex.IntelHex(self.f)
1684+
sio = StringIO()
1685+
ih.write_hex_file(sio, byte_count=1)
1686+
s = sio.getvalue()
1687+
# control written hex first line to check that byte count is 1
1688+
sio.seek(0)
1689+
self.assertEqual(sio.readline(), ':0100000002FD\n',
1690+
"Written hex is not in byte count 1")
1691+
sio.close()
1692+
1693+
fin = StringIO(s)
1694+
ih2 = intelhex.IntelHex(fin)
1695+
1696+
self.assertEqual(ih.tobinstr(), ih2.tobinstr(),
1697+
"Written hex file does not equal with original")
1698+
1699+
def test_write_hex_file_byte_count_13(self):
1700+
ih = intelhex.IntelHex(self.f)
1701+
sio = StringIO()
1702+
ih.write_hex_file(sio, byte_count=13)
1703+
s = sio.getvalue()
1704+
# control written hex first line to check that byte count is 13
1705+
sio.seek(0)
1706+
self.assertEqual(sio.readline(), ':0D0000000205A2E576246AF8E6057622786E\n',
1707+
"Written hex is not in byte count 1")
1708+
sio.close()
1709+
1710+
fin = StringIO(s)
1711+
ih2 = intelhex.IntelHex(fin)
1712+
1713+
self.assertEqual(ih.tobinstr(), ih2.tobinstr(),
1714+
"Written hex file does not equal with original")
1715+
1716+
def test_write_hex_file_byte_count_255(self):
1717+
ih = intelhex.IntelHex(self.f)
1718+
sio = StringIO()
1719+
ih.write_hex_file(sio, byte_count=255)
1720+
s = sio.getvalue()
1721+
# control written hex first line to check that byte count is 255
1722+
sio.seek(0)
1723+
self.assertEqual(sio.readline(), ':FF0000000205A2E576246AF8E60576227867300702786AE475F0011204AD0204552000EB7F2ED2008018EF540F2490D43440D4FF30040BEF24BFB41A0050032461FFE57760021577057AE57A7002057930070D7867E475F0011204ADEF02049B02057B7403D2078003E4C207F5768B678A688969E4F577F579F57AE57760077F2012003E80F57578FFC201C200C202C203C205C206C20812000CFF700D3007057F0012004FAF7AAE7922B4255FC2D5C20412000CFF24D0B40A00501A75F00A787730D50508B6FF0106C6A426F620D5047002D20380D924CFB41A00EF5004C2E5D20402024FD20180C6D20080C0D20280BCD2D580BAD20580B47F2012003E20020774010E\n',
1724+
"Written hex is not in byte count 1")
1725+
sio.close()
1726+
1727+
fin = StringIO(s)
1728+
ih2 = intelhex.IntelHex(fin)
1729+
1730+
self.assertEqual(ih.tobinstr(), ih2.tobinstr(),
1731+
"Written hex file does not equal with original")
1732+
16621733
##
16631734
# MAIN
16641735
if __name__ == '__main__':

0 commit comments

Comments
 (0)