/*********************************************************************************************************************
* RT1064DVL6A Opensourec Library RT1064DVL6A Դ⣩һڹٷ SDK ӿڵĵԴ
* Copyright (c) 2022 SEEKFREE ɿƼ
* 
* ļ RT1064DVL6A Դһ
* 
* RT1064DVL6A Դ 
* Ըᷢ GPLGNU General Public License GNUͨù֤
*  GPL ĵ3棨 GPL3.0ѡģκκİ汾·/޸
* 
* Դķϣܷãδκεı֤
* ûԻʺض;ı֤
* ϸμ GPL
* 
* ӦյԴͬʱյһ GPL ĸ
* ûУ<https://www.gnu.org/licenses/>
* 
* ע
* Դʹ GPL3.0 Դ֤Э Ϊİ汾
* Ӣİ libraries/doc ļµ GPL3_permission_statement.txt ļ
* ֤ libraries ļ ļµ LICENSE ļ
* ӭλʹò ޸ʱ뱣ɿƼİȨ
* 
* ļ          isr
* ˾          ɶɿƼ޹˾
* 汾Ϣ          鿴 libraries/doc ļ version ļ 汾˵
*           IAR 8.32.4 or MDK 5.33
* ƽ̨          RT1064DVL6A
*           https://seekfree.taobao.com/
* 
* ޸ļ¼
*                               ע
* 2022-09-21        SeekFree            first version
********************************************************************************************************************/

#include "common.h"
#include "data.h"

void CSI_IRQHandler(void)
{
    CSI_DriverIRQHandler();     // SDKԴжϺ õĻص
    __DSB();                    // ͬ
}

void PIT_IRQHandler(void)
{
	/* ֵ/ */
    if(pit_flag_get(PIT_CH0))
    {
		encoder_1.encoder_get(&encoder_1);
		encoder_2.encoder_get(&encoder_2);
		encoder_3.encoder_get(&encoder_3);
		#ifndef SPEED
		current_1.current_get(&current_1);
		current_2.current_get(&current_2);
		current_3.current_get(&current_3);
		#endif
		imu660ra.gyro_get(&imu660ra);
		imu660ra.acc_get(&imu660ra);
		euler_angle_solve.euler_angle(&euler_angle_solve, imu660ra);
		displacement_solve.move_solve(&displacement_solve, encoder_1.wheel_speed, encoder_2.wheel_speed, encoder_3.wheel_speed, euler_angle_solve.yaw);
		rotate_euler_angle_solve.diff_yaw = displacement_solve.diff_yaw;
		rotate_euler_angle_solve.euler_angle(&rotate_euler_angle_solve, imu660ra);
		circle_euler_angle_solve.diff_yaw = displacement_solve.diff_yaw;
		circle_euler_angle_solve.euler_angle(&circle_euler_angle_solve, imu660ra);
		box_euler_angle_solve.diff_yaw = displacement_solve.diff_yaw;
		box_euler_angle_solve.euler_angle(&box_euler_angle_solve, imu660ra);
		gray_sensor.voltage_get(&gray_sensor);
		bat_voltage.voltage_get(&bat_voltage);
		
        pit_flag_clear(PIT_CH0);
    }
	/* ˶ */
    if(pit_flag_get(PIT_CH1))
    {
		switch(control_kind){
			case Angle2Inv2Speed:{ Angle2Inv2Speed_control(); break; }
			case CircleAngle2Inv2Speed:{ CircleAngle2Inv2Speed_control(); break; }
			case Track_X2Inv2Speed:{ Track_X2Inv2Speed_control(); break; }
			case Track_Y2Inv2Speed:{Track_Y2Inv2Speed_control(); break; }
			case Track_XY2Inv2Speed:{ Track_XY2Inv2Speed_control(); break; }
			case Fxxk_X2Inv2Speed:{ Fxxk_X2Inv2Speed_control(); break; }
			case Inv2Speed:{ Inv2Speed_control(); break; }
			case Speed:{ Speed_control(); break; }
			case PWM:{ PWM_control(); break; }
			case Stop:{ motor_1.motor_stop(&motor_1); motor_2.motor_stop(&motor_2); motor_3.motor_stop(&motor_3); break; }
		}
        pit_flag_clear(PIT_CH1);
    }
    if(pit_flag_get(PIT_CH2))
    {
		// ʱ
		zebra_path_timer.ticking(&zebra_path_timer);
        motor_debug_timer.ticking(&motor_debug_timer);
		rwr_timer.ticking(&rwr_timer);
		
        pit_flag_clear(PIT_CH2);
    }
    if(pit_flag_get(PIT_CH3))
    {
		// 
		key_action_get();
		// RWR
		dog_rwr.radar_scanning(&dog_rwr);
		dog_rwr.radar_tracking(&dog_rwr);
		dog_rwr.missile_launch(&dog_rwr);
        pit_flag_clear(PIT_CH3);
    }

    __DSB();
}

