//----------------------------------------------------------------------------------
//	FILE:			ContinuousADC-Main.C
//
//	Description:	This program converts the 11 available ADC inputs on the 
//					Piccolo controlSTICK in continous mode. 
//
//	Version: 		1.0
//
//  Target:  		TMS320F2802x or TMS320F2803x families (Piccolo)
//
//----------------------------------------------------------------------------------
//  $TI Release:$ 	V1.1
//  $Release Date:$ 27 Nov 2008 - BRL
//----------------------------------------------------------------------------------
//
// PLEASE READ - Useful notes about this Project

// Although this project is made up of several files, the most important ones are:
//	 "ContinuousADC-Main.C"	- this file
//		- Application Initialization, Peripheral config
//		- Application management
//		- Slower background code loops and Task scheduling
//	 "ContinuousADC-DevInit_F28xxx.C
//		- Device Initialization, e.g. Clock, PLL, WD, GPIO mapping
//		- Peripheral clock enables
// The other files are generally used for support and defining the registers as C
// structs. In general these files will not need to be changed.
//   "F28022_RAM_ContinuousADC.CMD" or "F28022_FLASH_ContinuousADC.CMD"
//		- Allocates the program and data spaces into the device's memory map.
//   "DSP2802x_Headers_nonBIOS.cmd" and "DSP2802x_GlobalVariableDefs.c"
//		- Allocate the register structs into data memory.  These register structs are
//		  defined in the peripheral header includes (DSP2802x_Adc.h, ...)
//
//----------------------------------------------------------------------------------

#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


