Chris Morgan
Chris Morgan

Reputation: 1339

Is it possible to have arbitrary data in a zephyr device tree node?

I'd like to use the device tree to store some system level constants.

Is it possible to store and retrieve arbitrary values from the device tree?

Attempting to load these values fails to compile as build/zephyr/include/generated/devicetree_unfixed.h lacks the values for 'custom-num' or 'another-value'.

...
    int custom_num = DT_PROP(DT_PATH(settings), custom_num);
    printf("custom_num %d\n", custom_num);
...
...
zephyr/include/devicetree.h:81:17: error: 'DT_N_S_settings_P_custom_num' undeclared (first use in this function)
   81 | #define DT_ROOT DT_N
      |                 ^~~~
...

The devicetree overlay file:

/* SPDX-License-Identifier: Apache-2.0 */
/ {
        aliases {
                someuart-uart = &uart7;
        };

        settings {
                custom-num = < 29992 >;
                another-value = "some string";
        };
};

Upvotes: 0

Views: 873

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 1339

The missing part here was the devicetree bindings, https://docs.zephyrproject.org/latest/guides/dts/bindings.html#, that a dt entry has to match in order to be correctly processed.

The tip to figure this out came from the zephyrproject issue tracker: https://github.com/zephyrproject-rtos/zephyr/issues/42404

First we have to adjust the overlay slightly to include a 'compatible' entry like:

/* SPDX-License-Identifier: Apache-2.0 */
/ {
        aliases {
                someuart-uart = &uart7;
        };

        settings {
                compatible = "my-company,settings";  /* <--------- */
                custom-num = < 29992 >;
                another-value = "some string";
        };
};

and then creating a file in the project's dts/bindings/ folder named, per the Zephyr recommendations after the compatible string:

dts/bindings/my-company,settings.yaml

that contains the format expected in a 'my-company,settings' entry:

compatible: "my-company,settings"

description: "Project specific settings and configuration"

properties:
  custom-num:
    type: int
    required: true
  another-value:
    type: string
    required: false

Upvotes: 3

Related Questions