2022-08-14 10:29:05 +08:00
|
|
|
/*
|
2022-08-17 00:43:24 +08:00
|
|
|
* Copyright (c) 2006-2022, Synwit Technology Co.,Ltd.
|
2022-08-14 10:29:05 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2022-08-17 00:43:24 +08:00
|
|
|
* 2018-12-10 Zohar_Lee first version
|
|
|
|
* 2020-07-10 lik format file
|
2022-08-14 10:29:05 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "drv_wdt.h"
|
|
|
|
|
|
|
|
#ifdef RT_USING_WDT
|
|
|
|
#ifdef BSP_USING_WDT
|
|
|
|
//#define DRV_DEBUG
|
|
|
|
#define LOG_TAG "drv.wdt"
|
|
|
|
#include <drv_log.h>
|
|
|
|
|
|
|
|
struct swm_wdt_cfg
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
WDT_TypeDef *WDTx;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct swm_wdt_device
|
|
|
|
{
|
|
|
|
struct swm_wdt_cfg *wdt_cfg;
|
|
|
|
struct rt_watchdog_device wdt_device;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct swm_wdt_cfg swm_wdt_cfg =
|
|
|
|
{
|
|
|
|
.name = "wdt",
|
|
|
|
.WDTx = WDT,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct swm_wdt_device wdt_obj;
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
static rt_err_t swm_wdt_configure(rt_watchdog_t *wdt_device)
|
2022-08-14 10:29:05 +08:00
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t swm_wdt_control(rt_watchdog_t *wdt_device, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct swm_wdt_cfg *wdt_cfg;
|
|
|
|
RT_ASSERT(wdt_device != RT_NULL);
|
|
|
|
wdt_cfg = wdt_device->parent.user_data;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_DEVICE_CTRL_WDT_KEEPALIVE:
|
|
|
|
WDT_Feed(wdt_cfg->WDTx);
|
|
|
|
break;
|
|
|
|
case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
|
2022-08-17 00:43:24 +08:00
|
|
|
WDT_Init(wdt_cfg->WDTx, (SystemCoreClock * (*(rt_uint32_t *)arg)), WDT_MODE_RESET);
|
2022-08-14 10:29:05 +08:00
|
|
|
break;
|
|
|
|
case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
|
2022-08-17 00:43:24 +08:00
|
|
|
*(rt_uint32_t *)arg = (wdt_cfg->WDTx->LOAD) / SystemCoreClock;
|
2022-08-14 10:29:05 +08:00
|
|
|
break;
|
|
|
|
case RT_DEVICE_CTRL_WDT_GET_TIMELEFT:
|
2022-08-17 00:43:24 +08:00
|
|
|
*(rt_uint32_t *)arg = WDT_GetValue(wdt_cfg->WDTx) / SystemCoreClock;
|
2022-08-14 10:29:05 +08:00
|
|
|
break;
|
|
|
|
case RT_DEVICE_CTRL_WDT_START:
|
|
|
|
WDT_Start(wdt_cfg->WDTx);
|
|
|
|
break;
|
|
|
|
case RT_DEVICE_CTRL_WDT_STOP:
|
|
|
|
WDT_Stop(wdt_cfg->WDTx);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_W("This command is not supported.");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
static const struct rt_watchdog_ops swm_wdt_ops =
|
2022-08-14 10:29:05 +08:00
|
|
|
{
|
2022-08-17 00:43:24 +08:00
|
|
|
.init = swm_wdt_configure,
|
2022-08-14 10:29:05 +08:00
|
|
|
.control = swm_wdt_control};
|
|
|
|
|
|
|
|
int swm_wdt_init(void)
|
|
|
|
{
|
|
|
|
int result = RT_EOK;
|
|
|
|
|
|
|
|
wdt_obj.wdt_cfg = &swm_wdt_cfg;
|
|
|
|
wdt_obj.wdt_device.ops = &swm_wdt_ops;
|
|
|
|
|
|
|
|
result = rt_hw_watchdog_register(&wdt_obj.wdt_device, wdt_obj.wdt_cfg->name, RT_DEVICE_FLAG_RDWR, wdt_obj.wdt_cfg);
|
|
|
|
if(result != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("wdt device register fail.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_D("wdt device register success.");
|
|
|
|
}
|
2022-08-17 00:43:24 +08:00
|
|
|
return RT_EOK;
|
2022-08-14 10:29:05 +08:00
|
|
|
}
|
|
|
|
INIT_BOARD_EXPORT(swm_wdt_init);
|
|
|
|
|
|
|
|
#endif /* BSP_USING_WDT */
|
|
|
|
#endif /* RT_USING_WDT */
|