//###########################################################################
//
// FILE:    test.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 EV2  ( ( (DPWM_PERIOD) * 12)/16)
void PWM_reference_test()
{
	init_PWM();
	TimerRegs.T16PWM1CMP1DAT.bit.CMP_DAT = 64;
	TimerRegs.T16PWM0CMP1DAT.bit.CMP_DAT = 64;
	init_interrupts();
}

void dpwn_boost_test()
{

//
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM0B_IO_VALUE = 0;
//	MiscAnalogRegs.GLBIOOE.bit.DPWM0B_IO_OE = 1;
//	MiscAnalogRegs.GLBIOEN.bit.DPWM0B_IO_EN = 1;
//
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM1B_IO_VALUE = 0;
//	MiscAnalogRegs.GLBIOOE.bit.DPWM1B_IO_OE = 1;
//	MiscAnalogRegs.GLBIOEN.bit.DPWM1B_IO_EN = 1;


		Dpwm0Regs.DPWMCTRL0.bit.PWM_EN = 0;     //disable locally for init
		Dpwm0Regs.DPWMCTRL0.bit.CLA_EN = 0;		//default is 1 - use cla

		Dpwm0Regs.DPWMCTRL0.bit.PWM_MODE = 0; //normal mode
		Dpwm0Regs.DPWMPRD.all = DPWM_PERIOD;         //use .all for all values, make sure scaling matches.
		Dpwm0Regs.DPWMEV1.all = 10+DEAD_TIME_1;              //Put event 1 at start of period
		Dpwm0Regs.DPWMEV2.all =Dpwm0Regs.DPWMEV1.all + ( DPWM_PERIOD  * 0.2 );       //1/4 of period - divide is OK because it's all constants.
		Dpwm0Regs.DPWMEV3.all = Dpwm0Regs.DPWMEV2.all+DEAD_TIME_2;       //1/2 of period
		Dpwm0Regs.DPWMEV4.all =DPWM_PERIOD;
		Dpwm0Regs.DPWMSAMPTRIG1.all =2000 ;

		Dpwm0Regs.DPWMMINDUTYHI.all= 2250;
		Dpwm0Regs.DPWMMINDUTYLO.all= 2250;

		Dpwm0Regs.DPWMCTRL0.bit.MIN_DUTY_MODE=2;
		Dpwm0Regs.DPWMCTRL0.bit.PWM_A_INTRA_MUX=0;
		Dpwm0Regs.DPWMCTRL0.bit.PWM_B_INTRA_MUX=0;
		Dpwm0Regs.DPWMPHASETRIG.all=(DPWM_PERIOD)/2;
		Dpwm0Regs.DPWMCTRL0.bit.MSYNC_SLAVE_EN=0;
		Dpwm0Regs.DPWMCTRL0.bit.PWM_EN = 1;     //enable locally
		LoopMuxRegs.GLBEN.bit.DPWM0_EN=1;


		LoopMuxRegs.DPWMMUX.bit.DPWM0_SYNC_SEL =0; //DPWM0 is a master, sync with itself
		LoopMuxRegs.DPWMMUX.bit.DPWM1_SYNC_SEL =0;// syc with DPWM0



		Dpwm1Regs.DPWMCTRL0.bit.PWM_EN = 0;     //disable locally for init
		Dpwm1Regs.DPWMCTRL0.bit.CLA_EN = 0;		//default is 1 - use cla

		Dpwm1Regs.DPWMCTRL0.bit.PWM_MODE = 0; //normal mode
		Dpwm1Regs.DPWMPRD.all = DPWM_PERIOD;         //use .all for all values, make sure scaling matches.
		Dpwm1Regs.DPWMEV1.all = 10+DEAD_TIME_1;              //Put event 1 at start of period
		Dpwm1Regs.DPWMEV2.all =Dpwm1Regs.DPWMEV1.all +200;       //1/4 of period - divide is OK because it's all constants.
		Dpwm1Regs.DPWMEV3.all = Dpwm1Regs.DPWMEV2.all+DEAD_TIME_2;       //1/2 of period
		Dpwm1Regs.DPWMEV4.all =DPWM_PERIOD;
		Dpwm1Regs.DPWMSAMPTRIG1.all =2000 ;

		Dpwm1Regs.DPWMMINDUTYHI.all= 2250;
		Dpwm1Regs.DPWMMINDUTYLO.all= 2250;

		Dpwm1Regs.DPWMCTRL0.bit.MIN_DUTY_MODE=2;
		Dpwm1Regs.DPWMCTRL0.bit.PWM_A_INTRA_MUX=0;
		Dpwm1Regs.DPWMCTRL0.bit.PWM_B_INTRA_MUX=0;
		Dpwm1Regs.DPWMCTRL0.bit.PWM_EN = 1;     //enable locally
		Dpwm1Regs.DPWMCTRL0.bit.MSYNC_SLAVE_EN =1; // DPWM0 is master
		LoopMuxRegs.GLBEN.bit.DPWM1_EN=1;
}

