
/******************************************************************************
*@file  : app.c
*@brief : application
******************************************************************************/

#include "app.h"   

IWDT_HandleTypeDef IWDT_Handle;

/******************************************************************************
*@brief : app test
*@param : none
*@return: none
******************************************************************************/
void APP_Test(void)
{
    uint32_t reload;
    uint32_t freq;
    uint32_t resetTime;
    RCC_OscInitTypeDef      RCC_OscInitStruct = {0};
    
	BSP_LED_Init();
	
    BSP_LED_On();
    HAL_Delay(2000);
    
	/* 使能：RC32K */
    RCC_OscInitStruct.OscType = RCC_OSCTYPE_RC32K;
    RCC_OscInitStruct.RC32K = ENABLE;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        while (1);
    }  
    
	/* 使能：IWDT复位导致系统复位 */
	__HAL_RCC_IWDTRSTTOSYSRST_ENABLE();
    
    reload = 4000;
    
    __HAL_RCC_EXTI_CLK_ENABLE();
    
	/* 配置IWDT */
    IWDT_Handle.Instance = IWDT;
    IWDT_Handle.Init.Prescaler  = IWDT_CLOCK_PRESCALER_4;
    IWDT_Handle.Init.Reload     = reload;
    IWDT_Handle.Init.Window     = reload;
    IWDT_Handle.Init.Wakeup     = reload;
    
    HAL_IWDT_Init(&IWDT_Handle);

    freq = HAL_RCC_GetRC32KVal() / 4;
    resetTime = reload * 1000 / freq;
    
    HAL_IWDT_Refresh(&IWDT_Handle);
    
    while(1)
    {
        /* 在复位时间到达前50ms，刷新IWDT； */
        
        HAL_Delay(resetTime - 50);
        
		printfS("feed wachdog\r\n");
        
        HAL_IWDT_Refresh(&IWDT_Handle);
        
        BSP_LED_Toggle();
    };
}
