設備樹 → 編譯期生成 → 運行時訪問 流程圖:
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
指針 → 驅動運行時訪問。