//************************************************************************************************/
// @Module              Voltage Control
// @Filename            VCB_CTRL.H
// @Controller          XC16x Series
//
//
// @Description     This file contains references for Voltage control through 
//                              Block Commutation   
//************************************************************************************************/
#ifndef _VCB_CTRL_H_
#define _VCB_CTRL_H_

//*************************************************************************************************
// @Includes
//*************************************************************************************************


//*************************************************************************************************
// @Macros
//*************************************************************************************************
#define CTRL_VAR_DECL()\
    extern BIT gb_motor_status;\
    extern BIT gb_hs_error;\
    extern BIT gb_ctrap;\
    extern BIT gb_time_out;\
    extern BIT gb_overcurrent;\
    extern BIT gb_idle_state;\
    extern int C_SDATA g_adc_mem;\
    extern int C_SDATA g_direction;\
    extern int C_SDATA g_once;\
    extern int C_SDATA g_i_counter;\
    extern C_BDATA g_status_word;\
	extern unsigned char C_SDATA injection_channel_select;\
	extern int C_SDATA g_current_max;\
    extern int C_SDATA g_pic_speed_out;\
    extern int C_SDATA g_trap_reset;\
    extern long C_SDATA g_adc_offset_res;\
    extern int C_SDATA g_increment_free;\
    extern int C_SDATA g_current;\
    extern int C_SDATA g_i_mpattern;\
    extern int C_SDATA g_counter0;\
    extern int C_SDATA g_volt_increment;\
    extern int C_SDATA g_freq_increment;\
    extern int C_SDATA g_speed_counter;\
    extern int C_SDATA g_limit;\
	extern int C_SDATA g_direction_user;\
    extern unsigned int C_SDATA g_bemf_check_freq;\
    extern unsigned int C_SDATA g_bemf_end_freq;\
    extern unsigned int C_SDATA g_nv_start_ref;\
    extern unsigned int C_SDATA g_nv_ref;\
    extern unsigned int C_SDATA g_nv_end_ref;\
    extern unsigned int C_SDATA g_start_duty_c;\
    extern unsigned int C_SDATA g_duty_cycle;\
    extern unsigned int C_SDATA g_speed_CC61;\
    extern unsigned int C_SDATA g_state;\
    extern unsigned int C_SDATA g_slope;\
    extern unsigned int C_SDATA g_bemf_aux;\
    extern unsigned int C_SDATA g_adc_ctr_bemf;\
    extern unsigned int C_SDATA g_circ_index;\
	extern unsigned int C_SDATA g_start_freq;\
    extern unsigned long C_SDATA g_ramp_up_time;\
    extern unsigned int C_SDATA g_time_measure;\
	extern unsigned int C_SDATA COMPARE_VALUE;\
	extern unsigned int C_SDATA HIGH_LIMIT;\
	extern unsigned int C_SDATA LOW_LIMIT;
   
#define CTRL_FUNC_DECL()\
    extern void motor_start(void);\
    extern void motor_stop(void);\
    extern void commutation_init(void);\
    extern void init_control(void);\
    extern void commutation(void); \
    extern void duty_cycle_update(void);\
    extern void blink_led(void);\
    extern void adc_offset(void);\
    extern void motor_ramp_generation(void);\
	#if DAVECG_SPEED_CALCU
    extern int speed_calculation(unsigned int time_measurement, int *speed_counter, int limit);
	#endif

//*************************************************************************************************
CTRL_VAR_DECL()
CTRL_FUNC_DECL()
#if DAVECG_CONTROL_PANEL_ENABLE
COMM_FUNC_DECL()
#endif


#if DAVECG_CURRENT_SENSE
#if DAVECG_KEIL
void ADC_viError(void);
#endif

#if DAVECG_TASKING
#if REG_BANK_ADC == 1
interrupt (ADC_E_INT) _localbank(-1)_cached void ADC_viError(void);
#endif 
#if REG_BANK_ADC == 2
interrupt (ADC_E_INT) using (Reg_bank_ADC) void ADC_viError(void);
#endif
#endif // TASKING == 1
#endif // CURRENT_SENSE == 1

