nRF52: Move low-VDD System OFF logic to variant hook

This commit is contained in:
Ethan-chen1234-zy
2026-03-14 16:42:50 +08:00
parent 39292f53ca
commit 4f767e4126
2 changed files with 29 additions and 18 deletions
+5 -18
View File
@@ -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
@@ -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