/**
  **************************************************************************
  * @file     main.c
  * @version  v2.0.0
  * @date     2021-11-26
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to 
  * download from Artery official website is the copyrighted work of Artery. 
  * Artery authorizes customers to use, copy, and distribute the BSP 
  * software and its related documentation for the purpose of design and 
  * development in conjunction with Artery microcontrollers. Use of the 
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#include "at32f413_board.h"
#include "at32f413_clock.h"

/** @addtogroup AT32F413_periph_examples
  * @{
  */
  
/** @addtogroup 413_WWDT_reset WWDT_reset
  * @{
  */
  
/**
  * @brief  main function.
  * @param  none
  * @retval none
  */
int main(void)
{
  system_clock_config();

  at32_board_init();

  if(crm_flag_get(CRM_WWDT_RESET_FLAG) != RESET)
  {
    /* reset from wwdt */
    crm_flag_clear(CRM_WWDT_RESET_FLAG);
    
    at32_led_on(LED4);
  }
  else
  {
    /* reset from other mode */    
    at32_led_off(LED4);  
  }

  /* enable the wwdt clock */
  crm_periph_clock_enable(CRM_WWDT_PERIPH_CLOCK, TRUE);
  
  /* set the wwdt divider value */
  wwdt_divider_set(WWDT_PCLK1_DIV_32768);
  
  /* set the wwdt window counter value */
  wwdt_window_counter_set(0x6F);
 
  /* enable wwdt 
  
   window_value:   (0x7F - 0x6F)
   timeout_value:  (0x7F - 0x40) + 1
  
   timeout = timeout_value * (divider / pclk1_freq )    (s)
   window  = window_value  * (divider / pclk1_freq )    (s)    
  
   pclk1_freq   = 120 MHz
   divider      = 32768
   reload_value = 0x40 = 64
   window_value = 0x10 = 16 
  
   timeout = 64 * (32768 / 96000000 ) = 0.0218s = 21.8ms
   window  = 16 * (32768 / 96000000 ) = 0.0055s = 5.5ms  
   
  */
  wwdt_enable(0x7F);
  
  while(1)
  {
    at32_led_toggle(LED3);   
    
    /* the reload time must within the window (8ms > 5.5ms) */     
    delay_ms(8);    
    
    /* update the wwdt window counter */    
    wwdt_counter_set(0x7F);
    
    if(at32_button_press() == USER_BUTTON)
    {
      while(1);
    }
  }
}

/**
  * @}
  */ 

/**
  * @}
  */ 
