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
22 changes: 21 additions & 1 deletion examples/simple_repeater/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

#include "MyMesh.h"

#if defined(NRF52_PLATFORM)
#include <helpers/MeshWorkerTask.h>
static void meshAppLoop(); // forward decl: setup() passes it to startMeshWorker()
#endif

#ifdef DISPLAY_CLASS
#include "UITask.h"
static UITask ui_task(display);
Expand Down Expand Up @@ -105,9 +110,16 @@ void setup() {
#endif

board.onBootComplete();

#if defined(NRF52_PLATFORM)
if (!startMeshWorker(meshAppLoop)) {
MESH_DEBUG_PRINTLN("Failed to start mesh worker task!");
halt();
}
#endif
}

void loop() {
static void meshAppLoop() {
board.loop();

int len = strlen(command);
Expand Down Expand Up @@ -172,3 +184,11 @@ void loop() {
#endif
}
}

void loop() {
#if defined(NRF52_PLATFORM)
vTaskDelay(pdMS_TO_TICKS(1000)); // app loop runs on the dedicated mesh worker task
#else
meshAppLoop();
#endif
}
22 changes: 21 additions & 1 deletion examples/simple_room_server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

#include "MyMesh.h"

#if defined(NRF52_PLATFORM)
#include <helpers/MeshWorkerTask.h>
static void meshAppLoop(); // forward decl: setup() passes it to startMeshWorker()
#endif

#ifdef DISPLAY_CLASS
#include "UITask.h"
static UITask ui_task(display);
Expand Down Expand Up @@ -82,9 +87,16 @@ void setup() {
#endif

board.onBootComplete();

#if defined(NRF52_PLATFORM)
if (!startMeshWorker(meshAppLoop)) {
MESH_DEBUG_PRINTLN("Failed to start mesh worker task!");
halt();
}
#endif
Comment thread
disq marked this conversation as resolved.
}

void loop() {
static void meshAppLoop() {
board.loop();

int len = strlen(command);
Expand Down Expand Up @@ -118,3 +130,11 @@ void loop() {
#endif
rtc_clock.tick();
}

void loop() {
#if defined(NRF52_PLATFORM)
vTaskDelay(pdMS_TO_TICKS(1000)); // app loop runs on the dedicated mesh worker task
#else
meshAppLoop();
#endif
}
Comment thread
disq marked this conversation as resolved.
38 changes: 38 additions & 0 deletions src/helpers/MeshWorkerTask.cpp
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)
}
}
Comment thread
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) {
Comment thread
disq marked this conversation as resolved.
MESH_DEBUG_PRINTLN("startMeshWorker: xTaskCreate failed (out of FreeRTOS heap?)");
_meshTaskHandle = nullptr;
return false;
}
return true;
}
#endif
13 changes: 13 additions & 0 deletions src/helpers/MeshWorkerTask.h
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