Skip to content

Commit 86fe89f

Browse files
committed
Merge pull request #10 from https://github.com/erki1993/intelhex
2 parents 40d388a + 3c390ed commit 86fe89f

2 files changed

Lines changed: 90 additions & 4 deletions

File tree

intelhex/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2005-2016, Alexander Belchenko
1+
# Copyright (c) 2005-2018, Alexander Belchenko
22
# All rights reserved.
33
#
44
# Redistribution and use in source and binary forms,
@@ -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 value: %s" % 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: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2005-2016, Alexander Belchenko
1+
# Copyright (c) 2005-2018, Alexander Belchenko
22
# All rights reserved.
33
#
44
# Redistribution and use in source and binary forms,
@@ -1659,6 +1659,89 @@ 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+
self.assertRaises(ValueError, ih.write_hex_file, sio, byte_count=0)
1676+
self.assertRaises(ValueError, ih.write_hex_file, sio, byte_count=-1)
1677+
self.assertRaises(ValueError, ih.write_hex_file, sio, byte_count=256)
1678+
1679+
def test_write_hex_file_byte_count_1(self):
1680+
ih = intelhex.IntelHex(self.f)
1681+
ih1 = ih[:4]
1682+
sio = StringIO()
1683+
ih1.write_hex_file(sio, byte_count=1)
1684+
s = sio.getvalue()
1685+
sio.close()
1686+
# check that we have all data records with data length == 1
1687+
self.assertEqual((
1688+
':0100000002FD\n'
1689+
':0100010005F9\n'
1690+
':01000200A25B\n'
1691+
':01000300E517\n'
1692+
':00000001FF\n'
1693+
), s,
1694+
"Written hex is not in byte count 1")
1695+
# read back and check content
1696+
fin = StringIO(s)
1697+
ih2 = intelhex.IntelHex(fin)
1698+
self.assertEqual(ih1.tobinstr(), ih2.tobinstr(),
1699+
"Written hex file does not equal with original")
1700+
1701+
def test_write_hex_file_byte_count_13(self):
1702+
ih = intelhex.IntelHex(self.f)
1703+
sio = StringIO()
1704+
ih.write_hex_file(sio, byte_count=13)
1705+
s = sio.getvalue()
1706+
# control written hex first line to check that byte count is 13
1707+
sio.seek(0)
1708+
self.assertEqual(sio.readline(),
1709+
':0D0000000205A2E576246AF8E6057622786E\n',
1710+
"Written hex is not in byte count 13")
1711+
sio.close()
1712+
1713+
fin = StringIO(s)
1714+
ih2 = intelhex.IntelHex(fin)
1715+
1716+
self.assertEqual(ih.tobinstr(), ih2.tobinstr(),
1717+
"Written hex file does not equal with original")
1718+
1719+
def test_write_hex_file_byte_count_255(self):
1720+
ih = intelhex.IntelHex(self.f)
1721+
sio = StringIO()
1722+
ih.write_hex_file(sio, byte_count=255)
1723+
s = sio.getvalue()
1724+
# control written hex first line to check that byte count is 255
1725+
sio.seek(0)
1726+
self.assertEqual(sio.readline(),
1727+
(':FF0000000205A2E576246AF8E60576227867300702786AE475F0011204AD02'
1728+
'04552000EB7F2ED2008018EF540F2490D43440D4FF30040BEF24BFB41A0050'
1729+
'032461FFE57760021577057AE57A7002057930070D7867E475F0011204ADEF'
1730+
'02049B02057B7403D2078003E4C207F5768B678A688969E4F577F579F57AE5'
1731+
'7760077F2012003E80F57578FFC201C200C202C203C205C206C20812000CFF'
1732+
'700D3007057F0012004FAF7AAE7922B4255FC2D5C20412000CFF24D0B40A00'
1733+
'501A75F00A787730D50508B6FF0106C6A426F620D5047002D20380D924CFB4'
1734+
'1A00EF5004C2E5D20402024FD20180C6D20080C0D20280BCD2D580BAD20580'
1735+
'B47F2012003E20020774010E\n'),
1736+
"Written hex is not in byte count 255")
1737+
sio.close()
1738+
1739+
fin = StringIO(s)
1740+
ih2 = intelhex.IntelHex(fin)
1741+
1742+
self.assertEqual(ih.tobinstr(), ih2.tobinstr(),
1743+
"Written hex file does not equal with original")
1744+
16621745
##
16631746
# MAIN
16641747
if __name__ == '__main__':

0 commit comments

Comments
 (0)