//****************************************************************************
// @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 	 cCCH C_SDATA	cCCH0;
	#endif  //end of keil
	#if DAVECG_ControlAlgoKBS.Tasking
	extern 	 cCCH 			cCCH0;	
	#endif //end of Tasking
	extern 	uword 	C_SDATA 	Current_Ref;
//****************************************************************************
// @Global Variables
//****************************************************************************

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

 extern void MotorStop(void); 

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

INLINE void OverCurrentCheck(void)
{
    if (cCCH0.Motor_Current >= CURRENT_MAX)						   	// motor instantaneous current higher than
    {													   			// specified limit ?
        cCCH0.I_Counter++;									   			// if yes, increment s/w counter.
        if (cCCH0.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 (cCCH0.I_Counter > 0)							   			// then.....
        {
            cCCH0.I_Counter--;								   			// ....decrement the s/w counter.
        }
    }
}

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

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

