/**************************** (C) COPYRIGHT 2017 Fortiortech shenzhen *****************************
* File Name          : Interrupt.c
* Author             : Vina Peng,Fortiortech  Appliction Team
* Version            : V1.0
* Date               : 2017-12-26
* Description        : This file contains all the interrupt function used for Motor Control.
***************************************************************************************************
* All Rights Reserved
**************************************************************************************************/


/* Includes -------------------------------------------------------------------------------------*/
// #include <Interrupt.h>
#include <FU68xx_2.h>
#include <Myproject.h>
#include <Customer_Debug.h>
uint16 xdata spidebug[4] = { 0 };


/*-------------------------------------------------------------------------------------------------
    Function Name : void FO_INT(void)
    Description   : FO_INT interrupt，硬件FO过流保护，关断输出，中断优先级最高
    Input         : 无
		Output        : 无
-------------------------------------------------------------------------------------------------*/
void FO_INT(void) interrupt 1                                                   // 硬件FO过流中断，关闭输出
{
//    FaultProcess();                                                             // 关闭输出
//    mcFaultSource = FaultHardOVCurrent;                                         // 硬件过流保护
//    mcState       = mcFault;                                                    // 状态为mcFault
    
    dimmer_cnt = 0;
    IF0           = 0;                                                          // clear P00 interrupt flag
}
/*-------------------------------------------------------------------------------------------------
    Function Name : void CMP_ISR(void)
    Description   : CMP3：硬件比较器过流保护，关断输出，中断优先级最高
										CMP0/1/2：顺逆风判断
    Input         : 无
		Output        : 无
-------------------------------------------------------------------------------------------------*/
void CMP_ISR(void) interrupt 7
{
    if(ReadBit(CMP_SR, CMP3IF))
    {
        if(mcState!=mcPosiCheck)
        {
            FaultProcess();                                                     // 关闭输出
            mcFaultSource=FaultHardOVCurrent;                                   // 硬件过流保护
            mcState = mcFault;                                                  // 状态为mcFault
        }
        else
        {
            MOE     = 0;                                                                        // 关闭MOE
            RPDPara.InsetCount[RPDPara.injecttimes]  = TIM2__CNTR;                              // 将定时器2的计数值赋值给数组
            RPDPara.DetectCount[RPDPara.injecttimes] = RPDPara.InsetCount[RPDPara.injecttimes]; // 两组数据，一组用于观察原始数据，一组用于处理数据
            TIM2__CNTR                               = 0;                                       // TIM2计数器值清零
            RPDPara.injecttimes++;                                                              // RPD注入拍数累加

        }
        ClrBit(CMP_SR, CMP3IF);
    }

    #if (FRDetectMethod == BEMFMethod)
        //通过BEMF做顺风启动功能
        BEMFDetectFunc();
    #endif
}

