|
| 1 | +# Copyright 2026 Nordic Semiconductor ASA |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +""" |
| 5 | +Generate an Intel HEX file containing nRF71 WICR register values |
| 6 | +read from a Zephyr edtlib.EDT pickle. |
| 7 | +""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import pickle |
| 11 | +import struct |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +from intelhex import IntelHex |
| 16 | + |
| 17 | +WICR_COMPATIBLE = "nordic,nrf-wicr" |
| 18 | + |
| 19 | + |
| 20 | +# ── Field table ────────────────────────────────────────────────────── |
| 21 | +# |
| 22 | +# Each entry describes one WICR register field configurable via DTS. |
| 23 | +# |
| 24 | +# property – DTS property name (must match the binding YAML) |
| 25 | +# offset – byte offset from WICR base |
| 26 | +# size_offset – (phandle only) byte offset for the companion SIZE register |
| 27 | + |
| 28 | +WICR_FIELDS = [ |
| 29 | + {"property": "firmware-lmacinitpc", "offset": 0x000}, |
| 30 | + {"property": "firmware-umacinitpc", "offset": 0x004}, |
| 31 | + {"property": "firmware-lmacrompatchaddr", "offset": 0x008}, |
| 32 | + {"property": "firmware-umacrompatchaddr", "offset": 0x00C}, |
| 33 | + {"property": "ipcconfig-commandmbox", "offset": 0x080, "size_offset": 0x084}, |
| 34 | + {"property": "ipcconfig-eventmbox", "offset": 0x088, "size_offset": 0x08C}, |
| 35 | + {"property": "ipcconfig-sparembox", "offset": 0x090, "size_offset": 0x094}, |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +def setup_devicetree_path(zephyr_base): |
| 40 | + """Add the devicetree package to sys.path so EDT can be unpickled.""" |
| 41 | + |
| 42 | + devicetree_path = Path(zephyr_base) / "scripts/dts/python-devicetree/src" |
| 43 | + if not devicetree_path.is_dir(): |
| 44 | + sys.exit(f"Devicetree path does not exist: {devicetree_path}") |
| 45 | + sys.path.insert(0, str(devicetree_path)) |
| 46 | + |
| 47 | + |
| 48 | +def put_word(ih, addr, word): |
| 49 | + """Write a 32-bit little-endian word into the IntelHex.""" |
| 50 | + |
| 51 | + ih.puts(addr, struct.pack("<I", word)) |
| 52 | + |
| 53 | + |
| 54 | +def parse_wicr(args): |
| 55 | + setup_devicetree_path(args.zephyr_base) |
| 56 | + |
| 57 | + with open(args.edt_pickle, "rb") as f: |
| 58 | + edt = pickle.load(f) |
| 59 | + |
| 60 | + wicr_nodes = edt.compat2okay.get(WICR_COMPATIBLE, []) |
| 61 | + if not wicr_nodes: |
| 62 | + IntelHex().write_hex_file(args.output) |
| 63 | + return |
| 64 | + |
| 65 | + wicr = wicr_nodes[0] |
| 66 | + base = wicr.regs[0].addr |
| 67 | + |
| 68 | + ih = IntelHex() |
| 69 | + wrote_any = False |
| 70 | + |
| 71 | + for field in WICR_FIELDS: |
| 72 | + prop = wicr.props.get(field["property"]) |
| 73 | + if prop is None: |
| 74 | + continue |
| 75 | + |
| 76 | + target = prop.val |
| 77 | + if not target.regs: |
| 78 | + sys.exit( |
| 79 | + f"Phandle target for '{field['property']}' ({target.path}) has no reg property" |
| 80 | + ) |
| 81 | + |
| 82 | + put_word(ih, base + field["offset"], target.regs[0].addr) |
| 83 | + |
| 84 | + size_offset = field.get("size_offset") |
| 85 | + if size_offset is not None: |
| 86 | + size_val = target.regs[0].size |
| 87 | + if size_val is None: |
| 88 | + sys.exit( |
| 89 | + f"Phandle target for '{field['property']}' " |
| 90 | + f"({target.path}) has no size in its reg property" |
| 91 | + ) |
| 92 | + put_word(ih, base + size_offset, size_val) |
| 93 | + |
| 94 | + wrote_any = True |
| 95 | + |
| 96 | + if not wrote_any: |
| 97 | + IntelHex().write_hex_file(args.output) |
| 98 | + return |
| 99 | + |
| 100 | + ih.write_hex_file(args.output) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + parser = argparse.ArgumentParser(description=__doc__, allow_abbrev=False) |
| 105 | + parser.add_argument( |
| 106 | + "--zephyr-base", |
| 107 | + required=True, |
| 108 | + help="Path to the Zephyr base directory", |
| 109 | + ) |
| 110 | + parser.add_argument( |
| 111 | + "--edt-pickle", |
| 112 | + required=True, |
| 113 | + help="Path to the main application's EDT pickle file", |
| 114 | + ) |
| 115 | + parser.add_argument( |
| 116 | + "--output", |
| 117 | + required=True, |
| 118 | + help="Path for the output Intel HEX file", |
| 119 | + ) |
| 120 | + args = parser.parse_args() |
| 121 | + |
| 122 | + parse_wicr(args) |
0 commit comments