#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)
{
	while(1);
}

/*!
    \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)
{
	while(1);
}

void SVC_Handler(void) {
	while(1);
}

void PendSV_Handler(void) {
	while(1);
}
__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 PWM_UP_IRQHandler(void) {
	if (TIM_GetIntStatus(MOS_PWM_TIMER, TIM_INT_UPDATE)) {
		TIM_ClrIntPendingBit(MOS_PWM_TIMER, TIM_INT_UPDATE);
		TIMER_UP_IRQHandler();
	}
}

void PWM_BRK_IRQHandler(void) {
	if (TIM_GetIntStatus(MOS_PWM_TIMER, TIM_INT_BREAK)) {
		TIM_ClrIntPendingBit(MOS_PWM_TIMER, TIM_INT_BREAK);
		MC_Protect_IRQHandler();
	}
}

void EXTI0_IRQHandler(void)
{
	if(RESET != exti_interrupt_flag_get(EXTI_LINE0)){
		exti_interrupt_flag_clear(EXTI_LINE0);

	}	
}

void EXTI2_IRQHandler(void)
{
	if(RESET != exti_interrupt_flag_get(EXTI_LINE2)){
		exti_interrupt_flag_clear(EXTI_LINE2);
	}	
}


void EXTI3_IRQHandler(void)
{
	if(RESET != exti_interrupt_flag_get(EXTI_LINE3)){
		exti_interrupt_flag_clear(EXTI_LINE3);
		MC_Brake_IRQHandler();
	}
}

void EXTI4_IRQHandler(void)
{
	if(RESET != exti_interrupt_flag_get(EXTI_LINE4)){
		exti_interrupt_flag_clear(EXTI_LINE4);
	}	
}

void EXTI9_5_IRQHandler(void){
	if(RESET != exti_interrupt_flag_get(EXTI_LINE5)){
		exti_interrupt_flag_clear(EXTI_LINE5);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_LINE6)){
		exti_interrupt_flag_clear(EXTI_LINE6);
	}	
	if(RESET != exti_interrupt_flag_get(EXTI_LINE7)){
		exti_interrupt_flag_clear(EXTI_LINE7);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_LINE8)){
		exti_interrupt_flag_clear(EXTI_LINE8);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_LINE9)){
		exti_interrupt_flag_clear(EXTI_LINE9);
	}	
}

void EXTI15_10_IRQHandler(void){
	if(RESET != exti_interrupt_flag_get(EXTI_LINE10)){
		exti_interrupt_flag_clear(EXTI_LINE10);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_LINE11)){
		exti_interrupt_flag_clear(EXTI_LINE11);
		Fan_IRQHandler(0);
	}	
	if(RESET != exti_interrupt_flag_get(EXTI_LINE12)){
		exti_interrupt_flag_clear(EXTI_LINE12);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_LINE13)){
		exti_interrupt_flag_clear(EXTI_LINE13);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_LINE14)){
		exti_interrupt_flag_clear(EXTI_LINE14);
	}
	if(RESET != exti_interrupt_flag_get(EXTI_LINE15)){
		exti_interrupt_flag_clear(EXTI_LINE15);
		ABI_I_IRQHandler();
	}	
}
