Skip to content

Commit c58db0c

Browse files
mbolivar-nordiccarlescufi
authored andcommitted
samples: threads: cleanups
Align the docs and source code with other changes done to the basic samples. Use numbers 0 and 1 to refer to the LEDs consistently. This matches the generic devicetree aliases used by Zephyr, instead of using USR1 and USR2, which are specific to 96b_carbon. Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
1 parent e959386 commit c58db0c

File tree

3 files changed

+79
-46
lines changed

3 files changed

+79
-46
lines changed

samples/basic/threads/README.rst

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,48 @@ Basic Thread Example
66
Overview
77
********
88

9-
This example demonstrates spawning of multiple threads using K_THREAD_DEFINE.
9+
This example demonstrates spawning multiple threads using
10+
:c:func:`K_THREAD_DEFINE`. It spawns three threads. Each thread is then defined
11+
at compile time using K_THREAD_DEFINE.
1012

11-
The example works by spawning three threads. The first two threads control a
12-
separate LED. Both of these LEDs (USR1 and USR2) have their individual loop
13-
control and timing logic defined by separate functions.
13+
The first two each control an LED. These LEDs, ``led0`` and ``led1``, have
14+
loop control and timing logic controlled by separate functions.
1415

15-
After either thread toggles its LED, it also pushes information into a
16-
FIFO identifying which thread toggled its LED and how many times it
17-
was done.
16+
- ``blink0()`` controls ``led0`` and has a 100ms sleep cycle
17+
- ``blink1()`` controls ``led1`` and has a 1000ms sleep cycle
1818

19-
The third thread, ``uart_out()``, uses printk (over the UART) to
20-
display the information that comes through the FIFO.
19+
When either of these threads toggles its LED, it also pushes information into a
20+
:ref:`FIFO <fifos_v2>` identifying the thread/LED and how many times it has
21+
been toggled.
2122

22-
- blink1() controls the USR1 LED that has a 100ms sleep cycle
23-
- blink2() controls the USR2 LED that has a 1000ms sleep cycle
23+
The third thread uses :c:func:`printk` to print the information added to the
24+
FIFO to the device console.
2425

25-
Each thread is then defined at compile time using K_THREAD_DEFINE.
26+
Requirements
27+
************
28+
29+
The board must have two LEDs connected via GPIO pins. These are called "User
30+
LEDs" on many of Zephyr's :ref:`boards`. The LEDs must be configured using the
31+
``led0`` and ``led1`` :ref:`devicetree <dt-guide>` aliases, usually in the
32+
:ref:`BOARD.dts file <devicetree-in-out-files>`.
33+
34+
You will see one of these errors if you try to build this sample for an
35+
unsupported board:
36+
37+
.. code-block:: none
38+
39+
Unsupported board: led0 devicetree alias is not defined
40+
Unsupported board: led1 devicetree alias is not defined
2641
2742
Building
2843
********
2944

45+
For example, to build this sample for :ref:`96b_carbon_board`:
46+
3047
.. zephyr-app-commands::
3148
:zephyr-app: samples/basic/threads
3249
:board: 96b_carbon
3350
:goals: build flash
3451
:compact:
52+
53+
Change ``96b_carbon`` appropriately for other supported boards.

samples/basic/threads/sample.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ tests:
1313
type: multi_line
1414
ordered: false
1515
regex:
16-
- "Toggle USR0 LED(.*)"
17-
- "Toggle USR1 LED(.*)"
16+
- "Toggled led0; counter=(.*)"
17+
- "Toggled led1; counter=(.*)"

samples/basic/threads/src/main.c

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@
1717
/* scheduling priority used by each thread */
1818
#define PRIORITY 7
1919

