-
Notifications
You must be signed in to change notification settings - Fork 48
Add multi thread Example #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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=(.*)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,4 @@ build: | |
| cmake: . | ||
| kconfig: Kconfig | ||
| samples: | ||
| - examples | ||
| - samples | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.