1

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

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

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

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

但是,什么都没有出现devicetree_unfixed.h,这使得读取代码中的值有点困难......

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

4

1 回答 1

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
}
于 2021-11-12T17:24:26.377 回答