diff --git a/doc/build/dts/macros.bnf b/doc/build/dts/macros.bnf index f5e676f8f44a3..d47cb5365ad9d 100644 --- a/doc/build/dts/macros.bnf +++ b/doc/build/dts/macros.bnf @@ -74,6 +74,44 @@ node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_FOREACH_PROP_ELEM" node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_FOREACH_PROP_ELEM_SEP" node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_FOREACH_PROP_ELEM_VARGS" node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_FOREACH_PROP_ELEM_SEP_VARGS" +; Map properties generate additional macros consumed by DT_MAP_* APIs. +; The following examples assume something like this mapping nexus: +; +; connector { +; gpio-map = <1 2 &{/gpio-map-test/parent} 3 +; 4 5 &{/gpio-map-test/parent} 6>; +; }; +; +; Total number of entries in the mapping array. +; +; #define DT_N__P_gpio_map_MAP_LEN 2 +node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_MAP_LEN" +; Each mapping entry expands to the child specifier cells, the parent node, +; and the parent specifier cells. DT_MAP_BY_IDX() retrieves this list. +; +; #define DT_N__P_gpio_map_MAP_IDX_0 1, 2, DT_N_, 3 +node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_MAP_IDX_" DIGIT +; Offsets for the child specifier cells within an entry. These support +; DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(), which slices out just those cells. +; +; #define DT_N__P_gpio_map_MAP_IDX_0_CHILD_SPECIFIER_POS 0 +; #define DT_N__P_gpio_map_MAP_IDX_0_CHILD_SPECIFIER_LEN 2 +node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_MAP_IDX_" DIGIT %s"_CHILD_SPECIFIER_POS" +node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_MAP_IDX_" DIGIT %s"_CHILD_SPECIFIER_LEN" +; Offsets for the parent node argument. DT_MAP_PARENT_ARG_BY_IDX() uses +; these to extract the parent node identifier. +; +; #define DT_N__P_gpio_map_MAP_IDX_0_PARENT_POS 2 +; #define DT_N__P_gpio_map_MAP_IDX_0_PARENT_LEN 1 +node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_MAP_IDX_" DIGIT %s"_PARENT_POS" +node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_MAP_IDX_" DIGIT %s"_PARENT_LEN" +; Offsets for the parent specifier cells used by +; DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(). +; +; #define DT_N__P_gpio_map_MAP_IDX_0_PARENT_SPECIFIER_POS 3 +; #define DT_N__P_gpio_map_MAP_IDX_0_PARENT_SPECIFIER_LEN 1 +node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_MAP_IDX_" DIGIT %s"_PARENT_SPECIFIER_POS" +node-macro =/ %s"DT_N" path-id %s"_P_" prop-id %s"_MAP_IDX_" DIGIT %s"_PARENT_SPECIFIER_LEN" ; These are used by DT_CHILD_NUM and DT_CHILD_NUM_STATUS_OKAY macros node-macro =/ %s"DT_N" path-id %s"_CHILD_NUM" node-macro =/ %s"DT_N" path-id %s"_CHILD_NUM_STATUS_OKAY" diff --git a/doc/tools/validate_abnf.py b/doc/tools/validate_abnf.py new file mode 100755 index 0000000000000..2a18429a5c7fa --- /dev/null +++ b/doc/tools/validate_abnf.py @@ -0,0 +1,307 @@ +#!/usr/bin/env python3 +"""Validate RFC 7405 ABNF grammars used in Zephyr documentation. + +This script implements a small ABNF parser that can be used to sanity check +grammar fragments such as ``doc/build/dts/macros.bnf``. It focuses on the +constructs that appear in the Zephyr documentation today (rule concatenation, +repetition, groups, options, literals and numeric values) and reports the first +syntax error it encounters with a helpful line/column location. +""" + +from __future__ import annotations + +import argparse +from dataclasses import dataclass +import pathlib +import re +import sys +from typing import Iterable, List, Optional, Sequence, Tuple + + +class ABNFError(RuntimeError): + """Raised when a syntax error is encountered in the grammar.""" + + def __init__(self, message: str, *, line: int, column: int, rule: Optional[str]): + super().__init__(message) + self.line = line + self.column = column + self.rule = rule + + def __str__(self) -> str: # pragma: no cover - human readable error + location = f"line {self.line}, column {self.column}" + if self.rule: + location += f" (in rule '{self.rule}')" + return f"{location}: {super().__str__()}" + + +@dataclass +class Token: + kind: str + value: str + column: int + + +_TOKEN_REGEX = re.compile( + r"(?P\s+)" # whitespace + r"|(?P=/)" + r"|(?P=)" + r"|(?P\()" + r"|(?P\))" + r"|(?P\[)" + r"|(?P\])" + r"|(?P/)" + r"|(?P\*)" + r'|(?P%[si]"(?:\\.|[^"\\])*")' + r"|(?P%[bdx](?:[0-9A-F]+(?:-[0-9A-F]+)?)(?:\.[0-9A-F]+(?:-[0-9A-F]+)?)*)" + r'|(?P"(?:\\.|[^"\\])*")' + r"|(?P<(?:\\.|[^>\\])*>)" + r"|(?P[A-Za-z][A-Za-z0-9-]*)" + r"|(?P\d+)" + , +) + + +class Lexer: + def __init__(self, text: str, *, line_offset: int) -> None: + self.text = text + self.position = 0 + self.line_offset = line_offset + + def __iter__(self) -> Iterable[Token]: + while self.position < len(self.text): + match = _TOKEN_REGEX.match(self.text, self.position) + if not match: + column = self.position + 1 + raise ABNFError( + f"unexpected character {self.text[self.position]!r}", + line=self.line_offset, + column=column, + rule=None, + ) + if match.lastgroup is None: # whitespace + self.position = match.end() + continue + token = Token(match.lastgroup, match.group(match.lastgroup), match.start() + 1) + self.position = match.end() + yield token + yield Token("EOF", "", len(self.text) + 1) + + +class Parser: + def __init__(self, tokens: Sequence[Token], *, rule_name: str, line: int) -> None: + self.tokens = list(tokens) + self.index = 0 + self.rule_name = rule_name + self.line = line + + def current(self) -> Token: + return self.tokens[self.index] + + def consume(self, kind: str) -> Token: + token = self.current() + if token.kind != kind: + raise ABNFError( + f"expected {kind} but found {token.kind}", + line=self.line, + column=token.column, + rule=self.rule_name, + ) + self.index += 1 + return token + + def match(self, kind: str) -> Optional[Token]: + token = self.current() + if token.kind == kind: + self.index += 1 + return token + return None + + def parse(self) -> None: + self._parse_rule() + if self.current().kind != "EOF": + token = self.current() + raise ABNFError( + "unexpected trailing input", + line=self.line, + column=token.column, + rule=self.rule_name, + ) + + def _parse_rule(self) -> None: + self.consume("RULENAME") + self._consume_whitespace() + if self.match("DEFINED_AS") is None: + self.consume("EQUAL") + self._consume_whitespace() + self._parse_elements() + + def _parse_elements(self) -> None: + self._parse_alternation() + + def _parse_alternation(self) -> None: + self._parse_concatenation() + while True: + self._consume_whitespace() + checkpoint = self.index + if self.match("SLASH") is None: + self.index = checkpoint + break + self._consume_whitespace() + self._parse_concatenation() + + def _parse_concatenation(self) -> None: + self._parse_repetition() + while True: + checkpoint = self.index + if not self._consume_whitespace(): + self.index = checkpoint + break + token = self.current() + if token.kind in {"SLASH", "RBRK", "RPAREN", "EOF"}: + self.index = checkpoint + break + self._parse_repetition() + + def _consume_whitespace(self) -> bool: + consumed = False + while self.current().kind == "WS": + consumed = True + self.index += 1 + return consumed + + def _parse_repetition(self) -> None: + self._maybe_parse_repeat() + self._consume_whitespace() + self._parse_element() + + def _maybe_parse_repeat(self) -> None: + token = self.current() + if token.kind == "NUMBER": + self.index += 1 + if self.match("STAR") is not None: + if self.current().kind == "NUMBER": + self.index += 1 + else: + return + elif token.kind == "STAR": + self.index += 1 + if self.current().kind == "NUMBER": + self.index += 1 + + def _parse_element(self) -> None: + token = self.current() + if token.kind in {"RULENAME", "CHAR_VAL", "CASE_STRING", "NUM_VAL", "PROSE_VAL"}: + self.index += 1 + return + if token.kind == "LBRK": + self.index += 1 + self._consume_whitespace() + self._parse_alternation() + self._consume_whitespace() + self.consume("RBRK") + return + if token.kind == "LPAREN": + self.index += 1 + self._consume_whitespace() + self._parse_alternation() + self._consume_whitespace() + self.consume("RPAREN") + return + raise ABNFError( + f"unexpected token {token.kind}", + line=self.line, + column=token.column, + rule=self.rule_name, + ) + + +def strip_comments(lines: Iterable[str]) -> List[Tuple[str, int]]: + cleaned: List[Tuple[str, int]] = [] + for line_number, raw_line in enumerate(lines, start=1): + in_string = False + escape = False + result_chars: List[str] = [] + for ch in raw_line.rstrip("\n"): + if ch == "\\" and not escape: + escape = True + result_chars.append(ch) + continue + if ch == '"' and not escape: + in_string = not in_string + if ch == ';' and not in_string: + break + result_chars.append(ch) + escape = False + cleaned_line = "".join(result_chars).rstrip() + if cleaned_line: + cleaned.append((cleaned_line, line_number)) + return cleaned + + +def join_rule_lines(cleaned_lines: Sequence[Tuple[str, int]]) -> List[Tuple[str, int]]: + rules: List[Tuple[str, int]] = [] + current_line = "" + start_number = 0 + for text, line_number in cleaned_lines: + if text and not text[0].isspace(): + if current_line: + rules.append((current_line, start_number)) + current_line = text.strip() + start_number = line_number + else: + if not current_line: + raise ABNFError( + "continuation line encountered without preceding rule", + line=line_number, + column=1, + rule=None, + ) + current_line += " " + text.strip() + if current_line: + rules.append((current_line, start_number)) + return rules + + +def validate_rule(rule_text: str, line_number: int) -> None: + # Tokenise the rule. + tokens: List[Token] = [] + lexer = Lexer(rule_text, line_offset=line_number) + for token in lexer: + if token.kind == "EOF": + tokens.append(Token("EOF", "", token.column)) + elif token.kind == "WS": + tokens.append(Token("WS", token.value, token.column)) + else: + tokens.append(token) + if not tokens: + return + rule_name = tokens[0].value if tokens and tokens[0].kind == "RULENAME" else None + parser = Parser(tokens, rule_name=rule_name or "", line=line_number) + parser.parse() + + +def validate_file(path: pathlib.Path) -> None: + with path.open("r", encoding="utf-8") as fp: + cleaned = strip_comments(fp) + rules = join_rule_lines(cleaned) + for rule_text, line_number in rules: + validate_rule(rule_text, line_number) + + +def main(argv: Optional[Sequence[str]] = None) -> int: + parser = argparse.ArgumentParser(description="Validate RFC 7405 ABNF files.") + parser.add_argument("paths", metavar="FILE", nargs="+", type=pathlib.Path) + args = parser.parse_args(argv) + + had_error = False + for path in args.paths: + try: + validate_file(path) + except ABNFError as exc: # pragma: no cover - CLI entry point + had_error = True + print(f"{path}: {exc}", file=sys.stderr) + return 1 if had_error else 0 + + +if __name__ == "__main__": # pragma: no cover - CLI entry point + sys.exit(main()) diff --git a/dts/bindings/adc/arduino,uno-adc.yaml b/dts/bindings/adc/arduino,uno-adc.yaml index 854e640914a4f..6d409b861b13f 100644 --- a/dts/bindings/adc/arduino,uno-adc.yaml +++ b/dts/bindings/adc/arduino,uno-adc.yaml @@ -13,20 +13,4 @@ description: | compatible: "arduino,uno-adc" -include: base.yaml - -properties: - io-channel-map: - type: compound - required: true - - io-channel-map-mask: - type: compound - - io-channel-map-pass-thru: - type: compound - - "#io-channel-cells": - type: int - required: true - description: Number of items to expect in an ADC specifier +include: [base.yaml, io-channel-nexus.yaml] diff --git a/dts/bindings/gpio/gpio-nexus.yaml b/dts/bindings/gpio/gpio-nexus.yaml index a1bcbc61e5ae0..de8153b0f9a89 100644 --- a/dts/bindings/gpio/gpio-nexus.yaml +++ b/dts/bindings/gpio/gpio-nexus.yaml @@ -9,10 +9,10 @@ properties: required: true gpio-map-mask: - type: compound + type: array gpio-map-pass-thru: - type: compound + type: array "#gpio-cells": type: int diff --git a/dts/bindings/iio/io-channel-nexus.yaml b/dts/bindings/iio/io-channel-nexus.yaml new file mode 100644 index 0000000000000..d34ca7b5be607 --- /dev/null +++ b/dts/bindings/iio/io-channel-nexus.yaml @@ -0,0 +1,20 @@ +# Copyright (c) 2025 TOKITA Hiroshi +# SPDX-License-Identifier: Apache-2.0 + +# Common fields for io-channel nexus + +properties: + io-channel-map: + type: compound + required: true + + io-channel-map-mask: + type: array + + io-channel-map-pass-thru: + type: array + + "#io-channel-cells": + type: int + required: true + description: Number of items to expect in the io-channel specifier, such as ADC channels. diff --git a/dts/bindings/interrupt-controller/interrupt-nexus.yaml b/dts/bindings/interrupt-controller/interrupt-nexus.yaml new file mode 100644 index 0000000000000..cc7d6060fd023 --- /dev/null +++ b/dts/bindings/interrupt-controller/interrupt-nexus.yaml @@ -0,0 +1,20 @@ +# Copyright (c) 2025 TOKITA Hiroshi +# SPDX-License-Identifier: Apache-2.0 + +# Common fields for interrupt nexus nodes + +properties: + interrupt-map: + type: compound + required: true + + interrupt-map-mask: + type: array + + interrupt-map-pass-thru: + type: array + + "#interrupt-cells": + type: int + required: true + description: Number of items to expect in a interrupt specifier diff --git a/dts/bindings/pcie/host/pci-host-ecam-generic.yaml b/dts/bindings/pcie/host/pci-host-ecam-generic.yaml index fea7d35ef17a5..0a42b180665a3 100644 --- a/dts/bindings/pcie/host/pci-host-ecam-generic.yaml +++ b/dts/bindings/pcie/host/pci-host-ecam-generic.yaml @@ -5,7 +5,7 @@ description: PCIe Controller in ECAM mode compatible: "pci-host-ecam-generic" -include: pcie-controller.yaml +include: [pcie-controller.yaml, interrupt-nexus.yaml] properties: reg: @@ -22,11 +22,5 @@ properties: definition of non-prefetchable memory. One or both of prefetchable Memory and IO Space may also be provided. - interrupt-map-mask: - type: array - - interrupt-map: - type: compound - bus-range: type: array diff --git a/dts/bindings/test/vnd,gpio-nexus.yaml b/dts/bindings/test/vnd,gpio-nexus.yaml new file mode 100644 index 0000000000000..2274be7451da1 --- /dev/null +++ b/dts/bindings/test/vnd,gpio-nexus.yaml @@ -0,0 +1,8 @@ +# Copyright (c) 2025, TOKITA Hiroshi +# SPDX-License-Identifier: Apache-2.0 + +description: VND GPIO nexus + +include: [gpio-nexus.yaml] + +compatible: "vnd,gpio-nexus" diff --git a/dts/bindings/test/vnd,intr-nexus.yaml b/dts/bindings/test/vnd,intr-nexus.yaml new file mode 100644 index 0000000000000..f3b194c558385 --- /dev/null +++ b/dts/bindings/test/vnd,intr-nexus.yaml @@ -0,0 +1,8 @@ +# Copyright (c) 2025, TOKITA Hiroshi +# SPDX-License-Identifier: Apache-2.0 + +description: VND interrupt nexus + +include: [interrupt-nexus.yaml] + +compatible: "vnd,intr-nexus" diff --git a/include/zephyr/devicetree.h b/include/zephyr/devicetree.h index ded261d66cd08..c0c650e11971d 100644 --- a/include/zephyr/devicetree.h +++ b/include/zephyr/devicetree.h @@ -5570,5 +5570,6 @@ #include #include #include +#include #endif /* ZEPHYR_INCLUDE_DEVICETREE_H_ */ diff --git a/include/zephyr/devicetree/map.h b/include/zephyr/devicetree/map.h new file mode 100644 index 0000000000000..3afca063412bb --- /dev/null +++ b/include/zephyr/devicetree/map.h @@ -0,0 +1,331 @@ +/* + * Copyright (c) 2025 TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_INCLUDE_DEVICETREE_MAP_H_ +#define ZEPHYR_INCLUDE_DEVICETREE_MAP_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup devicetree-map Devicetree Map API + * + * @brief Helper macros for handling map properties. + * + * This module provides helper macros that facilitate interrupt mapping and + * specifier mapping based on DeviceTree specifications. It enables the extraction + * and interpretation of mapping data represented as phandle-arrays. + * + * In a typical DeviceTree fragment, properties ending with "-map" specify: + * - The child specifier to be mapped. + * - The parent node (phandle) to which the mapping applies. + * - The parent specifier associated with the mapping. + * + * For example, when the following DeviceTree snippet is defined: + * + * @code{.dts} + * n: node { + * gpio-map = <0 1 &gpio0 2 3>, <4 5 &gpio0 6 7>; + * }; + * @endcode + * + * In the first mapping entry: + * - `0 1` are the child specifiers. + * - &gpio0 is the parent node. + * - `2 3` are the parent specifiers. + * + * Since map properties are implemented as phandle-arrays, macros such as + * DT_PHANDLE_BY_IDX() and DT_PHA_BY_IDX() can be used to access individual elements. + * + * Both child and parent specifiers are treated as cells in a phandle-array. + * By default, each group of specifiers is given a sequential cell name + * (child_specifier_0, child_specifier_1, ..., parent_specifier_0, ...). + * + * If cell names are specified in dt-bindings, they will be used for the child specifier cell names. + * Parent specifiers always use the default naming convention. + * + * Example usage: + * + * A mapping entry is a phandle-array whose elements can be referenced as follows: + * - Child specifiers can be accessed via names such as `child_specifier_0`, + * `child_specifier_1`, ... + * - The parent node is accessed via DT_PHANDLE_BY_IDX(). + * - Parent specifiers are accessed via names such as `parent_specifier_0`, + * `parent_specifier_1`, ... + * + * @code{.c} + * int cspec_0 = DT_PHA_BY_IDX(DT_NODELABEL(n), gpio_map, 0, child_specifier_0); // 0 + * int cspec_1 = DT_PHA_BY_IDX(DT_NODELABEL(n), gpio_map, 0, child_specifier_1); // 1 + * const struct device *parent = + * device_get_binding(DT_PHANDLE_BY_IDX(DT_NODELABEL(n), gpio_map, 0)); // &gpio0 + * int pspec_0 = DT_PHA_BY_IDX(DT_NODELABEL(n), gpio_map, 0, parent_specifier_0); // 2 + * int pspec_1 = DT_PHA_BY_IDX(DT_NODELABEL(n), gpio_map, 0, parent_specifier_1); // 3 + * @endcode + * + * The map helper API also provides the following macros for convenient access to + * specific parts of a mapping entry: + * - DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX() + * - DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX() + * - DT_MAP_PARENT_ARG_BY_IDX() + * + * These macros extract, respectively, the child specifier arguments, the parent specifier + * arguments, and the parent node argument from a mapping element identified by its node ID, + * property name, and index. + * + * For instance: + * + * @code{.c} + * #define SRC_AND_DST(node_id, prop, idx) \ + * { GET_ARG_N(1, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(node_id, prop, idx)), \ + * GET_ARG_N(1, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(node_id, prop, idx)) } + * + * int src_and_dst[2][] = { + * DT_FOREACH_PROP_ELEM_SEP(DT_NODELABEL(n), gpio_map, SRC_AND_DST, (,)) + * }; + * @endcode + * + * The above expansion yields: + * + * @code{.c} + * int src_and_dst[2][] = {{0, 2}, {4, 6}}; + * @endcode + * + * @ingroup devicetree + * @{ + */ + +/** + * @brief Extracts a specified range of arguments. + * + * This helper macro first skips a given number of arguments and then selects + * the first @p len arguments from the remaining list. + * + * @param start The number of arguments to skip. + * @param len The number of arguments to extract after skipping. + * @param ... The list of input arguments. + */ +#define DT_MAP_HELPER_DO_ARGS_RANGE(start, len, ...) \ + GET_ARGS_FIRST_N(len, GET_ARGS_LESS_N(start, __VA_ARGS__)) + +/** + * @brief Extracts a range of mapping arguments for a specific field. + * + * This macro concatenates the field name with the appropriate suffixes to determine + * the starting index and length of the arguments for a map entry, and then extracts + * those arguments. + * + * @param name The mapping field name (e.g., CHILD_SPECIFIER, PARENT). + * @param node_id The node identifier. + * @param prop The property name in lowercase and underscores. + * @param idx The index of the mapping entry. + * @param ... Additional arguments corresponding to the mapping entry. + */ +#define DT_MAP_HELPER_ARGS_RANGE(name, node_id, prop, idx, ...) \ + DT_MAP_HELPER_DO_ARGS_RANGE(DT_CAT3(DT_MAP_, name, _POS_BY_IDX)(node_id, prop, idx), \ + DT_CAT3(DT_MAP_, name, _LEN_BY_IDX)(node_id, prop, idx), \ + __VA_ARGS__) + +/** + * @brief Retrieves the mapping entry at the specified index. + * + * @param node_id The node identifier. + * @param prop The property name in lowercase with underscores. + * @param idx The mapping entry index. + * @return The mapping entry as a list of comma-separated values. + */ +#define DT_MAP_BY_IDX(node_id, prop, idx) DT_CAT5(node_id, _P_, prop, _MAP_IDX_, idx) + +/** + * @brief Retrieves the first mapping entry. + * @see DT_MAP_BY_IDX + */ +#define DT_MAP(node_id, prop) DT_MAP_BY_IDX(node_id, prop, 0) + +/** + * @brief Returns the number of mapping entries for the given property. + * + * @param node_id The node identifier. + * @param prop The property name in lowercase with underscores. + * @return The total count of mapping entries. + */ +#define DT_MAP_LEN(node_id, prop) DT_CAT4(node_id, _P_, prop, _MAP_LEN) + +/** + * @brief Retrieves the starting index of the child specifier cell within a mapping entry. + * + * @param node_id The node identifier. + * @param prop The property name. + * @param idx The mapping entry index. + * @return The starting index of the child specifier cell. + */ +#define DT_MAP_CHILD_SPECIFIER_POS_BY_IDX(node_id, prop, idx) \ + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, CHILD_SPECIFIER_POS) + +/** + * @brief Retrieves the starting index of the child specifier cell within the first mapping entry. + * @see DT_MAP_CHILD_SPECIFIER_POS_BY_IDX + */ +#define DT_MAP_CHILD_SPECIFIER_POS(node_id, prop) \ + DT_MAP_CHILD_SPECIFIER_POS_BY_IDX(node_id, prop, 0) + +/** + * @brief Returns the length (number of cells) of the child specifier within a mapping entry. + * + * @param node_id The node identifier. + * @param prop The property name. + * @param idx The mapping entry index. + * @return The length (in cells) of the child specifier. + */ +#define DT_MAP_CHILD_SPECIFIER_LEN_BY_IDX(node_id, prop, idx) \ + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, CHILD_SPECIFIER_LEN) + +/** + * @brief Returns the length (number of cells) of the child specifier within the first mapping + * entry. + * @see DT_MAP_CHILD_SPECIFIER_LEN_BY_IDX + */ +#define DT_MAP_CHILD_SPECIFIER_LEN(node_id, prop) \ + DT_MAP_CHILD_SPECIFIER_LEN_BY_IDX(node_id, prop, 0) + +/** + * @brief Retrieves the starting index of the parent cell in a mapping entry. + * + * @param node_id The node identifier. + * @param prop The property name. + * @param idx The mapping entry index. + * @return The starting index of the parent cell. + */ +#define DT_MAP_PARENT_POS_BY_IDX(node_id, prop, idx) \ + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, PARENT_POS) + +/** + * @brief Retrieves the starting index of the parent cell in the first mapping entry. + * @see DT_MAP_PARENT_POS_BY_IDX + */ +#define DT_MAP_PARENT_POS(node_id, prop) DT_MAP_PARENT_POS_BY_IDX(node_id, prop, 0) + +/** + * @brief Returns the length (number of cells) of the parent cell in a mapping entry. + * + * @param node_id The node identifier. + * @param prop The property name. + * @param idx The mapping entry index. + * @return The length (in cells) of the parent cell. + */ +#define DT_MAP_PARENT_LEN_BY_IDX(node_id, prop, idx) \ + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, PARENT_LEN) + +/** + * @brief Returns the length (number of cells) of the parent cell in the first mapping entry. + * @see DT_MAP_PARENT_LEN_BY_IDX + */ +#define DT_MAP_PARENT_LEN(node_id, prop) DT_MAP_PARENT_LEN_BY_IDX(node_id, prop, 0) + +/** + * @brief Retrieves the starting index of the parent specifier cell within a mapping entry. + * + * @param node_id The node identifier. + * @param prop The property name. + * @param idx The mapping entry index. + * @return The starting index of the parent specifier cell. + */ +#define DT_MAP_PARENT_SPECIFIER_POS_BY_IDX(node_id, prop, idx) \ + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, PARENT_SPECIFIER_POS) + +/** + * @brief Retrieves the starting index of the parent specifier cell within the first mapping entry. + * @see DT_MAP_PARENT_SPECIFIER_POS_BY_IDX + */ +#define DT_MAP_PARENT_SPECIFIER_POS(node_id, prop) \ + DT_MAP_PARENT_SPECIFIER_POS_BY_IDX(node_id, prop, 0) + +/** + * @brief Returns the length (number of cells) of the parent specifier in a mapping entry. + * + * @param node_id The node identifier. + * @param prop The property name. + * @param idx The mapping entry index. + * @return The length (in cells) of the parent specifier. + */ +#define DT_MAP_PARENT_SPECIFIER_LEN_BY_IDX(node_id, prop, idx) \ + DT_CAT7(node_id, _P_, prop, _MAP_IDX_, idx, _, PARENT_SPECIFIER_LEN) + +/** + * @brief Returns the length (number of cells) of the parent specifier of the first mapping entry. + * @see DT_MAP_PARENT_SPECIFIER_LEN_BY_IDX + */ +#define DT_MAP_PARENT_SPECIFIER_LEN(node_id, prop) \ + DT_MAP_PARENT_SPECIFIER_LEN_BY_IDX(node_id, prop, 0) + +/** + * @brief Extracts the child specifier arguments from a mapping entry. + * + * This macro returns the comma-separated list of arguments for the child specifier. + * + * @param node_id The node identifier. + * @param prop The property name in lowercase with underscores. + * @param idx The mapping entry index. + * @return The child specifier arguments. + */ +#define DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(node_id, prop, idx) \ + DT_MAP_HELPER_ARGS_RANGE(CHILD_SPECIFIER, node_id, prop, idx, \ + DT_MAP_BY_IDX(node_id, prop, idx)) + +/** + * @brief Extracts the child specifier arguments from the first mapping entry. + * @see DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX + */ +#define DT_MAP_CHILD_SPECIFIER_ARGS(node_id, prop) \ + DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(node_id, prop, 0) + +/** + * @brief Extracts the parent node argument from a mapping entry. + * + * @param node_id The node identifier. + * @param prop The property name in lowercase with underscores. + * @param idx The mapping entry index. + * @return The parent node argument. + */ +#define DT_MAP_PARENT_ARG_BY_IDX(node_id, prop, idx) \ + DT_MAP_HELPER_ARGS_RANGE(PARENT, node_id, prop, idx, DT_MAP_BY_IDX(node_id, prop, idx)) + +/** + * @brief Extracts the parent node argument from the first mapping entry. + * @see DT_MAP_PARENT_ARG_BY_IDX + */ +#define DT_MAP_PARENT_ARG(node_id, prop) DT_MAP_PARENT_ARG_BY_IDX(node_id, prop, 0) + +/** + * @brief Extracts the parent specifier arguments from a mapping entry. + * + * This macro returns the comma-separated list of arguments for the parent specifier. + * + * @param node_id The node identifier. + * @param prop The property name in lowercase with underscores. + * @param idx The mapping entry index. + * @return The parent specifier arguments. + */ +#define DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(node_id, prop, idx) \ + DT_MAP_HELPER_ARGS_RANGE(PARENT_SPECIFIER, node_id, prop, idx, \ + DT_MAP_BY_IDX(node_id, prop, idx)) + +/** + * @brief Extracts the parent specifier arguments of the first mapping entry. + * @see DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX + */ +#define DT_MAP_PARENT_SPECIFIER_ARGS(node_id, prop) \ + DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(node_id, prop, 0) + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_DEVICETREE_MAP_H_ */ diff --git a/include/zephyr/sys/util_loops.h b/include/zephyr/sys/util_loops.h index 8c71edd16f363..afe2c004a6087 100644 --- a/include/zephyr/sys/util_loops.h +++ b/include/zephyr/sys/util_loops.h @@ -1051,6 +1051,388 @@ _47, _48, _49, _50, _51, _52, _53, _54, _55, \ _56, _57, _58, _59, _60, _61, _62, _63, ...) __VA_ARGS__ +#define Z_GET_ARGS_FIRST_0(...) + +#define Z_GET_ARGS_FIRST_1(_0, ...) _0 + +#define Z_GET_ARGS_FIRST_2(_0, _1, ...) _0, _1 + +#define Z_GET_ARGS_FIRST_3(_0, _1, _2, ...) _0, _1, _2 + +#define Z_GET_ARGS_FIRST_4(_0, _1, _2, _3, ...) _0, _1, _2, _3 + +#define Z_GET_ARGS_FIRST_5(_0, _1, _2, _3, _4, ...) _0, _1, _2, _3, _4 + +#define Z_GET_ARGS_FIRST_6(_0, _1, _2, _3, _4, _5, ...) _0, _1, _2, _3, _4, _5 + +#define Z_GET_ARGS_FIRST_7(_0, _1, _2, _3, _4, _5, _6, ...) _0, _1, _2, _3, _4, _5, _6 + +#define Z_GET_ARGS_FIRST_8(_0, _1, _2, _3, _4, _5, _6, _7, ...) _0, _1, _2, _3, _4, _5, _6, _7 + +#define Z_GET_ARGS_FIRST_9(_0, _1, _2, _3, _4, _5, _6, _7, _8, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8 + +#define Z_GET_ARGS_FIRST_10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9 + +#define Z_GET_ARGS_FIRST_11(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10 + +#define Z_GET_ARGS_FIRST_12(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11 + +#define Z_GET_ARGS_FIRST_13(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12 + +#define Z_GET_ARGS_FIRST_14(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13 + +#define Z_GET_ARGS_FIRST_15(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14 + +#define Z_GET_ARGS_FIRST_16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15 + +#define Z_GET_ARGS_FIRST_17(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + _16, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16 + +#define Z_GET_ARGS_FIRST_18(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + _16, _17, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17 + +#define Z_GET_ARGS_FIRST_19(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + _16, _17, _18, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18 + +#define Z_GET_ARGS_FIRST_20(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + _16, _17, _18, _19, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19 + +#define Z_GET_ARGS_FIRST_21(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + _16, _17, _18, _19, _20, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, \ + _20 + +#define Z_GET_ARGS_FIRST_22(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + _16, _17, _18, _19, _20, _21, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, \ + _20, _21 + +#define Z_GET_ARGS_FIRST_23(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + _16, _17, _18, _19, _20, _21, _22, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, \ + _20, _21, _22 + +#define Z_GET_ARGS_FIRST_24(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + _16, _17, _18, _19, _20, _21, _22, _23, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, \ + _20, _21, _22, _23 + +#define Z_GET_ARGS_FIRST_25(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ + _16, _17, _18, _19, _20, _21, _22, _23, _24, ...) \ + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, \ + _20, _21, _22, _23, _24 + +#define Z_GET_ARGS_FIRST_26(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_27(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_28(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_29(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_30(_0, _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, \ + ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_31(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_32(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_33(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_34(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_35(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_36(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_37(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_38(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_39(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_40(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_41(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_42(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_43(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_44(_0, _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, \ + ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_45(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_46(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_47(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_48(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_49(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_50(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_51(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_52(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_53(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_54(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_55(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_56(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_57(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_58(_0, _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, \ + ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_59(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_60(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_61(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_62(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_63(_0, _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, ...) \ + _0, _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 + +#define Z_GET_ARGS_FIRST_64(_0, _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, ...) \ + _0, _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 + #define Z_FOR_EACH_IDX_FIXED_ARG_EXEC(idx, x, fixed_arg0, fixed_arg1) \ fixed_arg0(idx, x, fixed_arg1) diff --git a/include/zephyr/sys/util_macro.h b/include/zephyr/sys/util_macro.h index c8901e21454ae..cba3a6140a76a 100644 --- a/include/zephyr/sys/util_macro.h +++ b/include/zephyr/sys/util_macro.h @@ -400,6 +400,16 @@ extern "C" { */ #define GET_ARGS_LESS_N(N, ...) Z_GET_ARGS_LESS_##N(__VA_ARGS__) +/** + * @brief Get the first N arguments from the argument list. + * + * @param N Number of arguments to take. + * @param ... Variable list of arguments. + * + * @return argument list only contains first N arguments. + */ +#define GET_ARGS_FIRST_N(N, ...) Z_GET_ARGS_FIRST_##N(__VA_ARGS__) + /** * @brief Like a || b, but does evaluation and * short-circuiting at C preprocessor time. diff --git a/scripts/dts/gen_defines.py b/scripts/dts/gen_defines.py index e3913dd585b79..42df47315942a 100755 --- a/scripts/dts/gen_defines.py +++ b/scripts/dts/gen_defines.py @@ -288,6 +288,7 @@ def write_special_props(node: edtlib.Node) -> None: write_pinctrls(node) write_fixed_partitions(node) write_gpio_hogs(node) + write_maps(node) def write_ranges(node: edtlib.Node) -> None: @@ -579,6 +580,72 @@ def write_gpio_hogs(node: edtlib.Node) -> None: out_dt_define(macro, val) +def write_maps(node: edtlib.Node) -> None: + if len(node.maps) == 0: + return + + out_comment("Map properties:") + + basename = str2ident(node.maps[0].basename) + macro = f"{node.z_path_id}_P_{basename}_map" + macro2val = {} + + for i, cd in enumerate(node.maps): + if basename != str2ident(cd.basename): + err(f"Map basename mismatch: {basename} != {str2ident(cd.basename)}") + + macro2val.update(controller_and_data_macros(cd, i, macro, "")) + + prop_id = f"{basename}_map" + plen = len(node.maps) + # DT_N__P__FOREACH_PROP_ELEM + macro2val[f"{macro}_FOREACH_PROP_ELEM(fn)"] = ' \\\n\t'.join( + f'fn(DT_{node.z_path_id}, {prop_id}, {i})' for i in range(plen) + ) + + # DT_N__P__FOREACH_PROP_ELEM_SEP + macro2val[f"{macro}_FOREACH_PROP_ELEM_SEP(fn, sep)"] = ' DT_DEBRACKET_INTERNAL sep \\\n\t'.join( + f'fn(DT_{node.z_path_id}, {prop_id}, {i})' for i in range(plen) + ) + + # DT_N__P__FOREACH_PROP_ELEM_VARGS + macro2val[f"{macro}_FOREACH_PROP_ELEM_VARGS(fn, ...)"] = ' \\\n\t'.join( + f'fn(DT_{node.z_path_id}, {prop_id}, {i}, __VA_ARGS__)' for i in range(plen) + ) + + # DT_N__P__FOREACH_PROP_ELEM_SEP_VARGS + macro2val[f"{macro}_FOREACH_PROP_ELEM_SEP_VARGS(fn, sep, ...)"] = ( + ' DT_DEBRACKET_INTERNAL sep \\\n\t'.join( + f'fn(DT_{node.z_path_id}, {prop_id}, {i}, __VA_ARGS__)' for i in range(plen) + ) + ) + + macro2val[f"{macro}_LEN"] = plen + macro2val[f"{macro}_EXISTS"] = 1 + + for i, cd in enumerate(node.maps): + parent_specifier_len = len([k for k in cd.data if k.startswith('parent_specifier_')]) + child_specifiers = list(cd.data.values())[:-parent_specifier_len] + parent_specifiers = list(cd.data.values())[-parent_specifier_len:] + child_specifier_len = len(child_specifiers) + + args = [] + args.extend([str(v) for v in child_specifiers]) + args.extend(["DT_" + node_z_path_id(cd.controller)]) + args.extend([str(v) for v in parent_specifiers]) + + macro2val[f"{macro}_MAP_IDX_{i}"] = ", ".join(args) + macro2val[f"{macro}_MAP_IDX_{i}_CHILD_SPECIFIER_POS"] = 0 + macro2val[f"{macro}_MAP_IDX_{i}_CHILD_SPECIFIER_LEN"] = child_specifier_len + macro2val[f"{macro}_MAP_IDX_{i}_PARENT_POS"] = child_specifier_len + macro2val[f"{macro}_MAP_IDX_{i}_PARENT_LEN"] = 1 + macro2val[f"{macro}_MAP_IDX_{i}_PARENT_SPECIFIER_POS"] = child_specifier_len + 1 + macro2val[f"{macro}_MAP_IDX_{i}_PARENT_SPECIFIER_LEN"] = parent_specifier_len + + for mc, val in macro2val.items(): + out_dt_define(mc, val) + + def write_vanilla_props(node: edtlib.Node) -> None: # Writes macros for any and all properties defined in the # "properties" section of the binding for the node. diff --git a/scripts/dts/python-devicetree/src/devicetree/edtlib.py b/scripts/dts/python-devicetree/src/devicetree/edtlib.py index 916cdc230d236..e9091388312d4 100644 --- a/scripts/dts/python-devicetree/src/devicetree/edtlib.py +++ b/scripts/dts/python-devicetree/src/devicetree/edtlib.py @@ -1321,6 +1321,97 @@ def gpio_hogs(self) -> list[ControllerAndData]: return res + @property + def maps(self) -> list[ControllerAndData]: + res: list[ControllerAndData] = [] + + def count_cells_num(node: dtlib_Node, specifier: str) -> int: + """ + Calculate the number of cells in the node. + When calculating the number of interrupt cells, + add up the values of the address cells. + """ + + if node is None: + _err("node is None.") + + num = node.props[f"#{specifier}-cells"].to_num() + + if specifier == "interrupt": + parent_props = None + if node.parent: + parent_props = node.parent.props + + if "#address-cells" in node.props: + num = num + node.props["#address-cells"].to_num() + elif parent_props and "#address-cells" in parent_props: + num = num + parent_props["#address-cells"].to_num() + else: + _err("Neither the node nor its parent has `#address-cells` property") + + return num + + for prop in [v for k, v in self._node.props.items() if k.endswith("-map")]: + specifier_space = prop.name[:-4] # Strip '-map' + raw = prop.value + while raw: + if len(raw) < 4: + # Not enough room for phandle + _err("bad value for " + repr(prop)) + + child_specifier_num = count_cells_num(prop.node, specifier_space) + + child_specifiers = to_nums(raw[: 4 * child_specifier_num]) + raw = raw[4 * child_specifier_num :] + phandle = to_num(raw[:4]) + raw = raw[4:] + + controller_node = prop.node.dt.phandle2node.get(phandle) + if controller_node is None: + _err(f"controller node cannot be found from phandle:{phandle}") + + controller: Node = self.edt._node2enode[controller_node] + if controller is None: + _err("controller cannot be found from: " + repr(controller_node)) + + parent_specifier_num = count_cells_num(controller_node, specifier_space) + parent_specifiers = to_nums(raw[: 4 * parent_specifier_num]) + raw = raw[4 * parent_specifier_num :] + + # Although this is rare, if a cell-name is specified for the map node, + # it will be reflected. + # If not specified, the name of child_specifier_[i] will be set. + values: dict[str, int] = {} + for i, v in enumerate(child_specifiers): + cell_name = f"child_specifier_{i}" + if (self._binding and + self._binding.specifier2cells and + specifier_space in self._binding.specifier2cells and + i < len(self._binding.specifier2cells[specifier_space])): + cell_name = self._binding.specifier2cells[specifier_space][i] + + values[cell_name] = v + + # The cell name for parent_specifier cannot be determined. + # For convenience, we assign it the name parent_specifier_[i]. + for i, v in enumerate(parent_specifiers): + values[f"parent_specifier_{i}"] = v + + res.append( + ControllerAndData( + node=self, + controller=controller, + data=values, + name=None, + basename=specifier_space, + ) + ) + + if len(raw) != 0: + _err(f"unexpected prop.value remainings: {raw}") + + return res + @property def has_child_binding(self) -> bool: """ diff --git a/scripts/dts/python-devicetree/tests/test_edtlib.py b/scripts/dts/python-devicetree/tests/test_edtlib.py index f26b5fa158150..6e9c5914889b3 100644 --- a/scripts/dts/python-devicetree/tests/test_edtlib.py +++ b/scripts/dts/python-devicetree/tests/test_edtlib.py @@ -130,6 +130,116 @@ def test_interrupts(): edtlib.ControllerAndData(node=node, controller=edt.get_node('/interrupt-map-bitops-test/controller'), data={'one': 3, 'two': 2}, name=None, basename=None) ] + +def test_maps(): + '''Tests for the maps property.''' + with from_here(): + edt = edtlib.EDT("test.dts", ["test-bindings"]) + + nexus = edt.get_node("/interrupt-map-test/nexus") + controller_0 = edt.get_node("/interrupt-map-test/controller-0") + controller_1 = edt.get_node("/interrupt-map-test/controller-1") + controller_2 = edt.get_node("/interrupt-map-test/controller-2") + + assert nexus.maps == [ + edtlib.ControllerAndData( + node=nexus, + controller=controller_0, + data={ + "child_specifier_0": 0, + "child_specifier_1": 0, + "child_specifier_2": 0, + "child_specifier_3": 0, + "parent_specifier_0": 0, + "parent_specifier_1": 0, + }, + name=None, + basename="interrupt", + ), + edtlib.ControllerAndData( + node=nexus, + controller=controller_1, + data={ + "child_specifier_0": 0, + "child_specifier_1": 0, + "child_specifier_2": 0, + "child_specifier_3": 1, + "parent_specifier_0": 0, + "parent_specifier_1": 0, + "parent_specifier_2": 0, + "parent_specifier_3": 1, + }, + name=None, + basename="interrupt", + ), + edtlib.ControllerAndData( + node=nexus, + controller=controller_2, + data={ + "child_specifier_0": 0, + "child_specifier_1": 0, + "child_specifier_2": 0, + "child_specifier_3": 2, + "parent_specifier_0": 0, + "parent_specifier_1": 0, + "parent_specifier_2": 0, + "parent_specifier_3": 0, + "parent_specifier_4": 0, + "parent_specifier_5": 2, + }, + name=None, + basename="interrupt", + ), + edtlib.ControllerAndData( + node=nexus, + controller=controller_0, + data={ + "child_specifier_0": 0, + "child_specifier_1": 1, + "child_specifier_2": 0, + "child_specifier_3": 0, + "parent_specifier_0": 0, + "parent_specifier_1": 3, + }, + name=None, + basename="interrupt", + ), + edtlib.ControllerAndData( + node=nexus, + controller=controller_1, + data={ + "child_specifier_0": 0, + "child_specifier_1": 1, + "child_specifier_2": 0, + "child_specifier_3": 1, + "parent_specifier_0": 0, + "parent_specifier_1": 0, + "parent_specifier_2": 0, + "parent_specifier_3": 4, + }, + name=None, + basename="interrupt", + ), + edtlib.ControllerAndData( + node=nexus, + controller=controller_2, + data={ + "child_specifier_0": 0, + "child_specifier_1": 1, + "child_specifier_2": 0, + "child_specifier_3": 2, + "parent_specifier_0": 0, + "parent_specifier_1": 0, + "parent_specifier_2": 0, + "parent_specifier_3": 0, + "parent_specifier_4": 0, + "parent_specifier_5": 5, + }, + name=None, + basename="interrupt", + ), + ] + def test_ranges(): '''Tests for the ranges property''' with from_here(): diff --git a/tests/lib/devicetree/api/app.overlay b/tests/lib/devicetree/api/app.overlay index 950ca9b403555..a2961f115a13d 100644 --- a/tests/lib/devicetree/api/app.overlay +++ b/tests/lib/devicetree/api/app.overlay @@ -921,4 +921,49 @@ compatible = "vnd,non-deprecated-label"; label = "FOO"; }; + + gpio-map-test { + connector { + compatible = "vnd,gpio-nexus"; + #gpio-cells = <2>; + gpio-map = <1 2 &{/gpio-map-test/parent} 3 + 4 5 &{/gpio-map-test/parent} 6>; + gpio-map-mask = <0xffffffff 0xffffffc0>; + gpio-map-pass-thru = <0x0 0x3f>; + }; + parent { + compatible = "gpio-dst"; + gpio-controller; + #gpio-cells = <1>; + }; + }; + + interrupt-map-test { + #address-cells = <2>; + #size-cells = <0>; + + controller-0@0 { + compatible = "vnd,cpu-intc"; + reg = <0x0 0x0>; + #address-cells = <1>; + #interrupt-cells = <1>; + interrupt-controller; + }; + controller-1@1 { + compatible = "vnd,intc"; + reg = <0x0 0x1>; + #address-cells = <2>; + #interrupt-cells = <2>; + interrupt-controller; + }; + nexus { + compatible = "vnd,intr-nexus"; + #interrupt-cells = <2>; + interrupt-map = < + 0 0 1 2 &{/interrupt-map-test/controller-0@0} 3 4 + 0 0 5 6 &{/interrupt-map-test/controller-1@1} 7 8 9 0 + 0 1 9 8 &{/interrupt-map-test/controller-0@0} 7 6 + 0 1 5 4 &{/interrupt-map-test/controller-1@1} 3 2 1 0>; + }; + }; }; diff --git a/tests/lib/devicetree/api/src/main.c b/tests/lib/devicetree/api/src/main.c index 98189ebe582cc..38acae5be4387 100644 --- a/tests/lib/devicetree/api/src/main.c +++ b/tests/lib/devicetree/api/src/main.c @@ -111,6 +111,9 @@ #define TEST_SUBPARTITION_1 DT_PATH(test, test_mtd_ffeeddcc, flash_20000000, partitions, \ partition_100, partition_40) +#define TEST_GPIO_CONNECTOR DT_PATH(gpio_map_test, connector) +#define TEST_INTERRUPT_NEXUS DT_PATH(interrupt_map_test, nexus) + #define ZEPHYR_USER DT_PATH(zephyr_user) #define TA_HAS_COMPAT(compat) DT_NODE_HAS_COMPAT(TEST_ARRAYS, compat) @@ -3832,4 +3835,87 @@ ZTEST(devicetree_api, test_interrupt_controller) zassert_true(DT_SAME_NODE(DT_INST_IRQ_INTC(0), TEST_INTC), ""); } +#define INTERRUPT_NEXUS_CHECK_0(n, p, i, ...) \ + zassert_equal(NUM_VA_ARGS(DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 4); \ + zassert_equal(GET_ARG_N(1, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 0); \ + zassert_equal(GET_ARG_N(2, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 0); \ + zassert_equal(GET_ARG_N(3, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 1); \ + zassert_equal(GET_ARG_N(4, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 2); \ + zassert_equal(NUM_VA_ARGS(DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 2); \ + zassert_equal(GET_ARG_N(1, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 3); \ + zassert_equal(GET_ARG_N(2, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 4); \ + zassert_equal(NUM_VA_ARGS(DT_MAP_PARENT_ARG_BY_IDX(n, p, i)), 1); \ + zassert_str_equal(STRINGIFY(DT_MAP_PARENT_ARG_BY_IDX(n, p, i)), \ + "DT_N_S_interrupt_map_test_S_controller_0_0"); + +#define INTERRUPT_NEXUS_CHECK_1(n, p, i, ...) \ + zassert_equal(NUM_VA_ARGS(DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 4); \ + zassert_equal(GET_ARG_N(1, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 0); \ + zassert_equal(GET_ARG_N(2, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 0); \ + zassert_equal(GET_ARG_N(3, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 5); \ + zassert_equal(GET_ARG_N(4, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 6); \ + zassert_equal(NUM_VA_ARGS(DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 4); \ + zassert_equal(GET_ARG_N(1, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 7); \ + zassert_equal(GET_ARG_N(2, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 8); \ + zassert_equal(GET_ARG_N(3, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 9); \ + zassert_equal(GET_ARG_N(4, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 0); \ + zassert_equal(NUM_VA_ARGS(DT_MAP_PARENT_ARG_BY_IDX(n, p, i)), 1); \ + zassert_str_equal(STRINGIFY(DT_MAP_PARENT_ARG_BY_IDX(n, p, i)), \ + "DT_N_S_interrupt_map_test_S_controller_1_1"); + +#define INTERRUPT_NEXUS_CHECK_2(n, p, i, ...) \ + zassert_equal(NUM_VA_ARGS(DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 4); \ + zassert_equal(GET_ARG_N(1, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 0); \ + zassert_equal(GET_ARG_N(2, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 1); \ + zassert_equal(GET_ARG_N(3, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 9); \ + zassert_equal(GET_ARG_N(4, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 8); \ + zassert_equal(NUM_VA_ARGS(DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 2); \ + zassert_equal(GET_ARG_N(1, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 7); \ + zassert_equal(GET_ARG_N(2, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 6); \ + zassert_equal(NUM_VA_ARGS(DT_MAP_PARENT_ARG_BY_IDX(n, p, i)), 1); \ + zassert_str_equal(STRINGIFY(DT_MAP_PARENT_ARG_BY_IDX(n, p, i)), \ + "DT_N_S_interrupt_map_test_S_controller_0_0"); + +#define INTERRUPT_NEXUS_CHECK_3(n, p, i, ...) \ + zassert_equal(NUM_VA_ARGS(DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 4); \ + zassert_equal(GET_ARG_N(1, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 0); \ + zassert_equal(GET_ARG_N(2, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 1); \ + zassert_equal(GET_ARG_N(3, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 5); \ + zassert_equal(GET_ARG_N(4, DT_MAP_CHILD_SPECIFIER_ARGS_BY_IDX(n, p, i)), 4); \ + zassert_equal(NUM_VA_ARGS(DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 4); \ + zassert_equal(GET_ARG_N(1, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 3); \ + zassert_equal(GET_ARG_N(2, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 2); \ + zassert_equal(GET_ARG_N(3, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 1); \ + zassert_equal(GET_ARG_N(4, DT_MAP_PARENT_SPECIFIER_ARGS_BY_IDX(n, p, i)), 0); \ + zassert_str_equal(STRINGIFY(DT_MAP_PARENT_ARG_BY_IDX(n, p, i)), \ + "DT_N_S_interrupt_map_test_S_controller_1_1"); + +#define INTERRUPT_NEXUS_CHECK(...) \ + UTIL_CAT(INTERRUPT_NEXUS_CHECK_, GET_ARG_N(3, __VA_ARGS__))(__VA_ARGS__) + +ZTEST(devicetree_api, test_map) +{ + zassert_equal(DT_PROP_LEN(TEST_GPIO_CONNECTOR, gpio_map), 2); + zassert_equal(DT_PHA_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map, 0, child_specifier_0), 1); + zassert_equal(DT_PHA_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map, 0, child_specifier_1), 2); + zassert_str_equal(STRINGIFY(DT_PHANDLE_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map, 0)), + "DT_N_S_gpio_map_test_S_parent"); + zassert_equal(DT_PHA_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map, 0, parent_specifier_0), 3); + zassert_equal(DT_PHA_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map, 1, child_specifier_0), 4); + zassert_equal(DT_PHA_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map, 1, child_specifier_1), 5); + zassert_str_equal(STRINGIFY(DT_PHANDLE_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map, 1)), + "DT_N_S_gpio_map_test_S_parent"); + zassert_equal(DT_PHA_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map, 1, parent_specifier_0), 6); + + zassert_equal(DT_PROP_LEN(TEST_GPIO_CONNECTOR, gpio_map_mask), 2); + zassert_equal(DT_PROP_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map_mask, 0), 0xffffffff); + zassert_equal(DT_PROP_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map_mask, 1), 0xffffffc0); + zassert_equal(DT_PROP_LEN(TEST_GPIO_CONNECTOR, gpio_map_pass_thru), 2); + zassert_equal(DT_PROP_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map_pass_thru, 0), 0x0); + zassert_equal(DT_PROP_BY_IDX(TEST_GPIO_CONNECTOR, gpio_map_pass_thru, 1), 0x3f); + + DT_FOREACH_PROP_ELEM_VARGS(TEST_INTERRUPT_NEXUS, interrupt_map, INTERRUPT_NEXUS_CHECK, + 9999); +} + ZTEST_SUITE(devicetree_api, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/lib/devicetree/api/testcase.yaml b/tests/lib/devicetree/api/testcase.yaml index ded886833ac87..2759a047ab7d6 100644 --- a/tests/lib/devicetree/api/testcase.yaml +++ b/tests/lib/devicetree/api/testcase.yaml @@ -5,6 +5,7 @@ tests: # will mostly likely be the fastest. platform_allow: - native_sim + - native_sim/native/64 - qemu_x86 - qemu_x86_64 - qemu_cortex_m3 diff --git a/tests/unit/util/main.c b/tests/unit/util/main.c index 6da45befcabaf..d976b8a8e8d2e 100644 --- a/tests/unit/util/main.c +++ b/tests/unit/util/main.c @@ -527,6 +527,28 @@ ZTEST(util, test_GET_ARGS_LESS_N) { zassert_equal(c[0], 3); } +ZTEST(util, test_GET_ARGS_FIRST_N) +{ + uint8_t a[] = {GET_ARGS_FIRST_N(0, 1, 2, 3)}; + uint8_t b[] = {GET_ARGS_FIRST_N(1, 1, 2, 3)}; + uint8_t c[] = {GET_ARGS_FIRST_N(2, 1, 2, 3)}; + uint8_t d[] = {GET_ARGS_FIRST_N(3, 1, 2, 3)}; + + zassert_equal(sizeof(a), 0); + + zassert_equal(sizeof(b), 1); + zassert_equal(b[0], 1); + + zassert_equal(sizeof(c), 2); + zassert_equal(c[0], 1); + zassert_equal(c[1], 2); + + zassert_equal(sizeof(d), 3); + zassert_equal(d[0], 1); + zassert_equal(d[1], 2); + zassert_equal(d[2], 3); +} + ZTEST(util, test_mixing_GET_ARG_and_FOR_EACH) { #undef TEST_MACRO #define TEST_MACRO(x) x,