Files
MeshCore/src/helpers/NRF52Board.h
T

136 lines
5.6 KiB
C++

#pragma once
#include <Arduino.h>
#include <MeshCore.h>
#include <helpers/KeyValueStore.h>
#include <helpers/RegulatorMode.h>
#if defined(NRF52_PLATFORM)
#ifdef NRF52_POWER_MANAGEMENT
// Shutdown Reason Codes (stored in GPREGRET before SYSTEMOFF)
#define SHUTDOWN_REASON_NONE 0x00
#define SHUTDOWN_REASON_LOW_VOLTAGE 0x4C // 'L' - Runtime low voltage threshold
#define SHUTDOWN_REASON_USER 0x55 // 'U' - User requested powerOff()
#define SHUTDOWN_REASON_BOOT_PROTECT 0x42 // 'B' - Boot voltage protection
// Boards provide this struct with their hardware-specific settings and callbacks.
struct PowerMgtConfig {
// LPCOMP wake configuration (for voltage recovery from SYSTEMOFF)
uint8_t lpcomp_ain_channel; // AIN0-7 for voltage sensing pin
uint8_t lpcomp_refsel; // REFSEL value: 0-6=1/8..7/8, 7=ARef, 8-15=1/16..15/16
// Boot protection voltage threshold (millivolts)
// Set to 0 to disable boot protection
uint16_t voltage_bootlock;
};
#endif
class NRF52Board : public mesh::MainBoard {
#ifdef NRF52_POWER_MANAGEMENT
void initPowerMgr();
void armVbusWake();
bool power_mgr_initialized;
#endif
protected:
uint8_t startup_reason;
char *ota_name;
bool cpu_dcdc_supported = false;
mesh::power::RegulatorMode cpu_regulator_mode = mesh::power::RegulatorMode::Ldo;
#ifdef NRF52_POWER_MANAGEMENT
void pwrmgtInit() { initPowerMgr(); }
uint32_t reset_reason; // RESETREAS register value
uint8_t shutdown_reason; // GPREGRET value (why we entered last SYSTEMOFF)
uint16_t boot_voltage_mv; // Battery voltage at boot (millivolts)
bool checkBootVoltage(const PowerMgtConfig* config);
void enterSystemOff(uint8_t reason);
void configureVoltageWake(uint8_t ain_channel, uint8_t refsel);
void pwrmgtWakeArmVbus() { armVbusWake(); }
virtual void initiateShutdown(uint8_t reason);
#endif
public:
NRF52Board(char *otaname) : ota_name(otaname)
#ifdef NRF52_POWER_MANAGEMENT
, power_mgr_initialized(false)
#endif
{}
virtual void begin();
void loop() override;
// Feed the hardware main-loop watchdog. The first call starts it, so normal
// setup (including filesystem mount and radio initialization) is not timed.
// A loop blocked forever in SoftDevice-backed flash I/O will stop feeding it
// and be reset automatically.
void feedWatchdog(bool enabled = true);
void serviceWatchdog() override;
virtual uint8_t getStartupReason() const override { return startup_reason; }
virtual float getMCUTemperature() override;
void reboot() override;
uint8_t mcuRegulatorModes() const override;
mesh::power::RegulatorMode defaultMcuRegulatorMode() const override;
bool applyMcuRegulator(mesh::power::RegulatorMode mode) override;
mesh::power::RegulatorMode activeMcuRegulatorMode() const override;
bool rebootToUf2Bootloader() override;
virtual void shutdownPeripherals();
virtual void powerOff() override;
virtual void enterDeepSleep(uint32_t secs) override;
virtual bool getBootloaderVersion(char* version, size_t max_len) override;
virtual bool startOTAUpdate(const char *id, char reply[], bool force_ap = false) override;
virtual bool stopOTAUpdate(char reply[]) override;
// Normal loop-entry preparation must precede all work servicing/checks. The
// profile-only second preparation below precedes a complete new guard pass;
// authoritative SDK queries and event clearing never belong in sleep().
void prepareSleepCycle();
// Radio-profile service can set an FPU event after loop entry. This second
// preparation belongs BEFORE the complete role sleep predicate, never after
// any work/USB/radio checks whose interrupt wake must remain visible.
void prepareProfileIdleCycle();
// Pure readiness check for the pinned 1024 Hz periodic RTC1 wake contract.
// The sketch task stays ready while waiting, so the idle task cannot suppress
// this tick. A profile must still reserve this interval plus its own guard.
static constexpr uint32_t ProfileIdleTickUs = 977;
static constexpr uint32_t ProfileIdleRtcTicks = 32;
static bool canIdleRadioProfile();
virtual void sleep(uint32_t secs) override;
bool isUserGpioAvailable(uint8_t pin) const override;
bool isExternalPowered() override;
bool isUsbDataConnected() override;
bool isUsbHostConnected() override;
#ifdef NRF52_POWER_MANAGEMENT
// Relative to each board's built-in ADC conversion; 1.000 is unchanged.
uint16_t calibrateBatteryMillivolts(uint16_t raw_mv) const;
#endif
void attachDynamicPrefs(KeyValueStore* prefs) { (void)prefs; } // no-op
#ifdef NRF52_POWER_MANAGEMENT
uint16_t getBootVoltage() override { return boot_voltage_mv; }
virtual uint32_t getResetReason() const override { return reset_reason; }
uint8_t getShutdownReason() const override { return shutdown_reason; }
const char* getResetReasonString(uint32_t reason) override;
const char* getShutdownReasonString(uint8_t reason) override;
bool isPowerManagementInitialized() const override { return power_mgr_initialized; }
bool supportsVoltageWake() const override { return true; }
bool isPwrMgtInitialised() const override { return isPowerManagementInitialized(); }
bool getWakeLpcompSupported() const override { return supportsVoltageWake(); }
#endif
};
/*
* The NRF52 has an internal DC/DC regulator that allows increased efficiency
* compared to the LDO regulator. For being able to use it, the module/board
* needs to have the required inductors and capacitors populated. If the
* hardware requirements are met, this subclass can be used to enable the DC/DC
* regulator.
*/
class NRF52BoardDCDC : virtual public NRF52Board {
public:
NRF52BoardDCDC() {}
virtual void begin() override;
};
#endif