Fix rak_wismeshtag low‑voltage reboot hang after App configuration (#9897)

* Fix TAG low‑voltage reboot hang after App configuration

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

* Addressed review

* serialize SAADC access with shared mutex for VDD and battery reads

* raise LPCOMP wake threshold to ensure rising-edge wake

* Trunk fmt

---------

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
Ethac.chen
2026-03-27 06:56:19 -05:00
committed by GitHub
co-authored by GitHub Ben Meadors
parent 33e7f16c05
commit c36ae159ed
6 changed files with 129 additions and 6 deletions
+8
View File
@@ -35,6 +35,11 @@
#include "nrfx_power.h"
#endif
#if defined(ARCH_NRF52)
#include "Nrf52SaadcLock.h"
#include "concurrency/LockGuard.h"
#endif
#if defined(DEBUG_HEAP_MQTT) && !MESHTASTIC_EXCLUDE_MQTT
#include "mqtt/MQTT.h"
#include "target_specific.h"
@@ -328,6 +333,9 @@ class AnalogBatteryLevel : public HasBatteryLevel
scaled = esp_adc_cal_raw_to_voltage(raw, adc_characs);
scaled *= operativeAdcMultiplier;
#else // block for all other platforms
#ifdef ARCH_NRF52
concurrency::LockGuard saadcGuard(concurrency::nrf52SaadcLock);
#endif
for (uint32_t i = 0; i < BATTERY_SENSE_SAMPLES; i++) {
raw += analogRead(BATTERY_PIN);
}
+13
View File
@@ -0,0 +1,13 @@
#include "Nrf52SaadcLock.h"
#include "concurrency/Lock.h"
#include "configuration.h"
#ifdef ARCH_NRF52
namespace concurrency
{
static Lock nrf52SaadcLockInstance;
Lock *nrf52SaadcLock = &nrf52SaadcLockInstance;
} // namespace concurrency
#endif
+12
View File
@@ -0,0 +1,12 @@
#pragma once
#ifdef ARCH_NRF52
namespace concurrency
{
class Lock;
/** Shared mutex for SAADC configuration and reads (VDD + battery analog path). */
extern Lock *nrf52SaadcLock;
} // namespace concurrency
#endif
+21 -5
View File
@@ -25,6 +25,8 @@
#include "power.h"
#include <power/PowerHAL.h>
#include "Nrf52SaadcLock.h"
#include "concurrency/LockGuard.h"
#include <hal/nrf_lpcomp.h>
#ifdef BQ25703A_ADDR
@@ -51,6 +53,10 @@ uint16_t getVDDVoltage();
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;
@@ -74,11 +80,18 @@ bool powerHAL_isVBUSConnected()
bool powerHAL_isPowerLevelSafe()
{
static bool powerLevelSafe = true;
uint16_t threshold = SAFE_VDD_VOLTAGE_THRESHOLD * 1000; // convert V to mV
uint16_t hysteresis = SAFE_VDD_VOLTAGE_THRESHOLD_HYST * 1000;
#ifdef SAFE_VDD_VOLTAGE_THRESHOLD_MV
uint16_t threshold = SAFE_VDD_VOLTAGE_THRESHOLD_MV;
#else
uint16_t threshold = (uint16_t)(SAFE_VDD_VOLTAGE_THRESHOLD * 1000.0f + 0.5f); // convert V to mV
#endif
#ifdef SAFE_VDD_VOLTAGE_THRESHOLD_HYST_MV
uint16_t hysteresis = SAFE_VDD_VOLTAGE_THRESHOLD_HYST_MV;
#else
uint16_t hysteresis = (uint16_t)(SAFE_VDD_VOLTAGE_THRESHOLD_HYST * 1000.0f + 0.5f);
#endif
if (powerLevelSafe) {
if (getVDDVoltage() < threshold) {
@@ -125,11 +138,12 @@ void powerHAL_platformInit()
// get VDD voltage (in millivolts)
uint16_t getVDDVoltage()
{
// we use the same values as regular battery read so there is no conflict on SAADC
concurrency::LockGuard guard(concurrency::nrf52SaadcLock);
// Match battery read resolution; SAADC is shared with AnalogBatteryLevel in Power.cpp.
analogReadResolution(BATTERY_SENSE_RESOLUTION_BITS);
// VDD range on NRF52840 is 1.8-3.3V so we need to remap analog reference to 3.6V
// let's hope battery reading runs in same task and we don't have race condition
analogReference(AR_INTERNAL);
uint16_t vddADCRead = analogReadVDD();
@@ -326,6 +340,8 @@ void nrf52Loop()
checkSDEvents();
reportLittleFSCorruptionOnce();
variant_nrf52LoopHook(); // Optional variant hook called each nrf52Loop();
}
#ifdef USE_SEMIHOSTING