/*
 * interrupts.c
 *
 *  Created on: 21 авг. 2023 г.
 *      Author: seklyuts
 */

#include <pwm_init.h>
#include "f28x_project.h"
#include "frm_uart.h"
#include "gpio_init.h"
#include "i2c_init.h"
#include "timer_base.h"
#include "pwm_interrupts.h"



volatile uint16_t AutoChange = 0;
volatile uint16_t PWM_out = 2500;
//volatile uint16_t PWM_motor = PERIOD_2;
uint16_t Fault = 0, Fault_fix = 0, Ready = 0, Ready_Fix = 0;
uint16_t counter1s=0;
uint16_t FaultABC = 0, FaultABCFix = 0;
uint16_t PwmFlagStartADC = 0;

typedef struct
{
    int16_t UA;
    int16_t UB;
    int16_t UC;
}strPWMABC;

volatile strPWMABC PWM_motor = {PERIOD_2, PERIOD_2, PERIOD_2};






//
// epwm1_isr - EPWM1 ISR
//
__interrupt void epwm1_isr(void)
{
//    if(AutoChange) pwm_AutoChange(1);
//    else
    EPwm1Regs.CMPA.bit.CMPA = PWM_out;

    //
    // Clear INT flag for this timer
    //
    EPwm1Regs.ETCLR.bit.INT = 1;

    MainTimerBaseTimeoutInc();

    //
    // Acknowledge this interrupt to receive more interrupts from group 3
    //
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}

//
// epwm2_isr - EPWM2 ISR
//

__interrupt void epwm2_isr(void)
{
//    Gpio57out(1);

//    EALLOW;
//    Sdfm1Regs.SDDFPARM4.bit.FEN  = 1;
//    EDIS;

//    GpioDataRegs.GPADAT.bit.GPIO0 = 1;

    //
    // Clear INT flag for this timer
    //
    EPwm2Regs.ETCLR.bit.INT = 1;
    TimerBaseTimeoutInc();
    FMSTR_enable_set();

    //
    // Acknowledge this interrupt to receive more interrupts from group 3
    //
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
//    GpioDataRegs.GPADAT.bit.GPIO0 = 0;
//    Gpio57out(0);

}

//
// epwm3_isr - EPWM3 ISR
//
__interrupt void epwm3_isr(void)
{

    EPwm3Regs.CMPA.bit.CMPA = PERIOD_MOTOR - PWM_motor.UB;

    //
    // Clear INT flag for this timer
    //
    EPwm3Regs.ETCLR.bit.INT = 1;

    //
    // Acknowledge this interrupt to receive more interrupts from group 3
    //
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;

}

__interrupt void epwm4_isr(void)
{

    EPwm4Regs.CMPA.bit.CMPA = PERIOD_MOTOR - PWM_motor.UC;
    //
    // Clear INT flag for this timer
    //
    EPwm4Regs.ETCLR.bit.INT = 1;

    //
    // Acknowledge this interrupt to receive more interrupts from group 3
    //
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}

__interrupt void epwm5_isr(void)
{
//    Ready = GpioDataRegs.GPADAT.bit.GPIO19;
//    if(Ready == 0)
//    {
//        Ready_Fix = 0;
//    }
//    Fault = !GpioDataRegs.GPADAT.bit.GPIO18;
//    if(Fault) Fault_fix = 1;
//    if(Fault_fix)
//    {
//        EPwm5Regs.CMPA.bit.CMPA = PERIOD_BRAKE;
//        PWM_out = 0;
//    }
//    else
//    {
        EPwm5Regs.CMPA.bit.CMPA = PERIOD_BRAKE - PWM_out;
//    }

    //
    // Clear INT flag for this timer
    //
    EPwm5Regs.ETCLR.bit.INT = 1;

    //
    // Acknowledge this interrupt to receive more interrupts from group 3
    //
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}



float Volt=0;

__interrupt void epwm6_isr(void)
{

    EPwm6Regs.CMPA.bit.CMPA = PERIOD_MOTOR - PWM_out;
    //
    // Clear INT flag for this timer
    //
    EPwm6Regs.ETCLR.bit.INT = 1;

    //
    // Acknowledge this interrupt to receive more interrupts from group 3
    //
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}



uint16_t PWM_test = SDFM_DELAY;



__interrupt void epwm11_isr(void)
{

//    Gpio55out(1);

    EPwm11Regs.CMPA.bit.CMPA =  PWM_test;
    EPwm11Regs.CMPB.bit.CMPB =  PWM_test;
    EPwm11Regs.CMPC =  PWM_test;
    EPwm11Regs.CMPD =  PWM_test;


    EPwm11Regs.ETCLR.bit.INT = 1;
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
//    Gpio55out(0);
}



//
// InitEPwm1Example - Initialize EPWM1 configuration
//
