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
********
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
<devicetree-in-out-files>`, :file:`<board>.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
<devicetree-in-out-files>`.
- ``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.

View file

@ -6,27 +6,32 @@
#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
/* 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;