void dpwm_buck_test_1()
{

	volatile Uint32 i=0;

	Dpwm0Regs.DPWMCTRL0.bit.PWM_EN = 0;     //disable locally for init
	Dpwm0Regs.DPWMCTRL0.bit.CLA_EN = 0;		//default is 1 - use cla

	Dpwm0Regs.DPWMCTRL0.bit.PWM_MODE = 0; //normal mode
	Dpwm0Regs.DPWMPRD.all = DPWM_PERIOD;         //use .all for all values, make sure scaling matches.
	Dpwm0Regs.DPWMEV1.all = (DEAD_TIME_1)+10;              //Put event 1 at start of period
	Dpwm0Regs.DPWMEV2.all =((DPWM_PERIOD)>>2)-(DEAD_TIME_2)/2;       //1/4 of period - divide is OK because it's all constants.
	Dpwm0Regs.DPWMEV3.all = ((DPWM_PERIOD)>>2)+(DEAD_TIME_2)/2;       //1/2 of period
	Dpwm0Regs.DPWMEV4.all = 10;
	Dpwm0Regs.DPWMSAMPTRIG1.all =2000 ;

	Dpwm0Regs.DPWMMINDUTYHI.all= 2250;
	Dpwm0Regs.DPWMMINDUTYLO.all= 2250;

	Dpwm0Regs.DPWMCTRL0.bit.MIN_DUTY_MODE=2;
	Dpwm0Regs.DPWMCTRL0.bit.PWM_A_INTRA_MUX=3;
	Dpwm0Regs.DPWMCTRL0.bit.PWM_B_INTRA_MUX=3;
	Dpwm0Regs.DPWMCTRL0.bit.PWM_EN = 1;     //enable locally
	LoopMuxRegs.GLBEN.bit.DPWM0_EN=1;
	if(dflash_variables.variables.short_test)
	{

	for( i=0;i<200000;i++);
	LoopMuxRegs.GLBEN.bit.DPWM0_EN=0;
	}



}


void dpwm_buck_test_2()
{
	volatile Uint32 i=0;
	Dpwm1Regs.DPWMCTRL0.bit.PWM_EN = 0;     //disable locally for init
	Dpwm1Regs.DPWMCTRL0.bit.CLA_EN = 0;		//default is 1 - use cla

	Dpwm1Regs.DPWMCTRL0.bit.PWM_MODE = 0; //normal mode
	Dpwm1Regs.DPWMPRD.all = DPWM_PERIOD;         //use .all for all values, make sure scaling matches.
	Dpwm1Regs.DPWMEV1.all = (DEAD_TIME_1)+10;              //Put event 1 at start of period
	Dpwm1Regs.DPWMEV2.all =((DPWM_PERIOD)>>2)-(DEAD_TIME_2)/2;       //1/4 of period - divide is OK because it's all constants.
	Dpwm1Regs.DPWMEV3.all = ((DPWM_PERIOD)>>2)+(DEAD_TIME_2)/2;       //1/2 of period
	Dpwm1Regs.DPWMEV4.all = 10;
	Dpwm1Regs.DPWMSAMPTRIG1.all =2000 ;

	Dpwm1Regs.DPWMMINDUTYHI.all= 2250;
	Dpwm1Regs.DPWMMINDUTYLO.all= 2250;

	Dpwm1Regs.DPWMCTRL0.bit.MIN_DUTY_MODE=2;
	Dpwm1Regs.DPWMCTRL0.bit.PWM_A_INTRA_MUX=3;
	Dpwm1Regs.DPWMCTRL0.bit.PWM_B_INTRA_MUX=3;
	Dpwm1Regs.DPWMCTRL0.bit.PWM_EN = 1;     //enable locally
	LoopMuxRegs.GLBEN.bit.DPWM1_EN=1;
	if(dflash_variables.variables.short_test)
		{
	for( i=0;i<200000;i++);
		LoopMuxRegs.GLBEN.bit.DPWM1_EN=0;
		}
}

