/* ==================================================================================
File name	:   F280X_uStep_8sw.C
                    
Originator	:	Department of Electrical Engineering, KMUTT

Description	:   This file contains source for the PWM drivers for the F2808  for Stepping Motor

    dc-bus+ --------------------------------------------------
                |                 |        |                 |   
               [1]               [3]	  [5]               [7]
                |----(Phase A)----|        |----(Phase B)----|
               [2]               [4]	  [6]               [8]
                |                 |		   |                 |
    dc-bus- --------------------------------------------------


Target: TMS320F281x family
              
=====================================================================================
History:
-------------------------------------------------------------------------------------
 06-15-2006	Release	Rev 1.0  
----------------------------------------------------------------------------------*/

#include "DSP280x_Device.h"
#include "f280x_uStep_8sw.h"

void F280X_uStep_8s_Init(uSTEP_8S *p) 
{       
         // Setup Sync
         EPwm1Regs.TBCTL.bit.SYNCOSEL = 0;          // Pass through
         EPwm2Regs.TBCTL.bit.SYNCOSEL = 0;          // Pass through
         EPwm3Regs.TBCTL.bit.SYNCOSEL = 0;          // Pass through
         EPwm4Regs.TBCTL.bit.SYNCOSEL = 0;          // Pass through
         EPwm5Regs.TBCTL.bit.SYNCOSEL = 0;          // Pass through
         EPwm6Regs.TBCTL.bit.SYNCOSEL = 0;          // Pass through

         // Allow each timer to be sync'ed
         EPwm1Regs.TBCTL.bit.PHSEN = 1;
         EPwm2Regs.TBCTL.bit.PHSEN = 1;
         EPwm3Regs.TBCTL.bit.PHSEN = 1;
         EPwm4Regs.TBCTL.bit.PHSEN = 1;
         EPwm5Regs.TBCTL.bit.PHSEN = 1;
         EPwm6Regs.TBCTL.bit.PHSEN = 1;

         // Init Timer-Base Period Register for EPWM1-EPWM4
         EPwm1Regs.TBPRD = p->PeriodMax;
         EPwm2Regs.TBPRD = p->PeriodMax;
		 EPwm3Regs.TBPRD = p->PeriodMax;
         EPwm4Regs.TBPRD = p->PeriodMax;

         // Init Timer-Base Phase Register for EPWM1-EPWM4
         EPwm1Regs.TBPHS.half.TBPHS = 0;
         EPwm2Regs.TBPHS.half.TBPHS = 0;
		 EPwm3Regs.TBPHS.half.TBPHS = 0;
         EPwm4Regs.TBPHS.half.TBPHS = 0;

         // Init Timer-Base Control Register for EPWM1-EPWM4
         EPwm1Regs.TBCTL.all = uStep_TimeBase_INIT;
         EPwm2Regs.TBCTL.all = uStep_TimeBase_INIT;
		 EPwm3Regs.TBCTL.all = uStep_TimeBase_INIT;
         EPwm4Regs.TBCTL.all = uStep_TimeBase_INIT;

         // Init Compare Control Register for EPWM1-EPWM4
         EPwm1Regs.CMPCTL.all = uStep_CompareControl_INIT;
         EPwm2Regs.CMPCTL.all = uStep_CompareControl_INIT;
		 EPwm3Regs.CMPCTL.all = uStep_CompareControl_INIT;
         EPwm4Regs.CMPCTL.all = uStep_CompareControl_INIT;

         // Init Action Qualifier Output A Register for EPWM1-EPWM4
         EPwm1Regs.AQCTLA.all = uStep_PWM1_AQCTLA_INIT;
         EPwm2Regs.AQCTLA.all = uStep_PWM2_AQCTLA_INIT;
		 EPwm3Regs.AQCTLA.all = uStep_PWM3_AQCTLA_INIT;
         EPwm4Regs.AQCTLA.all = uStep_PWM4_AQCTLA_INIT;

         // Init Dead-Band Generator Control Register for EPWM1-EPWM4
         EPwm1Regs.DBCTL.all = uStep_DBCTL_INIT;
         EPwm2Regs.DBCTL.all = uStep_DBCTL_INIT;
		 EPwm3Regs.DBCTL.all = uStep_DBCTL_INIT;
         EPwm4Regs.DBCTL.all = uStep_DBCTL_INIT;

		 // Init Dead-Band Generator Falling/Rising Edge Delay Register for EPWM1-EPWM4
         EPwm1Regs.DBFED = uStep_DBCNT_INIT;
         EPwm1Regs.DBRED = uStep_DBCNT_INIT;
         EPwm2Regs.DBFED = uStep_DBCNT_INIT;
         EPwm2Regs.DBRED = uStep_DBCNT_INIT;
		 EPwm3Regs.DBFED = uStep_DBCNT_INIT;
         EPwm3Regs.DBRED = uStep_DBCNT_INIT;
         EPwm4Regs.DBFED = uStep_DBCNT_INIT;
         EPwm4Regs.DBRED = uStep_DBCNT_INIT;

         // Init PWM Chopper Control Register for EPWM1-EPWM4
         EPwm1Regs.PCCTL.all = uStep_PCCTL_INIT;
         EPwm2Regs.PCCTL.all = uStep_PCCTL_INIT;
		 EPwm3Regs.PCCTL.all = uStep_PCCTL_INIT;
         EPwm4Regs.PCCTL.all = uStep_PCCTL_INIT;

         EALLOW;                       // Enable EALLOW 

         // Init Trip Zone Select Register
         EPwm1Regs.TZSEL.all = uStep_TZSEL_INIT_STATE;
         EPwm2Regs.TZSEL.all = uStep_TZSEL_INIT_STATE;
		 EPwm3Regs.TZSEL.all = uStep_TZSEL_INIT_STATE;
         EPwm4Regs.TZSEL.all = uStep_TZSEL_INIT_STATE;

         // Init Trip Zone Control Register
         EPwm1Regs.TZCTL.all = uStep_TZCTL_INIT;
         EPwm2Regs.TZCTL.all = uStep_TZCTL_INIT;
		 EPwm3Regs.TZCTL.all = uStep_TZCTL_INIT;
         EPwm4Regs.TZCTL.all = uStep_TZCTL_INIT;

         // Setting four EPWM as primary output pins
		 // GPIO = 1 => the pin is configured for the peripheral functions

         GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;   // EPWM1A pin
         GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;   // EPWM1B pin
         GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1;   // EPWM2A pin
         GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1;   // EPWM2B pin
		 GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1;   // EPWM3A pin
         GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 1;   // EPWM3B pin
		 GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 1;   // EPWM4A pin
         GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 1;   // EPWM4B pin

		 //GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0;   // GPIO4 pin
         //GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 0;   // GPIO5 pin
		 //GpioCtrlRegs.GPADIR.bit.GPIO4 = 1;    // GPIO4 as output
	  	 //GpioCtrlRegs.GPADIR.bit.GPIO5 = 1;    // GPIO5 as output

         EDIS;                         // Disable EALLOW
}
// ====================== End F280x_MicroStep_Init =============================


