4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
6
6
7
+ #define DT_DRV_COMPAT nordic_nrf_wdt
8
+
7
9
#include <zephyr/kernel.h>
8
10
#include <zephyr/sys/math_extras.h>
9
11
#include <nrfx_wdt.h>
@@ -19,6 +21,7 @@ LOG_MODULE_REGISTER(wdt_nrfx);
19
21
#endif
20
22
21
23
struct wdt_nrfx_data {
24
+ nrfx_wdt_t wdt ;
22
25
wdt_callback_t m_callbacks [NRF_WDT_CHANNEL_NUMBER ];
23
26
uint32_t m_timeout ;
24
27
uint8_t m_allocated_channels ;
@@ -28,15 +31,10 @@ struct wdt_nrfx_data {
28
31
#endif
29
32
};
30
33
31
- struct wdt_nrfx_config {
32
- nrfx_wdt_t wdt ;
33
- };
34
-
35
34
static int wdt_nrf_setup (const struct device * dev , uint8_t options )
36
35
{
37
- const struct wdt_nrfx_config * config = dev -> config ;
38
36
struct wdt_nrfx_data * data = dev -> data ;
39
- nrfx_err_t err_code ;
37
+ int err_code ;
40
38
41
39
nrfx_wdt_config_t wdt_config = {
42
40
.reload_value = data -> m_timeout
@@ -54,13 +52,13 @@ static int wdt_nrf_setup(const struct device *dev, uint8_t options)
54
52
wdt_config .behaviour |= NRF_WDT_BEHAVIOUR_RUN_HALT_MASK ;
55
53
}
56
54
57
- err_code = nrfx_wdt_reconfigure (& config -> wdt , & wdt_config );
55
+ err_code = nrfx_wdt_reconfigure (& data -> wdt , & wdt_config );
58
56
59
- if (err_code != NRFX_SUCCESS ) {
60
- return - EBUSY ;
57
+ if (err_code < 0 ) {
58
+ return err_code ;
61
59
}
62
60
63
- nrfx_wdt_enable (& config -> wdt );
61
+ nrfx_wdt_enable (& data -> wdt );
64
62
65
63
data -> enabled = true;
66
64
return 0 ;
@@ -69,23 +67,22 @@ static int wdt_nrf_setup(const struct device *dev, uint8_t options)
69
67
static int wdt_nrf_disable (const struct device * dev )
70
68
{
71
69
#if NRFX_WDT_HAS_STOP
72
- const struct wdt_nrfx_config * config = dev -> config ;
73
70
struct wdt_nrfx_data * data = dev -> data ;
74
- nrfx_err_t err_code ;
71
+ int err_code ;
75
72
int channel_id ;
76
73
77
- err_code = nrfx_wdt_stop (& config -> wdt );
74
+ err_code = nrfx_wdt_stop (& data -> wdt );
78
75
79
- if (err_code != NRFX_SUCCESS ) {
76
+ if (err_code < 0 ) {
80
77
/* This can only happen if wdt_nrf_setup() is not called first. */
81
- return - EFAULT ;
78
+ return err_code ;
82
79
}
83
80
84
81
#if defined(WDT_NRFX_SYNC_STOP )
85
82
k_sem_take (& data -> sync_stop , K_FOREVER );
86
83
#endif
87
84
88
- nrfx_wdt_channels_free (& config -> wdt );
85
+ nrfx_wdt_channels_free (& data -> wdt );
89
86
90
87
for (channel_id = 0 ; channel_id < data -> m_allocated_channels ; channel_id ++ ) {
91
88
data -> m_callbacks [channel_id ] = NULL ;
@@ -103,9 +100,8 @@ static int wdt_nrf_disable(const struct device *dev)
103
100
static int wdt_nrf_install_timeout (const struct device * dev ,
104
101
const struct wdt_timeout_cfg * cfg )
105
102
{
106
- const struct wdt_nrfx_config * config = dev -> config ;
107
103
struct wdt_nrfx_data * data = dev -> data ;
108
- nrfx_err_t err_code ;
104
+ int err_code ;
109
105
nrfx_wdt_channel_id channel_id ;
110
106
111
107
if (data -> enabled ) {
@@ -138,11 +134,11 @@ static int wdt_nrf_install_timeout(const struct device *dev,
138
134
return - EINVAL ;
139
135
}
140
136
141
- err_code = nrfx_wdt_channel_alloc (& config -> wdt ,
137
+ err_code = nrfx_wdt_channel_alloc (& data -> wdt ,
142
138
& channel_id );
143
139
144
- if (err_code == NRFX_ERROR_NO_MEM ) {
145
- return - ENOMEM ;
140
+ if (err_code == - ENOMEM ) {
141
+ return err_code ;
146
142
}
147
143
148
144
if (cfg -> callback != NULL ) {
@@ -155,7 +151,6 @@ static int wdt_nrf_install_timeout(const struct device *dev,
155
151
156
152
static int wdt_nrf_feed (const struct device * dev , int channel_id )
157
153
{
158
- const struct wdt_nrfx_config * config = dev -> config ;
159
154
struct wdt_nrfx_data * data = dev -> data ;
160
155
161
156
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)
166
161
return - EAGAIN ;
167
162
}
168
163
169
- nrfx_wdt_channel_feed (& config -> wdt ,
164
+ nrfx_wdt_channel_feed (& data -> wdt ,
170
165
(nrfx_wdt_channel_id )channel_id );
171
166
172
167
return 0 ;
@@ -205,84 +200,45 @@ static void wdt_event_handler(const struct device *dev, nrf_wdt_event_t event_ty
205
200
206
201
#define WDT (idx ) DT_NODELABEL(wdt##idx)
207
202
208
- #define WDT_NRFX_WDT_IRQ (idx ) \
203
+ #define WDT_NRFX_WDT_IRQ (inst ) \
209
204
COND_CODE_1(CONFIG_WDT_NRFX_NO_IRQ, \
210
205
(), \
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)))
213
208
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) \
218
213
{ \
219
- wdt_event_handler(DEVICE_DT_GET(WDT(idx) ), event_type, \
214
+ wdt_event_handler(DEVICE_DT_INST_GET(inst ), event_type, \
220
215
requests, p_context); \
221
216
} \
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) \
223
224
{ \
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
+ WDT_NRFX_WDT_IRQ(inst); \
227
+ err_code = nrfx_wdt_init(&data->wdt, \
228
228
NULL, \
229
229
IS_ENABLED(CONFIG_WDT_NRFX_NO_IRQ) \
230
230
? NULL \
231
- : wdt_##idx ##_event_handler, \
231
+ : wdt_##inst ##_event_handler, \
232
232
NULL); \
233
- if (err_code != NRFX_SUCCESS) { \
234
- return -EBUSY; \
235
- } \
236
- return 0; \
233
+ return err_code; \
237
234
} \
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
235
+ DEVICE_DT_INST_DEFINE(inst, \
236
+ wdt_##inst##_init, \
237
+ NULL, \
238
+ &wdt_##inst##_data, \
239
+ NULL, \
240
+ PRE_KERNEL_1, \
241
+ CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
242
+ &wdt_nrfx_driver_api)
243
+
244
+ DT_INST_FOREACH_STATUS_OKAY (WDT_NRFX_WDT_DEVICE )
0 commit comments