/**
  ******************************************************************************
  * @file    WWDT/WWDT_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 WWDT_Reset
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint32_t TimingDelay = 0;

/* 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
       */
#if defined (APM32_MINI)
    /* Initialize LED3 and Key Button mounted on APM32-MINI board */
    APM_MINI_LEDInit(LED3);
    APM_MINI_LEDInit(LED2);
    APM_MINI_PBInit(BUTTON_KEY1, BUTTON_MODE_EINT);
#elif defined(APM32_DISCOVERY)
    /* Initialize LED3 and Key Button mounted on APM32-MINI board */
    APM_DISCOVERY_LEDInit(LED3);
    APM_DISCOVERY_LEDInit(LED2);
    APM_DISCOVERY_PBInit(BUTTON_KEY1, BUTTON_MODE_EINT);
#endif

    /* Check if the system has resumed from WWDT reset */
    if (RCM_ReadFlag(RCM_FLAG_WWDTRST) != RESET)
    {
        /* WWDTRST flag set */
        /* Turn on LED3 */
#if defined (APM32_MINI)
        APM_MINI_LEDOn(LED3);
#elif defined(APM32_DISCOVERY)
        APM_DISCOVERY_LEDOn(LED3);
#endif
        /* Clear reset flags */
        RCM_ClearFlag();
    }
    else
    {
        /* WWDTRST flag is not set */
        /* Turn off LED3 */
#if defined (APM32_MINI)
        APM_MINI_LEDOff(LED3);
#elif defined(APM32_DISCOVERY)
        APM_DISCOVERY_LEDOff(LED3);
#endif
    }

    /* Setup SysTick Timer for 1 msec interrupts  */
    if (SysTick_Config(SystemCoreClock / 1000))
    {
        /* Capture error */
        while (1);
    }

    /* WWDT configuration */
    /* Enable WWDT clock */
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_WWDT);

    /* On Value line devices, WWDT clock counter = (PCLK1 (24MHz)/4096)/8 = 732 Hz (~1366 us)  */
    /* On other devices, WWDT clock counter = (PCLK1(36MHz)/4096)/8 = 1099 Hz (~910 us)  */
    WWDT_SetTimebase(WWDT_TIMEBASE_4);

    /* Set Window value to 80; WWDT counter should be refreshed only when the counter
      is below 80 (and greater than 64) otherwise a reset will be generated */
    WWDT_SetWindowData(80);

    /* - On Value line devices,
      Enable WWDT and set counter value to 127, WWDT timeout = ~1366 us * 64 = 87.42 ms
      In this case the refresh window is: ~1366us * (127-80) = 64.20 ms < refresh window < ~1366us * 64 = 87.42ms
       - On other devices
      Enable WWDT and set counter value to 127, WWDT timeout = ~910 us * 64 = 58.25 ms
      In this case the refresh window is: ~910 us * (127-80) = 42.77 ms < refresh window < ~910 us * 64 = 58.25ms
    */
    WWDT_Enable(127);

    while (1)
    {
        /* Toggle LED2 */
#if defined (APM32_MINI)
        APM_MINI_LEDToggle(LED2);
#elif defined(APM32_DISCOVERY)
        APM_DISCOVERY_LEDToggle(LED2);
#endif

        /* Update WWDT counter */
        WWDT_Enable(127);
    }
}

/**
  * @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

/**
  * @}
  */

/**
  * @}
  */

