|
| 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 */ |
0 commit comments