/*
 * Peripherals.c
 *
 *  Created on: 09-Jan-2024
 *      Author: 39138
 */


#include "stdio.h"
#include "F28x_Project.h"
#include <math.h>
#include "driverlib.h"
#include "device.h"
#include "Main.h"
#include "Peripherals.h"
//#include "f28004x_examples.h"



static void EPWMPinsCfg(void);
static void initADCA(void);
static void initADCB(void);
static void initADCC(void);
static void initADCSOC(void);
static void EPWMconfig(void);
static void InteruptConfig(void);
static void DACBCfg(void);
//static void Delay(int64_t);

void PeripheralsInit(void)
{
    //
    // Disable the watchdog
    //
    DisableDog();

    //
    // Copy time critical code and Flash setup code to RAM
    // This includes the following functions:  InitFlash();
    // The  RamfuncsLoadStart, RamfuncsLoadSize, and RamfuncsRunStart
    // symbols are created by the linker. Refer to the device .cmd file.
    //
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);

    //
    // Call Flash Initialization to setup flash waitstates
    // This function must reside in RAM
    //
    InitFlash();
    //
    // PLLSYSCLK = (XTAL_OSC) * (IMULT + FMULT) / (PLLSYSCLKDIV)
    //
   // InitSysPll(XTAL_OSC,IMULT_10,FMULT_0,PLLCLK_BY_2);
    InitSysPll(INT_OSC2, SYSCLK_PLL_IMULT, FMULT_0, SYSCLK_PLL_DIV);
    //InitSysPll(INT_OSC2, SYSCLK_PLL_IMULT, FMULT_0, SYSCLK_PLL_DIV);
    DINT;
    InitPieCtrl();

    // Disable CPU interrupts and clear all CPU interrupt flags:
    //
//    IER = 0x0000;
//    IFR = 0x0000;

    InitPieVectTable();
    // Turn on all peripherals
    //
    InitPeripheralClocks();
    // Disable CPU interrupts and clear all CPU interrupt flags:
//    IER = 0x0000;
//    IFR = 0x0000;

    InitGpio();
    EPWMPinsCfg();

    initADCA();
    initADCB();
    initADCC();
    DACBCfg();
    //Delay(10000000);
//    _F28x_usDelay(1000);
    DELAY_US(1000);
    initADCSOC();


    EALLOW;
    CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 0;
    EDIS;
    EPWMconfig();

    EALLOW;
    CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1;
    EDIS;

    //INterupt Config
    InteruptConfig();

    //
    // Enable global Interrupts and higher priority real-time debug events:
    //
    IER |= M_INT1;  // Enable group 1 interrupts

    EINT;           // Enable Global interrupt INTM
    ERTM;           // Enable Global realtime interrupt DBGM
}

void EPWMPinsCfg(void)
{


   // InitGpio();
    EALLOW;

    //...................PWM_pins_Config.........................

    GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1;    // Disable pull-up on GPIO0 (EPWM1A)
    GpioCtrlRegs.GPAPUD.bit.GPIO1 = 1;    // Disable pull-up on GPIO1 (EPWM1B)
    GpioCtrlRegs.GPAPUD.bit.GPIO2 = 1;    // Disable pull-up on GPIO2 (EPWM2A)
    GpioCtrlRegs.GPAPUD.bit.GPIO3 = 1;    // Disable pull-up on GPIO3 (EPWM2B)
    GpioCtrlRegs.GPAPUD.bit.GPIO4 = 1;    // Disable pull-up on GPIO4 (EPWM3A)
    GpioCtrlRegs.GPAPUD.bit.GPIO5 = 1;    // Disable pull-up on GPIO5 (EPWM3B)
    GpioCtrlRegs.GPAPUD.bit.GPIO6 = 1;    // Disable pull-up on GPIO6 (EPWM4A)
    GpioCtrlRegs.GPAPUD.bit.GPIO7 = 1;    // Disable pull-up on GPIO7 (EPWM4B)
    GpioCtrlRegs.GPAPUD.bit.GPIO8 = 1;    // Disable pull-up on GPIO7 (EPWM4B)
    GpioCtrlRegs.GPAPUD.bit.GPIO9 = 1;    // Disable pull-up on GPIO7 (EPWM4B)
    GpioCtrlRegs.GPAPUD.bit.GPIO10 = 1;    // Enable pull-up on GPIO10 (EPWM6A)

    GpioCtrlRegs.GPAGMUX1.bit.GPIO0 = 0;
    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;   // Configure GPIO0 as EPWM1A

    GpioCtrlRegs.GPAGMUX1.bit.GPIO1 = 0;
    GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;   // Configure GPIO1 as EPWM1B


    GpioCtrlRegs.GPAGMUX1.bit.GPIO2 = 0;
    GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1;   // Configure GPIO2 as EPWM2A

    GpioCtrlRegs.GPAGMUX1.bit.GPIO3 = 0;
    GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1;   // Configure GPIO3 as EPWM2B

    GpioCtrlRegs.GPAGMUX1.bit.GPIO4 = 0;
    GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1;   // Configure GPIO4 as EPWM3A

    GpioCtrlRegs.GPAGMUX1.bit.GPIO5 = 0;
    GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 1;   // Configure GPIO5 as EPWM3B


    GpioCtrlRegs.GPAGMUX1.bit.GPIO6 = 0;
    GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 1;   // Configure GPIO5 as EPWM4A

    GpioCtrlRegs.GPAGMUX1.bit.GPIO7 = 0;
    GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 1;   // Configure GPIO6 as EPWM4B

    GpioCtrlRegs.GPAGMUX1.bit.GPIO10 = 0;
    GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 1;   // Configure GPIO10 as EPWM6A

    EDIS;
}

