ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_nrf5_sdk15_watchdog.c
Go to the documentation of this file.
1 
42 #if RUUVI_NRF5_SDK15_WATCHDOG_ENABLED
43 #include "ruuvi_driver_error.h"
44 #include "ruuvi_nrf5_sdk15_error.h"
45 #include "ruuvi_interface_log.h"
46 #include "ruuvi_interface_power.h"
47 #include "nrf_drv_clock.h"
48 #include "nrf_drv_wdt.h"
49 #include <stdbool.h>
50 
51 nrf_drv_wdt_channel_id m_channel_id;
52 wdt_evt_handler_t m_on_trigger;
53 
57 void wdt_event_handler (void)
58 {
59  //NOTE: The max amount of time we can spend in WDT interrupt is two cycles of 32768[Hz] clock - after that, reset occurs
60  if (NULL != m_on_trigger)
61  {
62  m_on_trigger();
63  }
64 
65  ri_log (RI_LOG_LEVEL_ERROR, "WDT Triggered, reset\r\n");
66 
67  while (1);
68 }
69 
82 rd_status_t ri_watchdog_init (const uint32_t interval_ms, const wdt_evt_handler_t handler)
83 {
84  uint32_t err_code = NRF_SUCCESS;
85  nrf_drv_wdt_config_t config = NRF_DRV_WDT_DEAFULT_CONFIG;
86  config.reload_value = interval_ms;
87  err_code = nrf_drv_wdt_init (&config, wdt_event_handler);
88 
89  if (NRF_SUCCESS == err_code)
90  {
91  err_code = nrf_drv_wdt_channel_alloc (&m_channel_id);
92 
93  if (NRF_SUCCESS == err_code)
94  {
95  nrf_drv_wdt_enable();
96 
97  // Halting the LFCLK while WDT is running hangs the program.
98  // Request LFCLK to make sure it's never stopped after this point.
99  // https://devzone.nordicsemi.com/f/nordic-q-a/45584/softdevice-hangs-forever-during-disable-while-stopping-lfclk
100  // Initialize clock if not already initialized
101  if (false == nrf_drv_clock_init_check())
102  {
103  err_code |= nrf_drv_clock_init();
104  }
105 
106  nrf_drv_clock_lfclk_request (NULL);
107  }
108  }
109 
110  m_on_trigger = handler;
111  return ruuvi_nrf5_sdk15_to_ruuvi_error (err_code);
112 }
113 
119 {
120  nrf_drv_wdt_channel_feed (m_channel_id);
121  return RD_SUCCESS;
122 }
123 
124 #endif
uint32_t rd_status_t
bitfield for representing errors
rd_status_t ruuvi_nrf5_sdk15_to_ruuvi_error(const ret_code_t error)
convert nrf5 sdk15 error code into Ruuvi error code.
#define RD_SUCCESS
Internal Error.
void ri_log(const ri_log_severity_t severity, const char *const message)
Queues messages into log.
@ RI_LOG_LEVEL_ERROR
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
rd_status_t ri_watchdog_init(const uint32_t interval_ms, const wdt_evt_handler_t handler)
void(* wdt_evt_handler_t)(void)
Enable implementation selected by application.
rd_status_t ri_watchdog_feed(void)