#include <stdbool.h>
#include "bsp/bsp.h"
#include "bsp/bsp_driver.h"
/*!
    \brief      this function handles NMI exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void NMI_Handler(void)
{
}

/*!
    \brief      this function handles HardFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void HardFault_Handler(void){
	while(1) {
	}
}

/*!
    \brief      this function handles MemManage exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void MemManage_Handler(void)
{
    /* if Memory Manage exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles BusFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void BusFault_Handler(void)
{
    /* if Bus Fault exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles UsageFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void UsageFault_Handler(void)
{
    /* if Usage Fault exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles DebugMon exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DebugMon_Handler(void)
{
}


__weak void MC_Brake_IRQHandler(void) {

}
__weak void MC_Protect_IRQHandler(void) {

}
__weak void TIMER_UP_IRQHandler(void) {

}

__weak void ADC_IRQHandler(void) {

}

__weak void HALL_IRQHandler(void) {

}

__weak void ABI_I_IRQHandler(void) {

}

__weak void Fan_IRQHandler(int idx) {

}

void ADC1_2_IRQHandler(void)
{
	ADC_IRQHandler();
	adc_clear_irq_flags();
}

void TMR1_OVF_TMR10_IRQHandler(void) {
	if (tmr_flag_get(MOS_PWM_TIMER, TMR_OVF_FLAG)) {
		tmr_flag_clear(MOS_PWM_TIMER, TMR_OVF_FLAG);
		TIMER_UP_IRQHandler();
	}
}

void TMR1_BRK_TMR9_IRQHandler(void) {
	if (tmr_flag_get(MOS_PWM_TIMER, TMR_BRK_FLAG)) {
		tmr_flag_clear(MOS_PWM_TIMER, TMR_BRK_FLAG);
		MC_Protect_IRQHandler();
	}
}

void EXINT0_IRQHandler(void)
{
	if(RESET != exint_flag_get(EXINT_LINE_0)){
		exint_flag_clear(EXINT_LINE_0);
	}	
}

void EXINT2_IRQHandler(void)
{
	if(RESET != exint_flag_get(EXINT_LINE_2)){
		exint_flag_clear(EXINT_LINE_2);
		MC_Brake_IRQHandler();
	}	
}


void EXINT3_IRQHandler(void)
{
	if(RESET != exint_flag_get(EXINT_LINE_3)){
		exint_flag_clear(EXINT_LINE_3);
	}
}

void EXINT4_IRQHandler(void)
{
	if(RESET != exint_flag_get(EXINT_LINE_4)){
		exint_flag_clear(EXINT_LINE_4);
		//MC_Brake_IRQHandler();
	}	
}

void EXINT9_5_IRQHandler(void){
	if(RESET != exint_flag_get(EXINT_LINE_5)){
		exint_flag_clear(EXINT_LINE_5);
	}
	if(RESET != exint_flag_get(EXINT_LINE_6)){
		exint_flag_clear(EXINT_LINE_6);
		ABI_I_IRQHandler();
	}	
	if(RESET != exint_flag_get(EXINT_LINE_7)){
		exint_flag_clear(EXINT_LINE_7);
	}
	if(RESET != exint_flag_get(EXINT_LINE_8)){
		exint_flag_clear(EXINT_LINE_8);
	}
	if(RESET != exint_flag_get(EXINT_LINE_9)){
		exint_flag_clear(EXINT_LINE_9);
	}	
}

void EXINT15_10_IRQHandler(void){
	if(RESET != exint_flag_get(EXINT_LINE_10)){
		exint_flag_clear(EXINT_LINE_10);
	}
	if(RESET != exint_flag_get(EXINT_LINE_11)){
		exint_flag_clear(EXINT_LINE_11);
		Fan_IRQHandler(0);
	}	
	if(RESET != exint_flag_get(EXINT_LINE_12)){
		exint_flag_clear(EXINT_LINE_12);
	}
	if(RESET != exint_flag_get(EXINT_LINE_13)){
		exint_flag_clear((EXINT_LINE_13));
	}
	if(RESET != exint_flag_get(EXINT_LINE_14)){
		exint_flag_clear(EXINT_LINE_14);
	}
	if(RESET != exint_flag_get(EXINT_LINE_15)){
		exint_flag_clear(EXINT_LINE_15);
	}	
}
