Skip to content

Commit 81c6662

Browse files
hakonfamcarlescufi
authored andcommitted
cmake: flash: scripts: Include externally built hex files
Allow user to add externally built hex files to the cmake property HEX_FILES_TO_MERGE. The hex files in this list will be merged with the hex file generated when building the zephyr application. This allows users to leverage the application configuration available in Kconfig and CMake to help decide what hex file should be merged with the zephyr application. Signed-off-by: Håkon Øye Amundsen <[email protected]>
1 parent 9452a1f commit 81c6662

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,27 @@ add_custom_command(
13041304
# NB: COMMENT only works for some CMake-Generators
13051305
)
13061306

1307+
# To populate with hex files to merge, do the following:
1308+
# set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${my_local_list})
1309+
# Note that the zephyr.hex file will not be included automatically.
1310+
get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
1311+
if(HEX_FILES_TO_MERGE)
1312+
# Merge in out-of-tree hex files.
1313+
set(merged_hex_name merged.hex)
1314+
1315+
add_custom_command(
1316+
OUTPUT ${merged_hex_name}
1317+
COMMAND
1318+
${PYTHON_EXECUTABLE}
1319+
${ZEPHYR_BASE}/scripts/mergehex.py
1320+
-o ${merged_hex_name}
1321+
${HEX_FILES_TO_MERGE}
1322+
DEPENDS ${HEX_FILES_TO_MERGE} ${logical_target_for_zephyr_elf}
1323+
)
1324+
1325+
add_custom_target(mergehex ALL DEPENDS ${merged_hex_name})
1326+
endif()
1327+
13071328
if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE)
13081329
# Use --print-memory-usage with the first link.
13091330
#

cmake/flash/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ if(RUNNERS)
2929
CACHE STRING "Board definition directory" FORCE)
3030
set(ZEPHYR_RUNNER_CONFIG_KERNEL_ELF "${PROJECT_BINARY_DIR}/${KERNEL_ELF_NAME}"
3131
CACHE STRING "Path to kernel image in ELF format" FORCE)
32-
set(ZEPHYR_RUNNER_CONFIG_KERNEL_HEX "${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}"
33-
CACHE STRING "Path to kernel image in Intel Hex format" FORCE)
32+
get_property(HEX_FILES_TO_MERGE GLOBAL PROPERTY HEX_FILES_TO_MERGE)
33+
if(HEX_FILES_TO_MERGE)
34+
set(ZEPHYR_RUNNER_CONFIG_KERNEL_HEX "${PROJECT_BINARY_DIR}/${MERGED_HEX_NAME}"
35+
CACHE STRING "Path to merged image in Intel Hex format" FORCE)
36+
else()
37+
set(ZEPHYR_RUNNER_CONFIG_KERNEL_HEX "${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}"
38+
CACHE STRING "Path to kernel image in Intel Hex format" FORCE)
39+
endif()
3440
set(ZEPHYR_RUNNER_CONFIG_KERNEL_BIN "${PROJECT_BINARY_DIR}/${KERNEL_BIN_NAME}"
3541
CACHE STRING "Path to kernel image as raw binary" FORCE)
3642
# Not always applicable, but so often needed that they're provided

doc/application/application.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,19 @@ Below is a simple example :file:`CMakeList.txt`:
930930
931931
target_sources(app PRIVATE src/main.c)
932932
933+
The Cmake property ``HEX_FILES_TO_MERGE``
934+
leverages the application configuration provided by
935+
Kconfig and CMake to let you merge externally built hex files
936+
with the hex file generated when building the Zephyr application.
937+
For example:
938+
939+
.. code-block:: cmake
940+
941+
set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE
942+
${app_bootloader_hex}
943+
${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}
944+
${app_provision_hex})
945+
933946
CMakeCache.txt
934947
==============
935948

@@ -1292,3 +1305,4 @@ project that demonstrates some of these features.
12921305
.. _Eclipse IDE for C/C++ Developers: https://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/oxygen2
12931306
.. _GNU MCU Eclipse plug-ins: https://gnu-mcu-eclipse.github.io/plugins/install/
12941307
.. _pyOCD v0.11.0: https://github.com/mbedmicro/pyOCD/releases/tag/v0.11.0
1308+

scripts/mergehex.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2018 Nordic Semiconductor ASA
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
# This merges a set of input hex files into a single output hex file.
8+
# Any conflicts will result in an error being reported.
9+
10+
from intelhex import IntelHex
11+
12+
import argparse
13+
14+
15+
def merge_hex_files(output, input_hex_files):
16+
ih = IntelHex()
17+
18+
for hex_file_path in input_hex_files:
19+
to_merge = IntelHex(hex_file_path)
20+
21+
# Since 'arm-none-eabi-objcopy' incorrectly inserts record type '03 - Start Segment Address', we need to remove
22+
# the start_addr to avoid conflicts when merging.
23+
to_merge.start_addr = None
24+
25+
ih.merge(to_merge)
26+
ih.write_hex_file(output)
27+
28+
29+
def parse_args():
30+
parser = argparse.ArgumentParser(
31+
description="Merge hex files.",
32+
formatter_class=argparse.RawDescriptionHelpFormatter)
33+
parser.add_argument("-o", "--output", required=False, default="merged.hex",
34+
type=argparse.FileType('w', encoding='UTF-8'),
35+
help="Output file name.")
36+
parser.add_argument("input_files", nargs='*')
37+
return parser.parse_args()
38+
39+
40+
def main():
41+
args = parse_args()
42+
43+
merge_hex_files(args.output, args.input_files)
44+
45+
46+
if __name__ == "__main__":
47+
main()

scripts/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ pykwalify
1717
windows-curses; sys_platform == "win32"
1818
colorama
1919
Pillow
20+
intelhex

0 commit comments

Comments
 (0)