[bsp][stm32] drv_wdt.c | 完善 stm32 看门狗驱动

This commit is contained in:
tangweikang 2019-06-25 20:26:35 +08:00
parent 4712956b22
commit fae9518185
1 changed files with 32 additions and 2 deletions

View File

@ -23,7 +23,7 @@ static rt_watchdog_t watchdog;
static rt_err_t wdt_init(rt_watchdog_t *wdt)
{
hiwdg.Instance = IWDG;
hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
hiwdg.Init.Reload = 0x00000FFF;
#if defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7)
@ -48,13 +48,43 @@ static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
break;
/* set watchdog timeout */
case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
hiwdg.Init.Reload = *((rt_uint32_t*)arg);
#if defined(LSI_VALUE)
if(LSI_VALUE)
{
hiwdg.Init.Reload = (*((rt_uint32_t*)arg)) * LSI_VALUE / 256 ;
}
else
{
LOG_E("Please defined LSI_VALUE`s value!");
}
#else
#error "Please defined LSI_VALUE`s value!"
#endif
if(hiwdg.Init.Reload > 0xFFF)
{
LOG_E("wdg set timeout parameter too large.");
return -RT_EINVAL;
}
if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
{
LOG_E("wdg set timeout failed.");
return -RT_ERROR;
}
break;
case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
#if defined(LSI_VALUE)
if(LSI_VALUE)
{
(*((rt_uint32_t*)arg)) = hiwdg.Init.Reload * 256 / LSI_VALUE;
}
else
{
LOG_E("Please defined LSI_VALUE`s value!");
}
#else
#error "Please defined LSI_VALUE`s value!"
#endif
break;
default:
return -RT_ERROR;
}