Skip to content

Commit 5b0daad

Browse files
committed
PSOC6.py: do not require metadata during HEX merging
Replace hard-coded numeric offsets of PSoC 6 hex file sections with sensible constants. Do not attempt to update the checksum and metadata contents if the sections are not found in the original HEX file.
1 parent 226edc1 commit 5b0daad

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

tools/targets/PSOC6.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,71 @@
2020
import errno
2121
from array import array
2222
from struct import (pack, unpack)
23-
from distutils.spawn import find_executable
2423
from shutil import copyfile
2524
from intelhex import IntelHex
2625
from intelhex.compat import asbytes
2726

2827
from ..config import ConfigException
2928

29+
# The size of the program data in Cypress HEX files is limited to 0x80000000
30+
# Higher addresses contain additional metadata (chip protection, eFuse data, etc..)
31+
CY_PROGRAM_SIZE = 0x80000000
32+
33+
# The starting address of the program data checksum section
34+
CY_CHECKSUM_ADDR = 0x90300000
35+
36+
# The starting address of the .cymeta section (12 bytes)
37+
# Additional metadata include silicon revision, Silicon/JTAG ID, etc.
38+
CY_META_ADDR = 0x90500000
39+
40+
# The address of the silicon ID (4 bytes)
41+
CY_META_SILICON_ID_ADDR = 0x90500002
42+
43+
# The address of the metadata checksum (4 bytes)
44+
CY_META_CHECKSUM_ADDR = 0x90500008
45+
3046

3147
# Patch Cypress hex file:
3248
# - update checksum
3349
# - update metadata
3450
# - align regions to page (256 bytes) boundary
3551
def patch(message_func, ihex, hexf, align=256):
36-
#calculate checksum
52+
update_checksum = False
53+
update_metadata = False
54+
55+
# calculate checksum of the program section, detect metadata
3756
checksum = 0
3857
for start, end in ihex.segments():
39-
if start >= 0x090000000:
58+
if start == CY_CHECKSUM_ADDR:
59+
# checksum section found in the original hex
60+
update_checksum = True
61+
if start == CY_META_ADDR:
62+
# metadata section found in the original hex
63+
update_metadata = True
64+
if start >= CY_PROGRAM_SIZE:
4065
continue
4166
segment = ihex.tobinarray(start, end)
4267
checksum += sum(segment)
4368

44-
lowchecksum = checksum & 0x0FFFF
45-
message_func("Calculated checksum for %s is 0x%04x" % (hexf, lowchecksum))
69+
# only update checksum if it was found in the original hex
70+
if update_checksum:
71+
lowchecksum = checksum & 0x0FFFF
72+
message_func("Calculated checksum for %s is 0x%04x" % (hexf, lowchecksum))
4673

47-
# update checksum
48-
checksum_str = pack('>H', lowchecksum)
49-
ihex.frombytes(array('B', checksum_str), offset=0x90300000)
74+
checksum_str = pack('>H', lowchecksum)
75+
ihex.frombytes(array('B', checksum_str), offset=CY_CHECKSUM_ADDR)
5076

51-
# update metadata
52-
signature = unpack('>L', ihex.tobinstr(start=0x90500002, size=4))[0]
53-
sigcheck = pack('>L', (checksum + signature) & 0x0FFFF)
54-
ihex.frombytes(array('B',sigcheck), offset=0x90500008)
77+
# only update metadata if it was found in the original hex
78+
if update_metadata:
79+
signature = unpack('>L', ihex.tobinstr(start=CY_META_SILICON_ID_ADDR, size=4))[0]
80+
sigcheck = pack('>L', (checksum + signature) & 0x0FFFF)
81+
ihex.frombytes(array('B',sigcheck), offset=CY_META_CHECKSUM_ADDR)
5582

5683
# align flash segments
5784
align_mask = align - 1
5885
alignments = IntelHex()
5986
for start, end in ihex.segments():
60-
if start >= 0x090000000:
87+
if start >= CY_PROGRAM_SIZE:
6188
continue
6289
aligned_start = start & ~align_mask
6390
if start != aligned_start:

0 commit comments

Comments
 (0)