//****************************************************************************
// @Module        Block Commutation
// @Filename      CTRL.H
//----------------------------------------------------------------------------
// @Controller    Infineon XE164F-96F66
//
//
// @Description   This file contains function prototypes for Block Commutation 
//                
//
//****************************************************************************

#ifndef _CTRL_H_
#define _CTRL_H_

//****************************************************************************
// @Project Includes
//****************************************************************************
#include "MAIN.H"

//****************************************************************************
// @Project Includes
//****************************************************************************
 
//****************************************************************************
// @Macros
//****************************************************************************

//****************************************************************************
// @Defines
//****************************************************************************

//****************************************************************************
// @Typedefs
//****************************************************************************

//****************************************************************************
// @Imported Global Variables
//****************************************************************************
 	extern 	bit 				gb_Ramp;
	extern  bit					gb_overcurrent;
 
    #if DAVECG_ControlAlgoKBS.KEIL
	extern 	 cSCCH C_SDATA	cSCCH0;
	#endif  //end of keil
	#if DAVECG_ControlAlgoKBS.Tasking
	extern 	 cSCCH 			cSCCH0;	
	#endif //end of Tasking 
//****************************************************************************
// @Global Variables
//****************************************************************************

//****************************************************************************
// @External Prototypes
//****************************************************************************

 extern void MotorStop(void); 

//****************************************************************************
// @Prototypes Of Global Functions
//****************************************************************************

INLINE void SpeedRampup(void)
{
	if(cSCCH0.Ramp_Scheduler>=cSCCH0.Rampup_Counter)	 
	{
		if(cSCCH0.Speed_End_Ref > cSCCH0.Speed_Ref)								// End ref not reached?
		{                                                      
			cSCCH0.Speed_Ref += 1; 											// then increment. 
			gb_Ramp=1;							                    
		}         
		else if(cSCCH0.Speed_End_Ref < cSCCH0.Speed_Ref)						// actual ref greater than end ref?
		{                                                                         
			cSCCH0.Speed_Ref -= 1;											// then decrement.  
			gb_Ramp=1;
		}
		else
			gb_Ramp=0;                 
		
	 	cSCCH0.Ramp_Scheduler=1;
	}
	else
		cSCCH0.Ramp_Scheduler++;
}

INLINE void OverCurrentCheck(void)
{
    if (cSCCH0.Motor_Current >= CURRENT_MAX)						   	// motor instantaneous current higher than
    {													   			// specified limit ?
        cSCCH0.I_Counter++;									   			// if yes, increment s/w counter.
        if (cSCCH0.I_Counter == TIME_CURRENT_MAX)			   			// s/w counter reached the specified limit ?
        {
			 gb_overcurrent = 1;						   
		     MotorStop();								   
        }
    }
    else												   			// if motor 'current' sample is not higher
    {													   			// than the specified limit....
        if (cSCCH0.I_Counter > 0)							   			// then.....
        {
            cSCCH0.I_Counter--;								   			// ....decrement the s/w counter.
        }
    }
}

INLINE void Current_Measurement(void)
{
	cSCCH0.Motor_Current = (ADC0_RESR0<<1)& 0x1FFF;
	OverCurrentCheck();
}

INLINE void DutyCycleUpdate(void)
{
 	if(cSCCH0.Duty_Cyc < DUTY_CYCLE_MIN)
		cSCCH0.Duty_Cyc = DUTY_CYCLE_MIN;
	else if(cSCCH0.Duty_Cyc > DUTY_CYCLE_MAX)
		cSCCH0.Duty_Cyc = DUTY_CYCLE_MAX;
	
	CCU60_CC63SR = cSCCH0.Duty_Cyc;									// ....load it into the PWM duty cycle reg.
	CCU60_TCTR4 = 0x4000;
}
#endif	  //end of _CTRL_H_