void dpwm_buck_test_3()
{
	volatile Uint32 i=0;
	Dpwm2Regs.DPWMCTRL0.bit.PWM_EN = 0;     //disable locally for init
	Dpwm2Regs.DPWMCTRL0.bit.CLA_EN = 0;		//default is 1 - use cla

	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 = (DEAD_TIME_1)+10;              //Put event 1 at start of period
	Dpwm2Regs.DPWMEV2.all =((DPWM_PERIOD)>>2)-(DEAD_TIME_2)/2;       //1/4 of period - divide is OK because it's all constants.
	Dpwm2Regs.DPWMEV3.all = ((DPWM_PERIOD)>>2)+(DEAD_TIME_2)/2;       //1/2 of period
	Dpwm2Regs.DPWMEV4.all = 10;
	Dpwm2Regs.DPWMSAMPTRIG1.all =2000 ;

	Dpwm2Regs.DPWMMINDUTYHI.all= 2250;
	Dpwm2Regs.DPWMMINDUTYLO.all= 2250;

	Dpwm2Regs.DPWMCTRL0.bit.MIN_DUTY_MODE=2;
	Dpwm2Regs.DPWMCTRL0.bit.PWM_A_INTRA_MUX=3;
	Dpwm2Regs.DPWMCTRL0.bit.PWM_B_INTRA_MUX=3;
	Dpwm2Regs.DPWMCTRL0.bit.PWM_EN = 1;     //enable locally
	LoopMuxRegs.GLBEN.bit.DPWM2_EN=1;
	if(dflash_variables.variables.short_test)
		{
	for( i=0;i<200000;i++);
		LoopMuxRegs.GLBEN.bit.DPWM2_EN=0;
		}
}

