我想在设备树覆盖中定义一些常量。例如:为了给gpio引脚9命名led blue,我在设备树覆盖中添加了以下内容:
/ {
gpio_pin_names {
led-blue = < 9 >;
};
};
这会将以下内容添加到zephyr.dts
:
gpio_pin_names {
led-blue = < 0x9 >;
};
然而,devicetree_unfixed.h
上没有显示任何内容,这使得读取代码中的值变得有点困难。。。
向设备树覆盖添加常量的正确方法是什么?如何读取代码中的值?
在您的板设备树中:
/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
}