Skip to content

Commit efc0e75

Browse files
mbolivar-nordiccarlescufi
authored andcommitted
samples: blinky: use a gpio_dt_spec
Using a gpio_dt_spec is the current best practice for getting at pins defined in the DT, because it gets all the boilerplate and flags right for you. Use that in blinky, keeping the documentation in sync with the code. Signed-off-by: Martí Bolívar <[email protected]>
1 parent c0cc6c7 commit efc0e75

File tree

2 files changed

+78
-36
lines changed

2 files changed

+78
-36
lines changed

samples/basic/blinky/README.rst

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,27 @@ Blinky
66
Overview
77
********
88

9-
Blinky is a simple application which blinks an LED forever using the :ref:`GPIO
10-
API <gpio_api>`. The source code shows how to configure GPIO pins as outputs,
11-
then turn them on and off.
9+
The Blinky sample blinks an LED forever using the :ref:`GPIO API <gpio_api>`.
1210

13-
See :ref:`pwm-blinky-sample` for a sample which uses the PWM API to blink an
14-
LED.
11+
The source code shows how to:
12+
13+
#. Get a pin specification from the :ref:`devicetree <dt-guide>` as a
14+
:c:struct:`gpio_dt_spec`
15+
#. Configure the GPIO pin as an output
16+
#. Toggle the pin forever
17+
18+
See :ref:`pwm-blinky-sample` for a similar sample that uses the PWM API instead.
1519

1620
.. _blinky-sample-requirements:
1721

1822
Requirements
1923
************
2024

21-
You will see this error if you try to build Blinky for an unsupported board:
22-
23-
.. code-block:: none
24-
25-
Unsupported board: led0 devicetree alias is not defined
25+
Your board must:
2626

27-
The board must have an LED connected via a GPIO pin. These are called "User
28-
LEDs" on many of Zephyr's :ref:`boards`. The LED must be configured using the
29-
``led0`` :ref:`devicetree <dt-guide>` alias. This is usually done in the
30-
:ref:`BOARD.dts file <devicetree-in-out-files>` or a :ref:`devicetree overlay
31-
<set-devicetree-overlays>`.
27+
#. Have an LED connected via a GPIO pin (these are called "User LEDs" on many of
28+
Zephyr's :ref:`boards`).
29+
#. Have the LED configured using the ``led0`` devicetree alias.
3230

3331
Building and Running
3432
********************
@@ -41,4 +39,57 @@ Build and flash Blinky as follows, changing ``reel_board`` for your board:
4139
:goals: build flash
4240
:compact:
4341

44-
After flashing, the LED starts to blink. Blinky does not print to the console.
42+
After flashing, the LED starts to blink. If a runtime error occurs, the sample
43+
exits without printing to the console.
44+
45+
Build errors
46+
************
47+
48+
You will see a build error at the source code line defining the ``struct
49+
gpio_dt_spec led`` variable if you try to build Blinky for an unsupported
50+
board.
51+
52+
On GCC-based toolchains, the error looks like this:
53+
54+
.. code-block:: none
55+
56+
error: '__device_dts_ord_DT_N_ALIAS_led_P_gpios_IDX_0_PH_ORD' undeclared here (not in a function)
57+
58+
Adding board support
59+
********************
60+
61+
To add support for your board, add something like this to your devicetree:
62+
63+
.. code-block:: DTS
64+
65+
/ {
66+
aliases {
67+
led0 = &myled0;
68+
};
69+
70+
leds {
71+
compatible = "gpio-leds";
72+
myled0: led_0 {
73+
gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
74+
};
75+
};
76+
};
77+
78+
The above sets your board's ``led0`` alias to use pin 13 on GPIO controller
79+
``gpio0``. The pin flags :c:macro:`GPIO_ACTIVE_HIGH` mean the LED is on when
80+
the pin is set to its high state, and off when the pin is in its low state.
81+
82+
Tips:
83+
84+
- See :dtcompatible:`gpio-leds` for more information on defining GPIO-based LEDs
85+
in devicetree.
86+
87+
- If you're not sure what to do, check the devicetrees for supported boards which
88+
use the same SoC as your target. See :ref:`get-devicetree-outputs` for details.
89+
90+
- See :zephyr_file:`include/dt-bindings/gpio/gpio.h` for the flags you can use
91+
in devicetree.
92+
93+
- If the LED is built in to your board hardware, the alias should be defined in
94+
your :ref:`BOARD.dts file <devicetree-in-out-files>`. Otherwise, you can
95+
define one in a :ref:`devicetree overlay <set-devicetree-overlays>`.

samples/basic/blinky/src/main.c

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66

77
#include <zephyr.h>
8-
#include <device.h>
9-
#include <devicetree.h>
108
#include <drivers/gpio.h>
119

1210
/* 1000 msec = 1 sec */
@@ -15,37 +13,30 @@
1513
/* The devicetree node identifier for the "led0" alias. */
1614
#define LED0_NODE DT_ALIAS(led0)
1715

18-
#if DT_NODE_HAS_STATUS(LED0_NODE, okay)
19-
#define LED0 DT_GPIO_LABEL(LED0_NODE, gpios)
20-
#define PIN DT_GPIO_PIN(LED0_NODE, gpios)
21-
#define FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios)
22-
#else
23-
/* A build error here means your board isn't set up to blink an LED. */
24-
#error "Unsupported board: led0 devicetree alias is not defined"
25-
#define LED0 ""
26-
#define PIN 0
27-
#define FLAGS 0
28-
#endif
16+
/*
17+
* A build error on this line means your board is unsupported.
18+
* See the sample documentation for information on how to fix this.
19+
*/
20+
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
2921

3022
void main(void)
3123
{
32-
const struct device *dev;
33-
bool led_is_on = true;
3424
int ret;
3525

36-
dev = device_get_binding(LED0);
37-
if (dev == NULL) {
26+
if (!device_is_ready(led.port)) {
3827
return;
3928
}
4029

41-
ret = gpio_pin_configure(dev, PIN, GPIO_OUTPUT_ACTIVE | FLAGS);
30+
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
4231
if (ret < 0) {
4332
return;
4433
}
4534

4635
while (1) {
47-
gpio_pin_set(dev, PIN, (int)led_is_on);
48-
led_is_on = !led_is_on;
36+
ret = gpio_pin_toggle_dt(&led);
37+
if (ret < 0) {
38+
return;
39+
}
4940
k_msleep(SLEEP_TIME_MS);
5041
}
5142
}

0 commit comments

Comments
 (0)