From 0d1d441aec9e37c5b6c138f73ce041761d188405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Tue, 10 Mar 2020 11:44:10 -0700 Subject: [PATCH] samples: convert blinky to new DT API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This doesn't sacrifice any readability when compiled for boards that don't support this alias. Signed-off-by: Martí Bolívar --- samples/basic/blinky/README.rst | 29 +++++++++++++++-------------- samples/basic/blinky/src/main.c | 23 ++++++++++++++--------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/samples/basic/blinky/README.rst b/samples/basic/blinky/README.rst index 9adb3c69214..674ab1099ea 100644 --- a/samples/basic/blinky/README.rst +++ b/samples/basic/blinky/README.rst @@ -6,29 +6,31 @@ Blinky sample Overview ******** -The Blinky example shows how to configure GPIO pins as outputs which can also be -used to drive LEDs on the hardware usually delivered as "User LEDs" on many of -the supported boards in Zephyr. +Blinky is a simple application which blinks an LED forever. + +The source code shows how to configure GPIO pins as outputs, then turn them on +and off. .. _blinky-sample-requirements: Requirements ************ -The demo assumes that an LED is connected to one of GPIO lines. The -sample code is configured to work on boards that have defined the ``led0`` -alias in their :ref:`board's devicetree description file -`, :file:`.dts`. -Doing so will generate these variables: +The board must have an LED connected via a GPIO pin. These are called "User +LEDs" on many of Zephyr's :ref:`boards`. The LED must be configured using the +``led0`` :ref:`dt-guide` alias in the :ref:`BOARD.dts file +`. -- ``DT_ALIAS_LED0_GPIOS_CONTROLLER`` -- ``DT_ALIAS_LED0_GPIOS_PIN`` +You will see this error if you try to build Blinky for an unsupported board: + +.. code-block:: none + + Unsupported board: led0 devicetree alias is not defined Building and Running ******************** -This sample does not output anything to the console. It can be built and -flashed to a board as follows: +Build and flash Blinky as follows, changing ``reel_board`` for your board: .. zephyr-app-commands:: :zephyr-app: samples/basic/blinky @@ -36,5 +38,4 @@ flashed to a board as follows: :goals: build flash :compact: -After flashing the image to the board, the user LED on the board should start to -blink. +After flashing, the LED starts to blink. Blinky does not print to the console. diff --git a/samples/basic/blinky/src/main.c b/samples/basic/blinky/src/main.c index 21f33309194..198ad58ddf4 100644 --- a/samples/basic/blinky/src/main.c +++ b/samples/basic/blinky/src/main.c @@ -6,27 +6,32 @@ #include #include +#include #include /* 1000 msec = 1 sec */ #define SLEEP_TIME_MS 1000 -#ifndef DT_ALIAS_LED0_GPIOS_FLAGS -#define FLAGS 0 -#else -#define FLAGS DT_ALIAS_LED0_GPIOS_FLAGS -#endif +/* The devicetree node identifier for the "led0" alias. */ +#define LED0_NODE DT_ALIAS(led0) -/* Make sure the board's devicetree declares led0 in its /aliases. */ -#ifdef DT_ALIAS_LED0_GPIOS_CONTROLLER -#define LED0 DT_ALIAS_LED0_GPIOS_CONTROLLER -#define PIN DT_ALIAS_LED0_GPIOS_PIN +#if DT_HAS_NODE(LED0_NODE) +#define LED0 DT_GPIO_LABEL(LED0_NODE, gpios) +#define PIN DT_GPIO_PIN(LED0_NODE, gpios) +#if DT_PHA_HAS_CELL(LED0_NODE, gpios, flags) +#define FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios) +#endif #else +/* A build error here means your board isn't set up to blink an LED. */ #error "Unsupported board: led0 devicetree alias is not defined" #define LED0 "" #define PIN 0 #endif +#ifndef FLAGS +#define FLAGS 0 +#endif + void main(void) { struct device *dev;