/**
  ******************************************************************************
  * @file    IWDT/IWDT_Reset/main.c
  * @author  IOT Development Team
  * @version V1.0.0
  * @date    10-January-2020
  * @brief   Main program body.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "apm32f10x.h"
#include "platform_config.h"

/** @addtogroup APM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup IWDT_Reset
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint32_t TimingDelay = 0;
__IO uint32_t LsiFreq = 40000;

/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nTime);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_apm32f10x_xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_apm32f10x.c file
       */

    /* Initialize LED2 and Key Button mounted on APM3210X-MINI board */
#if defined(APM32_MINI)
    APM_MINI_LEDInit(LED2);
#elif defined (APM32_DISCOVERY)
    APM_DISCOVERY_LEDInit(LED2);
#elif defined(APM32F103V_EVAL)
    APM_EVAL_LEDInit(LED2);
#endif

    /* Check if the system has resumed from IWDT reset */
    if (RCM_ReadFlag(RCM_FLAG_IWDTRST) != RESET)
    {
        /* IWDTRST flag set */
        /* Turn on LED2 */
#if defined(APM32_MINI)
        APM_MINI_LEDOn(LED2);
#elif defined (APM32_DISCOVERY)
        APM_DISCOVERY_LEDOn(LED2);
#elif defined(APM32F103V_EVAL)
        APM_EVAL_LEDOn(LED2);
#endif

        /* Clear reset flags */
        RCM_ClearFlag();
    }
    else
    {
        /* IWDTRST flag is not set */
        /* Turn off LED2 */
#if defined(APM32_MINI)
        APM_MINI_LEDOff(LED2);
#elif defined (APM32_DISCOVERY)
        APM_DISCOVERY_LEDOff(LED2);
#elif defined(APM32F103V_EVAL)
        APM_EVAL_LEDOff(LED2);
#endif
    }

    /* Enable the LIRC OSC */
    RCM_EnableLIRC();

    /* Wait till LIRC is ready */
    while (RCM_ReadFlag(RCM_FLAG_LIRCRDY) == RESET)
    {}

    /* IWDT timeout equal to 250 ms (the timeout may varies due to LIRC frequency
       dispersion) */
    /* Enable write access to IWDT_KEYWORD registers */
    IWDT_WriteAccess_Enable();

    /* IWDT counter clock: LIRC/32 */
    IWDT_SetDivider(32);

    /* Set counter reload value to obtain 250ms IWDT TimeOut.
       Counter Reload Value = 250ms/IWDT counter clock period
                            = 250ms / (LIRC/32)
                            = 0.25s / (LsiFreq/32)
                            = LsiFreq/(32 * 4)
                            = LsiFreq/128
     */
    IWDT_SetCountReload(LsiFreq/128);

    /* Reload IWDT counter */
    IWDT_ReloadCounter();

    /* Enable IWDT (the LIRC oscillator will be enabled by hardware) */
    IWDT_Enable();

    while (1)
    {
        /* Toggle LED2 */

#if defined(APM32_MINI)
        APM_MINI_LEDToggle(LED2);
#elif defined (APM32_DISCOVERY)
        APM_DISCOVERY_LEDToggle(LED2);
#elif defined(APM32F103V_EVAL)
        APM_EVAL_LEDToggle(LED2);
#endif
    /* Insert 240 ms delay */
//    Delay(240);

        /* Reload IWDT counter */
        IWDT_ReloadCounter();
    }
}

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in milliseconds.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{
    TimingDelay = nTime;

    while (TimingDelay != 0);
}



#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {}
}

#endif

/**
  * @}
  */

/**
  * @}
  */