//*************************************************************************************************
#define		REQUESTED				1
#define		RESET					0
#define		MOTOR_READY_TO_START	0
#define		MOTOR_STOPPED			1
//*************************************************************************************************
//*************************************************************************************************
// @Function        void SET_CC2_REG(void)     
// Description		This Function initialises the CC2 control registers with appropriate values.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void SET_CC2_REG(void)
{
#if DAVECG_BTS
    SRCP2		=	C_SEG(g_adc_ctr_bemf);				   // Initialise the PEC CH2 source pointer.
    DSTP2		=	C_SOF(ADC_CTR0);					   // Initialise the PEC CH2 dest pointer.
    CC2_T7      =	T7_PERIOD;							   // Initialise CC2_T7 timer.
    CC2_T7REL   =	T7_PERIOD;							   // specify the reload value for CC2_T7.
    CC2_CC19    =	CC19_VALUE;							   // Initialise CC2 channel 19.
    CC2_CC31    =	CC31_VALUE;							   // Initialise CC2 channel 31.
#elif DAVECG_TLE
    SRCP2		=	C_SEG(g_adc_ctr_bemf);				   // Initialise the PEC CH2 source pointer.
    DSTP2		=	C_SOF(ADC_CTR0);					   // Initialise the PEC CH2 dest pointer. 
    REFRESH_CODE_BLOCK();								   // execute driver 'refresh' routine.
    CC2_T7      =	T7_PERIOD; 							   // Initialise CC2_T7 timer.               
    CC2_T7REL   =	T7_PERIOD; 						       // specify the reload value for CC2_T7.   
    CC2_CC19    =	CC19_VALUE;						       // Initialise CC2 channel 19.              
    CC2_CC31    =	CC31_VALUE;						       // Initialise CC2 channel 31.             
#endif // if (BTS == 1) && (BEMF == 1)
}
//*************************************************************************************************