/*---------------------------------------------------------------------------*/
/* Name     :   void TIM2_INT(void) interrupt 4
/* Input    :   NO
/* Output   :   NO
/* Description:	正反转检测(RSD)
/*---------------------------------------------------------------------------*/
void TIM2_INT(void) interrupt 4
{
    if(ReadBit(TIM2_CR1, T2IR))
    {
        ClrBit(TIM2_CR1, T2IR);
    }
    if(ReadBit(TIM2_CR1, T2IP))
    {
        #if (FRDetectMethod == RSDMethod)
            RSDFRDetect();//RSD正反转检测
        #endif

        ClrBit(TIM2_CR1, T2IP);
    }
    if(ReadBit(TIM2_CR1, T2IF))//溢出中断,用于判断静止,时间为349ms。
    {
        #if (FRDetectMethod == RSDMethod)
            RSDDetect.RSDState=Static;
            RSDDetect.RSDFlag=1;
        #endif

        ClrBit(TIM2_CR1, T2IF);
    }
}
/*---------------------------------------------------------------------------*/
/* Name     :   void TIM23_INT(void) interrupt 9
/* Input    :   NO
/* Output   :   NO
/* Description: Capture PWM ，中断优先级第二，高于FOC中断，用于PWM调速
/*---------------------------------------------------------------------------*/
void TIM3_INT(void) interrupt 9
{
    if(ReadBit(TIM3_CR1, T3IR))
    {
        ClrBit(TIM3_CR1, T3IR);
    }
    if(ReadBit(TIM3_CR1, T3IP))//周期中断
    {
        RFValue();
        ClrBit(TIM3_CR1, T3IP);
    }
    if(ReadBit(TIM3_CR1, T3IF))
    {
		ClrBit(TIM3_CR1, T3EN);
		TIM3__ARR = 0;							                               			// TIM3 Period = 100us				
		TIM3__DR = 0;																	// 1个PWM产生匹配
		TIM3__CNTR = 0;
		
		IRScan.BitCnt = 0;
		IRScan.ByteValue = 0;		
		IRScan.CurrentDataCode = 0;		
		IRScan.OldDataCodeMixTimes =0;													//重复按键清零
		IRScan.OverFlowStatus=0;
	
		IRScan.TempDataCode[0]=0;
		IRScan.TempDataCode[1]=0;	
		IRScan.TempDataCode[2]=0;
		IRScan.TempDataCode[3]=0;		
		IRScan.DataCodeNum = 0;
        ClrBit(TIM3_CR1, T3IF);
		SetBit(TIM3_CR1, T3EN);
    }
}
/*---------------------------------------------------------------------------*/
/* Name     :   void FOC_INT(void) interrupt 3
/* Input    :   NO
/* Output   :   NO
/* Description: FOC中断(Drv中断),每个载波周期执行一次，用于处理响应较高的程序，中断优先级第二。DCEN开了就会产生中断。
/*---------------------------------------------------------------------------*/
void FOC_INT(void) interrupt 3
{
    if(ReadBit(DRV_SR, DCIF))                                 // 比较中断
    {
        APP_DIV(); 	//启动除法器，避免与过调值中的除法冲突

        Fault_Overcurrent(&mcCurVarible); // 软件过流保护.约30us

        #if ((FRDetectMethod == FOCMethod) && (TailWind_Mode == TailWind))
            TailWindSpeedDetect();	//顺逆风检测
        #endif

        #if defined (SPI_DBG_SW)	//软件调试模式
            spidebug[0] = 0;
            spidebug[1] = 0;
            spidebug[2] = 0;
            spidebug[3] = SOFT_SPIDATA3;
        #endif

        ClrBit(DRV_SR, DCIF);
    }
}