void dpwm_buck_test_4()
{
	volatile Uint32 i=0;

	Dpwm3Regs.DPWMCTRL0.bit.PWM_EN = 0;     //disable locally for init
	Dpwm3Regs.DPWMCTRL0.bit.CLA_EN = 0;		//default is 1 - use 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 = (DEAD_TIME_1)+10;              //Put event 1 at start of period
	Dpwm3Regs.DPWMEV2.all =((DPWM_PERIOD)>>2)-(DEAD_TIME_2)/2;       //1/4 of period - divide is OK because it's all constants.
	Dpwm3Regs.DPWMEV3.all = ((DPWM_PERIOD)>>2)+(DEAD_TIME_2)/2;       //1/2 of period
	Dpwm3Regs.DPWMEV4.all = 10;
	Dpwm3Regs.DPWMSAMPTRIG1.all =2000 ;

	Dpwm3Regs.DPWMMINDUTYHI.all= 2250;
	Dpwm3Regs.DPWMMINDUTYLO.all= 2250;

	Dpwm3Regs.DPWMCTRL0.bit.MIN_DUTY_MODE=2;
	Dpwm3Regs.DPWMCTRL0.bit.PWM_A_INTRA_MUX=3;
	Dpwm3Regs.DPWMCTRL0.bit.PWM_B_INTRA_MUX=3;
	Dpwm3Regs.DPWMCTRL0.bit.PWM_EN = 1;     //enable locally
	LoopMuxRegs.GLBEN.bit.DPWM3_EN=1;

	if(dflash_variables.variables.short_test)
		{
	for( i=0;i<200000;i++);
		LoopMuxRegs.GLBEN.bit.DPWM3_EN=0;

		}

}
void test()
{
	volatile Uint32 i=0;

	init_pmbus(0x58);
	init_adc();

//	adc_struct._12_Vsen_ADC=AdcRegs.ADCRESULT[0].all;
//	adc_struct._14_8_Vsen_ADC=AdcRegs.ADCRESULT[1].all;
//
//	if ((adc_struct._12_Vsen_ADC > 800 ) || (adc_struct._14_8_Vsen_ADC > 1300 ))
//	{
//		while(1)
//		{
//			init_pmbus(0x58);
//			fine_tune();
//		}
//	}

//	MiscAnalogRegs.GLBIOVAL.all=0;
//	MiscAnalogRegs.GLBIOOE.all=0xFFFFFFFF;
//	MiscAnalogRegs.GLBIOEN.all=0xFFFFFFFF;
//
//
//	MiscAnalogRegs.GLBIOVAL.all=0;
//	MiscAnalogRegs.GLBIOVAL.bit.FAULT1_IO_VALUE=1;
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM0B_IO_VALUE=1;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.all=0;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.bit.TDO_IO_VALUE=1;
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM1B_IO_VALUE=1;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.all=0;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.bit.FAULT2_IO_VALUE=1;
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM2B_IO_VALUE=1;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.all=0;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.bit.FAULT3_IO_VALUE=1;
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM3B_IO_VALUE=1;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.all=0;
//
//	for( i=0;i<20000000;i++);
//
//	MiscAnalogRegs.GLBIOVAL.all=0;
//	MiscAnalogRegs.GLBIOVAL.bit.FAULT1_IO_VALUE=1;
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM0A_IO_VALUE=1;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.all=0;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.bit.TDO_IO_VALUE=1;
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM1A_IO_VALUE=1;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.all=0;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.bit.FAULT2_IO_VALUE=1;
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM2A_IO_VALUE=1;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.all=0;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.bit.FAULT3_IO_VALUE=1;
//	MiscAnalogRegs.GLBIOVAL.bit.DPWM3A_IO_VALUE=1;
//	for( i=0;i<20000000;i++);
//	MiscAnalogRegs.GLBIOVAL.all=0;







	MiscAnalogRegs.GLBIOVAL.all=0;
	MiscAnalogRegs.GLBIOOE.all=0;
	MiscAnalogRegs.GLBIOEN.all=0;

	test_state=0;
	for(;;)
	{
		pmbus_handler();
		if(store == 3)
		{
			if (store_to_DFLASH_slow())
			{
				store=0;
			}
		}
		switch(test_state)
		{
		case 1:	dpwm_buck_test_1();
				test_state=0;
				break;
		case 2:	dpwm_buck_test_2();
				test_state=0;
				break;
		case 3:	dpwm_buck_test_3();
				test_state=0;
				break;
		case 4:	dpwm_buck_test_4();
				test_state=0;
				break;
		case 5:
				Dpwm0Regs.DPWMCTRL0.bit.MSYNC_SLAVE_EN=0;
				Dpwm1Regs.DPWMCTRL0.bit.MSYNC_SLAVE_EN =1; // DPWM0 is master
				Dpwm2Regs.DPWMCTRL0.bit.MSYNC_SLAVE_EN =1; // DPWM1 is master
				Dpwm3Regs.DPWMCTRL0.bit.MSYNC_SLAVE_EN =1; // DPWM2 is master
				LoopMuxRegs.DPWMMUX.bit.DPWM0_SYNC_SEL =0; //DPWM0 is a master
				LoopMuxRegs.DPWMMUX.bit.DPWM1_SYNC_SEL =0;// syc with DPWM0
				LoopMuxRegs.DPWMMUX.bit.DPWM2_SYNC_SEL =1;// syc with DPWM1
				LoopMuxRegs.DPWMMUX.bit.DPWM3_SYNC_SEL =2;// syc with DPWM2
				Dpwm0Regs.DPWMPHASETRIG.all=DPWM_PERIOD/4;
				Dpwm1Regs.DPWMPHASETRIG.all=DPWM_PERIOD/4;
				Dpwm2Regs.DPWMPHASETRIG.all=DPWM_PERIOD/4;
				dpwm_buck_test_1();
				dpwm_buck_test_2();
				dpwm_buck_test_3();
				dpwm_buck_test_4();
				test_state=0;
				break;
		case 6:	PWM_reference_test();
				test_state=0;
				break;
		case 7: TimerRegs.T16PWM1CMP1DAT.bit.CMP_DAT = 0;
				TimerRegs.T16PWM0CMP1DAT.bit.CMP_DAT = 0;
				test_state=0;
				break;
		case 8: dpwn_boost_test();
				test_state=0;
				break;
		case 9:
				poll_adc();
				AdcRegs.ADCCTRL.bit.SW_START = 1;
				break;

		default:
				break;
		}
	}

}


