ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
nrfx_wdt.c
Go to the documentation of this file.
1 
41 #include <nrfx.h>
42 
43 #if NRFX_CHECK(NRFX_WDT_ENABLED)
44 #include <nrfx_wdt.h>
45 
46 #define NRFX_LOG_MODULE WDT
47 #include <nrfx_log.h>
48 
50 static nrfx_drv_state_t m_state;
51 
53 static uint8_t m_alloc_index;
54 
55 #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ)
57 static nrfx_wdt_event_handler_t m_wdt_event_handler;
58 
60 void nrfx_wdt_irq_handler(void)
61 {
62  if (nrf_wdt_event_check(NRF_WDT_EVENT_TIMEOUT))
63  {
64  m_wdt_event_handler();
65  nrf_wdt_event_clear(NRF_WDT_EVENT_TIMEOUT);
66  }
67 }
68 #endif
69 
70 
71 nrfx_err_t nrfx_wdt_init(nrfx_wdt_config_t const * p_config,
72  nrfx_wdt_event_handler_t wdt_event_handler)
73 {
74  NRFX_ASSERT(p_config);
75  nrfx_err_t err_code;
76 
77 #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ)
78  NRFX_ASSERT(wdt_event_handler != NULL);
79  m_wdt_event_handler = wdt_event_handler;
80 #else
81  NRFX_ASSERT(wdt_event_handler == NULL);
82  (void)wdt_event_handler;
83 #endif
84  if (m_state == NRFX_DRV_STATE_UNINITIALIZED)
85  {
86  m_state = NRFX_DRV_STATE_INITIALIZED;
87  }
88  else
89  {
90  err_code = NRFX_ERROR_INVALID_STATE;
91  NRFX_LOG_WARNING("Function: %s, error code: %s.",
92  __func__,
93  NRFX_LOG_ERROR_STRING_GET(err_code));
94  return err_code;
95  }
96  if((((uint64_t) p_config->reload_value * 32768) / 1000) >
97  UINT32_MAX) // Check for overflow
98  {
99  return NRF_ERROR_INVALID_PARAM;
100  }
101  nrf_wdt_behaviour_set(p_config->behaviour);
102 
103  nrf_wdt_reload_value_set(((uint64_t) p_config->reload_value * 32768) / 1000);
104 
105 #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ)
106  NRFX_IRQ_PRIORITY_SET(WDT_IRQn, p_config->interrupt_priority);
107  NRFX_IRQ_ENABLE(WDT_IRQn);
108 #endif
109 
110  err_code = NRFX_SUCCESS;
111  NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
112  return err_code;
113 }
114 
115 
116 void nrfx_wdt_enable(void)
117 {
118  NRFX_ASSERT(m_alloc_index != 0);
119  NRFX_ASSERT(m_state == NRFX_DRV_STATE_INITIALIZED);
120 #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ)
121  nrf_wdt_int_enable(NRF_WDT_INT_TIMEOUT_MASK);
122 #endif
123  nrf_wdt_task_trigger(NRF_WDT_TASK_START);
124  m_state = NRFX_DRV_STATE_POWERED_ON;
125  NRFX_LOG_INFO("Enabled.");
126 }
127 
128 
129 void nrfx_wdt_feed(void)
130 {
131  NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
132  for (uint8_t i = 0; i < m_alloc_index; i++)
133  {
134  nrf_wdt_reload_request_set((nrf_wdt_rr_register_t)(NRF_WDT_RR0 + i));
135  }
136 }
137 
138 nrfx_err_t nrfx_wdt_channel_alloc(nrfx_wdt_channel_id * p_channel_id)
139 {
140  nrfx_err_t result;
141  NRFX_ASSERT(p_channel_id);
142  NRFX_ASSERT(m_state == NRFX_DRV_STATE_INITIALIZED);
143 
144  NRFX_CRITICAL_SECTION_ENTER();
145  if (m_alloc_index < NRF_WDT_CHANNEL_NUMBER)
146  {
147  *p_channel_id = (nrfx_wdt_channel_id)(NRF_WDT_RR0 + m_alloc_index);
148  m_alloc_index++;
149  nrf_wdt_reload_request_enable(*p_channel_id);
150  result = NRFX_SUCCESS;
151  }
152  else
153  {
154  result = NRFX_ERROR_NO_MEM;
155  }
156  NRFX_CRITICAL_SECTION_EXIT();
157  NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(result));
158  return result;
159 }
160 
161 void nrfx_wdt_channel_feed(nrfx_wdt_channel_id channel_id)
162 {
163  NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
164  nrf_wdt_reload_request_set(channel_id);
165 }
166 
167 #endif // NRFX_CHECK(NRFX_WDT_ENABLED)