/**
 * main.c
 */
#include "F28x_Project.h"
#include "driverlib.h"
#include "device.h"

#include "MY_CLA.h"

// PI控制器结构体
typedef struct
{
    float kp;
    float ki;
    float error;
    float proportional;
    float integral;
    float integral_max;
    float integral_min;
    float output;
    float max_output;
    float min_output;
} PI_Controller_t;

// 参考值结构体
typedef struct
{
    float Vout_Ref_AC;        // 交流输出电压参考(瞬时值)
    float Vout_Ref_RMS;       // 输出电压有效值参考值
    float Vout_Ref_RMS_Temp;  // 软启动临时参考值
    float Vout_Ref_RMS_Target;// 输出目标有效值
    float I_ref;              // 外环输出的电流幅值参考
    float I_ref_instant;      // 电流瞬时参考值
} PI_Controller_Ref_t;

// 相位数据
typedef struct
{
    float Phase_Angle;        // 相位角(度)
    float Phase_Angle_Rad;    // 相位角(弧度)
} Phase_Data_t;

// ADC数据
typedef struct
{
    float Vout_AC;           // 输出电压瞬时值
    float Vin_AC;            // 输入电压瞬时值
    float IL_AC;             // 电感电流瞬时值
    float Vout_AC_Filter;    // 滤波后输出电压
    float Vin_AC_Filter;     // 滤波后输入电压
    float IL_AC_Filter;      // 滤波后电感电流
    float Vout_RMS;          // 输出电压有效值
    float Vin_RMS;           // 输入电压有效值
    float IL_RMS;            // 电感电流有效值
} ADC_Data_t;

// 原始ADC数据
typedef struct
{
    float vout_pos_voltage;
    float vout_neg_voltage;
    float vout_ac_voltage;
    float vin_pos_voltage;
    float vin_neg_voltage;
    float vin_ac_voltage;
    float biased_voltage;
    float current_voltage;
} ADC_RAW_Data_t;

// CLA数据段
#pragma DATA_SECTION(Voltage_PI, "CLADataLS0")
#pragma DATA_SECTION(Current_PI, "CLADataLS0")
#pragma DATA_SECTION(ADC_RAW_Data, "CLADataLS0")
#pragma DATA_SECTION(ADC_Data, "CLADataLS0")
#pragma DATA_SECTION(Ref, "CLADataLS0")
#pragma DATA_SECTION(Phase_Data, "CLADataLS0")

// CPU-CLA通信
#pragma DATA_SECTION(target_Vout_RMS, "CpuToCla1MsgRAM")     // CPU设置输出电压目标
#pragma DATA_SECTION(calculated_duty, "Cla1ToCpuMsgRAM")     // CLA计算的占空比
#pragma DATA_SECTION(duty_updated, "Cla1ToCpuMsgRAM")        // 更新标志
#pragma DATA_SECTION(system_status, "Cla1ToCpuMsgRAM")       // 系统状态

// 控制器变量
PI_Controller_t Voltage_PI;
PI_Controller_t Current_PI;
PI_Controller_Ref_t Ref;
Phase_Data_t Phase_Data;
ADC_RAW_Data_t ADC_RAW_Data;
ADC_Data_t ADC_Data;

// 常量定义
#define EPWM1_PERIOD 62500        // PWM周期值
#define PI 3.141592653589793f     // π值
#define Ts_adc 0.0001f            // 采样时间100us (10kHz)

volatile float target_Vout_RMS = 110.0f;      // 默认110V RMS
volatile float calculated_duty = 0.0f;        // CLA计算的占空比
volatile uint16_t duty_updated = 0;           // 更新标志
volatile uint16_t system_status = 0;          // 状态标志

volatile float theta;
volatile float sin_theta;
volatile float cos_theta;

