在您的板设备树中:
/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
}