2021-10-13 16:08:35 -04:00
|
|
|
#include "drivers/Watchdog.h"
|
2020-11-14 23:04:22 -05:00
|
|
|
#include <mdk/nrf.h>
|
2020-02-23 15:09:11 -05:00
|
|
|
using namespace Pinetime::Drivers;
|
|
|
|
|
|
|
|
void Watchdog::Setup(uint8_t timeoutSeconds) {
|
|
|
|
NRF_WDT->CONFIG &= ~(WDT_CONFIG_SLEEP_Msk << WDT_CONFIG_SLEEP_Pos);
|
|
|
|
NRF_WDT->CONFIG |= (WDT_CONFIG_HALT_Run << WDT_CONFIG_SLEEP_Pos);
|
|
|
|
|
|
|
|
NRF_WDT->CONFIG &= ~(WDT_CONFIG_HALT_Msk << WDT_CONFIG_HALT_Pos);
|
|
|
|
NRF_WDT->CONFIG |= (WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos);
|
|
|
|
|
|
|
|
/* timeout (s) = (CRV + 1) / 32768 */
|
|
|
|
// JF : 7500 = 7.5s
|
2021-04-18 13:28:14 -04:00
|
|
|
uint32_t crv = (((timeoutSeconds * 1000u) << 15u) / 1000) - 1;
|
2020-02-23 15:09:11 -05:00
|
|
|
NRF_WDT->CRV = crv;
|
|
|
|
|
|
|
|
/* Enable reload requests */
|
|
|
|
NRF_WDT->RREN = (WDT_RREN_RR0_Enabled << WDT_RREN_RR0_Pos);
|
2020-03-22 07:03:17 -04:00
|
|
|
|
|
|
|
resetReason = ActualResetReason();
|
2020-02-23 15:09:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Watchdog::Start() {
|
|
|
|
NRF_WDT->TASKS_START = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Watchdog::Kick() {
|
|
|
|
NRF_WDT->RR[0] = WDT_RR_RR_Reload;
|
|
|
|
}
|
|
|
|
|
2020-03-22 07:03:17 -04:00
|
|
|
Watchdog::ResetReasons Watchdog::ActualResetReason() const {
|
2020-06-07 14:31:13 -04:00
|
|
|
uint32_t reason = NRF_POWER->RESETREAS;
|
|
|
|
NRF_POWER->RESETREAS = 0xffffffff;
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
if (reason & 0x01u)
|
|
|
|
return ResetReasons::ResetPin;
|
|
|
|
if ((reason >> 1u) & 0x01u)
|
|
|
|
return ResetReasons::Watchdog;
|
|
|
|
if ((reason >> 2u) & 0x01u)
|
|
|
|
return ResetReasons::SoftReset;
|
|
|
|
if ((reason >> 3u) & 0x01u)
|
|
|
|
return ResetReasons::CpuLockup;
|
|
|
|
if ((reason >> 16u) & 0x01u)
|
|
|
|
return ResetReasons::SystemOff;
|
|
|
|
if ((reason >> 17u) & 0x01u)
|
|
|
|
return ResetReasons::LpComp;
|
|
|
|
if ((reason) &0x01u)
|
|
|
|
return ResetReasons::DebugInterface;
|
|
|
|
if ((reason >> 19u) & 0x01u)
|
|
|
|
return ResetReasons::NFC;
|
2020-02-23 15:09:11 -05:00
|
|
|
return ResetReasons::HardReset;
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
const char* Watchdog::ResetReasonToString(Watchdog::ResetReasons reason) {
|
|
|
|
switch (reason) {
|
|
|
|
case ResetReasons::ResetPin:
|
|
|
|
return "Reset pin";
|
|
|
|
case ResetReasons::Watchdog:
|
|
|
|
return "Watchdog";
|
|
|
|
case ResetReasons::DebugInterface:
|
|
|
|
return "Debug interface";
|
|
|
|
case ResetReasons::LpComp:
|
|
|
|
return "LPCOMP";
|
|
|
|
case ResetReasons::SystemOff:
|
|
|
|
return "System OFF";
|
|
|
|
case ResetReasons::CpuLockup:
|
|
|
|
return "CPU Lock-up";
|
|
|
|
case ResetReasons::SoftReset:
|
|
|
|
return "Soft reset";
|
|
|
|
case ResetReasons::NFC:
|
|
|
|
return "NFC";
|
|
|
|
case ResetReasons::HardReset:
|
|
|
|
return "Hard reset";
|
|
|
|
default:
|
|
|
|
return "Unknown";
|
2020-02-23 15:09:11 -05:00
|
|
|
}
|
|
|
|
}
|