1 /*
2  * Copyright (c) 2014 - 2025, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef NRFX_WDT_H__
35 #define NRFX_WDT_H__
36 
37 #include <nrfx.h>
38 #include <haly/nrfy_wdt.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* On devices with single instance (with no id) use instance 0. */
45 #if defined(NRF_WDT) && defined(NRFX_WDT_ENABLED) && !defined(NRFX_WDT0_ENABLED)
46 #define NRFX_WDT0_ENABLED 1
47 #endif
48 
49 /**
50  * @defgroup nrfx_wdt WDT driver
51  * @{
52  * @ingroup nrf_wdt
53  * @brief   Watchdog Timer (WDT) peripheral driver.
54  */
55 
56 #if NRF_WDT_HAS_STOP || defined(__NRFX_DOXYGEN__)
57 /** @brief Symbol indicating whether watchdog stopping is supported. */
58 #define NRFX_WDT_HAS_STOP 1
59 #else
60 #define NRFX_WDT_HAS_STOP 0
61 #endif
62 
63 #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ) || defined(__NRFX_DOXYGEN__)
64 /** @brief WDT instance interrupt priority configuration. */
65     #define NRFX_WDT_IRQ_CONFIG .interrupt_priority = NRFX_WDT_DEFAULT_CONFIG_IRQ_PRIORITY
66 #else
67     #define NRFX_WDT_IRQ_CONFIG
68 #endif
69 
70 #if NRFX_API_VER_AT_LEAST(3, 2, 0) || defined(__NRFX_DOXYGEN__)
71 /**
72  * @brief WDT event handler function type.
73  *
74  * @param[in] event_type WDT event.
75  * @param[in] requests   Value of the request status register. Bits that have been set can be
76  *                       used to determine which RR (Reload Request) register was the reason
77  *                       for timeout event.
78  *                       Valid only when @ref NRF_WDT_EVENT_TIMEOUT is passed in @p event_type.
79  * @param[in] p_context  User context.
80  */
81 typedef void (*nrfx_wdt_event_handler_t)(nrf_wdt_event_t event_type,
82                                          uint32_t        requests,
83                                          void *          p_context);
84 #else
85 /**
86  * @brief WDT event handler function type.
87  *
88  * @deprecated Use new variant instead.
89  *
90  * @param[in] requests Value of the request status register. Bits that have been set can be
91  *                     used to determine which RR (Reload Request) register was the reason
92  *                     for timeout event.
93  */
94 typedef void (*nrfx_wdt_event_handler_t)(uint32_t requests);
95 #endif
96 
97 /** @brief WDT channel ID type. */
98 typedef nrf_wdt_rr_register_t nrfx_wdt_channel_id;
99 
100 /** @brief Data structure of the Watchdog (WDT) driver instance. */
101 typedef struct
102 {
103     NRF_WDT_Type * p_reg;        ///< Pointer to a structure with WDT registers.
104     uint8_t        drv_inst_idx; ///< Index of the driver instance. For internal use only.
105 } nrfx_wdt_t;
106 
107 #ifndef __NRFX_DOXYGEN__
108 enum {
109     /* List all enabled driver instances (in the format NRFX_\<instance_name\>_INST_IDX). */
110     NRFX_INSTANCE_ENUM_LIST(WDT)
111     NRFX_WDT_ENABLED_COUNT
112 };
113 #endif
114 
115 /** @brief Macro for creating an instance of the WDT driver. */
116 #define NRFX_WDT_INSTANCE(id)                               \
117 {                                                           \
118     .p_reg        = NRFX_CONCAT(NRF_, WDT, id),             \
119     .drv_inst_idx = NRFX_CONCAT(NRFX_WDT, id, _INST_IDX),   \
120 }
121 
122 /** @brief Structure for WDT initialization. */
123 typedef struct
124 {
125     uint32_t behaviour;          ///< WDT behavior flags bitmask, constructed from @ref nrf_wdt_behaviour_mask_t.
126     uint32_t reload_value;       ///< WDT reload value in milliseconds.
127 #if !NRFX_CHECK(NRFX_WDT_CONFIG_NO_IRQ) || defined(__NRFX_DOXYGEN__)
128     uint8_t  interrupt_priority; ///< WDT interrupt priority.
129 #endif
130 } nrfx_wdt_config_t;
131 
132 /**
133  * @brief WDT driver default configuration.
134  *
135  * This configuration sets up WDT with the following options:
136  * - run when CPU is in SLEEP mode, pause when in HALT mode
137  * - reload value: 2000 ms
138  */
139 #define NRFX_WDT_DEFAULT_CONFIG                             \
140 {                                                           \
141     .behaviour          = NRF_WDT_BEHAVIOUR_RUN_SLEEP_MASK, \
142     .reload_value       = 2000,                             \
143     NRFX_WDT_IRQ_CONFIG                                     \
144 }
145 
146 #if NRFX_API_VER_AT_LEAST(3, 2, 0) || defined(__NRFX_DOXYGEN__)
147 /**
148  * @brief Function for initializing the WDT driver instance.
149  *
150  * @param[in] p_instance        Pointer to the driver instance structure.
151  * @param[in] p_config          Pointer to the structure with the initial configuration.
152  *                              NULL if configuration is to be skipped and will be done later
153  *                              using @ref nrfx_wdt_reconfigure.
154  * @param[in] wdt_event_handler Event handler provided by the user. Ignored when
155  *                              @ref NRFX_WDT_CONFIG_NO_IRQ option is enabled.
156  * @param[in] p_context         User context passed in event handler. Ignored when
157  *                              @ref NRFX_WDT_CONFIG_NO_IRQ option is enabled.
158  *
159  * @retval NRFX_SUCCESS             Initialization was successful.
160  * @retval NRFX_ERROR_ALREADY       The driver is already initialized.
161  * @retval NRFX_ERROR_INVALID_STATE The driver is already initialized.
162  *                                  Deprecated - use @ref NRFX_ERROR_ALREADY instead.
163  */
164 nrfx_err_t nrfx_wdt_init(nrfx_wdt_t const *        p_instance,
165                          nrfx_wdt_config_t const * p_config,
166                          nrfx_wdt_event_handler_t  wdt_event_handler,
167                          void *                    p_context);
168 #else
169 /**
170  * @brief Function for initializing the WDT driver instance.
171  *
172  * @deprecated Use new variant instead.
173  *
174  * @param[in] p_instance        Pointer to the driver instance structure.
175  * @param[in] p_config          Pointer to the structure with the initial configuration.
176  *                              NULL if configuration is to be skipped and will be done later
177  *                              using @ref nrfx_wdt_reconfigure.
178  * @param[in] wdt_event_handler Event handler provided by the user. Ignored when
179  *                              @ref NRFX_WDT_CONFIG_NO_IRQ option is enabled.
180  *
181  * @retval NRFX_SUCCESS             Initialization was successful.
182  * @retval NRFX_ERROR_INVALID_STATE The driver was already initialized.
183  */
184 nrfx_err_t nrfx_wdt_init(nrfx_wdt_t const *        p_instance,
185                          nrfx_wdt_config_t const * p_config,
186                          nrfx_wdt_event_handler_t  wdt_event_handler);
187 #endif
188 
189 /**
190  * @brief Function for uninitializing the WDT driver instance.
191  *
192  * The instance can be uninitialized only when not running.
193  *
194  * @param[in] p_instance Pointer to the driver instance structure.
195  */
196 void nrfx_wdt_uninit(nrfx_wdt_t const * p_instance);
197 
198 /**
199  * @brief Function for checking if the WDT driver instance is initialized.
200  *
201  * @param[in] p_instance Pointer to the driver instance structure.
202  *
203  * @retval true  Instance is already initialized.
204  * @retval false Instance is not initialized.
205  */
206 bool nrfx_wdt_init_check(nrfx_wdt_t const * p_instance);
207 
208 /**
209  * @brief Function for reconfiguring the watchdog.
210  *
211  * @param[in] p_instance Pointer to the driver instance structure.
212  * @param[in] p_config   Pointer to the structure with the initial configuration.
213  *
214  * @retval NRFX_SUCCESS             Reconfiguration was successful.
215  * @retval NRFX_ERROR_BUSY          The watchdog is already active.
216  * @retval NRFX_ERROR_INVALID_STATE The watchdog is uninitialized.
217  */
218 nrfx_err_t nrfx_wdt_reconfigure(nrfx_wdt_t const *        p_instance,
219                                 nrfx_wdt_config_t const * p_config);
220 
221 /**
222  * @brief Function for allocating a watchdog channel.
223  *
224  * @note This function can not be called after @ref nrfx_wdt_enable.
225  *
226  * @param[in]  p_instance   Pointer to the driver instance structure.
227  * @param[out] p_channel_id ID of granted channel.
228  *
229  * @retval NRFX_SUCCESS      The channel was successfully allocated.
230  * @retval NRFX_ERROR_NO_MEM There is no available channel to be used.
231  */
232 nrfx_err_t nrfx_wdt_channel_alloc(nrfx_wdt_t const *    p_instance,
233                                   nrfx_wdt_channel_id * p_channel_id);
234 
235 /**
236  * @brief Function for deallocating all previously allocated watchdog channels.
237  *
238  * @note This function can be called when watchdog is stopped,
239  *       that is before @ref nrfx_wdt_enable() or after @ref nrfx_wdt_stop().
240  *
241  * @param[in] p_instance Pointer to the driver instance structure.
242  */
243 void nrfx_wdt_channels_free(nrfx_wdt_t const * p_instance);
244 
245 /**
246  * @brief Function for starting the watchdog.
247  *
248  * @note After calling this function the watchdog is started, so the user needs to feed
249  *       all allocated watchdog channels to avoid reset. At least one watchdog channel
250  *       must be allocated.
251  *
252  * @param[in] p_instance Pointer to the driver instance structure.
253  */
254 void nrfx_wdt_enable(nrfx_wdt_t const * p_instance);
255 
256 /**
257  * @brief Function for feeding the watchdog.
258  *
259  * @details Function feeds all allocated watchdog channels.
260  *
261  * @param[in] p_instance Pointer to the driver instance structure.
262  */
263 void nrfx_wdt_feed(nrfx_wdt_t const * p_instance);
264 
265 /**
266  * @brief Function for feeding an individual watchdog channel.
267  *
268  * @param[in] p_instance Pointer to the driver instance structure.
269  * @param[in] channel_id ID of watchdog channel.
270  */
271 void nrfx_wdt_channel_feed(nrfx_wdt_t const * p_instance, nrfx_wdt_channel_id channel_id);
272 
273 #if NRFX_WDT_HAS_STOP
274 /**
275  * @brief Function for stopping the watchdog.
276  *
277  * @param[in] p_instance Pointer to the driver instance structure.
278  *
279  * @retval NRFX_SUCCESS         Watchdog has been successfully stopped.
280  * @retval NRFX_ERROR_FORBIDDEN Configuration does not allow for stopping the watchdog.
281  */
282 nrfx_err_t nrfx_wdt_stop(nrfx_wdt_t const * p_instance);
283 #endif
284 
285 /**
286  * @brief Function for returning a requested task address for the WDT driver module.
287  *
288  * @param[in] p_instance Pointer to the driver instance structure.
289  * @param[in] task       One of the WDT tasks.
290  *
291  * @return Task address.
292  */
293 NRFX_STATIC_INLINE uint32_t nrfx_wdt_task_address_get(nrfx_wdt_t const * p_instance,
294                                                       nrf_wdt_task_t     task);
295 
296 /**
297  * @brief Function for returning a requested event address for the WDT driver module.
298  *
299  * @param[in] p_instance Pointer to the driver instance structure.
300  * @param[in] event      One of the WDT events.
301  *
302  * @return Event address.
303  */
304 NRFX_STATIC_INLINE uint32_t nrfx_wdt_event_address_get(nrfx_wdt_t const * p_instance,
305                                                        nrf_wdt_event_t    event);
306 
307 #ifndef NRFX_DECLARE_ONLY
nrfx_wdt_task_address_get(nrfx_wdt_t const * p_instance,nrf_wdt_task_t task)308 NRFX_STATIC_INLINE uint32_t nrfx_wdt_task_address_get(nrfx_wdt_t const * p_instance,
309                                                       nrf_wdt_task_t     task)
310 {
311     return nrfy_wdt_task_address_get(p_instance->p_reg, task);
312 }
313 
nrfx_wdt_event_address_get(nrfx_wdt_t const * p_instance,nrf_wdt_event_t event)314 NRFX_STATIC_INLINE uint32_t nrfx_wdt_event_address_get(nrfx_wdt_t const * p_instance,
315                                                        nrf_wdt_event_t    event)
316 {
317     return nrfy_wdt_event_address_get(p_instance->p_reg, event);
318 }
319 #endif // NRFX_DECLARE_ONLY
320 
321 /**
322  * @brief Macro returning WDT interrupt handler.
323  *
324  * param[in] idx WDT index.
325  *
326  * @return Interrupt handler.
327  */
328 #define NRFX_WDT_INST_HANDLER_GET(idx) NRFX_CONCAT_3(nrfx_wdt_, idx, _irq_handler)
329 
330 /** @} */
331 
332 /*
333  * Declare interrupt handlers for all enabled driver instances in the following format:
334  * nrfx_\<periph_name\>_\<idx\>_irq_handler (for example, nrfx_wdt_0_irq_handler).
335  *
336  * A specific interrupt handler for the driver instance can be retrieved by using
337  * the NRFX_WDT_INST_HANDLER_GET macro.
338  *
339  * Here is a sample of using the NRFX_WDT_INST_HANDLER_GET macro to map an interrupt handler
340  * in a Zephyr application:
341  *
342  * IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_WDT_INST_GET(\<instance_index\>)), \<priority\>,
343  *             NRFX_WDT_INST_HANDLER_GET(\<instance_index\>), 0, 0);
344  */
345 NRFX_INSTANCE_IRQ_HANDLERS_DECLARE(WDT, wdt)
346 
347 #ifdef __cplusplus
348 }
349 #endif
350 
351 #endif
352