#include <MyProject.h>

int16 idata Power_Currt;

extern bool data TickFlag;

/**
    @brief        低于预警中断与过温中断
    @brief        开启低压检测中断后,MCU会对输入电压进行监测,当输入电压低于设定值，则会触发中断
    @brief        开启过温保护中断后,MCU会对内部结温进行监测,当内部结温高于设定值，则会触发中断
    @date         2022-07-14
*/
void LVW_TSD_INT(void) interrupt 0
{
    if (ReadBit(LVSR, LVWIF))
    {
        if (ReadBit(LVSR, LVWF))
        {
            FaultSource = MCU_LVM;
            ClrBit(LVSR, LVWF);
        }
        
        ClrBit(LVSR, LVWIF);
    }
    
    if (TSDIF)
    {
        if (ReadBit(LVSR, TSDF))
        {
            ClrBit(LVSR, TSDF);
        }
        
        TSDIF = 0;
    }
}

/**
    @brief        外部中断0
    @brief        一般用于响应IPM的FO过流信号
    @date         2022-07-14
*/
void EXTERN0_INT(void) interrupt 1                  //  外部中断0
{
    if (IF0)
    {
        IF0 = 0;                                // clear P00 interrupt flag
    }
}


/**
    @brief        FOC中断(Drv中断),每个载波周期执行一次，用于处理响应较高的程序，中断优先级第二
    @date         2022-07-14
*/
void DRV_ISR(void) interrupt 3
{
    if (ReadBit(DRV_SR, DCIF))    // 比较中断
    {
        SetReg(DRV_SR, 0xFF, SYSTIE | FGIF | DCIM1 | SYSTIF);
    }
}



/**
    @function     TIM3_INT_Handle
    @brief        定时器2中断
    @date         2025-12-24
*/
void TIM3_INT_Handle(void) interrupt 9
{
    if (ReadBit(TIM3_CR1, T3IR))
    { ClrBit(TIM3_CR1, T3IR); }
    
    if (ReadBit(TIM3_CR1, T3IP))
    {
        if (!inputCapture.UpdateFlag)
        {
            inputCapture.TimerARR = TIM3__ARR;
            inputCapture.TimerDR = TIM3__DR;
            inputCapture.UpdateFlag = 1;
        }
        
        ClrBit(TIM3_CR1, T3IP);
    }
    
    if (ReadBit(TIM3_CR1, T3IF))
    {
        if (!inputCapture.UpdateFlag)
        {
            if (GP11)
            {
                inputCapture.TimerARR = 60000;
                inputCapture.TimerDR = 60000;
            }
            else
            {
                inputCapture.TimerARR = 60000;
                inputCapture.TimerDR = 0;
            }
            
            inputCapture.UpdateFlag = 1;
        }
        
        ClrBit(TIM3_CR1, T3IF);
    }
}



/**
    @brief        滴答定时器，默认用于产生1ms定时间隔
    @date         2022-07-14
*/
void SYStick_INT(void) interrupt 10
{
    if (ReadBit(DRV_SR, SYSTIF))          // SYS TICK中断
    {
        TickFlag = true;
        
        if (mcFocCtrl.State_Count > 0)
        { mcFocCtrl.State_Count--; }
        
        SetReg(DRV_SR, 0xFF, SYSTIE | FGIF | DCIF | DCIM1);
    }
}



/**
    @brief        比较器硬件过流保护，该中断仅提供 故障码 赋值,用于状态机的切换。
                 需要开启比较器CMP3  发生过流 自动清除MOE功能
    @date         2022-07-14
*/

void CMP3_INT(void)  interrupt 12
{
    if (ReadBit(CMP_SR, CMP3IF))
    { ClrBit(CMP_SR, CMP3IF); }
}

void UART2_INT(void)  interrupt 14
{
}