/*---------------------------------------------------------------------------*/
/* Name     :   void TIM4S_INT(void) interrupt 10
/* Input    :   NO
/* Output   :   NO
/* Description: 1ms定时器中断（SYS TICK中断），用于处理附加功能，如控制环路响应、各种保护等。中断优先级低于FO中断和FOC中断。
/*---------------------------------------------------------------------------*/
void TIM4S_INT(void) interrupt 10
{
    if(ReadBit(TIM4_CR1, T4IF))//定时器4中断  调光
    {
        Dimmer_Control();
        GP37 = ~GP37;
        ClrBit(TIM4_CR1, T4IF);
    }
    
    if(ReadBit(DRV_SR, SYSTIF))          // SYS TICK中断
    {
        SetBit(ADC_CR, ADCBSY);          //使能ADC的DCBUS采样
        /****功率滤波*****/
        if(mcState == mcRun)
        {
            mcFocCtrl.CurrentPower = FOC__POW << 1;
            mcFocCtrl.Powerlpf     = LPFFunction(mcFocCtrl.CurrentPower,mcFocCtrl.Powerlpf,10);//注意低通滤波器系数范围为0---127
        }

        /****速度滤波、反电动势滤波*****/
        if((mcState != mcInit) && (mcState != mcReady))
        {
            mcFocCtrl.SpeedFlt = LPFFunction(FOC__EOME, mcFocCtrl.SpeedFlt, 10);//注意低通滤波器系数范围为0---127
            mcFocCtrl.EsValue  = LPFFunction(FOC__ESQU,mcFocCtrl.EsValue,10);
        }
        else
        {
            mcFocCtrl.SpeedFlt = 0;
        }

		//正反转控制
		FRControl();
		
		//红外遥控处理
		IRScanControl();
			
		NatureWind();

		AutoPowerControl();
		
        Speed_response();        //环路响应，如速度环、转矩环、功率环等

        #if (StartONOFF_Enable)
            ONOFF_Starttest(&ONOFFTest);
        #endif

        /*****DCbus的采样获取值并滤波******/
        AdcSampleValue.ADCDcbus = ADC2_DR;
        mcFocCtrl.mcDcbusFlt    = LPFFunction((AdcSampleValue.ADCDcbus<<3),mcFocCtrl.mcDcbusFlt,12);

        /*****故障保护函数功能，如过欠压保护、启动保护、缺相、堵转等********/
        Fault_Detection();
		
		//刹车电流归零检测
		CurrentZeroDetect();

       //电压在正常值以上时才允许控制蜂鸣器和继电器，防止掉电时电压波动影响
		//if(mcFocCtrl.mcDcbusFlt > UNDER_RECOVER_VALUE)
		{
			//LED灯控
			LEDDisplay();
			
			//蜂鸣器TIM2 P10
			BuzzerScan();
		}

        /****模拟调速****/
        #if (SPEED_MODE == SREFMODE)
            VSPSample();
        #endif

        /********睡眠模式*******/
//    Sleepmode();

        /*****电机状态机的时序处理*****/
        if(mcFocCtrl.State_Count > 0)      mcFocCtrl.State_Count--;
        if(BEMFDetect.BEMFTimeCount > 0)   BEMFDetect.BEMFTimeCount--;
        if(RSDDetect.RSDCCWSBRCnt > 0)     RSDDetect.RSDCCWSBRCnt--;
		if(Time.PowerUpTimeCount<60000)    Time.PowerUpTimeCount++;  //上电一分钟计时
		
        #if (FRDetectMethod==FOCMethod)
            FOCTailWindTimeLimit();
        #endif
		
		if(mcState == mcRun)
		{
			//Run状态计时
			if(Time.mcRunStateCount<60000)
			{
				Time.mcRunStateCount++;
			}
			//正反转自动测试
			if(Time.mcRunStateCount == 6000)
			{
				IRControl.FlagFR=1;
//				ONOFFTIMES++;
			}
		}
		else
		{
			Time.mcRunStateCount=0;
		}
		
        /*****电机启动爬坡函数处理*****/
        StarRampDealwith();
        
        ClrBit(DRV_SR, SYSTIF);                                      // 清零标志位
    }
}

/*---------------------------------------------------------------------------*/
/* Name     :   void USART_INT(void) interrupt 12
/* Input    :   NO
/* Output   :   NO
/* Description: 串口中断，中断优先级最低，用于接收调速信号,无中断插入时8us
/*---------------------------------------------------------------------------*/
void USART_INT(void)  interrupt 12
{
    if(RI == 1)
    {
        RI = 0;
        Uart.Uredata= UT_DR;            //读接收数据
    }
}
/*---------------------------------------------------------------------------*/
/* Name     :   void EXTERN_INT(void) interrupt 2
/* Input    :   NO
/* Output   :   NO
/* Description: 睡眠唤醒
/*---------------------------------------------------------------------------*/
void EXTERN_INT(void) interrupt 2
{
    if(SleepSet.SleepFlag)
    {
        SleepSet.SleepFlag = 0;
        SleepSet.SleepEn   = 1;
    }

    ClrBit(P1_IF, P11);                    // 清零P10标志位
}


/* Private variables ----------------------------------------------------------------------------*/
void INT0(void) interrupt 0
{
}
void INT5(void) interrupt 5
{
}
void INT6(void) interrupt 6
{
}
void INT8(void) interrupt 8
{
}
void INT11(void) interrupt 11
{
}
void INT13(void) interrupt 13
{
}
void INT14(void) interrupt 14
{
}
void INT15(void) interrupt 15
{
}


