forked from jsnyder/stm32loader
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Naive implementation, assuming the addresses start at 0 and increment.
- Loading branch information
Showing
6 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Load binary data from a file in Intel hex format.""" | ||
|
||
from stm32loader.bootloader import MissingDependencyError | ||
|
||
try: | ||
import intelhex | ||
except ImportError: | ||
intelhex = None | ||
|
||
|
||
def load_hex(file_path: str) -> bytes: | ||
""" | ||
Return bytes from the given hex file. | ||
Addresses should start at zero and always increment. | ||
""" | ||
if intelhex is None: | ||
raise MissingDependencyError( | ||
"Please install package 'intelhex' in order to read .hex files." | ||
) | ||
|
||
hex_content = intelhex.IntelHex() | ||
hex_content.loadhex(str(file_path)) | ||
hex_dict = hex_content.todict() | ||
|
||
addresses = list(hex_dict.keys()) | ||
assert addresses[0] == 0 | ||
assert addresses[-1] == len(addresses) - 1 | ||
|
||
return bytes(hex_content.todict().values()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:10000000000102030405060708090A0B0C0D0E0F78 | ||
:00000001FF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
from pathlib import Path | ||
|
||
HERE = Path(__file__).parent | ||
DATA = HERE / "../data" | ||
|
||
|
||
from stm32loader.hexfile import load_hex | ||
|
||
|
||
def test_load_hex_delivers_bytes(): | ||
small_hex_path = DATA / "small.hex" | ||
data = load_hex(small_hex_path) | ||
assert data == bytes(range(16)) |