Zephyr如何注册设备实例
设备树 → 编译期生成 → 运行时访问 流程图:
Zephyr dev->config
工作流程
设备树 (.dts)
─────────────────────────────
anx7451@39 {compatible = "analogix,anx7451";reg = <0x39>;reset-gpios = <&gpio1 5 GPIO_ACTIVE_LOW>;init-delay-ms = <50>;
};│▼
设备树编译 (dtc) + Zephyr 代码生成
─────────────────────────────
生成 C 宏:DT_PROP(DT_NODELABEL(anx7451), reg) = 0x39GPIO_DT_SPEC_GET(DT_NODELABEL(anx7451), reset_gpios) = {...}DT_PROP(DT_NODELABEL(anx7451), init_delay_ms) = 50│▼
编译期展开 DEVICE_DT_DEFINE()
─────────────────────────────
生成静态对象:static const struct anx7451_config anx7451_config = {.bus = ...,.reset_gpio = ...,.init_delay_ms = 50,};static struct anx7451_data anx7451_data;static struct device DEVICE_anx7451 = {.name = "ANX7451",.config = &anx7451_config, <─── 指针挂上去.data = &anx7451_data,.init = anx7451_init,};│▼
运行时(Zephyr 启动)
─────────────────────────────
1. 内核遍历所有 struct device
2. 调用 anx7451_init(&DEVICE_anx7451)│▼
驱动 init 函数
─────────────────────────────
static int anx7451_init(const struct device *dev) {const struct anx7451_config *config = dev->config;// 使用编译期配置参数gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT);k_msleep(config->init_delay_ms);
}
完整链路:
设备树参数 → 宏生成 → 编译期静态 config → dev->config
指针 → 驱动运行时访问。