From 1583ac4612f7aee05f8217c78c38acee010d6121 Mon Sep 17 00:00:00 2001 From: Dhruva Gole Date: Sat, 9 Jul 2022 23:52:02 +0530 Subject: [PATCH] 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 --- samples/threads_arduino/CMakeLists.txt | 7 ++++ samples/threads_arduino/README.rst | 47 ++++++++++++++++++++++++ samples/threads_arduino/prj.conf | 4 ++ samples/threads_arduino/src/main.cpp | 51 ++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 samples/threads_arduino/CMakeLists.txt create mode 100644 samples/threads_arduino/README.rst create mode 100644 samples/threads_arduino/prj.conf create mode 100644 samples/threads_arduino/src/main.cpp diff --git a/samples/threads_arduino/CMakeLists.txt b/samples/threads_arduino/CMakeLists.txt new file mode 100644 index 000000000..c0ccc28d0 --- /dev/null +++ b/samples/threads_arduino/CMakeLists.txt @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(threads) + +target_sources(app PRIVATE src/main.cpp) diff --git a/samples/threads_arduino/README.rst b/samples/threads_arduino/README.rst new file mode 100644 index 000000000..670cd23dd --- /dev/null +++ b/samples/threads_arduino/README.rst @@ -0,0 +1,47 @@ +.. _arduino_nano_33_ble_multi_thread_blinky: + +Basic Thread Example +#################### + +Overview +******** + +This example demonstrates spawning multiple threads using +:c:func:`K_THREAD_DEFINE`. It spawns three threads. Each thread is then defined +at compile time using `K_THREAD_DEFINE`. + +These three each control an LED. These LEDs, ``LED_BUILTIN``, ``D10`` and ``D11``, have +loop control and timing logic controlled by separate functions. + +- ``blink0()`` controls ``LED_BUILTIN`` and has a 100ms sleep cycle +- ``blink1()`` controls ``D11`` and has a 1000ms sleep cycle +- ``loop()`` controls ``D10`` and has a 300ms sleep cycle + +Requirements +************ + +The board must have two LEDs connected via GPIO pins and one builtin LED. These are called "User +LEDs" on many of Zephyr's :ref:`boards`. The LEDs must be mapped using the `` +``LED_BUILTIN``, ``D10`` and ``D11`` to the :ref:`devicetree ` aliases, in the +variants folder. + +You will see one of these errors if you try to build this sample for an +unsupported board: + +.. code-block:: none + + Unsupported board: LED_BUILTIN devicetree alias is not defined + Unsupported board: D11 devicetree alias is not defined + +Building +******** + +For example, to build this sample for :ref:`arduino_nano_33_ble`: + +.. zephyr-app-commands:: + :zephyr-app: samples/basic/arduino-threads + :board: arduino_nano_33_ble + :goals: build flash + :compact: + +Change ``arduino_nano_33_ble`` appropriately for other supported boards. diff --git a/samples/threads_arduino/prj.conf b/samples/threads_arduino/prj.conf new file mode 100644 index 000000000..d391af744 --- /dev/null +++ b/samples/threads_arduino/prj.conf @@ -0,0 +1,4 @@ +CONFIG_HEAP_MEM_POOL_SIZE=256 +CONFIG_GPIO=y +CONFIG_CPLUSPLUS=y +CONFIG_ARDUINO_API=y \ No newline at end of file diff --git a/samples/threads_arduino/src/main.cpp b/samples/threads_arduino/src/main.cpp new file mode 100644 index 000000000..7fa016500 --- /dev/null +++ b/samples/threads_arduino/src/main.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Dhruva Gole + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +/* size of stack area used by each thread */ +#define STACKSIZE 1024 + +/* scheduling priority used by each thread */ +#define PRIORITY 7 + +void blink0(void) +{ + while (1) { + digitalWrite(LED_BUILTIN, HIGH); + delay(100); + digitalWrite(LED_BUILTIN, LOW); + delay(100); + } +} + +void blink1(void) +{ + while (1) { + digitalWrite(D11, HIGH); + delay(1000); + digitalWrite(D11, LOW); + delay(1000); + } +} + +K_THREAD_DEFINE(blink0_id, STACKSIZE, blink0, NULL, NULL, NULL, PRIORITY, 0, 0); +K_THREAD_DEFINE(blink1_id, STACKSIZE, blink1, NULL, NULL, NULL, PRIORITY, 0, 0); +K_THREAD_DEFINE(blink2_id, STACKSIZE, loop, NULL, NULL, NULL, PRIORITY, 0, 0); + +void setup() +{ + pinMode(LED_BUILTIN, OUTPUT); + pinMode(D11, OUTPUT); + pinMode(D10, OUTPUT); +} +void loop() +{ + digitalWrite(D10, HIGH); + delay(300); + digitalWrite(D10, LOW); + delay(300); +}