From 4feeb765c05d9e8010e37c7ea3de5927a2b5bf16 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Thu, 3 Dec 2026 22:14:36 +0100 Subject: [PATCH] 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();