Zephyr OS 学习笔记1-1:点亮第一盏LED灯

#include 
#include 
#include 

#define LED_PORT LED0_GPIO_CONTROLLER
#define LED LED0_GPIO_PIN

void main(void)
{
    struct device *dev;
    dev = device_get_binding(LED_PORT);//获取设备
    gpio_pin_configure(dev,LED,GPIO_DIR_OUT);//将LED0_GPIO_PIN设为输出模式
    while (1) {
                //我的开发板上的灯是共阳极的,低电平亮
        gpio_pin_write(dev,LED,0);//将LED0_GPIO_PIN置低
    }
}

首先,用device_get_binding获取P0这个设备
然后,用gpio_pin_configure将P0.X这个脚设为输出模式
最后,用gpio_pin_write将P0.X置低

下面有个问题:LED0_GPIO_CONTROLLER、LED0_GPIO_PIN到底指的什么呢?

在Eclipse中用Search搜索LED0_GPIO_CONTROLLER,在zephyrproject\zephyr\samples\basic\light\build\zephyr\include\generated中的generated_dts_board_unfixed.h中可以找到

#define LED0_GPIO_CONTROLLER            DT_GPIO_LEDS_LED_0_GPIO_CONTROLLER

再次查找,可以找到如下内容:

DT_GPIO_LEDS_LED_0_GPIO_CONTROLLER="GPIO_0"

即LED0_GPIO_CONTROLLER="GPIO_0"

那么如何直接控制GPIO呢?

在zephyrproject\zephyr\dts\arm\nordic下打开nrf52832.dtsi
其中有以下内容:

gpio0: gpio@50000000 {
            compatible = "nordic,nrf-gpio";
            gpio-controller;
            reg = <0x50000000 0x200
                   0x50000500 0x300>;
            #gpio-cells = <2>;
            label = "GPIO_0";
            status = "disabled";
        };

所以GPIO0的标签是GPIO_0
所以以下代码能起到同样的效果

#include 
#include 
#include 
void main(void)
{
    struct device *Port0;
    Port0 = device_get_binding("GPIO_0");
    gpio_pin_configure(Port0,17,GPIO_DIR_OUT);
    while (1) {
        gpio_pin_write(Port0,17, 0);
    }
}

也可以用GPIO_0_LABEL

#include 
#include 
#include 
void main(void)
{
    struct device *Port0;
    Port0 = device_get_binding(GPIO_0_LABEL);
    gpio_pin_configure(Port0,17,GPIO_DIR_OUT);
    while (1) {
        gpio_pin_write(Port0,17, 0);
    }
}

效果如下

蓝灯既是所点亮的LED灯.jpg

你可能感兴趣的:(Zephyr OS 学习笔记1-1:点亮第一盏LED灯)