InfiniTime.git

ref: 0195ece317d15bcba7dfbd74b58bb8dbcd3bca0a

src/drivers/Watchdog.cpp


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <mdk/nrf52.h>
#include <mdk/nrf52_bitfields.h>
#include <nrf_soc.h>
#include "Watchdog.h"
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
  uint32_t crv = (((timeoutSeconds*1000u) << 15u) / 1000) - 1;
  NRF_WDT->CRV = crv;

  /* Enable reload requests */
  NRF_WDT->RREN = (WDT_RREN_RR0_Enabled << WDT_RREN_RR0_Pos);

  resetReason = ActualResetReason();
}

void Watchdog::Start() {
  NRF_WDT->TASKS_START = 1;
}

void Watchdog::Kick() {
  NRF_WDT->RR[0] = WDT_RR_RR_Reload;
}

Watchdog::ResetReasons Watchdog::ActualResetReason() const {
  uint32_t resetReason;
//  sd_power_reset_reason_get(&resetReason);
//  sd_power_reset_reason_clr(0xFFFFFFFF);
//  if(resetReason & 0x01u) return ResetReasons::ResetPin;
//  if((resetReason >> 1u) & 0x01u) return ResetReasons::Watchdog;
//  if((resetReason >> 2u) & 0x01u) return ResetReasons::SoftReset;
//  if((resetReason >> 3u) & 0x01u) return ResetReasons::CpuLockup;
//  if((resetReason >> 16u) & 0x01u) return ResetReasons::SystemOff;
//  if((resetReason >> 17u) & 0x01u) return ResetReasons::LpComp;
//  if((resetReason >> 18u) & 0x01u) return ResetReasons::DebugInterface;
//  if((resetReason >> 19u) & 0x01u) return ResetReasons::NFC;
  return ResetReasons::HardReset;
}

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";
  }
}