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