#include <stdbool.h>
#include "bsp.h"
#include "adc.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
*/
__asm void HardFault_Handler(void){
		IMPORT fault_backtrace
    mov     r0, lr                  ; get lr
    mov     r1, sp                  ; get stack pointer (current is MSP)
    bl      fault_backtrace
}

/*!
    \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 ADC0_1_IRQHandler(void)
{
	ADC_IRQHandler();
	adc_clear_irq_flags();
}

void PWM_UP_IRQHandler(void) {
	if (timer_interrupt_flag_get(TIMER0, TIMER_INT_FLAG_UP)) {
		timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_UP);
		TIMER_UP_IRQHandler();
	}
}

void PWM_BRK_IRQHandler(void) {
	if (timer_interrupt_flag_get(TIMER0, TIMER_INT_FLAG_BRK)) {
		timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_BRK);
		MC_Protect_IRQHandler();
	}
}

void EXTI0_IRQHandler(void)
{
	if(RESET != exti_interrupt_flag_get(EXTI_0)){
		exti_interrupt_flag_clear(EXTI_0);
#if (ENC_I_EXTI == EXTI_0)
		ABI_I_IRQHandler();
#elif defined (USE_ENCODER_HALL)
		HALL_IRQHandler();
#else
	#error "Postion sensor ERROR"
#endif
	}	
}

void EXTI2_IRQHandler(void)
{
	if(RESET != exti_interrupt_flag_get(EXTI_2)){
		exti_interrupt_flag_clear(EXTI_2);
#if (GPIO_BRAKE1_EXTI == EXTI_2)
		MC_Brake_IRQHandler();
#endif
	}	
}


void EXTI3_IRQHandler(void)
{
	if(RESET != exti_interrupt_flag_get(EXTI_3)){
		exti_interrupt_flag_clear(EXTI_3);
#if (GPIO_BRAKE_EXTI == EXTI_3)
		MC_Brake_IRQHandler();
#endif
	}
}

void EXTI4_IRQHandler(void)
{
	if(RESET != exti_interrupt_flag_get(EXTI_4)){
		exti_interrupt_flag_clear(EXTI_4);
		//MC_Brake_IRQHandler();
	}	
}

void EXTI5_9_IRQHandler(void){
	if(RESET != exti_interrupt_flag_get(EXTI_5)){
		exti_interrupt_flag_clear(EXTI_5);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_6)){
		exti_interrupt_flag_clear(EXTI_6);
#if (ENC_I_EXTI == EXTI_6)
		ABI_I_IRQHandler();
#endif
	}	
	if(RESET != exti_interrupt_flag_get(EXTI_7)){
		exti_interrupt_flag_clear(EXTI_7);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_8)){
		exti_interrupt_flag_clear(EXTI_8);
#if (ENC_I_EXTI == EXTI_8)
		ABI_I_IRQHandler();
#endif
	}
	if(RESET != exti_interrupt_flag_get(EXTI_9)){
		exti_interrupt_flag_clear(EXTI_9);
	}	
}

void EXTI10_15_IRQHandler(void){
	if(RESET != exti_interrupt_flag_get(EXTI_10)){
		exti_interrupt_flag_clear(EXTI_10);
#if (GPIO_FAN1_EXTI==EXTI_10)
		Fan_IRQHandler(0);
#endif
	}
	if(RESET != exti_interrupt_flag_get(EXTI_11)){
		exti_interrupt_flag_clear(EXTI_11);
#if (GPIO_FAN1_EXTI==EXTI_11)
		Fan_IRQHandler(0);
#elif (GPIO_FAN2_EXTI==EXTI_11)
		Fan_IRQHandler(1);
#endif
	}	
	if(RESET != exti_interrupt_flag_get(EXTI_12)){
		exti_interrupt_flag_clear(EXTI_12);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_13)){
		exti_interrupt_flag_clear(EXTI_13);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_14)){
		exti_interrupt_flag_clear(EXTI_14);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_15)){
		exti_interrupt_flag_clear(EXTI_15);
	}	
}