20+
#define LED0_NODE DT_ALIAS(led0)
21+
#define LED1_NODE DT_ALIAS(led1)
22+
23+
/*
24+
* Devicetree helper macro which gets the 'flags' cell from a 'gpios'
25+
* property, or returns 0 if the property has no 'flags' cell.
26+
*/
27+
28+
#define FLAGS_OR_ZERO(node) \
29+
COND_CODE_1(DT_PHA_HAS_CELL(node, gpios, flags), \
30+
(DT_GPIO_FLAGS(node, gpios)), \
31+
(0))
32+
2033
struct printk_data_t {
2134
void *fifo_reserved; /* 1st word reserved for use by fifo */
2235
u32_t led;
@@ -25,18 +38,6 @@ struct printk_data_t {
2538

2639
K_FIFO_DEFINE(printk_fifo);
2740

28-
#if DT_PHA_HAS_CELL(DT_ALIAS(led0), gpios, flags)
29-
#define LED0_FLAGS DT_GPIO_FLAGS(DT_ALIAS(led0), gpios)
30-
#else
31-
#define LED0_FLAGS 0
32-
#endif
33-
34-
#if DT_PHA_HAS_CELL(DT_ALIAS(led1), gpios, flags)
35-
#define LED1_FLAGS DT_GPIO_FLAGS(DT_ALIAS(led1), gpios)
36-
#else
37-
#define LED1_FLAGS 0
38-
#endif
39-
4041
struct led {
4142
const char *gpio_dev_name;
4243
const char *gpio_pin_name;
@@ -51,8 +52,11 @@ void blink(const struct led *led, u32_t sleep_ms, u32_t id)
5152
int ret;
5253

5354
gpio_dev = device_get_binding(led->gpio_dev_name);
54-
__ASSERT(gpio_dev != NULL, "Error: didn't find %s device\n",
55-
led->gpio_dev_name);
55+
if (gpio_dev == NULL) {
56+
printk("Error: didn't find %s device\n",
57+
led->gpio_dev_name);
58+
return;
59+
}
5660

5761
ret = gpio_pin_configure(gpio_dev, led->gpio_pin, led->gpio_flags);
5862
if (ret != 0) {
@@ -79,42 +83,52 @@ void blink(const struct led *led, u32_t sleep_ms, u32_t id)
7983
}
8084
}
8185

82-
void blink1(void)
86+
void blink0(void)
8387
{
84-
const struct led led1 = {
85-
.gpio_dev_name = DT_GPIO_LABEL(DT_ALIAS(led0), gpios),
86-
.gpio_pin_name = DT_LABEL(DT_ALIAS(led0)),
87-
.gpio_pin = DT_GPIO_PIN(DT_ALIAS(led0), gpios),
88-
.gpio_flags = GPIO_OUTPUT | LED0_FLAGS,
88+
const struct led led0 = {
89+
#if DT_NODE_HAS_STATUS(LED0_NODE, okay)
90+
.gpio_dev_name = DT_GPIO_LABEL(LED0_NODE, gpios),
91+
.gpio_pin_name = DT_LABEL(LED0_NODE),
92+
.gpio_pin = DT_GPIO_PIN(LED0_NODE, gpios),
93+
.gpio_flags = GPIO_OUTPUT | FLAGS_OR_ZERO(LED0_NODE),
94+
#else
95+
#error "Unsupported board: led0 devicetree alias is not defined"
96+
#endif
8997
};
9098

91-
blink(&led1, 100, 0);
99+
blink(&led0, 100, 0);
92100
}
93101

94-
void blink2(void)
102+
void blink1(void)
95103
{
96-
const struct led led2 = {
97-
.gpio_dev_name = DT_GPIO_LABEL(DT_ALIAS(led1), gpios),
98-
.gpio_pin_name = DT_LABEL(DT_ALIAS(led1)),
99-
.gpio_pin = DT_GPIO_PIN(DT_ALIAS(led1), gpios),
100-
.gpio_flags = GPIO_OUTPUT | LED1_FLAGS,
104+
const struct led led1 = {
105+
#if DT_NODE_HAS_STATUS(LED1_NODE, okay)
106+
.gpio_dev_name = DT_GPIO_LABEL(LED1_NODE, gpios),
107+
.gpio_pin_name = DT_LABEL(LED1_NODE),
108+
.gpio_pin = DT_GPIO_PIN(LED1_NODE, gpios),
109+
.gpio_flags = GPIO_OUTPUT | FLAGS_OR_ZERO(LED1_NODE),
110+
#else
111+
#error "Unsupported board: led1 devicetree alias is not defined"
112+
#endif
101113
};
102114

103-
blink(&led2, 1000, 1);
115+
blink(&led1, 1000, 1);
104116
}
105117

106118
void uart_out(void)
107119
{
108120
while (1) {
109-
struct printk_data_t *rx_data = k_fifo_get(&printk_fifo, K_FOREVER);
110-
printk("Toggle USR%d LED: Counter = %d\n", rx_data->led, rx_data->cnt);
121+
struct printk_data_t *rx_data = k_fifo_get(&printk_fifo,
122+
K_FOREVER);
123+
printk("Toggled led%d; counter=%d\n",
124+
rx_data->led, rx_data->cnt);
111125
k_free(rx_data);
112126
}
113127
}
114128

115-
K_THREAD_DEFINE(blink1_id, STACKSIZE, blink1, NULL, NULL, NULL,
129+
K_THREAD_DEFINE(blink0_id, STACKSIZE, blink0, NULL, NULL, NULL,
116130
PRIORITY, 0, 0);
117-
K_THREAD_DEFINE(blink2_id, STACKSIZE, blink2, NULL, NULL, NULL,
131+
K_THREAD_DEFINE(blink1_id, STACKSIZE, blink1, NULL, NULL, NULL,
118132
PRIORITY, 0, 0);
119133
K_THREAD_DEFINE(uart_out_id, STACKSIZE, uart_out, NULL, NULL, NULL,
120134
PRIORITY, 0, 0);

0 commit comments

Comments
 (0)