forked from meshcore-dev/MeshCore
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(nrf52): run app loop on dedicated FreeRTOS task to avoid stack overflow #40
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
disq
wants to merge
4
commits into
weebl2000:dev_plus
from
disq:fix/nrf52-mesh-worker-task-stack-devplus
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
034690e
fix(nrf52): run app loop on dedicated FreeRTOS task to avoid stack ov…
disq 09cc949
address review: move worker into .cpp, guard pointer, check task crea…
disq 8149af5
check startMeshWorker() result at call site, halt on failure
disq 795e1e4
harden startMeshWorker: guaranteed yield, reject null/double start
disq 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
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,38 @@ | ||
| #include "MeshWorkerTask.h" | ||
|
|
||
| #if defined(NRF52_PLATFORM) | ||
| #include <Arduino.h> | ||
| #include <MeshCore.h> // MESH_DEBUG_PRINTLN | ||
| #include <FreeRTOS.h> | ||
| #include <task.h> | ||
|
|
||
| static TaskHandle_t _meshTaskHandle = nullptr; | ||
| static void (*_meshLoopBody)() = nullptr; | ||
|
|
||
| static void mesh_worker_task(void*) { | ||
| for (;;) { | ||
| if (_meshLoopBody) _meshLoopBody(); | ||
| vTaskDelay(1); // yield at least one tick (tick-rate independent) | ||
| } | ||
| } | ||
|
disq marked this conversation as resolved.
|
||
|
|
||
| bool startMeshWorker(void (*loopBody)()) { | ||
| if (loopBody == nullptr) { | ||
| MESH_DEBUG_PRINTLN("startMeshWorker: null loopBody"); | ||
| return false; | ||
| } | ||
| if (_meshTaskHandle != nullptr) { | ||
| MESH_DEBUG_PRINTLN("startMeshWorker: already started"); | ||
| return false; | ||
| } | ||
| _meshLoopBody = loopBody; | ||
| // 2048 words = 8KB stack (measured peak ~4.7KB + headroom). | ||
| // TASK_PRIO_LOW (1) matches the framework's own loop_task priority. | ||
| if (xTaskCreate(mesh_worker_task, "mesh", 2048, NULL, TASK_PRIO_LOW, &_meshTaskHandle) != pdPASS) { | ||
|
disq marked this conversation as resolved.
|
||
| MESH_DEBUG_PRINTLN("startMeshWorker: xTaskCreate failed (out of FreeRTOS heap?)"); | ||
| _meshTaskHandle = nullptr; | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| #endif | ||
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,13 @@ | ||
| #pragma once | ||
|
|
||
| #if defined(NRF52_PLATFORM) | ||
| // Run an application loop body on a dedicated FreeRTOS task with an 8KB stack, | ||
| // instead of the Adafruit_nRF52_Arduino framework's Arduino loop_task (whose 4KB | ||
| // stack -- LOOP_STACK_SZ = 256*4, an unconditional #define that build flags can't | ||
| // override -- is too small for LittleFS file opens done from loop(), e.g. | ||
| // ClientACL::saveSessionKeys, which overflow it and corrupt adjacent heap). | ||
| // | ||
| // Definitions live in MeshWorkerTask.cpp so there is a single shared instance. | ||
| // Returns true if the task was created, false on FreeRTOS heap exhaustion. | ||
| bool startMeshWorker(void (*loopBody)()); | ||
| #endif |
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.
Uh oh!
There was an error while loading. Please reload this page.