//----------------------------------------------------------------------------------
//	FILE:			Main.C
//
//	Description:	This program converts ADC input ADCINB7(Trimer input) and blinks LED depending on the results. 
//					If the button is pressed then LED stops blinking.
//					The virtual COM port receives character and transmites ECHO+1. The COM settings are 9600 bps 8N1 
//
//					Jumper state:
//					TDO    - 1	|	
//					GPIO34 - 1   >	Get Mode
//					#TRST  - 1	|		
//		
//	Version: 		1.0
//
//  Target:  		TMS320F28027 (Piccolo)
//

#include "PeripheralHeaderIncludes.h"
#include "DSP2802x_Sci.h"												
														
unsigned long	Led_Blink;		
unsigned long const	LED_Blink_coeficient = 14650l;			//change led blink period		
int AdcResults[16];											
unsigned char test_flag = 0;


														
																		 

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// FUNCTION PROTOTYPES
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void DeviceInit(void);
void InitFlash();
void ADC_Init(void);

#define LED_ON			GpioDataRegs.AIOCLEAR.bit.AIO6 = 1
#define LED_OFF			GpioDataRegs.AIOSET.bit.AIO6 = 1
#define	USB_CONNECT		GpioDataRegs.AIODAT.bit.AIO14

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// VARIABLE DECLARATIONS - GENERAL
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

// Used for running BackGround in flash, and ISR in RAM
extern Uint16 *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;



//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// MAIN CODE - starts here
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void main(void)
{


//=================================
//	INITIALISATION - General
//=================================

	DeviceInit();	// Device Life support & GPIO mux settings

// Only used if running from FLASH
// Note that the variable FLASH is defined by the compiler with -d FLASH
// (see TwoChannelBuck.pjt file)
#ifdef FLASH		
// Copy time critical code and Flash setup code to RAM
// The  RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
// symbols are created by the linker. Refer to the linker files. 
	MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);

// Call Flash Initialization to setup flash waitstates
// This function must reside in RAM
	InitFlash();	// Call the flash wrapper init function
#endif //(FLASH)

//=================================
//	INITIALISATION - Peripherals
//=================================
	usDelay(50000);	
	ADC_Init();	
// Init USART GPIO
	InitSciaGpio();   
	scia_fifo_init();	   // Initialize the SCI FIFO
	scia_echoback_init();  // Initalize SCI for echoback
//	 Wait_For_Any_Key ();
	scia_msg("\r\n\nTMS320-P28027 \r\n\0");		

	EALLOW;
	GpioCtrlRegs.AIOMUX1.bit.AIO6 = 0;		//AIO6 like GPIO
	GpioDataRegs.AIOCLEAR.bit.AIO6 = 1;		//AIO6 is low - Light LED
	GpioCtrlRegs.AIODIR.bit.AIO6 = 1;		//AIO6 is output
	EDIS;
//=================================
//	Forever LOOP
//=================================
	for(;;)  //infinite loop
	{

		Led_Blink = AdcResult.ADCRESULT15 * LED_Blink_coeficient;	 
		CpuTimer0Regs.PRD.all =  Led_Blink;	 
	
		if(CpuTimer0Regs.TCR.bit.TIF == 1)
		  {
			CpuTimer0Regs.TCR.bit.TIF = 1;				//clear flag
			GpioDataRegs.AIOTOGGLE.bit.AIO6 = 1;		//Toggle AIO6 (Led)
		  }
	
		if (GpioDataRegs.GPADAT.bit.GPIO0 == 0) LED_ON;	//check BUTTON
		if (SciaRegs.SCIFFRX.bit.RXFFST != 0)			//UART echo
		{	
			while (SciaRegs.SCIFFTX.bit.TXFFST != 0) {}
			SciaRegs.SCITXBUF=SciaRegs.SCIRXBUF.all+1;
		}
	}
} //END MAIN CODE