void F280X_uStep_8s_Update(uSTEP_8S *p) 
{       
 int32 TmpA_1,TmpA_2,TmpB_1,TmpB_2;

        TmpA_1 = (int32)p->PeriodMax*(int32)p->DutyFunc_A;                    // Q15 = Q0*Q15
		TmpA_2 = (int32)p->PeriodMax*(int32)-p->DutyFunc_A;                    // Q15 = Q0*Q15

		TmpB_1 = (int32)p->PeriodMax*(int32)p->DutyFunc_B;                    // Q15 = Q0*Q15
		TmpB_2 = (int32)p->PeriodMax*(int32)-p->DutyFunc_B;					  // Q15 = Q0*Q15
		
		// Compute the compare A (Q0) from the EPWM1AO & EPWM1BO duty cycle ratio (Q15)
        EPwm1Regs.CMPA.half.CMPA = (int16)(TmpA_1>>16) + (int16)(p->PeriodMax>>1);   // Q0 = (Q15->Q0)/2 + (Q0/2)
		// Compute the compare B (Q0) from the EPWM2AO & EPWM2BO duty cycle ratio (Q15)
        EPwm2Regs.CMPA.half.CMPA = (int16)(TmpA_2>>16) + (int16)(p->PeriodMax>>1);  // Q0 = (Q15->Q0)/2 + (Q0/2)
		// Compute the compare A (Q0) from the EPWM1AO & EPWM3BO duty cycle ratio (Q15)
        EPwm3Regs.CMPA.half.CMPA = (int16)(TmpB_1>>16) + (int16)(p->PeriodMax>>1);   // Q0 = (Q15->Q0)/2 + (Q0/2)
		// Compute the compare B (Q0) from the EPWM2AO & EPWM4BO duty cycle ratio (Q15)
        EPwm4Regs.CMPA.half.CMPA = (int16)(TmpB_2>>16) + (int16)(p->PeriodMax>>1);  // Q0 = (Q15->Q0)/2 + (Q0/2)
}




