2018-12-10 09:48:01 +08:00
|
|
|
/*
|
2021-03-08 22:40:39 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2018-12-10 09:48:01 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2018-12-07 balanceTWK first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <board.h>
|
|
|
|
|
|
|
|
#ifdef RT_USING_WDT
|
|
|
|
|
|
|
|
//#define DRV_DEBUG
|
|
|
|
#define LOG_TAG "drv.wdt"
|
|
|
|
#include <drv_log.h>
|
|
|
|
|
2019-07-12 15:32:06 +08:00
|
|
|
struct stm32_wdt_obj
|
|
|
|
{
|
2020-07-23 18:37:35 +08:00
|
|
|
rt_watchdog_t watchdog;
|
2019-07-12 15:32:06 +08:00
|
|
|
IWDG_HandleTypeDef hiwdg;
|
|
|
|
rt_uint16_t is_start;
|
|
|
|
};
|
|
|
|
static struct stm32_wdt_obj stm32_wdt;
|
2018-12-10 09:48:01 +08:00
|
|
|
static struct rt_watchdog_ops ops;
|
|
|
|
|
|
|
|
static rt_err_t wdt_init(rt_watchdog_t *wdt)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* feed the watchdog */
|
|
|
|
case RT_DEVICE_CTRL_WDT_KEEPALIVE:
|
2019-07-12 15:32:06 +08:00
|
|
|
if(HAL_IWDG_Refresh(&stm32_wdt.hiwdg) != HAL_OK)
|
|
|
|
{
|
|
|
|
LOG_E("watch dog keepalive fail.");
|
|
|
|
}
|
2018-12-10 09:48:01 +08:00
|
|
|
break;
|
|
|
|
/* set watchdog timeout */
|
|
|
|
case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
|
2019-06-25 20:26:35 +08:00
|
|
|
#if defined(LSI_VALUE)
|
|
|
|
if(LSI_VALUE)
|
|
|
|
{
|
2019-07-12 15:32:06 +08:00
|
|
|
stm32_wdt.hiwdg.Init.Reload = (*((rt_uint32_t*)arg)) * LSI_VALUE / 256 ;
|
2019-06-25 20:26:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-25 22:18:26 +08:00
|
|
|
LOG_E("Please define the value of LSI_VALUE!");
|
2019-06-25 20:26:35 +08:00
|
|
|
}
|
2019-07-12 15:32:06 +08:00
|
|
|
if(stm32_wdt.hiwdg.Init.Reload > 0xFFF)
|
2019-06-25 20:26:35 +08:00
|
|
|
{
|
2019-07-12 15:32:06 +08:00
|
|
|
LOG_E("wdg set timeout parameter too large, please less than %ds",0xFFF * 256 / LSI_VALUE);
|
2019-06-25 20:26:35 +08:00
|
|
|
return -RT_EINVAL;
|
|
|
|
}
|
2019-07-12 15:32:06 +08:00
|
|
|
#else
|
|
|
|
#error "Please define the value of LSI_VALUE!"
|
|
|
|
#endif
|
|
|
|
if(stm32_wdt.is_start)
|
2018-12-10 09:48:01 +08:00
|
|
|
{
|
2019-07-12 15:32:06 +08:00
|
|
|
if (HAL_IWDG_Init(&stm32_wdt.hiwdg) != HAL_OK)
|
|
|
|
{
|
|
|
|
LOG_E("wdg set timeout failed.");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2018-12-10 09:48:01 +08:00
|
|
|
}
|
|
|
|
break;
|
2019-06-25 20:26:35 +08:00
|
|
|
case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
|
|
|
|
#if defined(LSI_VALUE)
|
|
|
|
if(LSI_VALUE)
|
|
|
|
{
|
2019-07-12 15:32:06 +08:00
|
|
|
(*((rt_uint32_t*)arg)) = stm32_wdt.hiwdg.Init.Reload * 256 / LSI_VALUE;
|
2019-06-25 20:26:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-25 22:18:26 +08:00
|
|
|
LOG_E("Please define the value of LSI_VALUE!");
|
2019-06-25 20:26:35 +08:00
|
|
|
}
|
|
|
|
#else
|
2019-06-25 22:18:26 +08:00
|
|
|
#error "Please define the value of LSI_VALUE!"
|
2019-06-25 20:26:35 +08:00
|
|
|
#endif
|
|
|
|
break;
|
2019-07-12 15:32:06 +08:00
|
|
|
case RT_DEVICE_CTRL_WDT_START:
|
|
|
|
if (HAL_IWDG_Init(&stm32_wdt.hiwdg) != HAL_OK)
|
|
|
|
{
|
|
|
|
LOG_E("wdt start failed.");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
stm32_wdt.is_start = 1;
|
2019-07-22 09:15:25 +08:00
|
|
|
break;
|
2018-12-10 09:48:01 +08:00
|
|
|
default:
|
2019-07-22 09:15:25 +08:00
|
|
|
LOG_W("This command is not supported.");
|
2018-12-10 09:48:01 +08:00
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rt_wdt_init(void)
|
|
|
|
{
|
2019-07-14 16:34:59 +08:00
|
|
|
#if defined(SOC_SERIES_STM32H7)
|
|
|
|
stm32_wdt.hiwdg.Instance = IWDG1;
|
|
|
|
#else
|
2019-07-12 15:32:06 +08:00
|
|
|
stm32_wdt.hiwdg.Instance = IWDG;
|
2019-07-14 16:34:59 +08:00
|
|
|
#endif
|
2019-07-12 15:32:06 +08:00
|
|
|
stm32_wdt.hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
|
|
|
|
|
|
|
|
stm32_wdt.hiwdg.Init.Reload = 0x00000FFF;
|
2019-07-14 16:34:59 +08:00
|
|
|
#if defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) \
|
|
|
|
|| defined(SOC_SERIES_STM32H7)
|
2019-07-12 15:32:06 +08:00
|
|
|
stm32_wdt.hiwdg.Init.Window = 0x00000FFF;
|
|
|
|
#endif
|
|
|
|
stm32_wdt.is_start = 0;
|
|
|
|
|
2018-12-10 09:48:01 +08:00
|
|
|
ops.init = &wdt_init;
|
|
|
|
ops.control = &wdt_control;
|
2020-07-23 18:37:35 +08:00
|
|
|
stm32_wdt.watchdog.ops = &ops;
|
2018-12-10 09:48:01 +08:00
|
|
|
/* register watchdog device */
|
2020-07-23 18:37:35 +08:00
|
|
|
if (rt_hw_watchdog_register(&stm32_wdt.watchdog, "wdt", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK)
|
2018-12-10 09:48:01 +08:00
|
|
|
{
|
|
|
|
LOG_E("wdt device register failed.");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
LOG_D("wdt device register success.");
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
INIT_BOARD_EXPORT(rt_wdt_init);
|
|
|
|
|
|
|
|
#endif /* RT_USING_WDT */
|