Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions samples/threads_arduino/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
47 changes: 47 additions & 0 deletions samples/threads_arduino/README.rst
Original file line number Diff line number Diff line change
@@ -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 `<board_name_pinmap.h>`
``LED_BUILTIN``, ``D10`` and ``D11`` to the :ref:`devicetree <dt-guide>` 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.
4 changes: 4 additions & 0 deletions samples/threads_arduino/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONFIG_HEAP_MEM_POOL_SIZE=256
CONFIG_GPIO=y
CONFIG_CPLUSPLUS=y
CONFIG_ARDUINO_API=y
51 changes: 51 additions & 0 deletions samples/threads_arduino/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022 Dhruva Gole
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <Arduino.h>

/* 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);
}