Skip to content

Commit 07ad08e

Browse files
authored
Merge pull request RT-Thread#3840 from ylz0923/nrf5x-wdt
[bsp/nrf5x/libraries/drivers] add wdt driver
2 parents f177f11 + 679bda5 commit 07ad08e

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

Diff for: bsp/nrf5x/libraries/drivers/SConscript

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ if GetDepend(['BSP_USING_SAADC']):
2929
if GetDepend(['BSP_USING_PWM']):
3030
src += ['drv_pwm.c']
3131

32+
if GetDepend(['BSP_USING_WDT']):
33+
src += ['drv_wdt.c']
34+
3235
path = [cwd]
3336

3437
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path)

Diff for: bsp/nrf5x/libraries/drivers/drv_wdt.c

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2020-08-18 ylz0923 first version
9+
*/
10+
11+
#include <board.h>
12+
#include <rtdevice.h>
13+
#include <nrfx_wdt.h>
14+
15+
#ifdef RT_USING_WDT
16+
17+
struct nrf5x_wdt_obj
18+
{
19+
rt_watchdog_t watchdog;
20+
nrfx_wdt_t wdt;
21+
nrfx_wdt_channel_id ch;
22+
rt_uint16_t is_start;
23+
};
24+
static struct nrf5x_wdt_obj nrf5x_wdt = {
25+
.wdt = NRFX_WDT_INSTANCE(0),
26+
};
27+
static nrfx_wdt_config_t nrf5x_wdt_cfg = NRFX_WDT_DEFAULT_CONFIG;
28+
static struct rt_watchdog_ops ops;
29+
30+
static rt_err_t wdt_init(rt_watchdog_t *wdt)
31+
{
32+
return RT_EOK;
33+
}
34+
35+
static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
36+
{
37+
switch (cmd)
38+
{
39+
/* feed the watchdog */
40+
case RT_DEVICE_CTRL_WDT_KEEPALIVE:
41+
nrfx_wdt_feed(&nrf5x_wdt.wdt);
42+
break;
43+
/* set watchdog timeout */
44+
case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
45+
nrf5x_wdt_cfg.reload_value = (rt_uint32_t)arg * 1000;
46+
break;
47+
case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
48+
*((rt_uint32_t*)arg) = nrf5x_wdt_cfg.reload_value;
49+
break;
50+
case RT_DEVICE_CTRL_WDT_START:
51+
if (nrf5x_wdt.is_start != 1)
52+
{
53+
nrfx_wdt_init(&nrf5x_wdt.wdt, &nrf5x_wdt_cfg, RT_NULL);
54+
nrfx_wdt_channel_alloc(&nrf5x_wdt.wdt, &nrf5x_wdt.ch);
55+
nrfx_wdt_enable(&nrf5x_wdt.wdt);
56+
nrf5x_wdt.is_start = 1;
57+
}
58+
break;
59+
default:
60+
return -RT_ERROR;
61+
}
62+
return RT_EOK;
63+
}
64+
65+
int rt_wdt_init(void)
66+
{
67+
nrf5x_wdt.is_start = 0;
68+
ops.init = &wdt_init;
69+
ops.control = &wdt_control;
70+
nrf5x_wdt.watchdog.ops = &ops;
71+
/* register watchdog device */
72+
if (rt_hw_watchdog_register(&nrf5x_wdt.watchdog, "wdt", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK)
73+
{
74+
return -RT_ERROR;
75+
}
76+
return RT_EOK;
77+
}
78+
INIT_BOARD_EXPORT(rt_wdt_init);
79+
80+
static int wdt_sample(int argc, char *argv[])
81+
{
82+
rt_device_t wdt = rt_device_find("wdt");
83+
rt_device_init(wdt);
84+
rt_device_control(wdt, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void *)2);
85+
rt_device_control(wdt, RT_DEVICE_CTRL_WDT_START, RT_NULL);
86+
}
87+
MSH_CMD_EXPORT(wdt_sample, wdt sample);
88+
89+
#endif /* RT_USING_WDT */

Diff for: bsp/nrf5x/nrf52840/board/Kconfig

+15
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,21 @@ endif
332332
range 0x1000 0x1000
333333
default 0x1000
334334
endmenu
335+
config BSP_USING_WDT
336+
bool "Enable WDT"
337+
select RT_USING_WDT
338+
default n
339+
if BSP_USING_WDT
340+
config NRFX_WDT_ENABLED
341+
int
342+
default 1
343+
config NRFX_WDT0_ENABLED
344+
int
345+
default 1
346+
config NRFX_WDT_CONFIG_NO_IRQ
347+
int
348+
default 1
349+
endif
335350
endmenu
336351

337352
endmenu

0 commit comments

Comments
 (0)