//*************************************************************************************************
// @Function        void SET_TRAP_STATUS(void)     
// Description		This Function will set the s/w 'trap' flag.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void SET_TRAP_STATUS(void)
{
	gb_ctrap = 1;										   // set the s/w trap flag to indicate 
														   // the detection of 'CTRAP'
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void TIME_OUT(void)     
// Description		This Function will set the s/w 'time out' flag.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void TIME_OUT(void)
{
	gb_time_out  = 1;									   // set the s/w time out flag to indicate  
														   // the detection of 'Time out'           
}
//*************************************************************************************************
#if DAVECG_CURRENT_SENSE
//*************************************************************************************************
// @Function        void OVR_CURRENT_CHECK(void)     
// Description		This Function implements the logic required for 'over current' detection.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void OVR_CURRENT_CHECK(void)
{
    if (g_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 ?
        {
            motor_stop();								   // if yes, then stop the motor.
            gb_motor_status = 1;						   // Set the 'motor status' flag to convey 
														   // to the control panel that motor has been stopped.
            gb_overcurrent = 1;							   // Set the 'overcurrent' flag to convey 
														   // the same to the control panel
        }
    }
    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.
        }
    }
}
//*************************************************************************************************
#endif	// CURRENT_SENSE
//*************************************************************************************************
// @Function        void TRAP_RESET(void)     
// Description		This Function will Unlatch/Reset the 'trap' flag if requested.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void TRAP_RESET(void)
{
    if (g_trap_reset == REQUESTED)						   // 'trap' reset requested ?
    { 
        g_trap_reset	= RESET;						   // if yes, acknowledge the request....
        gb_ctrap		= RESET;						   // ...and reset the s/w 'trap' flag.
    }
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void MOTOR_OPERATION_CONTROL(void)     
// Description		This Function will start or stop the motor depending on present inputs.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void MOTOR_OPERATION_CONTROL(void)
{
    if (gb_ctrap)										   // 'TRAP' event recorded ?
    {													   // if yes.....
        gb_motor_status = MOTOR_STOPPED;				   // ....then stop the motor.
    } 
    else												   // otherwise....
    { 
        if (gb_motor_status == MOTOR_READY_TO_START)	   // ....motor start requested by user ?
        {												   // if yes.....
            motor_start();								   // execute motor_start routine.
        } 
        else											   // if not requested....
        { 
            motor_stop();								   // then execute motor_stop routine.
        } 
    }
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void CONTROL_PANEL_PARAMS(void)     
// Description		This Function loads the values entered by the user in the 'Run Time Control Panel'
//					dialog of DID into the appropriate target variables. 
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void CONTROL_PANEL_PARAMS(void)
{
    g_trap_reset	=	1;								   // set the 'trap_reset' s/w flag so that 
														   // s/w 'trap' flag is cleared under power ON.
    gb_ctrap		=	0;								   // clear the s/w trap flag to begin with.
	g_nv_end_ref	=	MOTOR_END_REF;					   // Initialise the end reference with GUI(user) value.
	g_nv_start_ref	=	MOTOR_START_REF;				   // Initialise the start reference with GUI(user) value.
	g_ramp_up_time	=	RAMP_UP_TIME;					   // Initialise the rampup time with GUI(user) value.
	g_direction_user=	MOTOR_DIRECTION;				   // Initialise the motor direction variables
	g_direction		=	g_direction_user * 8;			   // with GUI(user) specified direction.
	#if DAVECG_CONTROL_PANEL_ENABLE
	gb_motor_status	=	1;								   // set the motor_status flag if motor
														   // start not requested by GUI(user).
	#else
	gb_motor_status =	0;								   // clear the motor_status flag if motor
														   // start is requested by GUI(user).
	#endif
}

//*************************************************************************************************
//*************************************************************************************************
// @Function        void BEMF_PARAM_INIT(void)     
// Description		This function loads the user entered values for back EMF start-up parameters
//					from the Run time control panel dialog of DID into the corresponding 
//					target variables.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void BEMF_PARAM_INIT(void)
{
	g_bemf_check_freq  = BEMF_CHECK_FREQ;				   // initialise 'bemf check freq' with GUI(user) value.       
	g_bemf_end_freq    = END_FREQUENCY;	 				   // initialise 'bemf end freq' with GUI(user) value.         
	g_freq_increment   = FREQ_INC;		 				   // initialise 'bemf freq increment' with GUI(user) value.   
	g_volt_increment   = VOLT_INC;		 				   // initialise 'bemf volt increment' with GUI(user) value.  
	g_start_duty_c     = START_DUTY_CYC; 				   // initialise 'bemf start duty cycle' with GUI(user) value. 
	g_start_freq       = START_FREQ;	 				   // initialise 'bemf start frequency' with GUI(user) value.  
}

//*************************************************************************************************
//*************************************************************************************************
// @Function        void INIT_PI_VAR(void)     
// Description		....
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void INIT_PI_VAR(void)
{
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void INIT_CTRL_VAR(void)     
// Description		This function initialises the various intermediate variables that are required to 
//					be set to pre-defined default values before any motor related operation.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void INIT_CTRL_VAR(void)
{
    g_start_duty_c	= 0x0;							   // Initialize BEMF start_duty_cycle to '0'.             
    g_direction		= 0x0;							   // Initialise default motor direction to 'clockwise'.   
    g_once			= 0x0;							   // Clear the variable 'once' so that by default         
						  							   // motor stop is assumed.                              
    g_status_word	= 0x0002;						   // Initialise status flags to reflect motor stopped condition.
	INIT_PI_VAR();									   // execute PI initialisation routine.
	#if DAVECG_BLOCK_COMMUTATION
	CONTROL_PANEL_PARAMS();							   // execute control panel dependent variable initialisation routine.
	#endif
	#if DAVECG_BEMF
	BEMF_PARAM_INIT();								   // execute Back EMF related variable initialisation routine.
	#endif
}
//*************************************************************************************************
#if DAVECG_KEIL
#define ADC_ERROR_BANK1_SIG()\
void ADC_viError(void) interrupt ADC_E=CACHED using _FAST_ABANK1_ \
{

#define ADC_ERROR_BANK2_SIG()\
void ADC_viError(void) interrupt ADC_E_INT using Reg_bank_ADC \
{
#endif	//KEIL
//*************************************************************************************************
#if DAVECG_TASKING
#define ADC_ERROR_BANK1_SIG()\
interrupt (ADC_E_INT) _localbank(-1)_cached void ADC_viError(void) \
{

#define ADC_ERROR_BANK2_SIG()\
interrupt (ADC_E_INT) using (Reg_bank_ADC) void ADC_viError(void) \
{
#endif	//TASKING
//*************************************************************************************************

//*************************************************************************************************
//*************************************************************************************************
// @Function        void ADC_OFFSET(void)     
// Description		This function computes the ADC offset value, by taking 
//					65,535 samples of the ADC measured data. This computed
//					offset value is preserved in the variable g_adc_offset_res.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void ADC_OFFSET(void)
{
	#if DAVECG_CURRENT_SENSE
		adc_offset();									   // compute 'adc_offset' value.
	#endif // CURRENT_SENSE ==1
}        
//*************************************************************************************************
//*************************************************************************************************
// @Function        void ADC_AUX_BEMF(void)     
// Description		This function determines which of the three motor phases is presently 
//					not activated, and sets the corresponding ADC channel for Zero crossing detection.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void ADC_AUX_BEMF(void)
{
	if((g_bemf_aux == BEMF_AUX_VAL11) || (g_bemf_aux == BEMF_AUX_VAL12)) 
    {													   // if phase '1' is Inactive....
        g_adc_ctr_bemf = BEMF_CTRL_VAL1;				   // ....then set ADC CH1 for Zero Cross Detection.
    } 
    if((g_bemf_aux == BEMF_AUX_VAL21) || (g_bemf_aux == BEMF_AUX_VAL22)) 
    {													   // if phase '2' is Inactive.... 
        g_adc_ctr_bemf = BEMF_CTRL_VAL2;				   // ....then set ADC CH2 for Zero Cross Detection. 
    } 
    if((g_bemf_aux == BEMF_AUX_VAL31) || (g_bemf_aux == BEMF_AUX_VAL32)) 
    {													   // if phase '3' is Inactive.... 
        g_adc_ctr_bemf = BEMF_CTRL_VAL3;				   // ....then set ADC CH3 for Zero Cross Detection. 
    }
}
//*************************************************************************************************       
INLINE void SET_MONITOR_STATUS(void)
{
    motor_stop(); 
    gb_motor_status = 1;
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void MCMOUT_UPDATE(void)     
// Description		This function loads the appropriate 'modulation pattern' into the MCMOUTS register.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void MCMOUT_UPDATE(void)
{

}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void SET_ADC_SRC_DST_PTR(void)     
// Description		This function initialises the PEC channel 7 source & destination pointers
//					with appropriate variables.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void SET_ADC_SRC_DST_PTR(void)
{
    SRCP7 = C_SOF(ADC_DAT);								   // point the source pointer to ADC result register.
    DSTP7 = C_SOF(g_adc_mem);							   // point the destination pointer to global variable.
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void SET_ADC_CTR2(void)     
// Description		This function initialises the ADC control register and ADC injection 
//					mode control reg with conversion time, sampling time etc.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void SET_ADC_CTR2(void)
{
	#if CPU_FREQUENCY <= 20
    ADC_CTR2	= 0x0000;								   // select the conversion time, sampling time 
														   // and resolution of the ADCs.
    ADC_CTR2IN	= 0x0000;								   // select the Injection mode -conversion time, 
														   // sampling time and resolution of injection 
														   // channels.
	#endif // CPU_FREQUENCY <= 20
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void SET_ADC_CTR0(void)     
// Description		This function initialises the ADC control registers with 
//					with appropriate values.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void SET_ADC_CTR0(unsigned int X, int Y, int Z)
{
		int temp;
		ADC_CTR0 = (X);									   // initialise ADC control reg '0'.
		temp = Y;
		temp = Z;
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void ADC_INIT3_CBLOCK2(void)     
// Description		This function executes the routines which initialise the ADC control registers.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void ADC_INIT3_CBLOCK2(unsigned int X, int Y, int Z)
{
    SET_ADC_SRC_DST_PTR();								   // execute PEC channel source & dest pointer 
														   // initialisation routine.
    SET_ADC_CTR2();										   // execute the routine which configures the
														   // conversion, sampling times & resolution.
    SET_ADC_CTR0(X, Y, Z);								   // execute the routine which selects the Mode
														   // of operation, Interrupts, etc for ADCs.
}
//*************************************************************************************************
INLINE void RESET_CCU6_WHE(void)
{

}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void CCU6_NI2_BEMF_SLOPE_CBLOCK0(void)     
// Description		This function specifies whether we need to look for 'rising' edge
//					or 'falling' edge of the Back EMF signal.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void CCU6_NI2_BEMF_SLOPE_CBLOCK0(void)
{
    if (g_slope == 2)									   // ...???......
    {
        g_slope = 0;									   // we need to look for 'rising' edge.
    }
    if (g_slope == 3)									   // ...???.....
    {	
        g_slope = 1;									   // we need to look for 'falling' edge.
    }
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void CCU6_CC6SR_LOAD_VALUE(void)     
// Description		....
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void CCU6_CC6SR_LOAD_VALUE(void)
{
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void MOTOR_RAMP_GEN(void)     
// Description		This function implements the ramping of motor speed to the new end reference.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void MOTOR_RAMP_GEN(void)
{
	motor_ramp_generation();							   // ramp up the motor voltage to the 
														   // new end reference.
}
//*************************************************************************************************
#if DAVECG_SPEED_CALCU
//*************************************************************************************************
// @Function        void CCU6_NI2_SPEED_CAL_CBLOCK(void)     
// Description		This function calls the 'speed calculation' routine to get the present motor speed.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void CCU6_NI2_SPEED_CAL_CBLOCK(void)
{
    if (g_state == 3)									   // if motor is running in steady state( state 3)...
    {
        g_speed_CC61 = speed_calculation(g_time_measure, &g_speed_counter, (6*PP));
    }													   // ....then get the present 'speed'.

    if (g_speed_counter < (LIMIT_C))					   // if motor speed is too small....
    {
        g_speed_CC61 = 0;								   // ....then reset speed to '0'.
    }
}
//*************************************************************************************************
#endif
//*************************************************************************************************
// @Function        void ADC_INIT3_CBLOCK(void)     
// Description		This function Initialises the ADC control registers.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void ADC_INIT3_CBLOCK(void)
{
	#if DAVECG_BTS
	#if DAVECG_CURRENT_SENSE
	ADC_INIT3_CBLOCK2(0x9603, 0x0028, 0x000E);			   // execute ADC initialisation routines.
	#else
	ADC_INIT3_CBLOCK2(0x9604, 0x0028, 0x0017);			   // execute ADC initialisation routines.
	#endif
	#endif 	//BTS
	
#if DAVECG_TLE
	#if DAVECG_CURRENT_SENSE
	ADC_INIT3_CBLOCK2(0x9601, 0x0028, 0x000E);			   // execute ADC initialisation routines.
	#else
	ADC_INIT3_CBLOCK2(0x8201, 0x0028, 0x000E);			   // execute ADC initialisation routines.
	#endif
#endif
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void ADC_ERR2_CBLOCK(void)     
// Description		This function implements the following:
//					1. reads motor 'current' from ADC result register.
//					2. Then call 'PI controller' algorithm to compute new duty cycle.
//					3. update new duty cycle into the compare register and
//					4. check for 'over current' event.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void ADC_ERR2_CBLOCK(void)
{
//************************************************************************
#if DAVECG_BTS
 	//********************************************************************
		#if DAVECG_XC164CS
		if ((ADC_CTR0_ADBSY == 0)&&(ADC_CTR0_ADCRQ==0) && (ADC_CTR0_SMPL==0))
		#endif
		#if DAVECG_XC164CM
		if ((ADC_CTR0_ADBSY == 0)&&(ADC_CTR0_ADCRQ==0) && (ADC_CTR0_SAMPLE==0))
		#endif		
		{
			#if DAVECG_CURRENT_SENSE
			injection_channel_select = (unsigned char)((ADC_DAT2 >> 8)&0x00f0);
			if(injection_channel_select == 0x30)
			{
			g_current 	= ((ADC_DAT2 << 3) & 0x7FFF) - g_adc_offset_res;
			ADC_DAT2 	&= 0x0fff;
			ADC_DAT2 	|= 0x4000;
			}
			else if(injection_channel_select==0x40)
			{
			COMPARE_VALUE 	 	= (ADC_DAT2 & 0x0FFC);
			COMPARE_VALUE		=	COMPARE_VALUE>>1;
			ADC_DAT2 			&= 0x0fff;
			ADC_DAT2 			|= 0x3000;

			HIGH_LIMIT			=	COMPARE_VALUE + (COMPARE_VALUE >> 4); 
			LOW_LIMIT			=	COMPARE_VALUE - (COMPARE_VALUE >> 4);
			}
			#else
			COMPARE_VALUE 	 	= 	(ADC_DAT2 & 0x0FFC);
			COMPARE_VALUE		=	COMPARE_VALUE>>1;
			HIGH_LIMIT			=	COMPARE_VALUE + (COMPARE_VALUE >> 4); 
			LOW_LIMIT			=	COMPARE_VALUE - (COMPARE_VALUE >> 4);
			#endif
	  	}
	//************************************************************************
#endif	//BTS
//*************************************************************************************************
#if DAVECG_TLE
	#if DAVECG_CURRENT_SENSE
	#if DAVECG_KEIL
    int l_current_aux = 0;
    l_current_aux = (ADC_DAT2 << 4) + 0x8000;
    _mac_(CoABS, 0, l_current_aux);
    g_current_max = _imac_sat_();
	#elif DAVECG_TASKING
    int l_current_aux = 0;
    long l_co_instructions = 0;
    l_co_instructions = (unsigned int)((ADC_DAT2 << 4) + 0x8000);
    _CoLOAD(l_co_instructions << 16);
    _CoABS();
    g_current_max = _CoSTOREMAS();
	#endif // if KEIL == 1
	#endif	//CURRENT_SENSE
#endif	//TLE
	#if DAVECG_CURRENT_SENSE
    OVR_CURRENT_CHECK();
	#endif // CURRENT_SENSE
#if DAVECG_TLE
#if DAVECG_CURRENT_SENSE
    l_current_aux = (CCU6_MCMOUT & 0x003F);				   // read the present Modulation Pattern.
    if(g_i_mpattern != l_current_aux)					   // if there is a change in the mod pattern......
    {													   // ........
        g_i_mpattern = l_current_aux;					   // preserve the present Mod Pattern.
        if(((g_i_mpattern == 0x0012) || (g_i_mpattern == 0x0018)) || (g_i_mpattern == 0x0021))
        {												   // .....????????.......
            ADC_DAT2 = 0x5000;							   // Select ADC CH 5 for Injection.
        }
        else
        {
            ADC_DAT2 = 0x4000;							   // Select ADC CH 4 for Injection.
        }
    }
#endif	//CURRENT_SENSE
#endif	//TLE
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void CCU6_NODEI3_LOCAL_VAR(void)     
// Description		This function initialises the local variable used in CCU6 interrupt routine..
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void CCU6_NODEI3_LOCAL_VAR(void)
{
	int l_bemf_aux = 0;									   // declare & initialise the local variable.
}
//*************************************************************************************************
// @Function        void CCU6_NODEI3_ZCRS_CHK_CBLOCK(void)     
// Description		....
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void CCU6_NODEI3_ZCRS_CHK_CBLOCK(void)
{
	#if DAVECG_BTS
		commutation();
	#endif // BTS == 1
	#if DAVECG_TLE
	#if DAVECG_ENABLE_REFRESH_PULSE
		PECC0 = 0x0001;
	#endif // ENABLE_REFRESH_PULSE
		commutation();
	#endif // TLE == 1
}
//*************************************************************************************************
//*************************************************************************************************
// @Function        void CCU6_NODEI3_CBLOCK(void)     
// Description		This function implements the following:
//					1. Takes the motor into 'steady' state through 'transient' state by executing 
//						"commutation" function.	
//					2.	Dynamically determines which motor 'phase' need to be looked for Zero crossing signal.
//					3.	executes sampling/ trace of the graph variable.
// Parameters		none
// Return value		none
//-------------------------------------------------------------------------------------------------
INLINE void CCU6_NODEI3_CBLOCK(void)
{
    	int l_bemf_aux=0;
	    commutation();									   // execute the motor transient/ steady state routines.
 		l_bemf_aux = (CCU6_MCMOUT & 0x003F);			   // read the present Modulation Pattern....
		if(g_bemf_aux != l_bemf_aux)					   // ...compare it with previous Mod Pattern.....
	    {												   // if Mod Pattern has changed......
        g_bemf_aux = l_bemf_aux;						   // then preserve present Mod Pattern...
        ADC_AUX_BEMF();									   // ....and determine the next ADC channel for Zero crossing sensing.
		}
 		#if DAVECG_CONTROL_PANEL_ENABLE
		trace();										   // buffer the samples of the variable selected for graph plotting.
		#endif 
}
//*************************************************************************************************
//*************************************************************************************************
#endif // _CTRL_H_
