【问题标题】:How to add constants to a devicetree overlay?如何将常量添加到设备树覆盖?
【发布时间】:2021-12-20 16:31:16
【问题描述】:

我想在设备树覆盖中定义一些常量。例如:为了给 gpio pin 9 命名为 led-blue,我在 devicetree 叠加层中添加了以下内容:

/ {
    gpio_pin_names {
        led-blue = < 9 >;
    };
};

这会将以下内容添加到zephyr.dts

gpio_pin_names {
    led-blue = < 0x9 >;
};

但是,devicetree_unfixed.h 没有显示任何内容,这使得读取代码中的值有点困难......

将常量添加到设备树覆盖的正确方法是什么?以及如何读取代码中的值?

【问题讨论】:

    标签: device-tree zephyr-rtos


    【解决方案1】:

    在您的看板设备树中:

    /dts-v1/;
    #include <nordic/nrf51822_qfaa.dtsi>
    
    / {
        model = "nRF51test";
        compatible = "nrf51test";
    
        chosen {
            zephyr,sram = &sram0;
            zephyr,flash = &flash0;
            zephyr,flash-controller = &flash_controller;
        };
    
        leds {
            compatible = "gpio-leds";
            ledblue: ledblue {
                gpios = <&gpio0 23 0>;
                label = "Blue LED";
            };
        };
    
        aliases {
            ledblue = &ledblue;
        };
    };
    
    &gpiote {
        status = "okay";
    };
    
    &gpio0 {
        status = "okay";
    };
    

    然后在你的代码中使用ledblue:

    //ledblue config
    #define LEDBLUE_NODE DT_ALIAS(ledblue)
    #define LEDBLUE      DT_GPIO_LABEL(LEDBLUE_NODE, gpios)
    #define LEDBLUEPIN   DT_GPIO_PIN(LEDBLUE_NODE, gpios)
    #define FLAGS        DT_GPIO_FLAGS(LEDBLUE_NODE, gpios)
    
    
    void main(void)
    {
        const struct device *dev;
        dev = device_get_binding(LEDBLUE);
        int ret;
        ret = gpio_pin_configure(dev, LEDBLUEPIN, GPIO_OUTPUT_ACTIVE | FLAGS);
    
        bool led_is_on = true;
        while (true) {
            //flash blue led
            gpio_pin_set(dev, LEDBLUEPIN, (int)led_is_on);
            led_is_on = !led_is_on;
            k_msleep(500);
        }
    }
    

    编辑: 如果您想忽略设备树并在 main.c 中执行所有操作,则可以使用更少的代码 - 尽管如果板切换您将需要更改 main.c 代码以反映这一点(在此示例中,不会声明 LED设备树):

    #define BLUE_LED_PIN 23
    
    void main(void)
    {
        const struct device *devGPIO;
        int ret;
        devGPIO = device_get_binding("GPIO0");
        ret = gpio_pin_configure(devGPIO, BLUE_LED_PIN, GPIO_OUTPUT_ACTIVE);
        gpio_pin_set(devGPIO, BLUE_LED_PIN, 1); //1 for on, 0 for off
    }
    

    【讨论】:

    • 谢谢!但是有一个问题:有没有一种更简单(更短)的方法来做到这一点?例如,只是添加一个常数? (即不是指实际硬件的值。)
    • @SePröbläm 我添加了一个非常小的示例。
    猜你喜欢
    • 1970-01-01
    • 2018-11-29
    • 2021-12-29
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多