// TI File $Revision: /main/4 $
// Checkin $Date: April 18, 2005   10:07:48 $
//###########################################################################
//
// FILE:	Example_freqcal.c
//
// TITLE:	Frequency measurement using EQEP peripheral
//
// DESCRIPTION:
//
// This file contains source for the freq calculation module
//
//###########################################################################
// Original Author: SD
//
// $TI Release: DSP280x V1.10 $
// $Release Date: April 18, 2005 $
//###########################################################################

#include "DSP280x_Device.h"     // DSP280x Headerfile Include File
#include "DSP280x_Examples.h"   // DSP280x Examples Include File
#include "Example_freqcal.h"    // Example specific include file

void  FREQCAL_Init(void)
{

		EQep1Regs.QUPRD=1000000;			// Unit Timer for 100Hz
		EQep1Regs.QDECCTL.bit.QSRC=2;		// QEP clock Inputs
		EQep1Regs.QDECCTL.bit.XCR=0;
		
		EQep1Regs.QEPCTL.bit.FREE_SOFT=2;
		EQep1Regs.QEPCTL.bit.PCRM=00;		// PCRM=00 mode
		EQep1Regs.QEPCTL.bit.UTE=1; 		// Unit Timeout Enable 
		EQep1Regs.QEPCTL.bit.QCLM=1; 		// Latch on unit time out
		EQep1Regs.QPOSMAX=0xffffffff;
		EQep1Regs.QEPCTL.bit.QPEN=1; 		// QEP enable
		
		EQep1Regs.QCAPCTL.bit.UPPS=3;   	// 1/8 for unit position
		EQep1Regs.QCAPCTL.bit.CCPS=7;		// 1/128 for CAP clock
		EQep1Regs.QCAPCTL.bit.CEN=1; 		// QEP Capture Enable
		

        EALLOW;                       		// Enable EALLOW
        GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 1;// Assuming GPIO20 is EQEP1A
        GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 1;// Assuming GPIO21 is EQEP1B
        GpioCtrlRegs.GPAMUX2.bit.GPIO23 = 1;// Assuming GPIO23 is EQEP1I
        EDIS;                         		// Disable EALLOW

}

void FREQCAL_Calc(FREQCAL *p)
{
     unsigned long tmp;
   	 _iq newp,oldp;

// Check unit Time out-event for speed calculation:
// Unit Timer is configured for 100Hz in INIT function
//**** Freq Calcultation using QEP position counter ****//
	if(EQep1Regs.QFLG.bit.UTO==1)
	{ 
		/** Differentiator	**/
	 	newp=EQep1Regs.QPOSLAT;	// Latched POSCNT value		
		oldp=p->oldpos;

    	if (newp>oldp)
      		tmp = newp - oldp;
    	else
      		tmp = (0xFFFFFFFF-oldp)+newp;
   	

		p->freq_fr = _IQdiv(tmp,p->freqScaler_fr); 
		tmp=p->freq_fr;
	
		if (tmp>=_IQ(1))
	 		p->freq_fr = _IQ(1);   
		else
	 		p->freq_fr = tmp;
	
		p->freqhz_fr = _IQmpy(p->BaseFreq,p->freq_fr); 	// Q0 = Q0*GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
		
		// Update position counter
    	p->oldpos = newp;
		//=======================================

		EQep1Regs.QCLR.bit.UTO=1;					// Clear interrupt flag
	}	

//**** Freq Calcultation using QEP capture counter ****//	
	if(EQep1Regs.QEPSTS.bit.UPEVNT==1)
	{
		if(EQep1Regs.QEPSTS.bit.COEF==0) // No Capture overflow
			tmp=(unsigned long)EQep1Regs.QCPRDLAT;
		else							// Capture overflow, saturate the result
			tmp=0xFFFF;
		
		p->freq_pr = _IQdiv(p->freqScaler_pr,tmp); 
		tmp=p->freq_pr;
	
		if (tmp>_IQ(1))
	 		p->freq_pr = _IQ(1);   
		else
	 		p->freq_pr = tmp;
	
		p->freqhz_pr = _IQmpy(p->BaseFreq,p->freq_pr); 	// Q0 = Q0*GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
	
		EQep1Regs.QEPSTS.all=0x88;					// Clear Unit position event flag	
													// Clear overflow error flag
	}


}


