#include <MyProject.h>

// ------------------------------------------------------------------------------------------------------------------
// 1. PI控制器初始化

/**
    @function     HW_Zero_PI_Init
    @brief        0号硬件PI控制器初始化
    @date         2025-11-01
*/
void HW_Zero_PI_Init(void)
{
    PI0_KP = SKP;
    PI0_KI = SKI;
    PI0_EK = 0;
    PI0_EK1 = 0;
    PI0_UKMAX = SOUTMAX;
    PI0_UKMIN = SOUTMIN;
    PI0_UKH = 0;
    PI0_UKL = 0;
}


/**
    @function     HW_One_PI_Init
    @brief        1号硬件PI控制器初始化
    @date         2025-11-01
*/
void HW_One_PI_Init(void)
{
    PI1_KP = 0;
    PI1_KI = 0;
    PI1_EK = 0;
    PI1_EK1 = 0;
    PI1_UKMAX = 0;
    PI1_UKMIN = 0;
    PI1_UKH = 0;
    PI1_UKL = 0;
}

/**
    @function     HW_Two_PI_Init
    @brief        2号硬件PI控制器初始化
    @date         2025-11-01
*/
void HW_Two_PI_Init(void)
{
    PI2_KP = 0;
    PI2_KI = 0;
    PI2_EK = 0;
    PI2_EK1 = 0;
    PI2_UKMAX = 0;
    PI2_UKMIN = 0;
    PI2_UKH = 0;
    PI2_UKL = 0;
}

/**
    @function     HW_Three_PI_Init
    @brief        3号硬件PI控制器初始化
    @date         2025-11-01
*/
void HW_Three_PI_Init(void)
{
    PI3_KP = 0;
    PI3_KI = 0;
    PI3_EK = 0;
    PI3_EK1 = 0;
    PI3_UKMAX = 0;
    PI3_UKMIN = 0;
    PI3_UKH = 0;
    PI3_UKL = 0;
}
// ------------------------------------------------------------------------------------------------------------------
// 2. PI计算
/**
    @function     HW_Zero_Calc
    @brief        0号PI计算
    @param[in]    Xn0: [输入/出]
    @return       PI计算值
    @date         2025-11-01
*/
int16_t HW_Zero_Calc(int16_t Xn0)
{
    PI0_EK = Xn0;
    SMDU_RunBlock(0, PI);
    return PI0_UKH;
}

/**
    @function     HW_One_Calc
    @brief        1号PI计算
    @param[in]    Xn0: [输入/出]
    @return       PI计算值
    @date         2025-11-01
*/
int16_t HW_One_Calc(int16_t Xn0)
{
    PI1_EK = Xn0;
    SMDU_RunBlock(1, PI);
    return PI1_UKH;
}

/**
    @function     HW_Two_Calc
    @brief        2号PI计算
    @param[in]    Xn0: [输入/出]
    @return       PI计算值
    @date         2025-11-01
*/
int16_t HW_Two_Calc(int16_t Xn0)
{
    PI2_EK = Xn0;
    SMDU_RunBlock(2, PI);
    return PI2_UKH;
}

/**
    @function     HW_Three_Calc
    @brief        3号PI计算
    @param[in]    Xn0: [输入/出]
    @return       PI计算值
    @date         2025-11-01
*/
int16_t HW_Three_Calc(int16_t Xn0)
{
    PI3_EK = Xn0;
    SMDU_RunBlock(3, PI);
    return PI3_UKH;
}

// ------------------------------------------------------------------------------------------------------------------
// 3.LPF 低通滤波计算

/**
    @function     LPF_Zero_Update
    @brief        0号低通滤波计算
    @param[in]    Xn1: [输入/出]
**           Xn0: [输入/出]
**           K: [输入/出]
    @return       滤波结果
    @date         2025-11-03
*/
int16_t LPF_Zero_Update(int16_t Xn1, int16_t Xn0, int16_t K)
{
    LPF0_K = K << 8;
    LPF0_X = Xn1;
    LPF0_YH = Xn0;
    SMDU_RunBlock(0, LPF);
    return LPF0_YH;
}

