/**
  ******************************************************************************
  * @file    RCM/RCM_ClockConfig/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 "main.h"

/** @addtogroup APM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup RCM_ClockConfig
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

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

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
  /* This interrupt is generated when HXT clock fails */

  if (RCM_ReadIntFlag(RCM_INT_CSS) != RESET)
  {/* At this stage: HXT, PLL are disabled (but no change on PLL config) and HIRC
       is selected as system clock source */

    /* Enable HXT */
    RCM_ConfigHXT(RCM_HXT_OPEN);

    /* Enable HXT Ready interrupt */
    RCM_EnableInterrupt(RCM_INT_HXTRDY);

#ifndef SYSCLK_HXT
 #ifdef APM32F10X_CL
    /* Enable PLL and PLL2 Ready interrupts */
    RCM_ITConfig(RCM_IT_PLLRDY | RCM_IT_PLL2RDY, ENABLE);
 #else
    /* Enable PLL Ready interrupt */
    RCM_EnableInterrupt(RCM_INT_PLLRDY);
 #endif	/* APM32F10X_CL */
#endif /* SYSCLK_HXT */

    /* Clear Clock Security System interrupt pending bit */
    RCM_ClearIntFlag(RCM_INT_CSS);

    /* Once HXT clock recover, the HXTRDY interrupt is generated and in the RCM ISR
       routine the system clock will be reconfigured to its previous state (before
       HXT clock failure) */
  }
}

/**
  * @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 RCM interrupt request. 
  * @param  None
  * @retval None
  */
void RCM_IRQHandler(void)
{
  if(RCM_ReadIntFlag(RCM_INT_HXTRDY) != RESET)
  { 
    /* Clear HXTRDY interrupt pending bit */
    RCM_ClearIntFlag(RCM_INT_HXTRDY);

    /* Check if the HXT clock is still available */
    if (RCM_ReadFlag(RCM_FLAG_HXTRDY) != RESET)
    { 
#ifdef SYSCLK_HXT
      /* Select HXT as system clock source */
      RCM_SYSCLKConfig(RCM_SYSCLKSource_HXT);
#else
 #ifdef APM32F10X_CL
      /* Enable PLL2 */
      RCM_PLL2Cmd(ENABLE);
 #else
      /* Enable PLL: once the PLL is ready the PLLRDY interrupt is generated */ 
      RCM_EnablePLL();
 #endif	/* APM32F10X_CL */
#endif /* SYSCLK_HXT */      
    }
  }

#ifdef APM32F10X_CL
  if(RCM_GetITStatus(RCM_IT_PLL2RDY) != RESET)
  { 
    /* Clear PLL2RDY interrupt pending bit */
    RCM_ClearITPendingBit(RCM_IT_PLL2RDY);

    /* Enable PLL: once the PLL is ready the PLLRDY interrupt is generated */ 
    RCM_PLLCmd(ENABLE);
  }
#endif /* APM32F10X_CL */   

  if(RCM_ReadIntFlag(RCM_INT_PLLRDY) != RESET)
  { 
    /* Clear PLLRDY interrupt pending bit */
    RCM_ClearIntFlag(RCM_INT_PLLRDY);

    /* Check if the PLL is still locked */
    if (RCM_ReadFlag(RCM_FLAG_PLLRDY) != RESET)
    { 
      /* Select PLL as system clock source */
      RCM_ConfigSYSCLK(RCM_SYSCLK_SEL_PLL);
    }
  }
}

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

/**
  * @}
  */ 

/**
  * @}
  */ 

