/**
  ******************************************************************************
  * @file    PMU/STOP/apm32f10x_it.c 
  * @author  IOT Development Team
  * @version V1.0.0
  * @date    10-January-2020
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and peripherals
  *          interrupt service routine.
  ******************************************************************************
  */ 

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

/** @addtogroup APM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup PMU_STOP
  * @{
  */

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

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSV_Handler exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
  TimingDelay--;
}

/******************************************************************************/
/*            APM32F10x Peripherals Interrupt Handlers                        */
/******************************************************************************/

/**
  * @brief  This function handles External lines 9 to 5 interrupt request.
  * @param  None
  * @retval None
  */
void EINT1_IRQHandler(void)
{
  if(EINT_ReadIntFlag(KEY1_BUTTON_EINT_LINE) != RESET)
  { 
    /* Clear the Key Button EINT line pending bit */  
    EINT_ClearIntFlag(KEY1_BUTTON_EINT_LINE);

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

/**
  * @brief  This function handles RTC Alarm interrupt request.
  * @param  None
  * @retval None
  */
void RTCAlarm_IRQHandler(void)
{
  if(RTC_ReadIntFlag(RTC_INT_ALM) != RESET)
  {
    /* Toggle LED3 */
#if defined (APM32_MINI)
    APM_MINI_LEDToggle(LED3);
#elif defined(APM32_DISCOVERY)
		APM_DISCOVERY_LEDToggle(LED3);
#elif defined(APM32F103V_EVAL)	
    APM_EVAL_LEDToggle(LED3);	
#endif
		
    /* Clear EINT line17 pending bit */
    EINT_ClearIntFlag(0x20000);

    /* Check if the Wake-Up flag is set */
    if(PMU_ReadFlag(PMU_FLAG_WUPF) != RESET)
    {
      /* Clear Wake Up flag */
      PMU_ClerFlag(PMU_FLAG_WUPF);
    }

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();   
    /* Clear RTC Alarm interrupt pending bit */
    RTC_ClearIntFlag(RTC_INT_ALM);
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
  }
}

/******************************************************************************/
/*                 APM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_apm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */

/**
  * @}
  */

