/**
  ******************************************************************************
  * @file    NVIC/IRQ_Mask/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 IRQ_Mask
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint32_t index = 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)
{
}

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

/**
  * @brief  This function handles EINT Lines 0 interrupt request.
  * @param  None
  * @retval None
  */
void EINT0_IRQHandler(void)
{
#if defined(APM32_MINI)
  if(EINT_ReadIntFlag(KEY2_BUTTON_EINT_LINE) != RESET)		
#elif defined (APM32_DISCOVERY)
  if(EXTI_GetITStatus(KEY2_BUTTON_EXTI_LINE) != RESET)		
#elif defined(APM32F103V_EVAL)	
  if(EXTI_GetITStatus(WAKEUP_BUTTON_EXTI_LINE) != RESET)
#endif
  {
    if(index == 0)
    {
      /* Configure the BASEPRI register to 0x40 (Preemption priority = 1). 
         Only IRQ with higher preemption priority than 1 are permitted. 
         This will mask TMR3 and TMR4 IRQ from generation. */
      __set_BASEPRI(0x40);
      index++;
    }
    else
    {
      /* Configure the BASEPRI register to 0x00 (Preemption priority = 0). 
         When this BASEPRI register is set to 0, it has no effect on the current 
         priority.
         TMR2, TMR3 and TMR4 generation is controlled by NVIC priority registers. */
      __set_BASEPRI(0x00);
      index = 0;
    }
    /* Clears the SEL Button EINT line pending bits. */
#if defined(APM32_MINI)
    EINT_ClearIntFlag(KEY2_BUTTON_EINT_LINE);		
#elif defined (APM32_DISCOVERY)
    EXTI_ClearITPendingBit(KEY2_BUTTON_EXTI_LINE);		
#elif defined(APM32F103V_EVAL)
    EXTI_ClearITPendingBit(WAKEUP_BUTTON_EXTI_LINE);  
#endif
  }
} 

/**
  * @brief  This function handles TMR2 global interrupt request.
  * @param  None
  * @retval None
  */
void TMR2_IRQHandler(void)
{
  /* Clear TMR2 update interrupt */
  TMR_ClearIntFlag(TMR2, TMR_TI_UPDATE);
  
  /* Toggle LED1 */
#if defined(APM32_MINI)
  APM_MINI_LEDToggle(LED3);		
#elif defined (APM32_DISCOVERY)
  APM_DISCOVERY_LEDToggle(LED3);		
#elif defined(APM32F103V_EVAL)
  APM_EVAL_LEDToggle(LED1);
#endif
}

/**
  * @brief  This function handles TMR3 global interrupt request.
  * @param  None
  * @retval None
  */
void TMR3_IRQHandler(void)
{
  /* Clear TMR3 update interrupt */
  TMR_ClearIntFlag(TMR3, TMR_TI_UPDATE);
  
  /* 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 TMR4 global interrupt request.
  * @param  None
  * @retval None
  */
void TMR4_IRQHandler(void)
{
  /* Clear TMR4 update interrupt */
  TMR_ClearIntFlag(TMR4, TMR_TI_UPDATE);
  
  /* 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
}

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

/**
  * @}
  */

/**
  * @}
  */

