ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
nrf_bootloader_wdt.c
Go to the documentation of this file.
1 
40 #include "nrf_bootloader_wdt.h"
41 #include "nrf_wdt.h"
42 #include "nrf_bootloader_dfu_timers.h"
43 #include "nrf_log_ctrl.h"
44 
45 #define NRF_LOG_MODULE_NAME nrf_bootloader_wdt
46 #include "nrf_log.h"
48 
49 
50 static void wdt_feed(void)
51 {
52  if (nrf_wdt_started())
53  {
54  for (nrf_wdt_rr_register_t i = NRF_WDT_RR0; i < NRF_WDT_RR7; i++)
55  {
56  if (nrf_wdt_reload_request_is_enabled(i))
57  {
58  nrf_wdt_reload_request_set(i);
59  }
60  }
61  }
62 }
63 
64 
65 static void wdt_feed_timer_handler(void)
66 {
67  NRF_LOG_INFO("Internal feed");
68  wdt_feed();
69 }
70 
71 
72 void WDT_IRQHandler(void)
73 {
74  nrf_wdt_event_clear(NRF_WDT_EVENT_TIMEOUT);
75  NRF_LOG_FINAL_FLUSH();
76 }
77 
78 #define MAX_FLASH_OP_TIME_TICKS 3200 // ~100 ms
79 
81 {
82  static bool initialized = false;
83 
84  if (initialized)
85  {
86  return;
87  }
88 
89  if (nrf_wdt_started())
90  {
91  uint32_t wdt_ticks = nrf_wdt_reload_value_get();
92 
93  NRF_LOG_INFO("WDT enabled CRV:%d ticks", wdt_ticks);
94 
95  //wdt_ticks must be reduced to feed the watchdog before the timeout.
96  uint32_t reduced_timeout_ticks = MAX((int32_t)wdt_ticks - MAX_FLASH_OP_TIME_TICKS,
97  NRF_BOOTLOADER_MIN_TIMEOUT_TICKS);
98 
99  // Timer is 24-bit and asserts if larger value than 2`24-1 is given.
100  reduced_timeout_ticks = (reduced_timeout_ticks > (1<<20))? (1<<20) : reduced_timeout_ticks;
101  NRF_LOG_INFO("Starting a timer (%d ticks) for feeding watchdog.", reduced_timeout_ticks);
102  nrf_bootloader_wdt_feed_timer_start(reduced_timeout_ticks, wdt_feed_timer_handler);
103 
104  /* initial watchdog feed */
105  wdt_feed();
106 
107 
108  NVIC_EnableIRQ(WDT_IRQn);
109  }
110  else
111  {
112  NRF_LOG_INFO("WDT is not enabled");
113  }
114 
115  initialized = true;
116 }
117 
119 {
120  if (nrf_wdt_started())
121  {
122  wdt_feed();
123  }
124 }
void WDT_IRQHandler(void)
void nrf_bootloader_wdt_init(void)
void nrf_bootloader_wdt_feed(void)
#define MAX_FLASH_OP_TIME_TICKS
NRF_LOG_MODULE_REGISTER()