void initADCA(void)
{
    // Setup VREF as internal
    //SetVREF(ADC_ADCA, ADC_INTERNAL, ADC_VREF3P3);
    EALLOW;
    AnalogSubsysRegs.ANAREFCTL.bit.ANAREFASEL = 0;   //Set Internal reference to ADC A
    AnalogSubsysRegs.ANAREFCTL.bit.ANAREFA2P5SEL = 0; //Set 3.3V reference

   // AnalogSubsysRegs.ANAREFCTL.bit.ANAREFBSEL = 0;   //Set Internal reference to ADC B
    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFB2P5SEL = 0; //Set 3.3V reference

    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFCSEL = 0;   //Set Internal reference to ADC C
    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFC2P5SEL = 0; //Set 3.3V reference

    // Set ADCCLK divider to /4
    AdcaRegs.ADCCTL2.bit.PRESCALE = 6;
    // Set pulse positions to late //Interrupt pulse generation occurs at the end of the conversion
    AdcaRegs.ADCCTL1.bit.INTPULSEPOS = 1;
    // Power up the ADC and then delay for 1 ms
    AdcaRegs.ADCCTL1.bit.ADCPWDNZ = 1;
    EDIS;
    //DELAY_US(1000);
}
void initADCB(void)
{
    // Setup VREF as internal
    //SetVREF(ADC_ADCA, ADC_INTERNAL, ADC_VREF3P3);
    EALLOW;
    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFASEL = 0;   //Set Internal reference to ADC A
    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFA2P5SEL = 0; //Set 3.3V reference

    AnalogSubsysRegs.ANAREFCTL.bit.ANAREFBSEL = 0;   //Set Internal reference to ADC B
    AnalogSubsysRegs.ANAREFCTL.bit.ANAREFB2P5SEL = 0; //Set 3.3V reference

    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFCSEL = 0;   //Set Internal reference to ADC C
    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFC2P5SEL = 0; //Set 3.3V reference

    // Set ADCCLK divider to /4
    AdcbRegs.ADCCTL2.bit.PRESCALE = 6;
    // Set pulse positions to late //Interrupt pulse generation occurs at the end of the conversion
    AdcbRegs.ADCCTL1.bit.INTPULSEPOS = 1;
    // Power up the ADC and then delay for 1 ms
    AdcbRegs.ADCCTL1.bit.ADCPWDNZ = 1;
    EDIS;
    //DELAY_US(1000);
}
void initADCC(void)
{
    // Setup VREF as internal
    //SetVREF(ADC_ADCA, ADC_INTERNAL, ADC_VREF3P3);
    EALLOW;
    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFASEL = 0;   //Set Internal reference to ADC A
    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFA2P5SEL = 0; //Set 3.3V reference

    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFBSEL = 0;   //Set Internal reference to ADC B
    //AnalogSubsysRegs.ANAREFCTL.bit.ANAREFB2P5SEL = 0; //Set 3.3V reference

    AnalogSubsysRegs.ANAREFCTL.bit.ANAREFCSEL = 0;   //Set Internal reference to ADC C
    AnalogSubsysRegs.ANAREFCTL.bit.ANAREFC2P5SEL = 0; //Set 3.3V reference

    // Set ADCCLK divider to /4
    AdccRegs.ADCCTL2.bit.PRESCALE = 6;
    // Set pulse positions to late //Interrupt pulse generation occurs at the end of the conversion
    AdccRegs.ADCCTL1.bit.INTPULSEPOS = 1;
    // Power up the ADC and then delay for 1 ms
    AdccRegs.ADCCTL1.bit.ADCPWDNZ = 1;
    EDIS;
    //DELAY_US(1000);
}
void initADCSOC(void)
{
    EALLOW;
    // Select the channels to convert and the end of conversion flag
    AdccRegs.ADCSOC0CTL.bit.CHSEL = 0;     // C0->PV1 Voltage Sense
    AdccRegs.ADCSOC0CTL.bit.ACQPS = 9;     // Sample window is 10 SYSCLK cycles
    AdccRegs.ADCSOC0CTL.bit.TRIGSEL = 5;   // Trigger on ePWM1 SOCA

    AdccRegs.ADCSOC1CTL.bit.CHSEL = 3;     // C3->Offset Measure
    AdccRegs.ADCSOC1CTL.bit.ACQPS = 9;     // Sample window is 10 SYSCLK cycles
    AdccRegs.ADCSOC1CTL.bit.TRIGSEL = 5;   // Trigger on ePWM1 SOCA

    AdcbRegs.ADCSOC0CTL.bit.CHSEL = 0;     // B0->PV1 Voltage Sense
    AdcbRegs.ADCSOC0CTL.bit.ACQPS = 9;     // Sample window is 10 SYSCLK cycles
    AdcbRegs.ADCSOC0CTL.bit.TRIGSEL = 5;   // Trigger on ePWM1 SOCA

    AdcbRegs.ADCSOC1CTL.bit.CHSEL = 1;     // B1->Offset Measure
    AdcbRegs.ADCSOC1CTL.bit.ACQPS = 9;     // Sample window is 10 SYSCLK cycles
    AdcbRegs.ADCSOC1CTL.bit.TRIGSEL = 5;   // Trigger on ePWM1 SOCA

    AdcaRegs.ADCSOC0CTL.bit.CHSEL = 0;     // A0->PV1 Voltage Sense
    AdcaRegs.ADCSOC0CTL.bit.ACQPS = 9;     // Sample window is 10 SYSCLK cycles
    AdcaRegs.ADCSOC0CTL.bit.TRIGSEL = 5;   // Trigger on ePWM1 SOCA


    EDIS;

}
void EPWMconfig(void)
{
    // EPWM Module 1 configuration
    EPwm1Regs.TBCTL.bit.FREE_SOFT = 2;
    EPwm1Regs.TBCTL.bit.CLKDIV=TB_DIV1;
    EPwm1Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;
    EPwm1Regs.TBPRD = PWM1_TIMER_PRD;
    EPwm1Regs.CMPA.bit.CMPA =PWM1_CMPA;
    EPwm1Regs.CMPB.bit.CMPB = PWM1_CMPA;
    EPwm1Regs.TBCTL.bit.CTRMODE = EPWM_TIMER_UP_DOWN; // Symmetrical mode
    EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
    EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
    EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on CTR=Zero
    EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on CTR=Zero
    // Action Qualifier
    EPwm1Regs.AQCTLA.bit.ZRO=AQ_NO_ACTION;
    EPwm1Regs.AQCTLA.bit.CAU=AQ_CLEAR;
    EPwm1Regs.AQCTLA.bit.PRD=AQ_NO_ACTION;
    EPwm1Regs.AQCTLA.bit.CAD=AQ_SET;

    EPwm1Regs.AQCTLB.bit.ZRO=AQ_NO_ACTION;
    EPwm1Regs.AQCTLB.bit.CBU=AQ_SET;
    EPwm1Regs.AQCTLB.bit.PRD=AQ_NO_ACTION;
    EPwm1Regs.AQCTLB.bit.CBD=AQ_CLEAR;

    //Dead-Band
    EPwm1Regs.DBCTL.bit.OUT_MODE = DB_DISABLE; //disabled
    //SOC signal for ADCs
    //EPwm1Regs.ETSEL.bit.SOCAEN  = 0; // Disable SOC on A group
    EPwm1Regs.ETSEL.bit.SOCASEL = ET_CTR_ZERO ; // Event Trigger on CTR=Zero
    EPwm1Regs.ETPS.bit.SOCAPRD  = ET_1ST;     // Event Trigger every one cycle
    EPwm1Regs.ETSEL.bit.SOCAEN  = 1;          // Enable SOC on A group
}
void InteruptConfig(void)
{
    // Map ISR functions
    //
    EALLOW;
    PieVectTable.ADCA1_INT = &Main_ISR;     // Function for ADCA interrupt 1

    AdcaRegs.ADCINTSEL1N2.bit.INT1SEL = 0; // End of SOC0 will set INT1 flag
    AdcaRegs.ADCINTSEL1N2.bit.INT1E = 1;   // Enable INT1 flag
    AdcaRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; // Make sure INT1 flag is cleared
/*
    AdccRegs.ADCINTSEL1N2.bit.INT1SEL = 1; // End of SOC2 will set INT1 flag
    AdccRegs.ADCINTSEL1N2.bit.INT1E = 1;   // Enable INT1 flag
    AdccRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; // Make sure INT1 flag is cleared
*/
    EDIS;
    PieCtrlRegs.PIEIER1.bit.INTx1 = 1;
}
void DACACfg(void)
{
    EALLOW;
    DacaRegs.DACCTL.bit.DACREFSEL=1;
    DacaRegs.DACCTL.bit.LOADMODE=0;
    DacaRegs.DACCTL.bit.SYNCSEL=1;
    DacaRegs.DACOUTEN.bit.DACOUTEN=1;
    DacaRegs.DACCTL.bit.MODE = 1;
    //DELAY_US(10);
    DacaRegs.DACVALS.all=0;
    EDIS;
}
void DACBCfg(void)
{
    EALLOW;
    DacbRegs.DACCTL.bit.DACREFSEL=1;
    DacbRegs.DACCTL.bit.LOADMODE=0;
    DacbRegs.DACCTL.bit.SYNCSEL=0;
    DacbRegs.DACOUTEN.bit.DACOUTEN=1;
    DacbRegs.DACCTL.bit.MODE = 1;
    //DELAY_US(1000);
    DacbRegs.DACVALS.all=0;
    EDIS;
}

