Skip to content

Commit 7f8ebc7

Browse files
author
Cruz Monrreal
authored
Merge pull request #9466 from vmedcy/psoc6-target-hook
Improve PSoC 6 post-build hooks, whitelist makefile export
2 parents b49d949 + a48ee11 commit 7f8ebc7

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

targets/targets.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7839,12 +7839,11 @@
78397839
},
78407840
"FUTURE_SEQUANA": {
78417841
"inherits": ["MCU_PSOC6_M4"],
7842-
"sub_target": "FUTURE_SEQUANA_M0",
78437842
"supported_form_factors": ["ARDUINO"],
78447843
"extra_labels_add": ["CY8C63XX", "CORDIO"],
78457844
"macros_add": ["CY8C6347BZI_BLD53"],
78467845
"detect_code": ["6000"],
7847-
"m0_core_img": "psoc63_m0_default_1.02.hex",
7846+
"hex_filename": "psoc63_m0_default_1.02.hex",
78487847
"post_binary_hook": {
78497848
"function": "PSOC6Code.complete"
78507849
},
@@ -7894,12 +7893,11 @@
78947893
},
78957894
"FUTURE_SEQUANA_PSA": {
78967895
"inherits": ["NSPE_Target", "FUTURE_SEQUANA"],
7897-
"sub_target": "FUTURE_SEQUANA_M0_PSA",
78987896
"extra_labels_add": ["PSA"],
78997897
"extra_labels_remove": ["CORDIO"],
79007898
"components_add": ["SPM_MAILBOX"],
79017899
"macros_add": ["PSOC6_DYNSRM_DISABLE=1", "MBEDTLS_PSA_CRYPTO_C"],
7902-
"m0_core_img": "psa_release_1.0.hex",
7900+
"hex_filename": "psa_release_1.0.hex",
79037901
"overrides": {
79047902
"secure-rom-start": "0x10000000",
79057903
"secure-rom-size": "0x80000",

tools/export/makefile/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class Makefile(Exporter):
5454
"MCU_NRF51Code.binary_hook",
5555
"TEENSY3_1Code.binary_hook",
5656
"LPCTargetCode.lpc_patch",
57-
"LPC4088Code.binary_hook"
57+
"LPC4088Code.binary_hook",
58+
"PSOC6Code.complete"
5859
])
5960

6061
@classmethod
@@ -83,6 +84,11 @@ def generate(self):
8384
sys_libs = [self.prepare_sys_lib(lib) for lib
8485
in self.toolchain.sys_libs]
8586

87+
hex_files = self.resources.hex_files
88+
if hasattr(self.toolchain.target, 'hex_filename'):
89+
hex_filename = self.toolchain.target.hex_filename
90+
hex_files = list(f for f in hex_files if basename(f) == hex_filename)
91+
8692
ctx = {
8793
'name': self.project_name,
8894
'to_be_compiled': to_be_compiled,
@@ -92,7 +98,7 @@ def generate(self):
9298
'linker_script': self.resources.linker_script,
9399
'libraries': libraries,
94100
'ld_sys_libs': sys_libs,
95-
'hex_files': self.resources.hex_files,
101+
'hex_files': hex_files,
96102
'vpath': (["../../.."]
97103
if (basename(dirname(dirname(self.export_dir)))
98104
== "projectfiles")

tools/targets/PSOC6.py

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#
22
# Copyright (c) 2017-2018 Future Electronics
3+
# Copyright (c) 2018-2019 Cypress Semiconductor Corporation
34
#
45
# Licensed under the Apache License, Version 2.0 (the "License");
56
# you may not use this file except in compliance with the License.
@@ -20,45 +21,71 @@
2021
import errno
2122
from array import array
2223
from struct import (pack, unpack)
23-
from distutils.spawn import find_executable
2424
from shutil import copyfile
2525
from intelhex import IntelHex
2626
from intelhex.compat import asbytes
2727

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

3147

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

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

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

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

5784
# align flash segments
5885
align_mask = align - 1
5986
alignments = IntelHex()
6087
for start, end in ihex.segments():
61-
if start >= 0x090000000:
88+
if start >= CY_PROGRAM_SIZE:
6289
continue
6390
aligned_start = start & ~align_mask
6491
if start != aligned_start:
@@ -76,14 +103,8 @@ def merge_images(hexf0, hexf1=None):
76103
ihex.padding = 0x00
77104
ihex.loadfile(hexf0, "hex")
78105
if hexf1 is not None:
79-
# get chip ID from metadata and compare
106+
# Merge the CM0+ image
80107
ihex1 = IntelHex(hexf1)
81-
type0 = ihex.tobinarray(start=0x90500002, size=4)
82-
type1 = ihex1.tobinarray(start=0x90500002, size=4)
83-
if type0 != type1:
84-
raise HookError(
85-
"Incompatible processor type: %s in '%s' and 0x%s in '%s'"
86-
% (hexf0, type0, hexf1, type1))
87108
ihex.merge(ihex1, 'ignore')
88109
return ihex
89110

@@ -94,18 +115,18 @@ def complete_func(message_func, elf0, hexf0, hexf1=None, dest=None):
94115
ihex.write_hex_file(dest if dest else hexf0, write_start_addr=False, byte_count=64)
95116

96117
# Find Cortex M0 image.
97-
def find_cm0_image(toolchain, resources, elf, hexf):
118+
def find_cm0_image(toolchain, resources, elf, hexf, hex_filename):
98119
# Locate user-specified image
99120
from tools.resources import FileType
100121
hex_files = resources.get_file_paths(FileType.HEX)
101-
m0hexf = next((f for f in hex_files if os.path.basename(f) == toolchain.target.m0_core_img), None)
122+
m0hexf = next((f for f in hex_files if os.path.basename(f) == hex_filename), None)
102123
if toolchain.target.name.endswith('_PSA'):
103124
m0hexf = next((f for f in hex_files if os.path.basename(f) == os.path.basename(hexf)), m0hexf)
104125

105126
if m0hexf:
106127
toolchain.notify.debug("M0 core image file found: %s." % os.path.basename(m0hexf))
107128
else:
108-
toolchain.notify.debug("M0 core hex image file %s not found. Aborting." % toolchain.target.m0_core_img)
129+
toolchain.notify.debug("M0 core hex image file %s not found. Aborting." % hex_filename)
109130
raise ConfigException("Required M0 core hex image not found.")
110131

111132
return m0hexf

tools/targets/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,11 @@ class PSOC6Code:
583583
@staticmethod
584584
def complete(t_self, resources, elf, binf):
585585
from tools.targets.PSOC6 import complete as psoc6_complete
586-
if hasattr(t_self.target, "sub_target"):
586+
if hasattr(t_self.target, "hex_filename"):
587+
hex_filename = t_self.target.hex_filename
587588
# Completing main image involves merging M0 image.
588589
from tools.targets.PSOC6 import find_cm0_image
589-
m0hexf = find_cm0_image(t_self, resources, elf, binf)
590+
m0hexf = find_cm0_image(t_self, resources, elf, binf, hex_filename)
590591
psoc6_complete(t_self, elf, binf, m0hexf)
591592
else:
592593
psoc6_complete(t_self, elf, binf)

0 commit comments

Comments
 (0)