void LPUART1_IRQHandler(void)
{
    if(kLPUART_RxDataRegFullFlag && LPUART_GetStatusFlags(LPUART1))
    {
        // ж
		// AIͷ1ڽж
        extern void uart_rx_interrupt_handler_ai_camera_1();
		uart_rx_interrupt_handler_ai_camera_1();
		
//		wireless_vofa.justfloat_add(&wireless_vofa, 1, (float)detection_result.tool_raw);
//		wireless_vofa.justfloat_send(&wireless_vofa);
		
    #if DEBUG_UART_USE_INTERRUPT                        //  debug ж
        debug_interrupr_handler();                      //  debug ڽմ ݻᱻ debug λȡ
    #endif                                              // ޸ DEBUG_UART_INDEX δҪŵӦĴжȥ
    }
        
    LPUART_ClearStatusFlags(LPUART1, kLPUART_RxOverrunFlag);    // ɾ
}

void LPUART2_IRQHandler(void)
{
    if(kLPUART_RxDataRegFullFlag && LPUART_GetStatusFlags(LPUART2))
    {
        // ж
        // AIͷ2ڽж
		#ifndef AI_CAMERA_MERGE
		extern void uart_rx_interrupt_handler_ai_camera_2();
        uart_rx_interrupt_handler_ai_camera_2();
		#endif
	
//		wireless_vofa.justfloat_add(&wireless_vofa, 1, (float)22);
//		wireless_vofa.justfloat_send(&wireless_vofa);
    }
        
    LPUART_ClearStatusFlags(LPUART2, kLPUART_RxOverrunFlag);    // ɾ
}

void LPUART3_IRQHandler(void)
{
    if(kLPUART_RxDataRegFullFlag && LPUART_GetStatusFlags(LPUART3))
    {
        // ж
        
    }
        
    LPUART_ClearStatusFlags(LPUART3, kLPUART_RxOverrunFlag);    // ɾ
}

void LPUART4_IRQHandler(void)
{
    if(kLPUART_RxDataRegFullFlag && LPUART_GetStatusFlags(LPUART4))
    {
        // ж 
		// AIͷ0ڽж
		extern void uart_rx_interrupt_handler_ai_camera_0();
        uart_rx_interrupt_handler_ai_camera_0();
		
//		wireless_vofa.justfloat_add(&wireless_vofa, 1, (float)0);
//		wireless_vofa.justfloat_send(&wireless_vofa);
    }
        
    LPUART_ClearStatusFlags(LPUART4, kLPUART_RxOverrunFlag);    // ɾ
}

void LPUART5_IRQHandler(void)
{
    if(kLPUART_RxDataRegFullFlag & LPUART_GetStatusFlags(LPUART5))
    {
        // ж
        camera_uart_handler();
    }
        
    LPUART_ClearStatusFlags(LPUART5, kLPUART_RxOverrunFlag);    // ɾ
}

void LPUART6_IRQHandler(void)
{
    if(kLPUART_RxDataRegFullFlag & LPUART_GetStatusFlags(LPUART6))
    {
        // ж
        
    }
        
    LPUART_ClearStatusFlags(LPUART6, kLPUART_RxOverrunFlag);    // ɾ
}


void LPUART8_IRQHandler(void)
{
    if(kLPUART_RxDataRegFullFlag & LPUART_GetStatusFlags(LPUART8))
    {
        // ж
        wireless_module_uart_handler();
        
    }
        
    LPUART_ClearStatusFlags(LPUART8, kLPUART_RxOverrunFlag);    // ɾ
}


void GPIO1_Combined_0_15_IRQHandler(void)
{
    if(exti_flag_get(B0))
    {
        exti_flag_clear(B0);// жϱ־λ
    }
    
}


void GPIO1_Combined_16_31_IRQHandler(void)
{
    wireless_module_spi_handler();
    if(exti_flag_get(B16))
    {
        exti_flag_clear(B16); // жϱ־λ
    }

    
}

void GPIO2_Combined_0_15_IRQHandler(void)
{
    flexio_camera_vsync_handler();
    
    if(exti_flag_get(C0))
    {
        exti_flag_clear(C0);// жϱ־λ
    }

}

