Skip to content
Closed
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions examples/arduino-threads/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)
46 changes: 46 additions & 0 deletions examples/arduino-threads/README.rst
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +8 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be updated to explain all 3 LED (built-in, D10, D11) and the role of the Loop.


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 <dt-guide>` aliases, usually in the
:ref:`BOARD.dts file <devicetree-in-out-files>`.
Comment on lines +22 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still correct? It looks like the code is using pin numbers (which is great!) but this references device tree alias'


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.
4 changes: 4 additions & 0 deletions examples/arduino-threads/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
17 changes: 17 additions & 0 deletions examples/arduino-threads/sample.yaml
Original file line number Diff line number Diff line change
@@ -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=(.*)"
53 changes: 53 additions & 0 deletions examples/arduino-threads/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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()
{
while (1) {
digitalWrite(D10, HIGH);
delay(300);
digitalWrite(D10, LOW);
delay(300);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion zephyr/module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build:
cmake: .
kconfig: Kconfig
samples:
- examples
- samples