44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7+ #define DT_DRV_COMPAT nordic_nrf_wdt
8+
79#include <zephyr/kernel.h>
810#include <zephyr/sys/math_extras.h>
911#include <nrfx_wdt.h>
@@ -19,6 +21,7 @@ LOG_MODULE_REGISTER(wdt_nrfx);
1921#endif
2022
2123struct wdt_nrfx_data {
24+ nrfx_wdt_t wdt ;
2225 wdt_callback_t m_callbacks [NRF_WDT_CHANNEL_NUMBER ];
2326 uint32_t m_timeout ;
2427 uint8_t m_allocated_channels ;
@@ -28,15 +31,10 @@ struct wdt_nrfx_data {
2831#endif
2932};
3033
31- struct wdt_nrfx_config {
32- nrfx_wdt_t wdt ;
33- };
34-
3534static int wdt_nrf_setup (const struct device * dev , uint8_t options )
3635{
37- const struct wdt_nrfx_config * config = dev -> config ;
3836 struct wdt_nrfx_data * data = dev -> data ;
39- nrfx_err_t err_code ;
37+ int err_code ;
4038
4139 nrfx_wdt_config_t wdt_config = {
4240 .reload_value = data -> m_timeout
@@ -54,13 +52,13 @@ static int wdt_nrf_setup(const struct device *dev, uint8_t options)
5452 wdt_config .behaviour |= NRF_WDT_BEHAVIOUR_RUN_HALT_MASK ;
5553 }
5654
57- err_code = nrfx_wdt_reconfigure (& config -> wdt , & wdt_config );
55+ err_code = nrfx_wdt_reconfigure (& data -> wdt , & wdt_config );
5856
59- if (err_code != NRFX_SUCCESS ) {
60- return - EBUSY ;
57+ if (err_code < 0 ) {
58+ return err_code ;
6159 }
6260
63- nrfx_wdt_enable (& config -> wdt );
61+ nrfx_wdt_enable (& data -> wdt );
6462
6563 data -> enabled = true;
6664 return 0 ;
@@ -69,23 +67,22 @@ static int wdt_nrf_setup(const struct device *dev, uint8_t options)
6967static int wdt_nrf_disable (const struct device * dev )
7068{
7169#if NRFX_WDT_HAS_STOP
72- const struct wdt_nrfx_config * config = dev -> config ;
7370 struct wdt_nrfx_data * data = dev -> data ;
74- nrfx_err_t err_code ;
71+ int err_code ;
7572 int channel_id ;
7673
77- err_code = nrfx_wdt_stop (& config -> wdt );
74+ err_code = nrfx_wdt_stop (& data -> wdt );
7875
79- if (err_code != NRFX_SUCCESS ) {
76+ if (err_code < 0 ) {
8077 /* This can only happen if wdt_nrf_setup() is not called first. */
81- return - EFAULT ;
78+ return err_code ;
8279 }
8380
8481#if defined(WDT_NRFX_SYNC_STOP )
8582 k_sem_take (& data -> sync_stop , K_FOREVER );
8683#endif
8784
88- nrfx_wdt_channels_free (& config -> wdt );
85+ nrfx_wdt_channels_free (& data -> wdt );
8986
9087 for (channel_id = 0 ; channel_id < data -> m_allocated_channels ; channel_id ++ ) {
9188 data -> m_callbacks [channel_id ] = NULL ;
@@ -103,9 +100,8 @@ static int wdt_nrf_disable(const struct device *dev)
103100static int wdt_nrf_install_timeout (const struct device * dev ,
104101 const struct wdt_timeout_cfg * cfg )
105102{
106- const struct wdt_nrfx_config * config = dev -> config ;
107103 struct wdt_nrfx_data * data = dev -> data ;
108- nrfx_err_t err_code ;
104+ int err_code ;
109105 nrfx_wdt_channel_id channel_id ;
110106
111107 if (data -> enabled ) {
@@ -138,11 +134,11 @@ static int wdt_nrf_install_timeout(const struct device *dev,
138134 return - EINVAL ;
139135 }
140136
141- err_code = nrfx_wdt_channel_alloc (& config -> wdt ,
137+ err_code = nrfx_wdt_channel_alloc (& data -> wdt ,
142138 & channel_id );
143139
144- if (err_code == NRFX_ERROR_NO_MEM ) {
145- return - ENOMEM ;
140+ if (err_code == - ENOMEM ) {
141+ return err_code ;
146142 }
147143
148144 if (cfg -> callback != NULL ) {
@@ -155,7 +151,6 @@ static int wdt_nrf_install_timeout(const struct device *dev,
155151
156152static int wdt_nrf_feed (const struct device * dev , int channel_id )
157153{
158- const struct wdt_nrfx_config * config = dev -> config ;
159154 struct wdt_nrfx_data * data = dev -> data ;
160155
161156 if ((channel_id >= data -> m_allocated_channels ) || (channel_id < 0 )) {
@@ -166,7 +161,7 @@ static int wdt_nrf_feed(const struct device *dev, int channel_id)
166161 return - EAGAIN ;
167162 }
168163
169- nrfx_wdt_channel_feed (& config -> wdt ,
164+ nrfx_wdt_channel_feed (& data -> wdt ,
170165 (nrfx_wdt_channel_id )channel_id );
171166
172167 return 0 ;
@@ -205,84 +200,46 @@ static void wdt_event_handler(const struct device *dev, nrf_wdt_event_t event_ty
205200
206201#define WDT (idx ) DT_NODELABEL(wdt##idx)
207202
208- #define WDT_NRFX_WDT_IRQ (idx ) \
203+ #define WDT_NRFX_WDT_IRQ (inst ) \
209204 COND_CODE_1(CONFIG_WDT_NRFX_NO_IRQ, \
210205 (), \
211- (IRQ_CONNECT(DT_IRQN(WDT(idx)), DT_IRQ(WDT(idx) , priority), \
212- nrfx_isr, nrfx_wdt_##idx##_irq_handler , 0)))
206+ (IRQ_CONNECT(DT_INST_IRQN(inst), DT_INST_IRQ(inst , priority), \
207+ nrfx_wdt_irq_handler, &wdt_##inst##_data.wdt , 0)))
213208
214- #define WDT_NRFX_WDT_DEVICE (idx ) \
215- static void wdt_##idx ##_event_handler(nrf_wdt_event_t event_type, \
216- uint32_t requests, \
217- void *p_context) \
209+ #define WDT_NRFX_WDT_DEVICE (inst ) \
210+ static void wdt_##inst ##_event_handler(nrf_wdt_event_t event_type, \
211+ uint32_t requests, \
212+ void *p_context) \
218213 { \
219- wdt_event_handler(DEVICE_DT_GET(WDT(idx) ), event_type, \
214+ wdt_event_handler(DEVICE_DT_INST_GET(inst ), event_type, \
220215 requests, p_context); \
221216 } \
222- static int wdt_##idx##_init(const struct device *dev) \
217+ static struct wdt_nrfx_data wdt_##inst##_data = { \
218+ .wdt = NRFX_WDT_INSTANCE(DT_INST_REG_ADDR(inst)), \
219+ IF_ENABLED(WDT_NRFX_SYNC_STOP, \
220+ (.sync_stop = Z_SEM_INITIALIZER( \
221+ wdt_##inst##_data.sync_stop, 0, 1),)) \
222+ }; \
223+ static int wdt_##inst##_init(const struct device *dev) \
223224 { \
224- const struct wdt_nrfx_config *config = dev->config; \
225- nrfx_err_t err_code; \
226- WDT_NRFX_WDT_IRQ(idx ); \
227- err_code = nrfx_wdt_init(&config ->wdt, \
225+ int err_code; \
226+ struct wdt_nrfx_data *data = dev->data; \
227+ WDT_NRFX_WDT_IRQ(inst ); \
228+ err_code = nrfx_wdt_init(&data ->wdt, \
228229 NULL, \
229230 IS_ENABLED(CONFIG_WDT_NRFX_NO_IRQ) \
230231 ? NULL \
231- : wdt_##idx ##_event_handler, \
232+ : wdt_##inst ##_event_handler, \
232233 NULL); \
233- if (err_code != NRFX_SUCCESS) { \
234- return -EBUSY; \
235- } \
236- return 0; \
234+ return err_code; \
237235 } \
238- static struct wdt_nrfx_data wdt_##idx##_data = { \
239- IF_ENABLED(WDT_NRFX_SYNC_STOP, \
240- (.sync_stop = Z_SEM_INITIALIZER( \
241- wdt_##idx##_data.sync_stop, 0, 1),)) \
242- }; \
243- static const struct wdt_nrfx_config wdt_##idx##z_config = { \
244- .wdt = NRFX_WDT_INSTANCE(idx), \
245- }; \
246- DEVICE_DT_DEFINE(WDT(idx), \
247- wdt_##idx##_init, \
248- NULL, \
249- &wdt_##idx##_data, \
250- &wdt_##idx##z_config, \
251- PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
252- &wdt_nrfx_driver_api)
253-
254- #ifdef CONFIG_HAS_HW_NRF_WDT0
255- WDT_NRFX_WDT_DEVICE (0 );
256- #endif
257-
258- #ifdef CONFIG_HAS_HW_NRF_WDT1
259- WDT_NRFX_WDT_DEVICE (1 );
260- #endif
261-
262- #ifdef CONFIG_HAS_HW_NRF_WDT30
263- WDT_NRFX_WDT_DEVICE (30 );
264- #endif
265-
266- #ifdef CONFIG_HAS_HW_NRF_WDT31
267- WDT_NRFX_WDT_DEVICE (31 );
268- #endif
269-
270- #ifdef CONFIG_HAS_HW_NRF_WDT010
271- WDT_NRFX_WDT_DEVICE (010 );
272- #endif
273-
274- #ifdef CONFIG_HAS_HW_NRF_WDT011
275- WDT_NRFX_WDT_DEVICE (011 );
276- #endif
277-
278- #ifdef CONFIG_HAS_HW_NRF_WDT130
279- WDT_NRFX_WDT_DEVICE (130 );
280- #endif
281-
282- #ifdef CONFIG_HAS_HW_NRF_WDT131
283- WDT_NRFX_WDT_DEVICE (131 );
284- #endif
285-
286- #ifdef CONFIG_HAS_HW_NRF_WDT132
287- WDT_NRFX_WDT_DEVICE (132 );
288- #endif
236+ DEVICE_DT_INST_DEFINE(inst, \
237+ wdt_##inst##_init, \
238+ NULL, \
239+ &wdt_##inst##_data, \
240+ NULL, \
241+ PRE_KERNEL_1, \
242+ CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
243+ &wdt_nrfx_driver_api)
244+
245+ DT_INST_FOREACH_STATUS_OKAY (WDT_NRFX_WDT_DEVICE )
0 commit comments