From 87ec7b09aa2bff015fc82a69182cdef25a20b3b0 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 8 Mar 2021 18:12:21 +0800 Subject: [PATCH] add crude check for brownout --- src/mesh/generated/mesh.pb.h | 7 ++--- src/nrf52/main-nrf52.cpp | 52 +++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/mesh/generated/mesh.pb.h b/src/mesh/generated/mesh.pb.h index a8f36a140..b7df1079e 100644 --- a/src/mesh/generated/mesh.pb.h +++ b/src/mesh/generated/mesh.pb.h @@ -25,7 +25,8 @@ typedef enum _CriticalErrorCode { CriticalErrorCode_UBloxInitFailed = 5, CriticalErrorCode_NoAXP192 = 6, CriticalErrorCode_InvalidRadioSetting = 7, - CriticalErrorCode_TransmitFailed = 8 + CriticalErrorCode_TransmitFailed = 8, + CriticalErrorCode_Brownout = 9 } CriticalErrorCode; typedef enum _Routing_Error { @@ -177,8 +178,8 @@ typedef struct _ToRadio { #define _Constants_ARRAYSIZE ((Constants)(Constants_DATA_PAYLOAD_LEN+1)) #define _CriticalErrorCode_MIN CriticalErrorCode_None -#define _CriticalErrorCode_MAX CriticalErrorCode_TransmitFailed -#define _CriticalErrorCode_ARRAYSIZE ((CriticalErrorCode)(CriticalErrorCode_TransmitFailed+1)) +#define _CriticalErrorCode_MAX CriticalErrorCode_Brownout +#define _CriticalErrorCode_ARRAYSIZE ((CriticalErrorCode)(CriticalErrorCode_Brownout+1)) #define _Routing_Error_MIN Routing_Error_NONE #define _Routing_Error_MAX Routing_Error_TOO_LARGE diff --git a/src/nrf52/main-nrf52.cpp b/src/nrf52/main-nrf52.cpp index 8b7bb65b9..1ba0602b9 100644 --- a/src/nrf52/main-nrf52.cpp +++ b/src/nrf52/main-nrf52.cpp @@ -1,5 +1,6 @@ #include "NRF52Bluetooth.h" #include "configuration.h" +#include "error.h" #include "graphics/TFTDisplay.h" #include #include @@ -51,14 +52,14 @@ void getMacAddr(uint8_t *dmac) NRF52Bluetooth *nrf52Bluetooth; static bool bleOn = false; -static const bool enableBle = false; // Set to false for easier debugging +static const bool useSoftDevice = false; // Set to false for easier debugging void setBluetoothEnable(bool on) { if (on != bleOn) { if (on) { if (!nrf52Bluetooth) { - if (!enableBle) + if (!useSoftDevice) DEBUG_MSG("DISABLING NRF52 BLUETOOTH WHILE DEBUGGING\n"); else { nrf52Bluetooth = new NRF52Bluetooth(); @@ -87,6 +88,49 @@ int printf(const char *fmt, ...) #include "BQ25713.h" +void initBrownout() +{ + auto vccthresh = POWER_POFCON_THRESHOLD_V28; + auto vcchthresh = POWER_POFCON_THRESHOLDVDDH_V27; + + if (useSoftDevice) { + auto err_code = sd_power_pof_enable(POWER_POFCON_POF_Enabled); + assert(err_code == NRF_SUCCESS); + + err_code = sd_power_pof_threshold_set(vccthresh); + assert(err_code == NRF_SUCCESS); + } + else { + NRF_POWER->POFCON = POWER_POFCON_POF_Msk | (vccthresh << POWER_POFCON_THRESHOLD_Pos) | (vcchthresh << POWER_POFCON_THRESHOLDVDDH_Pos); + } +} + +void checkSDEvents() +{ + if (useSoftDevice) { + uint32_t evt; + while (NRF_ERROR_NOT_FOUND == sd_evt_get(&evt)) { + switch (evt) { + case NRF_EVT_POWER_FAILURE_WARNING: + recordCriticalError(CriticalErrorCode_Brownout); + break; + + default: + DEBUG_MSG("Unexpected SDevt %d\n", evt); + break; + } + } + } else { + if(NRF_POWER->EVENTS_POFWARN) + recordCriticalError(CriticalErrorCode_Brownout); + } +} + +void nrf52Loop() +{ + checkSDEvents(); +} + void nrf52Setup() { @@ -112,6 +156,8 @@ void nrf52Setup() // randomSeed(r); DEBUG_MSG("FIXME, call randomSeed\n"); // ::printf("TESTING PRINTF\n"); + + initBrownout(); } void cpuDeepSleep(uint64_t msecToWake) @@ -128,7 +174,7 @@ void cpuDeepSleep(uint64_t msecToWake) // https://devzone.nordicsemi.com/f/nordic-q-a/48919/ram-retention-settings-with-softdevice-enabled auto ok = sd_power_system_off(); - if(ok != NRF_SUCCESS) { + if (ok != NRF_SUCCESS) { DEBUG_MSG("FIXME: Ignoring soft device (EasyDMA pending?) and forcing system-off!\n"); NRF_POWER->SYSTEMOFF = 1; }