/**
    @function     LPF_One_Update
    @brief        1号低通滤波计算
    @param[in]    Xn1: [输入/出]
**           Xn0: [输入/出]
**           K: [输入/出]
    @return       滤波结果
    @date         2025-11-03
*/
int16_t LPF_One_Update(int16_t Xn1, int16_t Xn0, int16_t K)
{
    LPF1_K = K << 8;
    LPF1_X = Xn1;
    LPF1_YH = Xn0;
    SMDU_RunBlock(1, LPF);
    return LPF1_YH;
}

/**
    @function     LPF_Two_Update
    @brief        2号低通滤波计算
    @param[in]    Xn1: [输入/出]
**           Xn0: [输入/出]
**           K: [输入/出]
    @return       滤波结果
    @date         2025-11-03
*/
int16_t LPF_Two_Update(int16_t Xn1, int16_t Xn0, int16_t K)
{
    LPF2_K = K << 8;
    LPF2_X = Xn1;
    LPF2_YH = Xn0;
    SMDU_RunBlock(2, LPF);
    return LPF2_YH;
}

/**
    @function     LPF_Three_Update
    @brief        2号低通滤波计算
    @param[in]    Xn1: [输入/出]
**           Xn0: [输入/出]
**           K: [输入/出]
    @return       滤波结果
    @date         2025-11-03
*/
int16_t LPF_Three_Update(int16_t Xn1, int16_t Xn0, int16_t K)
{
    LPF3_K = K << 8;
    LPF3_X = Xn1;
    LPF3_YH = Xn0;
    SMDU_RunBlock(3, LPF);
    return LPF3_YH;
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Atan_Us_MDU
    Description    : XX-XX-XX
    Date           : 2021-11-06
    Parameter      : Xn1: [输入/出]
**                 Xn0: [输入/出]
**                 K: [输入/出]
    ------------------------------------------------------------------------------------------------- */
int16 Atan_Us_MDU(int16 Xn1, int16 Xn0)
{
    SCAT1_COS = Xn1;
    SCAT1_SIN = Xn0;
    SCAT1_THE = 0;
    SMDU_RunBlock(1, ATAN);
    return SCAT1_RES1;
}

/*  -------------------------------------------------------------------------------------------------
    Function Name  : Atan_Us_MDU
    Description    : XX-XX-XX
    Date           : 2021-11-06
    Parameter      : Xn1: [输入/出]
**                       Xn0: [输入/出]
**                       Yn:  [输入/出]
    ------------------------------------------------------------------------------------------------- */
int16 DivQ_L_MDU(uint16 Xn1, uint16 Xn0, uint16 Yn)
{
    DIV0_DAH = Xn1;
    DIV0_DAL = Xn0;
    DIV0_DB  = Yn;
    SMDU_RunBlock(0, DIV);
    return DIV0_DQL;
}

int16 DivQ_L_MDU2(uint16 Xn1, uint16 Xn0, uint16 Yn)
{
    DIV1_DAH = Xn1;
    DIV1_DAL = Xn0;
    DIV1_DB  = Yn;
    SMDU_RunBlock(1, DIV);
    return DIV1_DQL;
}

/**
    @brief       计算Is
    @param[in]   i_alp: alp轴电流
    @param[in]   i_bet: bet轴电流
    @return      is
    @date        2022-07-29
*/
uint16 Sqrt_alpbet(int16 i_alp, int16 i_bet)
{
    SCAT2_COS    =   i_alp;
    SCAT2_SIN    =   i_bet;
    SMDU_RunBlock(2, ATAN);
    return SCAT2_RES1;
}

/**
    @brief       乘法器
    @param[in]   Xn1: 乘数0(16位
    @param[in]   Xn2: 乘数1(16位)
    @return      Yn:  结果
    @date        2022-11-25
*/

int16 MULU_H_MDU(uint16 Xn0, uint16 Xn1)
{
    MUL3_MA = Xn0;
    MUL3_MB = Xn1;
    SMDU_RunBlock(3, UMUL);
    return  MUL3_MCH;
}

void MULS_H_MDU(int16 Xn0, int16 Xn1, uint16 xdata * lusResult32)
{
    MUL0_MA = Xn0;
    MUL0_MB = Xn1;
    SMDU_RunBlock(0, SMUL);
    *lusResult32 = MUL0_MCH;
    *(lusResult32 + 1) = MUL0_MCL;
}


