//###########################################################################
//
// FILE:    init_dpwm.c
//
// TITLE:
//
// NOTES:
//  1)
//###########################################################################
//
//  Ver  | dd mmm yyyy | Who  		| Description of changes
// ======|=============|============|========================================
//  00 	   05 04 2015 	 HPCS
//
//  Texas Instruments, Inc
//  Copyright Texas Instruments 2008. All rights reserved.
//###########################################################################
#include "system_defines.h"
#include "cyclone_device.h"
#include "variables.h"
#include "function_definitions.h"
#include "software_interrupts.h"
#include "cyclone_defines.h"
#include "pmbus_common.h"
#include "pmbus_topology.h"
#include "pmbus_constants.h"



#define INDUCTOR_IN_MICRO_HENRY 0.25
#define TURN_ON_TIME_FOR_3A_PEAK (1000*INDUCTOR_IN_MICRO_HENRY)

#define EV2  ( ( (DPWM_PERIOD) * 12)/16)
void init_dpwm2()
{


        Dpwm2Regs.DPWMCTRL0.bit.PWM_EN = 0;     //disable locally for init
        Dpwm2Regs.DPWMCTRL0.bit.CLA_EN = 0;
        Dpwm2Regs.DPWMCTRL0.bit.PWM_MODE = 0; //normal mode
        Dpwm2Regs.DPWMPRD.all = DPWM_PERIOD;         //use .all for all values, make sure scaling matches.
//        Dpwm2Regs.DPWMEV1.all = 10+DEAD_TIME_1;              //Put event 1 at start of period
//        Dpwm2Regs.DPWMEV2.all = DPWM_PERIOD * DPWM2_FIXED_DUTY;       //1/4 of period - divide is OK because it's all constants.
//        Dpwm2Regs.DPWMEV3.all = Dpwm2Regs.DPWMEV2.all +DEAD_TIME_2;       //1/2 of period
//        Dpwm2Regs.DPWMEV4.all = 10;

        Dpwm2Regs.DPWMEV1.all = DPWM_PERIOD * 0.1 + DEAD_TIME_1;              //Put event 1 at start of period
        Dpwm2Regs.DPWMEV2.all = DPWM_PERIOD;       //1/4 of period - divide is OK because it's all constants.
        Dpwm2Regs.DPWMEV3.all = 10 + DEAD_TIME_2;       //1/2 of period
        Dpwm2Regs.DPWMEV4.all = DPWM_PERIOD * 0.1;

        Dpwm2Regs.DPWMSAMPTRIG1.all =20 ;
        Dpwm2Regs.DPWMCTRL2.bit.SAMPLE_TRIG_1_EN = 1; //enable 1 sample trigger
        Dpwm2Regs.DPWMPHASETRIG.all=4;

        Dpwm2Regs.DPWMCTRL0.bit.MSYNC_SLAVE_EN =0;// DPWM2 is master


}


void init_dpwm3()
{



        Dpwm3Regs.DPWMCTRL0.bit.PWM_EN = 0;     //disable locally for init
        Dpwm3Regs.DPWMCTRL0.bit.CLA_EN = 0;     //Temporarly disable CLA
        Dpwm3Regs.DPWMCTRL0.bit.PWM_MODE = 0; //normal mode
        Dpwm3Regs.DPWMPRD.all = DPWM_PERIOD;         //use .all for all values, make sure scaling matches.
        Dpwm3Regs.DPWMEV1.all = 10+DEAD_TIME_1;              //Put event 1 at start of period

        Dpwm3Regs.DPWMEV2.all =  Dpwm3Regs.DPWMEV1.all + ( Dpwm3Regs.DPWMPRD.all  * DPWM2_FIXED_DUTY);       //82.5 percent duty cycle .
        Dpwm3Regs.DPWMEV2.all =  Dpwm3Regs.DPWMEV1.all;

        Dpwm3Regs.DPWMEV3.all = Dpwm3Regs.DPWMEV2.all +DEAD_TIME_2;       //1/2 of period
        Dpwm3Regs.DPWMEV4.all = 10;
        Dpwm3Regs.DPWMCTRL1.bit.EVENT_UP_SEL = 1; //update at end of period
        Dpwm3Regs.DPWMCTRL0.bit.MSYNC_SLAVE_EN=1;// Slave to DPWM2


}

void init_dpwm()
{
	init_dpwm2();
	init_dpwm3();
}


