/*!
 * @file        apm32f00x_int.c
 *
 * @brief       Main Interrupt Service Routines
 *
 * @version     V1.0.1
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "apm32f00x_int.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup BootLoader
  @{
*/

/** @defgroup BootLoader_INT_Variables INT_Variables
  @{
*/

typedef void  (*INTERRUPT_FUNCTION)(void);

#if defined (__CC_ARM)

/* Indicates that the current interrupt vector */
uint32_t FLASH_interrupt_vector __attribute__((at(0x20000004))) = IAP_INTERRUPT_VECTOR;

/* application interrupt vector address */
INTERRUPT_FUNCTION APP_interrupt_vector_addr __attribute__((at(0x20000000))) = (INTERRUPT_FUNCTION)0;

#elif defined (__ICCARM__)
#pragma location = 0x20000000
__root INTERRUPT_FUNCTION APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)0;;
#pragma location = 0x20000004
__root uint32_t FLASH_interrupt_vector = IAP_INTERRUPT_VECTOR;

#else
/* application interrupt vector address */
INTERRUPT_FUNCTION APP_interrupt_vector_addr __attribute__((section(".my_data"))) = (INTERRUPT_FUNCTION)0;

/* Indicates that the current interrupt vector */
uint32_t FLASH_interrupt_vector __attribute__((section(".my_data"))) = IAP_INTERRUPT_VECTOR;

#endif

/**@} end of group BootLoader_INT_Variables */

/** @defgroup BootLoader_INT_Functions INT_Functions
  @{
*/

/*!
 * @brief       This function handles NMI exception
 *
 * @param       None
 *
 * @retval      None
 */
void NMI_Handler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_NonMaskableInt_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_NonMaskableInt_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles Hard Fault exception
 *
 * @param       None
 *
 * @retval      None
 */
void HardFault_Handler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_HardFault_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_HardFault_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles SVCall exception
 *
 * @param       None
 *
 * @retval      None
 */
void SVC_Handler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_SVC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_SVC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles PendSV_Handler exception
 *
 * @param       None
 *
 * @retval      None
 */
void PendSV_Handler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_PendSV_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_PendSV_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles SysTick exception
 *
 * @param       None
 *
 * @retval      None
 */
void SysTick_Handler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_SysTick_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_SysTick_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles WUPT exception
 *
 * @param       None
 *
 * @retval      None
 */
void WUPT_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_WUPT_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_WUPT_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles RCM exception
 *
 * @param       None
 *
 * @retval      None
 */
void RCM_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_RCM_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_RCM_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles EINTA exception
 *
 * @param       None
 *
 * @retval      None
 */
void EINTA_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_EINTA_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_EINTA_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles EINTB exception
 *
 * @param       None
 *
 * @retval      None
 */
void EINTB_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_EINTB_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_EINTB_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles EINTC exception
 *
 * @param       None
 *
 * @retval      None
 */
void EINTC_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_EINTC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_EINTC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles EINTD exception
 *
 * @param       None
 *
 * @retval      None
 */
void EINTD_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_EINTD_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_EINTD_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles SPI exception
 *
 * @param       None
 *
 * @retval      None
 */
void SPI_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_SPI_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_SPI_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles TIMER1 update/overflow/underflow/trigger/break exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_UT_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_TMR1_UT_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_TMR1_UT_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles TIMER1 capture/compare exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_CC_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_TMR1_CC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_TMR1_CC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles TIMER2 update /overflow exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR2_UO_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_TMR2_UO_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_TMR2_UO_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles TIMER2 update /overflow exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR2_CC_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_TMR2_CC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_TMR2_CC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles USART1 TX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART1_TX_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_USART1_TX_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_USART1_TX_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles USART1 RX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART1_RX_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {

    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_USART1_RX_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_USART1_RX_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles I2C exception
 *
 * @param       None
 *
 * @retval      None
 */
void IIC_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_IIC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_IIC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles ADC exception
 *
 * @param       None
 *
 * @retval      None
 */
void ADC_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_ADC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_ADC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles TIMER4 update /overflow exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR4_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_TMR4_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_TMR4_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles Flash exception
 *
 * @param       None
 *
 * @retval      None
 */
void FLASH_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_FLASH_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_FLASH_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles USART2 TX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART2_TX_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_USART2_TX_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();

    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_USART2_TX_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();

    }
}

/*!
 * @brief       This function handles USART2 RX exception
 *
 * @param       None
 *
 * @retval      None
 */
void USART2_RX_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_USART2_RX_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_USART2_RX_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles TIMER1A update/overflow/underflow/trigger/break exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1A_UT_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_TMR1A_UT_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_TMR1A_UT_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/*!
 * @brief       This function handles TIMER1A capture/compare exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1A_CC_IRQHandler(void)
{
    if (FLASH_interrupt_vector == IAP_INTERRUPT_VECTOR)
    {
    }
    else if (FLASH_interrupt_vector == APP1_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP1_TMR1A_CC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
    else if (FLASH_interrupt_vector == APP2_INTERRUPT_VECTOR)
    {
        APP_interrupt_vector_addr = (INTERRUPT_FUNCTION)*(uint32_t*)APP2_TMR1A_CC_IRQn_APP_ADDR;
        APP_interrupt_vector_addr();
    }
}

/**@} end of group BootLoader_INT_Functions */
/**@} end of group BootLoader */
/**@} end of group Examples */
