我想使用设备树来存储一些系统级常量。
是否可以从设备树中存储和检索任意值?
尝试加载这些值无法编译为build/zephyr/include/generated/devicetree_unfixed.h缺少"custom-num"或"other-value"的值。
...
int custom_num = DT_PROP(DT_PATH(settings), custom_num);
printf("custom_num %dn", 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
| ^~~~
...
设备树覆盖文件:
/* SPDX-License-Identifier: Apache-2.0 */
/ {
aliases {
someuart-uart = &uart7;
};
settings {
custom-num = < 29992 >;
another-value = "some string";
};
};
这里缺少的部分是设备树绑定,https://docs.zephyrproject.org/latest/guides/dts/bindings.html#,dt条目必须匹配才能正确处理。
解决这个问题的技巧来自zephyrproject问题跟踪器:https://github.com/zephyrproject-rtos/zephyr/issues/42404
首先,我们必须稍微调整覆盖,以包含">兼容"条目,如:
/* SPDX-License-Identifier: Apache-2.0 */
/ {
aliases {
someuart-uart = &uart7;
};
settings {
compatible = "my-company,settings"; /* <--------- */
custom-num = < 29992 >;
another-value = "some string";
};
};
然后在项目的dts/bindings/文件夹中创建一个文件,根据Zephyr的建议,在兼容字符串之后命名:
dts/bindings/my-company,settings.yaml
包含">我的公司,设置"条目中所需的格式:
compatible: "my-company,settings"
description: "Project specific settings and configuration"
properties:
custom-num:
type: int
required: true
another-value:
type: string
required: false