/*
函数描述: ADC配置
说明: 无
*/
void MY_ADC_Init(void)
{

    // === GPIO39-43: ADC输入配置 ===
        GPIO_setPinConfig(GPIO_39_GPIO39);
        GPIO_setDirectionMode(39, GPIO_DIR_MODE_IN);
        GPIO_setPadConfig(39, GPIO_PIN_TYPE_STD);
        GPIO_setQualificationMode(39, GPIO_QUAL_SYNC);
        GPIO_setControllerCore(39, GPIO_CORE_CPU1);

        GPIO_setPinConfig(GPIO_40_GPIO40);
        GPIO_setDirectionMode(40, GPIO_DIR_MODE_IN);
        GPIO_setPadConfig(40, GPIO_PIN_TYPE_STD);
        GPIO_setQualificationMode(40, GPIO_QUAL_SYNC);
        GPIO_setControllerCore(40, GPIO_CORE_CPU1);

        GPIO_setPinConfig(GPIO_41_GPIO41);
        GPIO_setDirectionMode(41, GPIO_DIR_MODE_IN);
        GPIO_setPadConfig(41, GPIO_PIN_TYPE_STD);
        GPIO_setQualificationMode(41, GPIO_QUAL_SYNC);
        GPIO_setControllerCore(41, GPIO_CORE_CPU1);

        GPIO_setPinConfig(GPIO_42_GPIO42);
        GPIO_setDirectionMode(42, GPIO_DIR_MODE_IN);
        GPIO_setPadConfig(42, GPIO_PIN_TYPE_STD);
        GPIO_setQualificationMode(42, GPIO_QUAL_SYNC);
        GPIO_setControllerCore(42, GPIO_CORE_CPU1);

        GPIO_setPinConfig(GPIO_43_GPIO43);
        GPIO_setDirectionMode(43, GPIO_DIR_MODE_IN);
        GPIO_setPadConfig(43, GPIO_PIN_TYPE_STD);
        GPIO_setQualificationMode(43, GPIO_QUAL_SYNC);
        GPIO_setControllerCore(43, GPIO_CORE_CPU1);
    // === ADC基础配置 ===
        ADC_setPrescaler(ADCA_BASE, ADC_CLK_DIV_4_0);
        ADC_setInterruptPulseMode(ADCA_BASE, ADC_PULSE_END_OF_CONV);
        ADC_setMode(ADCA_BASE, ADC_RESOLUTION_12BIT, ADC_MODE_SINGLE_ENDED);
        ADC_enableConverter(ADCA_BASE);

        ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER0, ADC_TRIGGER_EPWM2_SOCA,ADC_CH_ADCIN0, 20);
        ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER1, ADC_TRIGGER_EPWM2_SOCA,ADC_CH_ADCIN1, 20);
        ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER2, ADC_TRIGGER_EPWM2_SOCA,ADC_CH_ADCIN2, 20);
        ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER3, ADC_TRIGGER_EPWM2_SOCA,ADC_CH_ADCIN3, 20);
        ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER4, ADC_TRIGGER_EPWM2_SOCA,ADC_CH_ADCIN4, 20);

        //ADC_setBurstModeConfig(ADCA_BASE, ADC_TRIGGER_EPWM2_SOCA, 5);  // 突发5个SOC
        //ADC_enableBurstMode(ADCA_BASE);                                // 使能突发模式

        ADC_setInterruptSource(ADCA_BASE, ADC_INT_NUMBER1, ADC_SOC_NUMBER4); // INT1在SOC4完成后触发
        ADC_enableInterrupt(ADCA_BASE, ADC_INT_NUMBER1);                     // 使能INT1
        ADC_disableContinuousMode(ADCA_BASE, ADC_INT_NUMBER1);               // 禁用连续模式
        ADC_clearInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1);                // 清除INT1标志位

        DELAY_US(1000);
}


/*
函数描述：Timer0初始化
说明：4秒中断
*/
void MY_Timer0_Init(void)
{
    // === TIMER0 配置 : 定时 ===
    CPUTimer_stopTimer(CPUTIMER0_BASE);
    ConfigCpuTimer(&CpuTimer0, 200, 4000000);
    CPUTimer_reloadTimerCounter(CPUTIMER0_BASE);
    CPUTimer_setEmulationMode(CPUTIMER0_BASE,CPUTIMER_EMULATIONMODE_STOPAFTERNEXTDECREMENT);
    CPUTimer_enableInterrupt(CPUTIMER0_BASE);
    CPUTimer_startTimer(CPUTIMER0_BASE);
}

/*
函数描述：Timer1初始化
说明：8秒中断
*/
void MY_Timer1_Init(void)
{
    // === TIMER0 配置 : 定时 ===
    CPUTimer_stopTimer(CPUTIMER1_BASE);
    ConfigCpuTimer(&CpuTimer1, 200, 8000000);
    CPUTimer_reloadTimerCounter(CPUTIMER1_BASE);
    CPUTimer_setEmulationMode(CPUTIMER1_BASE,CPUTIMER_EMULATIONMODE_STOPAFTERNEXTDECREMENT);
    CPUTimer_enableInterrupt(CPUTIMER1_BASE);
    CPUTimer_startTimer(CPUTIMER1_BASE);
}


int main(void)
{

    // 步骤1: 初始化设备时钟和外设
        Device_init();
        Device_initGPIO();

        // 步骤2: 初始化定时器
        InitCpuTimers();

        // 步骤3: 中断初始化
    //    Interrupt_initModule();
    //    Interrupt_initVectorTable();
        DINT;
        InitPieCtrl();
        IER = 0x0000;
        IFR = 0x0000;
        InitPieVectTable();

        // 步骤4: 初始化各个外设模块
        MY_ADC_Init();                //ADC配置
        MY_CLA_Init();                // CLA配置

        // 步骤6: 启用CPU中断
        Interrupt_enableInCPU(INTERRUPT_CPU_INT1);  // 使能PIE组1 (包含ADCA1和TIMER0)
        Interrupt_enableInCPU(INTERRUPT_CPU_INT3);  // 使能PIE组3 (包含EPWM1)
    //    Interrupt_enableInCPU(INTERRUPT_CPU_INT4);  // 使能PIE组4 (包含ECAP1)
    //    Interrupt_enableInCPU(INTERRUPT_CPU_INT11); // 使能PIE组11(包含CLA)
    //    IER |= M_INT1;  // 使能PIE组1 (包含ADCA1和TIMER0)
    //    IER |= M_INT3;  // 使能PIE组3 (包含EPWM1)
        //IER |= M_INT4;  // 使能PIE组4 (包含ECAP1)
        //IER |= M_INT11; // 使能PIE组11(包含CLA)
        //Interrupt_enableGlobal();
        EINT;
        ERTM;

        while(1)
        {
            asm(" NOP");
        }
}
