From 4f767e412687d3400eeebabee74691ec31a997b8 Mon Sep 17 00:00:00 2001 From: Ethan-chen1234-zy Date: Sat, 14 Mar 2026 16:42:50 +0800 Subject: [PATCH] nRF52: Move low-VDD System OFF logic to variant hook --- src/platform/nrf52/main-nrf52.cpp | 23 ++++--------------- variants/nrf52840/rak_wismeshtag/variant.cpp | 24 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/platform/nrf52/main-nrf52.cpp b/src/platform/nrf52/main-nrf52.cpp index af65446b4..9b26721c0 100644 --- a/src/platform/nrf52/main-nrf52.cpp +++ b/src/platform/nrf52/main-nrf52.cpp @@ -45,13 +45,16 @@ #endif uint16_t getVDDVoltage(); -void cpuDeepSleep(uint32_t msecToWake); // Weak empty variant shutdown prep function. // May be redefined by variant files. void variant_shutdown() __attribute__((weak)); void variant_shutdown() {} +// Optional variant hook called each nrf52Loop(); e.g. for low-VDD System OFF. +void variant_nrf52LoopHook(void) __attribute__((weak)); +void variant_nrf52LoopHook(void) {} + static nrfx_wdt_t nrfx_wdt = NRFX_WDT_INSTANCE(0); static nrfx_wdt_channel_id nrfx_wdt_channel_id_nrf52_main; @@ -328,23 +331,7 @@ void nrf52Loop() checkSDEvents(); reportLittleFSCorruptionOnce(); -#ifdef LOW_VDD_SYSTEMOFF_DELAY_MS // rak_wismeshtag does not have a USB VBUS connected - // If VDD stays unsafe for a while (typically during brownout), - // force System OFF to avoid flash writes / SoftDevice instability loops. - // Skip when VBUS is present to allow recovery while USB-powered. - if (!powerHAL_isVBUSConnected()) { - static uint32_t low_vdd_since_ms = 0; - if (!powerHAL_isPowerLevelSafe()) { - if (low_vdd_since_ms == 0) - low_vdd_since_ms = millis(); - if ((uint32_t)(millis() - low_vdd_since_ms) >= (uint32_t)LOW_VDD_SYSTEMOFF_DELAY_MS) { - cpuDeepSleep(portMAX_DELAY); - } - } else { - low_vdd_since_ms = 0; - } - } -#endif + variant_nrf52LoopHook(); // Optional variant hook called each nrf52Loop(); } #ifdef USE_SEMIHOSTING diff --git a/variants/nrf52840/rak_wismeshtag/variant.cpp b/variants/nrf52840/rak_wismeshtag/variant.cpp index a035fbaf0..c2a899ed5 100644 --- a/variants/nrf52840/rak_wismeshtag/variant.cpp +++ b/variants/nrf52840/rak_wismeshtag/variant.cpp @@ -22,6 +22,10 @@ #include "nrf.h" #include "wiring_constants.h" #include "wiring_digital.h" +#include "power/PowerHAL.h" +#include "sleep.h" +#include "FreeRTOS.h" +#include "Arduino.h" const uint32_t g_ADigitalPinMap[] = { // P0 @@ -40,3 +44,23 @@ void initVariant() pinMode(PIN_3V3_EN, OUTPUT); digitalWrite(PIN_3V3_EN, HIGH); } + +#ifdef LOW_VDD_SYSTEMOFF_DELAY_MS +void variant_nrf52LoopHook(void) +{ + // If VDD stays unsafe for a while (brownout), force System OFF. + // Skip when VBUS present to allow recovery while USB-powered. + if (!powerHAL_isVBUSConnected()) { + static uint32_t low_vdd_since_ms = 0; + if (!powerHAL_isPowerLevelSafe()) { + if (low_vdd_since_ms == 0) + low_vdd_since_ms = millis(); + if ((uint32_t)(millis() - low_vdd_since_ms) >= (uint32_t)LOW_VDD_SYSTEMOFF_DELAY_MS) { + cpuDeepSleep(portMAX_DELAY); + } + } else { + low_vdd_since_ms = 0; + } + } +} +#endif