//****************************************************************************
// @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;
 	extern 	sword	C_SDATA		g_bemf_aux;
  	extern 	uword  	C_SDATA		g_speed_CC60;
 	extern 	ubyte 	C_SDATA		g_speed_counter;
 	extern 	ulong	C_SDATA		speed_buf;
 	extern  uword	C_SDATA 	Channel_N;
 	extern 	uword 	C_SDATA		compare_value;
 	extern 	uword 	C_SDATA		high_limit;						      
 	extern 	uword 	C_SDATA		low_limit;
 	extern 	sword	C_SDATA		g_i_counter;
 	#if DAVECG_ControlAlgoKBS.KEIL
    extern   	cCCB  C_SDATA	cCCB0;	
    #endif //End of Keil
    #if DAVECG_ControlAlgoKBS.Tasking
    extern   	cCCB  			cCCB0; 
    #endif //End of TASKING 
    extern 	uword 	C_SDATA 	Current_Ref;

//****************************************************************************
// @Global Variables
//****************************************************************************

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

 extern void MotorStop(void); 

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

INLINE void Channel_Select(void)  
{	  
	g_bemf_aux = (CCU60_MCMOUT & 0x003F);									// read the present Modulation Pattern....
	
	if((g_bemf_aux == BEMF_AUX_VAL11) || (g_bemf_aux == BEMF_AUX_VAL12)) 
	{																		// if phase '1' is Inactive....
		ADC0_CRPR1 = 0x0001;												// write channel information to be converted
	} 
	if((g_bemf_aux == BEMF_AUX_VAL21) || (g_bemf_aux == BEMF_AUX_VAL22)) 
	{																		// if phase '2' is Inactive.... 
		ADC0_CRPR1 = 0x0008;												// write channel information to be converted
	} 
	if((g_bemf_aux == BEMF_AUX_VAL31) || (g_bemf_aux == BEMF_AUX_VAL32)) 
	{																		// if phase '3' is Inactive.... 
		ADC0_CRPR1 = 0x0200;												// write channel information to be converted
	}
 }


#if DAVECG_ControlAlgoKBS.SPEED_CALCU
INLINE void Speed_Filter(void)
{
	cCCB0.Motor_Speed	= g_speed_CC60-cCCB0.Motor_Speed;
	speed_buf			= speed_buf+(long)(cCCB0.Motor_Speed);
	cCCB0.Motor_Speed	= (int)(speed_buf>>8);
}
#endif
INLINE void OverCurrentCheck(void)
{
    if (cCCB0.Motor_Current >= CURRENT_MAX)						   	// motor instantaneous current higher than
    {													   			// specified limit ?
        g_i_counter++;									   			// if yes, increment s/w counter.
        if (g_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 (g_i_counter > 0)							   			// then.....
        {
            g_i_counter--;								   			// ....decrement the s/w counter.
        }
    }
}

INLINE void Current_CV_Measurement(void)
{
	  	Channel_N	=	(ADC0_RESR0>>12) & 0x000F;								// read channel information 
		if (Channel_N == 15)		
		{
			ADC0_QINR0 = 0x0008;
			compare_value =  (ADC0_RESR0>>3)&0x01FF;
			high_limit		=	compare_value + (compare_value >> 4);
			low_limit		=	compare_value - (compare_value >> 4);
		}	
		else if(Channel_N == 8)
		{
			ADC0_QINR0 = 0x000F;
			cCCB0.Motor_Current = (ADC0_RESR0<<1)& 0x1FFF;
			OverCurrentCheck();
		} 
		else
			ADC0_QINR0 = 0x000F;												// write channel information to be converted

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

}

#endif	  //end of _CTRL_H_

