Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
#
# Firmware prep utility for Fadecandy production.
#
# This tool bundles a bootloader and firmware image into a source
# file that can be included in the "production" firmware for the testjig.
#
########################### Configuration ###########################
# Paths for all .hex files to include
SOURCE_FILES = [
"../bin/fc-boot-v101.hex",
"../firmware/fc-firmware.hex",
]
# Where to generate output
OUTPUT_FILE = "production/firmware_data.h"
#####################################################################
import intelhex, time, hashlib, struct
# Flash memory sector size
SECTOR_SIZE = 1024
output = open(OUTPUT_FILE, 'w')
output.write("""/*
* Firmware data for Fadecandy production.
* AUTOMATICALLY GENERATED by firmwareprep.py
*
* Date: %s
*
* Source files:
%s *
* Copyright (c) 2013 Micah Elizabeth Scott
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
""" % (time.asctime(),
''.join([
" * %s (%s)\n" %
(f, hashlib.sha1(open(f, 'rb').read()).hexdigest())
for f in SOURCE_FILES
])))
# Load all .hex files into a flat memory image
loader = intelhex.IntelHex()
for path in SOURCE_FILES:
loader.loadhex(path)
image = loader.tobinstr()
# Pad to a sector boundary
numSectors = (len(image) + SECTOR_SIZE - 1) // SECTOR_SIZE
numBytes = numSectors * SECTOR_SIZE
numWords = numBytes / 4
image += chr(loader.padding) * (numBytes - len(image))
output.write("static const unsigned firmwareSectorCount = %d;\n" % numSectors)
output.write("static const uint32_t firmwareData[%d] = {\n" % numWords)
wordsPerLine = 4
numLines = numWords / wordsPerLine
for line in range(numLines):
addr = line * wordsPerLine * 4
words = [
"0x%08x, " % struct.unpack('<I',
image[addr+4*x:addr+4*(x+1)]
)[0]
for x in range(wordsPerLine)
]
output.write(" %s // 0x%08x\n" % (''.join(words), addr))
output.write("};\n")