Skip to content
This repository was archived by the owner on Aug 12, 2022. It is now read-only.

Commit 438271d

Browse files
DhruvaG2000szczys
authored andcommitted
Add multi thread Example
- Demonstrate that direct zephyr calls can be made even with the Arduino API in use. - Examples shows concurrency by async blinking of 3 LEDs Signed-off-by: Dhruva Gole <goledhruva@gmail.com>
1 parent a4609d5 commit 438271d

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(threads)
6+
7+
target_sources(app PRIVATE src/main.cpp)

samples/threads_arduino/README.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. _arduino_nano_33_ble_multi_thread_blinky:
2+
3+
Basic Thread Example
4+
####################
5+
6+
Overview
7+
********
8+
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`.
12+
13+
These three each control an LED. These LEDs, ``LED_BUILTIN``, ``D10`` and ``D11``, have
14+
loop control and timing logic controlled by separate functions.
15+
16+
- ``blink0()`` controls ``LED_BUILTIN`` and has a 100ms sleep cycle
17+
- ``blink1()`` controls ``D11`` and has a 1000ms sleep cycle
18+
- ``loop()`` controls ``D10`` and has a 300ms sleep cycle
19+
20+
Requirements
21+
************
22+
23+
The board must have two LEDs connected via GPIO pins and one builtin LED. These are called "User
24+
LEDs" on many of Zephyr's :ref:`boards`. The LEDs must be mapped using the `<board_name_pinmap.h>`
25+
``LED_BUILTIN``, ``D10`` and ``D11`` to the :ref:`devicetree <dt-guide>` aliases, in the
26+
variants folder.
27+
28+
You will see one of these errors if you try to build this sample for an
29+
unsupported board:
30+
31+
.. code-block:: none
32+
33+
Unsupported board: LED_BUILTIN devicetree alias is not defined
34+
Unsupported board: D11 devicetree alias is not defined
35+
36+
Building
37+
********
38+
39+
For example, to build this sample for :ref:`arduino_nano_33_ble`:
40+
41+
.. zephyr-app-commands::
42+
:zephyr-app: samples/basic/arduino-threads
43+
:board: arduino_nano_33_ble
44+
:goals: build flash
45+
:compact:
46+
47+
Change ``arduino_nano_33_ble`` appropriately for other supported boards.

samples/threads_arduino/prj.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_HEAP_MEM_POOL_SIZE=256
2+
CONFIG_GPIO=y
3+
CONFIG_CPLUSPLUS=y
4+
CONFIG_ARDUINO_API=y
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2022 Dhruva Gole
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <Arduino.h>
8+
9+
/* size of stack area used by each thread */
10+
#define STACKSIZE 1024
11+
12+
/* scheduling priority used by each thread */
13+
#define PRIORITY 7
14+
15+
void blink0(void)
16+
{
17+
while (1) {
18+
digitalWrite(LED_BUILTIN, HIGH);
19+
delay(100);
20+
digitalWrite(LED_BUILTIN, LOW);
21+
delay(100);
22+
}
23+
}
24+
25+
void blink1(void)
26+
{
27+
while (1) {
28+
digitalWrite(D11, HIGH);
29+
delay(1000);
30+
digitalWrite(D11, LOW);
31+
delay(1000);
32+
}
33+
}
34+
35+
K_THREAD_DEFINE(blink0_id, STACKSIZE, blink0, NULL, NULL, NULL, PRIORITY, 0, 0);
36+
K_THREAD_DEFINE(blink1_id, STACKSIZE, blink1, NULL, NULL, NULL, PRIORITY, 0, 0);
37+
K_THREAD_DEFINE(blink2_id, STACKSIZE, loop, NULL, NULL, NULL, PRIORITY, 0, 0);
38+
39+
void setup()
40+
{
41+
pinMode(LED_BUILTIN, OUTPUT);
42+
pinMode(D11, OUTPUT);
43+
pinMode(D10, OUTPUT);
44+
}
45+
void loop()
46+
{
47+
digitalWrite(D10, HIGH);
48+
delay(300);
49+
digitalWrite(D10, LOW);
50+
delay(300);
51+
}

0 commit comments

Comments
 (0)