samples: convert blinky to new DT API

This doesn't sacrifice any readability when compiled for boards that
don't support this alias.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2020-03-10 11:44:10 -07:00 committed by Kumar Gala
commit 0d1d441aec
2 changed files with 29 additions and 23 deletions

View file

@ -6,29 +6,31 @@ Blinky sample
Overview Overview
******** ********
The Blinky example shows how to configure GPIO pins as outputs which can also be Blinky is a simple application which blinks an LED forever.
used to drive LEDs on the hardware usually delivered as "User LEDs" on many of
the supported boards in Zephyr. The source code shows how to configure GPIO pins as outputs, then turn them on
and off.
.. _blinky-sample-requirements: .. _blinky-sample-requirements:
Requirements Requirements
************ ************
The demo assumes that an LED is connected to one of GPIO lines. The The board must have an LED connected via a GPIO pin. These are called "User
sample code is configured to work on boards that have defined the ``led0`` LEDs" on many of Zephyr's :ref:`boards`. The LED must be configured using the
alias in their :ref:`board's devicetree description file ``led0`` :ref:`dt-guide` alias in the :ref:`BOARD.dts file
<devicetree-in-out-files>`, :file:`<board>.dts`. <devicetree-in-out-files>`.
Doing so will generate these variables:
- ``DT_ALIAS_LED0_GPIOS_CONTROLLER`` You will see this error if you try to build Blinky for an unsupported board:
- ``DT_ALIAS_LED0_GPIOS_PIN``
.. code-block:: none
Unsupported board: led0 devicetree alias is not defined
Building and Running Building and Running
******************** ********************
This sample does not output anything to the console. It can be built and Build and flash Blinky as follows, changing ``reel_board`` for your board:
flashed to a board as follows:
.. zephyr-app-commands:: .. zephyr-app-commands::
:zephyr-app: samples/basic/blinky :zephyr-app: samples/basic/blinky
@ -36,5 +38,4 @@ flashed to a board as follows:
:goals: build flash :goals: build flash
:compact: :compact:
After flashing the image to the board, the user LED on the board should start to After flashing, the LED starts to blink. Blinky does not print to the console.
blink.

View file

@ -6,27 +6,32 @@
#include <zephyr.h> #include <zephyr.h>
#include <device.h> #include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h> #include <drivers/gpio.h>
/* 1000 msec = 1 sec */ /* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000 #define SLEEP_TIME_MS 1000
#ifndef DT_ALIAS_LED0_GPIOS_FLAGS /* The devicetree node identifier for the "led0" alias. */
#define FLAGS 0 #define LED0_NODE DT_ALIAS(led0)
#else
#define FLAGS DT_ALIAS_LED0_GPIOS_FLAGS
#endif
/* Make sure the board's devicetree declares led0 in its /aliases. */ #if DT_HAS_NODE(LED0_NODE)
#ifdef DT_ALIAS_LED0_GPIOS_CONTROLLER #define LED0 DT_GPIO_LABEL(LED0_NODE, gpios)
#define LED0 DT_ALIAS_LED0_GPIOS_CONTROLLER #define PIN DT_GPIO_PIN(LED0_NODE, gpios)
#define PIN DT_ALIAS_LED0_GPIOS_PIN #if DT_PHA_HAS_CELL(LED0_NODE, gpios, flags)
#define FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios)
#endif
#else #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" #error "Unsupported board: led0 devicetree alias is not defined"
#define LED0 "" #define LED0 ""
#define PIN 0 #define PIN 0
#endif #endif
#ifndef FLAGS
#define FLAGS 0
#endif
void main(void) void main(void)
{ {
struct device *dev; struct device *dev;