|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2026, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2026-02-04 Yixizhang first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <board.h> |
| 12 | +#include <rtthread.h> |
| 13 | +#ifdef RT_USING_WDT |
| 14 | + |
| 15 | +#if defined(BSP_USING_FWDT) |
| 16 | + #define HW_WDGT_DEV_NAME "fwdgt" |
| 17 | +#elif defined(BSP_USING_WWDT) |
| 18 | + #define HW_WDGT_DEV_NAME "wwdgt" |
| 19 | +#endif |
| 20 | + |
| 21 | +#define DBG_TAG "wdt" |
| 22 | +#define DBG_LVL DBG_LOG |
| 23 | + |
| 24 | +#include <rtdbg.h> |
| 25 | +struct gd32_wdt_obj |
| 26 | +{ |
| 27 | + rt_watchdog_t watchdog; |
| 28 | + rt_uint16_t is_start; |
| 29 | +}; |
| 30 | +static struct gd32_wdt_obj gd32_wdt; |
| 31 | +static struct rt_watchdog_ops ops; |
| 32 | + |
| 33 | +static rt_err_t wdt_init(rt_watchdog_t *wdt) |
| 34 | +{ |
| 35 | + LOG_D("wdt init success."); |
| 36 | + return RT_EOK; |
| 37 | +} |
| 38 | + |
| 39 | +static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg) |
| 40 | +{ |
| 41 | + |
| 42 | + rt_uint32_t time_sec; |
| 43 | + rt_uint32_t time_msec; |
| 44 | + rt_uint32_t wdgt_count; |
| 45 | + rt_uint32_t window_value; |
| 46 | + switch (cmd) |
| 47 | + { |
| 48 | + /* feed the watchdog */ |
| 49 | + case RT_DEVICE_CTRL_WDT_KEEPALIVE: |
| 50 | +#if defined(BSP_USING_FWDT) |
| 51 | + fwdgt_counter_reload(); |
| 52 | +#elif defined(BSP_USING_WWDT) |
| 53 | + wwdgt_counter_update(0x70); |
| 54 | + LOG_D("wdt update success."); |
| 55 | +#endif |
| 56 | + break; |
| 57 | + case RT_DEVICE_CTRL_WDT_SET_TIMEOUT: |
| 58 | + |
| 59 | +#if defined(BSP_USING_FWDT) |
| 60 | + /* set timeout sec*/ |
| 61 | + time_sec = *(rt_uint32_t*)arg; |
| 62 | + wdgt_count = 32*1000/32*time_sec; |
| 63 | + ErrStatus res = fwdgt_prescaler_value_config(FWDGT_PSC_DIV32); |
| 64 | + res = fwdgt_reload_value_config(wdgt_count); |
| 65 | + LOG_D("timeout=%d sec,count=%d res=%d ", time_sec, wdgt_count, res); |
| 66 | +#elif defined(BSP_USING_WWDT) |
| 67 | + /* set timeout ms */ |
| 68 | + window_value = 0x7F; |
| 69 | + time_msec = *(rt_uint32_t*)arg; |
| 70 | + wdgt_count = (rt_uint32_t)((float)((float)1/0.78)*time_msec) + 0x3F; |
| 71 | + wwdgt_config(wdgt_count, window_value, WWDGT_CFG_PSC_DIV8); |
| 72 | + LOG_D("timeout=%d msec,count=%d ", time_msec, wdgt_count); |
| 73 | +#endif |
| 74 | + |
| 75 | + break; |
| 76 | + case RT_DEVICE_CTRL_WDT_START: |
| 77 | +#if defined(BSP_USING_FWDT) |
| 78 | + fwdgt_enable(); |
| 79 | +#elif defined(BSP_USING_WWDT) |
| 80 | + wwdgt_enable(); |
| 81 | +#endif |
| 82 | + LOG_D("wdt control enable success."); |
| 83 | + break; |
| 84 | + default: |
| 85 | + LOG_W("This command is not supported."); |
| 86 | + return -RT_ERROR; |
| 87 | + } |
| 88 | + return RT_EOK; |
| 89 | +} |
| 90 | + |
| 91 | +int rt_wdt_init(void) |
| 92 | +{ |
| 93 | + rcu_periph_clock_enable(RCU_WWDGT); |
| 94 | + gd32_wdt.is_start = 0; |
| 95 | + ops.init = &wdt_init; |
| 96 | + ops.control = &wdt_control; |
| 97 | + gd32_wdt.watchdog.ops = &ops; |
| 98 | + /* register watchdog device */ |
| 99 | + if (rt_hw_watchdog_register(&gd32_wdt.watchdog, HW_WDGT_DEV_NAME, RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK) |
| 100 | + { |
| 101 | + LOG_E("wdt device register failed."); |
| 102 | + return -RT_ERROR; |
| 103 | + } |
| 104 | + LOG_D(" wdt device register success."); |
| 105 | + return RT_EOK; |
| 106 | +} |
| 107 | +INIT_BOARD_EXPORT(rt_wdt_init); |
| 108 | + |
| 109 | +#if defined(BSP_USING_FWDT) |
| 110 | +int fwdt_test_sample() |
| 111 | +{ |
| 112 | + rt_err_t ret = RT_EOK; |
| 113 | + rt_device_t hw_dev = RT_NULL; |
| 114 | + rt_ubase_t time_out_sec = 4; |
| 115 | + hw_dev = rt_device_find(HW_WDGT_DEV_NAME); |
| 116 | + |
| 117 | + LOG_D("find fwdt device success,device=%x",hw_dev); |
| 118 | + if (hw_dev == RT_NULL) |
| 119 | + { |
| 120 | + LOG_D("fwdt sample run failed! can't find %s device!", HW_WDGT_DEV_NAME); |
| 121 | + return -RT_ERROR; |
| 122 | + } |
| 123 | + ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR); |
| 124 | + LOG_D("open fwdt device success"); |
| 125 | + if (ret != RT_EOK) |
| 126 | + { |
| 127 | + LOG_D("open %s device failed!", HW_WDGT_DEV_NAME); |
| 128 | + return ret; |
| 129 | + } |
| 130 | + |
| 131 | + ret = rt_device_control(hw_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &time_out_sec); |
| 132 | + if (ret != RT_EOK) |
| 133 | + { |
| 134 | + LOG_D("control %s device failed!", HW_WDGT_DEV_NAME); |
| 135 | + return ret; |
| 136 | + } |
| 137 | + rt_device_control(hw_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL); |
| 138 | + rt_device_control(hw_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, RT_NULL); |
| 139 | + return ret; |
| 140 | +} |
| 141 | + |
| 142 | +MSH_CMD_EXPORT(fwdt_test_sample, fwdt timeout 4 sec reset) |
| 143 | + |
| 144 | +#elif defined(BSP_USING_WWDT) |
| 145 | +int wwdt_test_sample() |
| 146 | +{ |
| 147 | + rt_err_t ret = RT_EOK; |
| 148 | + rt_device_t hw_dev = RT_NULL; |
| 149 | + rt_ubase_t time_out_msec = 40; |
| 150 | + hw_dev = rt_device_find(HW_WDGT_DEV_NAME); |
| 151 | + |
| 152 | + LOG_D("find wwdt device success,device=%x",hw_dev); |
| 153 | + if (hw_dev == RT_NULL) |
| 154 | + { |
| 155 | + LOG_D("wwdt sample run failed! can't find %s device!", HW_WDGT_DEV_NAME); |
| 156 | + return -RT_ERROR; |
| 157 | + } |
| 158 | + ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR); |
| 159 | + LOG_D("open wwdt device success"); |
| 160 | + if (ret != RT_EOK) |
| 161 | + { |
| 162 | + LOG_D("open %s device failed!", HW_WDGT_DEV_NAME); |
| 163 | + return ret; |
| 164 | + } |
| 165 | + |
| 166 | + ret = rt_device_control(hw_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &time_out_msec); |
| 167 | + if (ret != RT_EOK) |
| 168 | + { |
| 169 | + LOG_D("control %s device failed!", HW_WDGT_DEV_NAME); |
| 170 | + return ret; |
| 171 | + } |
| 172 | + rt_device_control(hw_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL); |
| 173 | + rt_device_control(hw_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, RT_NULL); |
| 174 | + return ret; |
| 175 | +} |
| 176 | + |
| 177 | +MSH_CMD_EXPORT(wwdt_test_sample, wwdt timeout 40 msec reset) |
| 178 | +#endif |
| 179 | + |
| 180 | +#endif /* RT_USING_WDT */ |
| 181 | + |
0 commit comments