From 4feeb765c05d9e8010e37c7ea3de5927a2b5bf16 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Thu, 3 Dec 2026 22:14:36 +0100 Subject: [PATCH 1/2] Repeater, room server, sensor (nRF52): the main loop sleeps tickless instead of waking ~1000 times a second MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changelog-cs: Repeater, room server, sensor (nRF52): hlavní smyčka spí bez pravidelného tiku místo ~1000 probuzení za sekundu Changelog-topic: tickless Co-Authored-By: Claude Opus 5.5 --- src/helpers/NRF52Board.cpp | 31 ++++++++++++++++++++++- src/helpers/radiolib/RadioLibWrappers.cpp | 7 +++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/helpers/NRF52Board.cpp b/src/helpers/NRF52Board.cpp index 8457f21fd..69df228f2 100644 --- a/src/helpers/NRF52Board.cpp +++ b/src/helpers/NRF52Board.cpp @@ -344,6 +344,30 @@ extern uint64_t g_pkt_proc_us; extern uint32_t g_pkt_proc_count; #endif +// CUSTOM (TeTeHacko): tickless idle for the loop (tth #20). sd_app_evt_wait() +// inside loop_task never lets FreeRTOS reach its idle task, so the 1024 Hz +// RTC1 tick wakes the CPU ~1000x a second (measured: a 100 Hz tick alone saves +// 0.34 mA on XIAO at 3.3 V). Blocking the loop task on a task notification +// instead hands the CPU to the idle task, whose vPortSuppressTicksAndSleep() +// (configUSE_TICKLESS_IDLE=1, clears FPU/CRYPTOCELL pending itself) sleeps +// until the next real deadline. The DIO1 ISR (RadioLibWrappers setFlag) gives +// the notification, so a received packet is handled at once; everything else +// that only polls (USB/BLE console, timers) waits at most the timeout. +// Queued TX never waits: hasPendingWork() keeps the loop from sleeping at all. +// 0 = old behaviour (sd_app_evt_wait in the loop task). +#ifndef NRF52_TICKLESS_MAX_MS + #define NRF52_TICKLESS_MAX_MS 50 +#endif +static TaskHandle_t s_sleep_task = NULL; + +extern "C" void nrf52WakeFromISR(void) { + TaskHandle_t t = s_sleep_task; + if (t == NULL) return; + BaseType_t woken = pdFALSE; + vTaskNotifyGiveFromISR(t, &woken); + portYIELD_FROM_ISR(woken); +} + void NRF52Board::sleep(uint32_t secs) { // Clear FPU interrupt flags to avoid insomnia // see errata 87 for details https://docs.nordicsemi.com/bundle/errata_nRF52840_Rev3/page/ERR/nRF52840/Rev3/latest/anomaly_840_87.html @@ -371,7 +395,12 @@ void NRF52Board::sleep(uint32_t secs) { if (s_have_exit) s_awake_us += (uint32_t)(t_enter - s_last_exit_us); #endif - if (sd_enabled) { + if (sd_enabled && NRF52_TICKLESS_MAX_MS > 0 && xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) { + // A notification given before this point (ISR raced the loop) is counted, + // so the take returns at once instead of sleeping on a ready packet. + s_sleep_task = xTaskGetCurrentTaskHandle(); + ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(NRF52_TICKLESS_MAX_MS)); + } else if (sd_enabled) { // first call processes pending softdevice events, second call sleeps. sd_app_evt_wait(); sd_app_evt_wait(); diff --git a/src/helpers/radiolib/RadioLibWrappers.cpp b/src/helpers/radiolib/RadioLibWrappers.cpp index 1420e91cd..fad6e489a 100644 --- a/src/helpers/radiolib/RadioLibWrappers.cpp +++ b/src/helpers/radiolib/RadioLibWrappers.cpp @@ -23,6 +23,10 @@ static volatile uint32_t diag_isr_count = 0; // DIO1 edges that reached the MC static volatile uint32_t diag_isr_ms = 0; // millis() of the last one #endif +#if defined(NRF52_PLATFORM) +extern "C" void nrf52WakeFromISR(void); // NRF52Board.cpp: ends the loop's tickless wait +#endif + // this function is called when a complete packet // is transmitted by the module static @@ -32,6 +36,9 @@ static void setFlag(void) { // we sent a packet, set the flag state |= STATE_INT_READY; +#if defined(NRF52_PLATFORM) + nrf52WakeFromISR(); +#endif #ifdef RADIO_DIAG diag_isr_count++; diag_isr_ms = millis(); From 61f15eeddf2515ca977d0d8b08ba80ea03bb510a Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Thu, 3 Dec 2026 23:55:09 +0100 Subject: [PATCH 2/2] Docs: tickless loop measured at -0.35 mA on XIAO at 3.3 V (tth #20) Co-Authored-By: Claude Opus 5.5 --- docs/power.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/power.md b/docs/power.md index d2ef055de..6e0f9fa08 100644 --- a/docs/power.md +++ b/docs/power.md @@ -356,6 +356,16 @@ An idle TWIM costs nothing measurable, so `Wire.end()` on an empty bus is kept only because it is harmless. The tick is worth about 0.34 mA (5 % of this node): ten times fewer wake-ups. That is close to the ceiling of a tickless `loop()` (issue tth #20), which would remove the remaining ~100 wakes a second too. + +**Tickless loop, implemented (3. 12. 2026).** `NRF52Board::sleep()` now blocks +the loop task on a FreeRTOS task notification (at most 50 ms, +`NRF52_TICKLESS_MAX_MS`; 0 = the old `sd_app_evt_wait()`), so the idle task +runs and `configUSE_TICKLESS_IDLE` suppresses the tick; the DIO1 interrupt +gives the notification, so a received packet is handled at once, and a queued +TX keeps the loop awake (`hasPendingWork()`). Same rig, A/T/A/T/A, both images +from one tree: A 7.06 / 7.10 / 7.10 mA, T **6.73 / 6.74 mA**, i.e. **−0.35 mA**, +the whole ceiling of the 100 Hz test. Reception checked with x1 as the +sender (3 of 3 zero-hop adverts). Only nodes with `powersaving on` sleep at all. A first try of this image hung: it ended the bus while sensors were still listed, and the local framework (symlink, without the Wire timeout of PR #873) waits forever on a disabled TWIM. A main loop that hangs still answers buttonless