void GPIO2_Combined_16_31_IRQHandler(void)
{
    // -----------------* ToF INT ж Ԥжϴ *-----------------
    tof_module_exti_handler();
    // -----------------* ToF INT ж Ԥжϴ *-----------------
    
    if(exti_flag_get(C16))
    {
        exti_flag_clear(C16); // жϱ־λ
    }
    
}




void GPIO3_Combined_0_15_IRQHandler(void)
{

    if(exti_flag_get(D4))
    {
        exti_flag_clear(D4);// жϱ־λ
    }
}









/*
жϺƣöӦܵжϺ
Sample usage:ǰڶʱж
void PIT_IRQHandler(void)
{
    //־λ
    __DSB();
}
ǵýжϺ־λ
CTI0_ERROR_IRQHandler
CTI1_ERROR_IRQHandler
CORE_IRQHandler
FLEXRAM_IRQHandler
KPP_IRQHandler
TSC_DIG_IRQHandler
GPR_IRQ_IRQHandler
LCDIF_IRQHandler
CSI_IRQHandler
PXP_IRQHandler
WDOG2_IRQHandler
SNVS_HP_WRAPPER_IRQHandler
SNVS_HP_WRAPPER_TZ_IRQHandler
SNVS_LP_WRAPPER_IRQHandler
CSU_IRQHandler
DCP_IRQHandler
DCP_VMI_IRQHandler
Reserved68_IRQHandler
TRNG_IRQHandler
SJC_IRQHandler
BEE_IRQHandler
PMU_EVENT_IRQHandler
Reserved78_IRQHandler
TEMP_LOW_HIGH_IRQHandler
TEMP_PANIC_IRQHandler
USB_PHY1_IRQHandler
USB_PHY2_IRQHandler
ADC1_IRQHandler
ADC2_IRQHandler
DCDC_IRQHandler
Reserved86_IRQHandler
Reserved87_IRQHandler
GPIO1_INT0_IRQHandler
GPIO1_INT1_IRQHandler
GPIO1_INT2_IRQHandler
GPIO1_INT3_IRQHandler
GPIO1_INT4_IRQHandler
GPIO1_INT5_IRQHandler
GPIO1_INT6_IRQHandler
GPIO1_INT7_IRQHandler
GPIO1_Combined_0_15_IRQHandler
GPIO1_Combined_16_31_IRQHandler
GPIO2_Combined_0_15_IRQHandler
GPIO2_Combined_16_31_IRQHandler
GPIO3_Combined_0_15_IRQHandler
GPIO3_Combined_16_31_IRQHandler
GPIO4_Combined_0_15_IRQHandler
GPIO4_Combined_16_31_IRQHandler
GPIO5_Combined_0_15_IRQHandler
GPIO5_Combined_16_31_IRQHandler
WDOG1_IRQHandler
RTWDOG_IRQHandler
EWM_IRQHandler
CCM_1_IRQHandler
CCM_2_IRQHandler
GPC_IRQHandler
SRC_IRQHandler
Reserved115_IRQHandler
GPT1_IRQHandler
GPT2_IRQHandler
PWM1_0_IRQHandler
PWM1_1_IRQHandler
PWM1_2_IRQHandler
PWM1_3_IRQHandler
PWM1_FAULT_IRQHandler
SEMC_IRQHandler
USB_OTG2_IRQHandler
USB_OTG1_IRQHandler
XBAR1_IRQ_0_1_IRQHandler
XBAR1_IRQ_2_3_IRQHandler
ADC_ETC_IRQ0_IRQHandler
ADC_ETC_IRQ1_IRQHandler
ADC_ETC_IRQ2_IRQHandler
ADC_ETC_ERROR_IRQ_IRQHandler
PIT_IRQHandler
ACMP1_IRQHandler
ACMP2_IRQHandler
ACMP3_IRQHandler
ACMP4_IRQHandler
Reserved143_IRQHandler
Reserved144_IRQHandler
ENC1_IRQHandler
ENC2_IRQHandler
ENC3_IRQHandler
ENC4_IRQHandler
TMR1_IRQHandler
TMR2_IRQHandler
TMR3_IRQHandler
TMR4_IRQHandler
PWM2_0_IRQHandler
PWM2_1_IRQHandler
PWM2_2_IRQHandler
PWM2_3_IRQHandler
PWM2_FAULT_IRQHandler
PWM3_0_IRQHandler
PWM3_1_IRQHandler
PWM3_2_IRQHandler
PWM3_3_IRQHandler
PWM3_FAULT_IRQHandler
PWM4_0_IRQHandler
PWM4_1_IRQHandler
PWM4_2_IRQHandler
PWM4_3_IRQHandler
PWM4_FAULT_IRQHandler
Reserved171_IRQHandler
GPIO6_7_8_9_IRQHandler*/



