Skip to content

Commit 557840f

Browse files
committed
Added python tool for converting a .hex file into the appropriate C++ code.
Merge branch 'NicoHood-python'
2 parents 6acd28e + d11b04d commit 557840f

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,12 @@ Arduino Target chip
361361
Not connected on target: pin 3.
362362
```
363363

364+
convertHexToByteArray.py
365+
------------------------
364366

367+
The `convertHexToByteArray.py` tool allows you to easily convert `.hex` files
368+
into a c array that you can use inside the tools above. Its an alternative to
369+
the lua script and easier to setup in most cases.
370+
371+
To run the script you need `python2` and the `intelhex` python library which can
372+
be downloaded [here](https://github.com/bialix/intelhex) or via `pip`.

convertHexToByteArray.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python2
2+
3+
# Import initelhex: https://pythonhosted.org/IntelHex/index.html
4+
import sys
5+
import os
6+
import hashlib
7+
from intelhex import IntelHex
8+
9+
def main():
10+
# Check if one input argument is provided
11+
if len(sys.argv) != 2:
12+
print "Usage:", sys.argv[0], "bootloader.hex"
13+
sys.exit(1)
14+
15+
# Input arguments
16+
hexfile = sys.argv[1]
17+
18+
# Check if file exists
19+
if not os.path.isfile(hexfile):
20+
print "Error: File does not exist."
21+
sys.exit(2)
22+
23+
# Read bootloader data
24+
bootloader = IntelHex(hexfile)
25+
bootloader_bin = bootloader.tobinarray()
26+
#bootloader.dump()
27+
28+
# Start address
29+
loaderStart = bootloader.minaddr()
30+
31+
# Given a start address, deduce where the bootloader ends
32+
end_addresses = {
33+
0x1000: 0x2000,
34+
0x1C00: 0x2000,
35+
0x1D00: 0x2000,
36+
0x1E00: 0x2000,
37+
0x3000: 0x4000,
38+
0x3800: 0x4000,
39+
0x3E00: 0x4000,
40+
0x7000: 0x8000,
41+
0x7800: 0x8000,
42+
0x7E00: 0x8000,
43+
0xF800: 0x10000,
44+
0x1F000: 0x20000,
45+
0x1FC00: 0x20000,
46+
0x3E000: 0x40000,
47+
}
48+
if not loaderStart in end_addresses:
49+
print "Error: Unkown bootloader start address."
50+
sys.exit(3)
51+
loaderEnd = end_addresses[loaderStart]
52+
53+
# Len
54+
loaderLen = loaderEnd - loaderStart
55+
if loaderLen < len(bootloader_bin):
56+
print "Error: Invalid bootloader length."
57+
sys.exit(4)
58+
59+
# Calculate md5sum from hexfile and add padding bytes with 0xFF
60+
md5 = hashlib.md5()
61+
md5.update(bootloader_bin)
62+
padding = [0xFF] * (loaderLen - len(bootloader_bin))
63+
md5.update(bytearray(padding))
64+
65+
# Filename without full path and without ".hex"
66+
filename = os.path.splitext(os.path.basename(hexfile))[0]
67+
68+
# Print header
69+
print '// File =', filename + ".hex"
70+
print '// Loader start:', hex(loaderStart), 'length', loaderLen
71+
print '// MD5 sum =', md5.hexdigest()
72+
print
73+
print 'const uint8_t', filename + '_hex [] PROGMEM = {'
74+
75+
# Print data
76+
line = ""
77+
for i in range(len(bootloader_bin)):
78+
line += hex(bootloader_bin[i]) + ', '
79+
if i % 16 == 15:
80+
print line
81+
line = ''
82+
if line:
83+
print line
84+
85+
# Print footer
86+
print '}; // end of', filename + '_hex'
87+
88+
if __name__ == "__main__":
89+
main()

0 commit comments

Comments
 (0)