/**
    @copyright None
    @file      protect_def.h
    @author    Comment Vivre
    @date      2025-12-29
    @brief     None
*/
#ifndef __PROTECT_DEF_H_
#define __PROTECT_DEF_H_

#include <FU65_72.h>



// 单相缺相计数更新宏：保持原有判定逻辑不变
#define UPDATE_PHASE_LOSS_CNT(max_self, max_o1, max_o2, cnt) \
    do { \
        if ((((max_self) > ((max_o1) << 1)) || ((max_self) > ((max_o2) << 1))) &&\
            ((max_self) > PHASE_NOLOAD_CURR)) \
        { cnt++; } \
        else if (cnt) \
        { cnt--; } \
    } while (0)

/**
    @brief 故障恢复延时计数宏
    @param cnt  对应故障的延时计数器变量

    @note  用于故障恢复逻辑，当检测条件已恢复正常后，
          不会立刻清除故障，而是等待计数超过
          PROTECT_RECOVER_TIME 后才解除故障，
          以避免抖动恢复。
*/
#define RECOVER_DELAY(cnt) \
    do {                                \
        cnt++; \
        if (cnt > PROTECT_RECOVER_TIME) { \
            cnt = 0; \
            FaultSource = SYS_NO_FAULT; \
        }                               \
    } while(0)


/**
    @brief 通用的故障计数检测宏
    @param cnt        计数器变量
    @param cond       检测条件 (true 表示异常)
    @param threshold  连续超过阈值次数才判定故障
    @param faultCode  触发的故障源代码
*/
#define FAULT_COUNT_UPDOWN(cnt, cond, threshold, faultCode) \
    do { \
        if (cond) { \
            if (++(cnt) > (threshold)) { \
                cnt = 0; \
                FaultSource = (faultCode); \
            } \
        } else if (cnt) { \
            cnt--; \
        } \
    } while (0)


typedef enum
{
    SYS_NO_FAULT = 0x00,
    // 过流类
    HARD_OVER_CURR = 0x11,
    SOFT_OVER_CURR = 0x12,
    FAN_OVER_CURR = 0x13,
    // 自检类
    FCT_BUS_OFFSET = 0x21,
    FCT_LOSS_PHASE = 0x22,
    FCT_SHORT_UP_BRIDGE = 0x23,
    FCT_SHORT_DOWN_BRIDGE = 0x24,
    FCT_SHORT_PHASE = 0x25,
    // 运行类
    MOTOR_STALL = 0x31,
    OVER_POWER = 0x32,
    // 过欠压类
    VDC_OVER = 0x41,
    VDC_UNDER = 0x42,
    MCU_LVM = 0x43,
    // 温度类
    TEMPER_NTC = 0x51,
    TEMPER_MOTOR = 0x52,
    TEMPER_IPM = 0x53,
    TEMPER_TSD = 0x54,
    // 通讯类
    LOSS_UART = 0x61,
    LOSS_PWM = 0x62
} Fault_Type_e;

extern Fault_Type_e xdata FaultSource;


typedef struct
{
    // 检测数据
    uint16_t BackEMF;                       // 反电动势
    int16_t ActualSpeed;                    // 实际速度
    uint16_t BusVoltage;                    // 母线电压
    uint16_t NTCTemper;                     // NTC温度
    int16_t BusCurr;                        // 估算母线电流
    uint16_t Power;                         // 估算功率
    // 三相电流极值
    uint16_t MaxIa;
    uint16_t MaxIb;
    uint16_t MaxIc;
    
    // 保护检测计数
    struct
    {
        uint16_t DelayPhase;
        uint16_t LossPhase;
        uint16_t PhaseA;
        uint16_t PhaseB;
        uint16_t PhaseC;
        uint16_t CloseLoop;
        uint16_t DelayStall;
        uint16_t EsValue;
        uint16_t StallSpeed;
        uint16_t OverVolt;
        uint16_t UnderVolt;
        uint8_t OverCurr;
        uint16_t OverTemper;
        uint16_t OverPower;
        uint16_t PwmLoss;
        uint16_t UartLoss;
    } CheckCnt;
    // 恢复计数
    struct
    {
        uint16_t Stall;
        uint16_t OverVolt;
        uint16_t UnderVolt;
        uint16_t OverCurr;
        uint16_t OverTemper;
        uint16_t OverPower;
        uint16_t PhaseLoss;
        uint16_t PWMLoss;
        uint16_t UartLoss;
    } RecoverCnt;
    uint16_t DelayRecover;
} Fault_Check_t;

extern Fault_Check_t xdata faultCheck;





#endif