/**********************************************************************
* File: Main_14.c -- File for Lab 14
* Devices: TMS320F2806x
* Author: Technical Training Organization (TTO), Texas Instruments
* History:
*   06/15/11 - original
**********************************************************************/

#include "Lab.h"						// Main include file

//--- Global Variables
Uint16 DEBUG_TOGGLE = 1;					// Used for realtime mode investigation test
Uint16 DEBUG_FILTER = 1;				// Used to turn filter on/off
Uint16 AdcBuf[ADC_BUF_LEN];					// ADC buffer allocation
Uint16 AdcBufFiltered[ADC_BUF_LEN];		// ADC filtered data buffer allocation
_iq AdcBufIQ[ADC_BUF_LEN];				// ADC data buffer allocation - IQ
_iq AdcBufFilteredIQ[ADC_BUF_LEN];		// ADC filtered data buffer allocation - IQ
Uint32 PwmDuty;								// measured PWM duty cycle
Uint32 PwmPeriod;							// measured PWM period
Uint16 ClaFilteredOutput;				// Output of CLA filter
long	GlobalQ = GLOBAL_Q;				// IQmath debug support

_iq xDelayIQ[FILTER_LEN] = {0,0,0,0,0};	// filter delay chain
_iq coeffsIQ[FILTER_LEN] = {_IQ(0.0625),_IQ(0.25),_IQ(0.375),_IQ(0.25),_IQ(0.0625)};	// filter coefficients

float32 xDelay[FILTER_LEN] = {0,0,0,0,0};	// filter delay chain
float32 coeffs[FILTER_LEN] = {0.0625, 0.25, 0.375, 0.25, 0.0625};	// filter coefficients

#pragma DATA_SECTION(xDelay, "ClaToCpuMsgRAM");
#pragma DATA_SECTION(ClaFilteredOutput, "ClaToCpuMsgRAM");
#pragma DATA_SECTION(coeffs, "CpuToClaMsgRAM");

#pragma DATA_SECTION(AdcBufRaw, "dmaMemBufs");
Uint16 AdcBufRaw[2*ADC_BUF_LEN];			// ADC raw data buffer allocation


/**********************************************************************
* Function: main()
*
* Description: Main function for C28x workshop labs
**********************************************************************/
void main(void)
{
//--- CPU Initialization
	InitSysCtrl();						// Initialize the CPU (FILE: SysCtrl.c)
	InitGpio();							// Initialize the shared GPIO pins (FILE: Gpio.c)
	InitPieCtrl();						// Initialize and enable the PIE (FILE: PieCtrl.c)
	InitWatchdog();						// Initialize the Watchdog Timer (FILE: WatchDog.c)

//--- Copy all Flash sections that need to run from RAM (use memcpy() from RTS library)

// Section secureRamFuncs contains user defined code that runs from CSM secured RAM
	memcpy(&secureRamFuncs_runstart, &secureRamFuncs_loadstart, (Uint32)&secureRamFuncs_loadsize);

//--- Initialize the Flash and OTP
	InitFlash();						// Initialize the Flash

//--- Peripheral Initialization
	InitAdc();							// Initialize the ADC (FILE: Adc.c)
	InitEPwm();							// Initialize the EPwm (FILE: EPwm.c)
	InitECap();							// Initialize the ECap (FILE: ECap.c)
//	InitDma();							// Initialize the DMA (FILE: Dma.c)
	InitCla();							// Initialize the Cla (FILE: Cla.c)

//--- Enable global interrupts
	asm(" CLRC INTM, DBGM");			// Enable global interrupts and realtime debug

//--- Main Loop
	while(1)							// endless loop - wait for an interrupt
	{
		asm(" NOP");
	}


} //end of main()



/**********************************************************************
* Function: UserInit()
*
* Description: This is the user initialization file to be specified in
* the DSP/BIOS configuration file, System - Global Settings.
**********************************************************************/
void UserInit(void)
{

// Section .trcdata is generated by DSP/BIOS.
// It must be copied from its load to its run address BEFORE main().
	memcpy(&trcdata_runstart, &trcdata_loadstart, (Uint32)&trcdata_loadsize);

} // end of UserInit()



/**********************************************************************
* Function: LedBlink()
*
* Description: Blinks LED on Control Card. 
**********************************************************************/
void LedBlink(void)
{
static Uint16 LedSwiCount=0;					// Used for LOG_printf example

//--- Show an example of using LOG_printf() to write to a log buffer ---//
	LOG_printf(&trace, "LedSwiCount = %u", LedSwiCount++);

	GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1;		// Toggle the pin

} // end of LedBlink()



/**********************************************************************
* Function: Cla1Swi()
*
* Description: Cla1 interrupt SWI
**********************************************************************/
void Cla1Swi(void)
{
static Uint16 *AdcBufPtr = AdcBuf;					// Pointer to buffer
static Uint16 *AdcBufFilteredPtr = AdcBufFiltered;	// Pointer to ADC filtered data buffer

//--- Manage the ADC registers     
	AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1;		// Clear ADCINT1 flag

//--- Read the ADC result
	*AdcBufPtr++ = 	AdcResult.ADCRESULT0;     	// Read the result

//--- Read the CLA filter result
	*AdcBufFilteredPtr++ = ClaFilteredOutput++;	// Read the CLA filter output

//--- Brute-force the circular buffer
	if( AdcBufPtr == (AdcBuf + ADC_BUF_LEN) )
	{	
		AdcBufPtr = AdcBuf;						// Rewind the pointer to the beginning
		AdcBufFilteredPtr = AdcBufFiltered;		// Rewind the pointer to the beginning
	}

} // end of Cla1Swi()



/*** end of file *****************************************************/
