From a614a999af3787b2cbb533b509385cffa4af6b30 Mon Sep 17 00:00:00 2001 From: Dhruva Gole Date: Sat, 9 Jul 2022 23:52:02 +0530 Subject: [PATCH 1/4] 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 --- examples/arduino-threads/CMakeLists.txt | 7 ++++ examples/arduino-threads/README.rst | 46 +++++++++++++++++++++ examples/arduino-threads/prj.conf | 4 ++ examples/arduino-threads/sample.yaml | 17 ++++++++ examples/arduino-threads/src/main.cpp | 53 +++++++++++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 examples/arduino-threads/CMakeLists.txt create mode 100644 examples/arduino-threads/README.rst create mode 100644 examples/arduino-threads/prj.conf create mode 100644 examples/arduino-threads/sample.yaml create mode 100644 examples/arduino-threads/src/main.cpp diff --git a/examples/arduino-threads/CMakeLists.txt b/examples/arduino-threads/CMakeLists.txt new file mode 100644 index 000000000..c0ccc28d0 --- /dev/null +++ b/examples/arduino-threads/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/examples/arduino-threads/README.rst b/examples/arduino-threads/README.rst new file mode 100644 index 000000000..43fa0b340 --- /dev/null +++ b/examples/arduino-threads/README.rst @@ -0,0 +1,46 @@ +.. _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 two threads. Each thread is then defined +at compile time using K_THREAD_DEFINE. + +These two each control an LED. These LEDs, ``led0`` and ``led1``, have +loop control and timing logic controlled by separate functions. + +- ``blink0()`` controls ``led0`` and has a 100ms sleep cycle +- ``blink1()`` controls ``led1`` and has a 1000ms sleep cycle + +Requirements +************ + +The board must have two LEDs connected via GPIO pins. These are called "User +LEDs" on many of Zephyr's :ref:`boards`. The LEDs must be configured using the +``led0`` and ``led1`` :ref:`devicetree ` aliases, usually in the +:ref:`BOARD.dts file `. + +You will see one of these errors if you try to build this sample for an +unsupported board: + +.. code-block:: none + + Unsupported board: led0 devicetree alias is not defined + Unsupported board: led1 devicetree alias is not defined + +Building +******** + +For example, to build this sample for :ref:`96b_carbon_board`: + +.. 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/examples/arduino-threads/prj.conf b/examples/arduino-threads/prj.conf new file mode 100644 index 000000000..d391af744 --- /dev/null +++ b/examples/arduino-threads/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/examples/arduino-threads/sample.yaml b/examples/arduino-threads/sample.yaml new file mode 100644 index 000000000..e6605a0fb --- /dev/null +++ b/examples/arduino-threads/sample.yaml @@ -0,0 +1,17 @@ +sample: + description: A basic demo to showcase multi-threading + using K_THREAD_DEFINE + name: Basic Thread Demo +tests: + sample.basic.threads: + tags: kernel threads gpio + filter: dt_enabled_alias_with_parent_compat("led0", "gpio-leds") and + dt_enabled_alias_with_parent_compat("led1", "gpio-leds") + depends_on: gpio + harness: console + harness_config: + type: multi_line + ordered: false + regex: + - "Toggled led0; counter=(.*)" + - "Toggled led1; counter=(.*)" diff --git a/examples/arduino-threads/src/main.cpp b/examples/arduino-threads/src/main.cpp new file mode 100644 index 000000000..ba178917d --- /dev/null +++ b/examples/arduino-threads/src/main.cpp @@ -0,0 +1,53 @@ +/* + * 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() +{ + while (1) { + digitalWrite(D10, HIGH); + delay(300); + digitalWrite(D10, LOW); + delay(300); + } +} \ No newline at end of file From 1d69eed8c06ab43c7f77a38b1ac499e73df2c8f3 Mon Sep 17 00:00:00 2001 From: Jonathan Beri Date: Sat, 9 Jul 2022 18:44:21 -0700 Subject: [PATCH 2/4] Rename examples to samples to be consistent --- {examples => samples}/blinky_arduino/CMakeLists.txt | 0 {examples => samples}/blinky_arduino/README.rst | 0 {examples => samples}/blinky_arduino/prj.conf | 0 {examples => samples}/blinky_arduino/sample.yaml | 0 {examples => samples}/blinky_arduino/src/main.cpp | 0 {examples => samples}/button_press_led/CMakeLists.txt | 0 {examples => samples}/button_press_led/README.rst | 0 {examples => samples}/button_press_led/prj.conf | 0 {examples => samples}/button_press_led/sample.yaml | 0 {examples => samples}/button_press_led/src/main.cpp | 0 {examples => samples}/hello_arduino/CMakeLists.txt | 0 {examples => samples}/hello_arduino/README.rst | 0 {examples => samples}/hello_arduino/prj.conf | 0 {examples => samples}/hello_arduino/sample.yaml | 0 {examples => samples}/hello_arduino/src/app.cpp | 0 15 files changed, 0 insertions(+), 0 deletions(-) rename {examples => samples}/blinky_arduino/CMakeLists.txt (100%) rename {examples => samples}/blinky_arduino/README.rst (100%) rename {examples => samples}/blinky_arduino/prj.conf (100%) rename {examples => samples}/blinky_arduino/sample.yaml (100%) rename {examples => samples}/blinky_arduino/src/main.cpp (100%) rename {examples => samples}/button_press_led/CMakeLists.txt (100%) rename {examples => samples}/button_press_led/README.rst (100%) rename {examples => samples}/button_press_led/prj.conf (100%) rename {examples => samples}/button_press_led/sample.yaml (100%) rename {examples => samples}/button_press_led/src/main.cpp (100%) rename {examples => samples}/hello_arduino/CMakeLists.txt (100%) rename {examples => samples}/hello_arduino/README.rst (100%) rename {examples => samples}/hello_arduino/prj.conf (100%) rename {examples => samples}/hello_arduino/sample.yaml (100%) rename {examples => samples}/hello_arduino/src/app.cpp (100%) diff --git a/examples/blinky_arduino/CMakeLists.txt b/samples/blinky_arduino/CMakeLists.txt similarity index 100% rename from examples/blinky_arduino/CMakeLists.txt rename to samples/blinky_arduino/CMakeLists.txt diff --git a/examples/blinky_arduino/README.rst b/samples/blinky_arduino/README.rst similarity index 100% rename from examples/blinky_arduino/README.rst rename to samples/blinky_arduino/README.rst diff --git a/examples/blinky_arduino/prj.conf b/samples/blinky_arduino/prj.conf similarity index 100% rename from examples/blinky_arduino/prj.conf rename to samples/blinky_arduino/prj.conf diff --git a/examples/blinky_arduino/sample.yaml b/samples/blinky_arduino/sample.yaml similarity index 100% rename from examples/blinky_arduino/sample.yaml rename to samples/blinky_arduino/sample.yaml diff --git a/examples/blinky_arduino/src/main.cpp b/samples/blinky_arduino/src/main.cpp similarity index 100% rename from examples/blinky_arduino/src/main.cpp rename to samples/blinky_arduino/src/main.cpp diff --git a/examples/button_press_led/CMakeLists.txt b/samples/button_press_led/CMakeLists.txt similarity index 100% rename from examples/button_press_led/CMakeLists.txt rename to samples/button_press_led/CMakeLists.txt diff --git a/examples/button_press_led/README.rst b/samples/button_press_led/README.rst similarity index 100% rename from examples/button_press_led/README.rst rename to samples/button_press_led/README.rst diff --git a/examples/button_press_led/prj.conf b/samples/button_press_led/prj.conf similarity index 100% rename from examples/button_press_led/prj.conf rename to samples/button_press_led/prj.conf diff --git a/examples/button_press_led/sample.yaml b/samples/button_press_led/sample.yaml similarity index 100% rename from examples/button_press_led/sample.yaml rename to samples/button_press_led/sample.yaml diff --git a/examples/button_press_led/src/main.cpp b/samples/button_press_led/src/main.cpp similarity index 100% rename from examples/button_press_led/src/main.cpp rename to samples/button_press_led/src/main.cpp diff --git a/examples/hello_arduino/CMakeLists.txt b/samples/hello_arduino/CMakeLists.txt similarity index 100% rename from examples/hello_arduino/CMakeLists.txt rename to samples/hello_arduino/CMakeLists.txt diff --git a/examples/hello_arduino/README.rst b/samples/hello_arduino/README.rst similarity index 100% rename from examples/hello_arduino/README.rst rename to samples/hello_arduino/README.rst diff --git a/examples/hello_arduino/prj.conf b/samples/hello_arduino/prj.conf similarity index 100% rename from examples/hello_arduino/prj.conf rename to samples/hello_arduino/prj.conf diff --git a/examples/hello_arduino/sample.yaml b/samples/hello_arduino/sample.yaml similarity index 100% rename from examples/hello_arduino/sample.yaml rename to samples/hello_arduino/sample.yaml diff --git a/examples/hello_arduino/src/app.cpp b/samples/hello_arduino/src/app.cpp similarity index 100% rename from examples/hello_arduino/src/app.cpp rename to samples/hello_arduino/src/app.cpp From 9edcd5f12727f8143ddd0271e14384b320132274 Mon Sep 17 00:00:00 2001 From: Jonathan Beri Date: Sat, 9 Jul 2022 20:15:17 -0700 Subject: [PATCH 3/4] Update maintainers links --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4cf6e3f69..9726c1060 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,9 @@ While compiling with the ArduinoCore-API `WCharacter.h` produces many errors. It **Maintainers**: -- beriberikix -- szczys -- alvarowolfx +- [beriberikix](https://github.com/beriberikix) +- [szczys](https://github.com/szczys) +- [alvarowolfx](https://github.com/alvarowolfx) **Contributor**: [DhruvaG2000](https://github.com/DhruvaG2000) From 0bbe956dffdf5d4b740e29f57cf911d35e1bbc1d Mon Sep 17 00:00:00 2001 From: Jonathan Beri Date: Sat, 9 Jul 2022 19:37:09 -0700 Subject: [PATCH 4/4] Update to use samples directory --- zephyr/module.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zephyr/module.yml b/zephyr/module.yml index 6e41ddfbc..9b9b1ca8c 100644 --- a/zephyr/module.yml +++ b/zephyr/module.yml @@ -4,4 +4,4 @@ build: cmake: . kconfig: Kconfig samples: - - examples + - samples