//*****************************************************************************/
// @Module         COMMON APPLICATION INTERFACE
// @Filename       API_LIB.H
// @Controller     XC800 Series
// @Control Tech   Block Commutation
//
// @Description    This file contains all API function calls for general application
//					development based on motor control.
//*****************************************************************************/
//
#ifndef _API_LIB_H_
#define _API_LIB_H_
//
//----------------------------------------------------------------------------
//****************************************************************************
// @Project Includes
//****************************************************************************
#include "CONFIG.H"
#if DAVECG_ControlAlgoKBS.KEIL
#if DAVECG_ControlAlgoKBS.BEMF
#if DAVECG_ControlPanelKBS.Set_Bemf_Freq_Inc
#include <MATH.H>
#endif
#endif
#endif

//----------------------------------------------------------------------------
//
//****************************************************************************
// @Defines
//****************************************************************************
// NOTE:	0-x value of any int var => 0 - 32767 => pow(2,15)
//----------------------------------------------------------------------------

#define POWER215 32768

//----------------------------------------------------------------------------

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

#if DAVECG_ControlAlgoKBS.KEIL
 
extern	data 	unsigned char 	Status_word; 
extern 			bit				gb_overcurrent;
extern 			bit 			gb_hs_error;
extern 			bit 			gb_ctrap;
extern 			bit	 			gb_time_out;
extern 			bit 			gb_On;
extern 			bit 			gb_Off;

extern	data	unsigned int 	g_direction;
#endif
#if DAVECG_ControlAlgoKBS.SDCC

extern	data at 0x0020	unsigned char 	Status_word; 
extern 					bit 			gb_hs_error;
extern 					bit 			gb_ctrap;
extern 					bit	 			gb_time_out;
extern 					bit 			gb_On;
extern 					bit 			gb_Off;
extern 					bit				gb_overcurrent;

extern 	data at 0x002E	unsigned int 	g_direction;
#endif


#if DAVECG_ControlAlgoKBS.SPEED_CURRENT_CTRL
#if DAVECG_ControlAlgoKBS.KEIL
extern data     unsigned int 		g_speed_start_ref;	
extern idata    unsigned int 		g_speed_end_ref; 
extern idata	int   				Rampup_Counter;
extern idata	unsigned	int 	speed_kp;
extern idata 	unsigned int 		speed_ki;
  
extern data		unsigned int		current_kp;
extern idata	unsigned int 		current_ki;
#endif
#if DAVECG_ControlAlgoKBS.SDCC
extern	data  at 0x0028		unsigned int	g_speed_start_ref;	
extern	idata at 0x008C		unsigned int	g_speed_end_ref; 	
extern  idata at 0x008E		int   		    Rampup_Counter;
extern	idata at 0x0092		unsigned int 	speed_kp;
extern	idata at 0x0094		unsigned int 	speed_ki;
extern	data  at 0x002A 		unsigned int	current_kp;
extern	idata at 0x0096 	unsigned int 	current_ki;		

#endif
#endif
#if DAVECG_ControlAlgoKBS.CURRENT_CTRL

#if DAVECG_ControlAlgoKBS.KEIL
extern data		unsigned int 	g_current_ref;
extern data		unsigned int	current_kp;
extern idata	unsigned int 	current_ki;
#endif
#if DAVECG_ControlAlgoKBS.SDCC
extern data 	at 0x0028	unsigned int  	g_current_ref;
extern idata 	at 0x0092  	unsigned int 	current_kp;
extern idata 	at 0x0094  	unsigned int 	current_ki;

#endif
#endif
#if DAVECG_ControlAlgoKBS.SPEED_CTRL

#if DAVECG_ControlAlgoKBS.KEIL
extern	data	unsigned int 	g_speed_start_ref;	
extern	idata	unsigned int 	g_speed_end_ref; 
extern  idata	int   			Rampup_Counter;
extern	idata	unsigned int 	speed_kp;
extern	idata 	unsigned int 	speed_ki;
#endif
#if DAVECG_ControlAlgoKBS.SDCC

extern	data  at 0x0028		unsigned int	g_speed_start_ref;	
extern	idata at 0x008C		unsigned int	g_speed_end_ref; 
extern  idata at 0x008E		int   		    Rampup_Counter;
extern	idata at 0x0092		unsigned int 	speed_kp;
extern	idata at 0x0094		unsigned int 	speed_ki;
#endif
#endif
#if DAVECG_ControlAlgoKBS.VOLTAGE_CTRL 

#if DAVECG_ControlAlgoKBS.KEIL
extern data 	unsigned int	g_nv_start_ref;	
extern idata 	unsigned int 	g_nv_end_ref; 
extern idata	int   		    Rampup_Counter;
#endif
#if DAVECG_ControlAlgoKBS.SDCC
extern	data  at 0x0028	 unsigned int 	g_nv_start_ref;	
extern	idata at 0x008C  unsigned int 	g_nv_end_ref; 
extern  idata at 0x008E	 int   		    Rampup_Counter;

#endif
#endif
//----------------------------------------------------------------------------

#if DAVECG_ControlAlgoKBS.BEMF

#if DAVECG_ControlAlgoKBS.KEIL
extern  idata   int 			g_speed_CC60;
extern  idata	unsigned int 	g_bemf_check_freq;					   
extern  idata 	unsigned int  	g_bemf_end_freq;	
extern  idata	int  			g_freq_increment;	
extern  idata	int 			g_volt_increment;
extern  idata	unsigned int 	g_start_duty_c;
extern  idata	unsigned int 	g_start_freq;
#endif
#if DAVECG_ControlAlgoKBS.SDCC
extern 	idata at 0x0090 int 			g_speed_CC60;
extern	idata at 0x0088	unsigned int 	g_bemf_check_freq;					   
extern	idata at 0x008A	unsigned int  	g_bemf_end_freq;	
extern	idata at 0x0082	int  			g_freq_increment;	
extern	idata at 0x0084	int 			g_volt_increment;
extern	idata at 0x0086	unsigned int 	g_start_duty_c;
extern	idata at 0x0080	unsigned int 	g_start_freq;
#endif
#endif

#if DAVECG_ControlAlgoKBS.HALL

#if DAVECG_ControlAlgoKBS.KEIL
extern idata 	unsigned int  	g_speed_CC60;
#endif
#if DAVECG_ControlAlgoKBS.SDCC
extern idata 	at 0x90 	unsigned int  	g_speed_CC60;

#endif
#endif
extern int API_Ref;
//----------------------------------------------------------------------------
//****************************************************************************
// @External Prototypes
//****************************************************************************

extern  void MotorStart(void);
extern  void MotorStop(void);

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Reset_Ctrap_Req
//****************************************************************************
// @Function        Reset_Ctrap_Req(void)
// Description		This Function resets the CTRAP bit
//----------------------------------------------------------------------------

#define Reset_Ctrap_Req() gb_Trap=0

#endif						//reset ctrap
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Motor_Start
//****************************************************************************
// @Function        Set_Motor_Start(void)
// Description		This Function sets the bit required for motor to start.
//					This bit should be 0 for motor to start. This will be 1
//					when motor is in start condition.
//----------------------------------------------------------------------------

 #define Set_Motor_Start()  MotorStart()

#endif					//Motor start
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Motor_Stop
//****************************************************************************
// @Function        Set_Motor_Stop(void)
// Description		This Function sets the bit required for motor to stop.
//					This bit is set to 1 if the motor has to be stopped.
//----------------------------------------------------------------------------

#define Set_Motor_Stop() MotorStop()

#endif					//Motor stop
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Direction_Forward
//****************************************************************************
// @Function        Set_Direction_Forward(void)
// Description		This Function sets the bit required for motor to rotate in
//					forward direction.
//					This var is assigned with 0 if the motor has to rotate in
//					forward direction.
//----------------------------------------------------------------------------

#define Set_Direction_Forward() g_direction =0

#endif					//Motor Forward Direction
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Direction_Reverse
//****************************************************************************
// @Function        Set_Direction_Reverse(void)
// Description		This Function sets the bit required for motor to rotate in
//					reverse direction.
//					This var is assigned with 1 if the motor has to rotate in
//					reverse direction.
//----------------------------------------------------------------------------

#define Set_Direction_Reverse()  g_direction =8

#endif					//Motor Reverse Direction
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Get_Motor_Target_Status
//****************************************************************************
// @Function        int Get_Motor_Target_Status(void)
// Description		This Function returns the global var g_status_word which 
//					gives the complete status information of target.
//					Each bit of this var contains different info on target
//					status.
//
//					Bit 0 --> gb_off
//					Bit 1 --> gb_on
//					Bit 2 --> gb_time_out
//					Bit 3 --> gb_ctrap
//					Bit 4 --> gb_hs_error
//					Bit 5 --> gb_overcurrent
//				
//					Remaining bits in this var can be used for futher more 
//					status flags if required.
//					Returns '0x0002' if the motor is in normal run mode.
//					It may return may other values under abnormal condition.
//----------------------------------------------------------------------------

#define  Get_Motor_Target_Status()	Status_word

#endif					//Get motor target status
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Get_Stop_Condition_Status
//****************************************************************************
// @Function        int Get_Stop_Condition_Status(void)
// Description		This Function returns the global var bit gb_off which 
//					gives the status information of whether the motor is in stop mode.
//					This bit is bit 0 of global var g_status_word.
//					This bit is set when the stop command for the motor is passed.
//					This can also be set if the motor stops due to overcurrent or 
//					timeout or wrong hall event.
//					Returns '1' if the motor is in stop mode.
//					Returns '0' if the motor is in run mode..
//----------------------------------------------------------------------------

#define Get_Stop_Condition_Status() gb_off

#endif					//Get stop condition status
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Get_Start_Condition_Status
//****************************************************************************
// @Function        int Get_Start_Condition_Status(void)
// Description		This Function returns the global var bit gb_on which 
//					gives the status information of whether motor is in run mode.
//					This bit is bit 1 of global var g_status_word.
//					This bit is set when the start command for the motor is passed.
//					Returns '1' if the motor is in run mode.
//					Returns '0' if the motor is in stop mode.
//----------------------------------------------------------------------------

#define Get_Start_Condition_Status() gb_on

#endif					//Get start condition status
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Get_Time_Out_Status
//****************************************************************************
// @Function        int Get_Time_Out_Status(void)
// Description		This Function returns the global var bit gb_time_out which 
//					gives the status information of whether motor is able to 
//					start within a limited time boundary.
//					This bit is bit 2 of global var g_status_word.
//					This bit gets set by software when the motor does not start
//					within some min time after the start command is issued.
//					Returns '1' under motor start fault condition.
//					Returns '0' in normal operating condition.
//----------------------------------------------------------------------------

#define Get_Time_Out_Status() gb_time_out

#endif					//Time out
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Get_Ctrap_Status
//****************************************************************************
// @Function        int Get_Ctrap_Status(void)
// Description		This Function returns the global var bit gb_ctrap which 
//					gives the status information of hardware CTrap.
//					This bit is bit 3 of global var g_status_word.
//					This bit is set by software when a hardware ctrap occurs
//					and is cleared by software on reset/restart.
//					Returns '1' if the hardware ctrap occurs.
//					Returns '0' in normal operating condition.
//----------------------------------------------------------------------------

#define Get_Ctrap_Status() gb_ctrap

#endif					//ctrap status
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Get_Hall_Error_Status
//****************************************************************************
// @Function        int Get_Hall_Error_Status(void)
// Description		This Function returns the global var bit gb_hs_error which 
//					gives the status information of wrong hall event in hall mode.
//					This bit is bit 4 of global var g_status_word.
//					This bit is set by software when a wrong hall event interrupt
//					occurs on the controller side.
//					Returns '1' if the wrong hall event occurs.
//					Returns '0' in normal operating condition.
//----------------------------------------------------------------------------

#define Get_Hall_Error_Status() gb_hs_error

#endif					//Get Hall Error status
//----------------------------------------------------------------------------

#if DAVECG_ControlPanelKBS.Get_Over_Current_Status
//****************************************************************************
// @Function        int Get_Over_Current_Status(void)
// Description		This Function returns the global var bit gb_overcurrent which 
//					gives the status information of current consumption on the target.
//					This bit is bit 5 of global var g_status_word.
//					This bit is set when microcontroller senses current which is
//					beyond the specified range.
//					Returns '1' if the current on the target side exceeds the limit.
//					Returns '0' in normal operating condition.
//----------------------------------------------------------------------------

#define Get_Over_Current_Status() gb_overcurrent

#endif					//over current
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Read_Motor_Current
//****************************************************************************
// @Function        int Read_Motor_Current(void)
// Description		This Function returns the global var g_current. The value
//					of this variable specifies the target current.
//					Returns int value in run mode.
//					Returns '0' at stop mode.
//----------------------------------------------------------------------------

#define Read_Motor_Current() g_current

#endif					//read motor current
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Read_Motor_Speed
//****************************************************************************
// @Function        unsigned int Read_Motor_Speed(void)
// Description		This Function returns the global var g_speed_CC60 when
//					operating in Hall Mode and motor_speed when operating in
//					BEMF mode. These global vars specify the actual speed of motor.
//					at that instant.
//					Returns unsigned int value if the motor is in run mode.
//					Returns '0' in stop mode.
//----------------------------------------------------------------------------

#if DAVECG_ControlAlgoKBS.HALL
    #define Read_Motor_Speed() g_speed_cc60*API_NORM_SPEED
#elif DAVECG_ControlAlgoKBS.BEMF
    #define Read_Motor_Speed() g_speed_CC60*API_NORM_SPEED
#endif

#endif					//read motor speed
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_ControlAlgoKBS.VOLTAGE_CTRL 
#if DAVECG_ControlPanelKBS.Set_Voltage_Start_Ref
//****************************************************************************
// @Function        Set_Voltage_Start_Ref(float v_start_ref)
// Description		This Function sets start voltage reference value for the motor to run.
//					This function effects the global var g_nv_start_ref as specified by
//					the user through 'v_start_ref' var. This is valid only in case of
//					voltage control algorithm. In case of invalid user entry
//					the functions assigns the default value for that particular motor.
//----------------------------------------------------------------------------

#define Set_Voltage_Start_Ref(val)   API_Ref=(int)(val*API_NORM_VOLTAGE);\
							 if (API_Ref>=0 && API_Ref<=(API_MAX_VOLTAGE)) (g_nv_start_ref=API_Ref);\
							 else (g_nv_start_ref=MOTOR_START_REF)


#endif						//voltage start ref
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Voltage_End_Ref
//****************************************************************************
// @Function        Set_Voltage_End_Ref(float v_end_ref)
// Description		This Function sets end voltage reference value for the motor to run.
//					This function effects the global var g_nv_end_ref as specified by
//					the user through 'v_end_ref' var. This is valid only in case of
//					voltage control algorithm. In case of invalid user entry
//					the functions assigns the default value for that particular motor.
//----------------------------------------------------------------------------

#define Set_Voltage_End_Ref(val)	 API_Ref=(int)(val*API_NORM_VOLTAGE);\
							 if (API_Ref>=0 && API_Ref<=(API_MAX_VOLTAGE)) (g_nv_end_ref=API_Ref);\
							 else (g_nv_end_ref=MOTOR_END_REF)

#endif						//voltage end ref
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Ramp_Up_Time
//****************************************************************************
// @Function        Set_Ramp_Up_Time(unsigned long r_time)
// Description		This Function sets the ramp up time parameter which
//					decides at what time steps, the motor speed ramps up and
//					reaches the desired speed.
//					This function effects global vars g_ramp_up_time and is
//					dependent on global var g_pwm_freq and user entered value
//					through r_time.In case of invalid user entry the functions
//					assigns the default value for that particular motor.
//----------------------------------------------------------------------------

#define  Set_Slew_Rate(val)  API_Ref =(int)val; if (API_Ref>=API_VOLTAGE_RAMPUP_MIN && API_Ref<=API_VOLTAGE_RAMPUP_MAX) (API_Ref=1);\
							 else   API_Ref=-2;\
							 if (API_Ref==1) (Rampup_Counter=(int)(API_NORM_VOLTAGE_RAMPUP/val));\
						  	 else (Rampup_Counter=VOLTAGE_RAMPUP_COUNT)
#endif					//ramp up time
//----------------------------------------------------------------------------
#endif					//VOLTAGE_CTRL END
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_ControlAlgoKBS.CURRENT_CTRL  
#if DAVECG_ControlPanelKBS.Set_Motor_Current
//****************************************************************************
// @Function        Set_Motor_Current(float current)
// Description		This Function sets the current value for which which the
//					motor has to run and maintain at that value. This function
//					effects the global var g_current_ref as specified by
//					the user through 'current var'. This is valid only in case of
//					current control algorithm.In case of invalid user entry the 
//					functions assigns the default value for that particular motor.
//----------------------------------------------------------------------------

#define Set_Motor_Current(val)  API_Ref=(int)(val*API_NORM_CURRENT);\
								if (API_Ref>=0 && API_Ref<=(API_MAX_CURRENT)) (g_current_ref=API_Ref);\
								else (g_current_ref=MOTOR_CURRENT_REF)


#endif					//set motor current
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Proportional_Gain
//****************************************************************************
// @Function        Set_Proportional_Gain(float pi_prop_gain)
// Description		This Function sets the proportional gain for the PI control
//					algorithm. This function effects the global var g_pi_array.kp
//					as specified by the user through 'pi_prop_gain var'. This is 
//					not valid only in case of voltage control algorithm.In case of 
//					invalid user entry the functions assigns the default value 
//					for that particular motor.
//----------------------------------------------------------------------------

#define Set_Proportional_Gain(val)  API_Ref=(int)val; if (API_Ref<=64 && API_Ref>=0) (API_Ref=(int)(val*(POWER215/64)-1));\
						  			else  API_Ref=-2;\
									if (API_Ref>=0) (current_kp=API_Ref);\
									else if(API_Ref==-1) (current_kp=0);\
									else (current_kp=INIT_CURRENT_KP)

#endif					//set proportional gain
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Integral_Gain
//****************************************************************************
// @Function        Set_Integral_Gain(float pi_integ_gain)
// Description		This Function sets the integral gain for the PI control
//					algorithm. This function effects the global var g_pi_array.ki
//					as specified by the user through 'pi_integ_gain var'. This is 
//					not valid only in case of voltage control algorithm.In case of 
//					invalid user entry the functions assigns the default value 
//					for that particular motor.
//----------------------------------------------------------------------------

#define Set_Integral_Gain(val) API_Ref=(int)val; if (API_Ref<=1 && API_Ref>=0) (API_Ref=(int)(val*POWER215)-1);\
						  	   else  API_Ref=-2;\
							   if (API_Ref>=0) (current_ki=API_Ref);\
							   else if(API_Ref==-1) (current_ki=0);\
							   else (current_ki=INIT_CURRENT_KI)

#endif					//set integral gain
//----------------------------------------------------------------------------
#endif					//CURRENT_CTRL END
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_ControlAlgoKBS.SPEED_CTRL 
#if DAVECG_ControlPanelKBS.Set_Start_Speed_Ref
//****************************************************************************
// @Function        Set_Start_Speed_Ref(int spd_start_ref)
// Description		This Function sets start speed reference value for the motor to run.
//					This function effects the global var g_nv_start_ref as specified by
//					the user through 'spd_start_ref' var. This is valid only in case of
//					speed/speed_current control algorithm. In case of invalid user entry
//					the functions assigns the default value for that particular motor.
//----------------------------------------------------------------------------

#define Set_Start_Speed_Ref(val)	API_Ref=(int)(val/API_NORM_SPEED);\
									if (API_Ref>=0 && API_Ref<=(API_SPEED_MAX_TARGET)) (g_speed_start_ref=API_Ref);\
									else (g_speed_start_ref=MOTOR_START_REF)

#endif					//set speed start ref
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_End_Speed_Ref
//****************************************************************************
// @Function        Set_End_Speed_Ref(int spd_end_ref)
// Description		This Function sets start speed reference value for the motor to run.
//					This function effects the global var g_nv_end_ref as specified by
//					the user through 'spd_end_ref' var. This is valid only in case of
//					speed/speed_current control algorithm. In case of invalid user entry
//					the functions assigns the default value for that particular motor.
//----------------------------------------------------------------------------

#define  Set_End_Speed_Ref(val) API_Ref=(int)(val/API_NORM_SPEED);\
								if (API_Ref>=0 && API_Ref<=(API_SPEED_MAX_TARGET)) (g_speed_end_ref=API_Ref);\
								else (g_speed_end_ref=MOTOR_END_REF)

#endif					//set end speed ref
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Proportional_Gain
//****************************************************************************
// @Function        Set_Proportional_Gain(float pi_prop_gain)
// Description		This Function sets the proportional gain for the PI control
//					algorithm. This function effects the global var g_pi_array.kp
//					as specified by the user through 'pi_prop_gain var'. This is 
//					not valid only in case of voltage control algorithm.In case of 
//					invalid user entry the functions assigns the default value 
//					for that particular motor.
//----------------------------------------------------------------------------

#define  Set_Proportional_Gain(val)  	API_Ref=(int)val; if (API_Ref<=64 && API_Ref>=0) (API_Ref=(int)(val*(POWER215/64)-1));\
						  				else  API_Ref=-2;\
									  	if (API_Ref>=0) (speed_kp=API_Ref);\
										else if(API_Ref==-1) (speed_kp=0);\
									  	else (speed_kp=INIT_SPEED_KP)

#endif					//set Proportional gain
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Integral_Gain
//****************************************************************************
// @Function        Set_Integral_Gain(float pi_integ_gain)
// Description		This Function sets the integral gain for the PI control
//					algorithm. This function effects the global var g_pi_array.ki
//					as specified by the user through 'pi_integ_gain var'. This is 
//					not valid only in case of voltage control algorithm.In case of 
//					invalid user entry the functions assigns the default value 
//					for that particular motor.
//----------------------------------------------------------------------------

#define Set_Integral_Gain(val)	API_Ref=(int)val; if (API_Ref<=1 && API_Ref>=0) (API_Ref=(int)(val*POWER215)-1);\
						  		else  API_Ref=-2;\
								if (API_Ref>=0) (speed_ki=API_Ref);\
								else if(API_Ref==-1) (speed_ki=0);\
								else (speed_ki=INIT_SPEED_KI)

#endif					//set integral gain
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Ramp_Up_Time
//****************************************************************************
// @Function        Set_Ramp_Up_Time(unsigned long r_time)
// Description		This Function sets the ramp up time parameter which
//					decides at what time steps, the motor speed ramps up and
//					reaches the desired speed.
//					This function effects global vars g_ramp_up_time and is
//					dependent on global var g_pwm_freq and user entered value
//					through r_time.In case of invalid user entry the functions
//					assigns the default value for that particular motor.
//----------------------------------------------------------------------------

#define Set_Slew_Rate(val) 		API_Ref=(int)val; if (API_Ref>=API_SPEED_RAMPUP_MIN && API_Ref<=API_SPEED_RAMPUP_MAX) (API_Ref=(int)(API_NORM_SPEED*(API_FREQ/val)));\
								else   API_Ref=0;\
						  		if (API_Ref>0 && API_Ref<(POWER215)) (Rampup_Counter=API_Ref);\
						  		else (Rampup_Counter=SPEED_RAMPUP_COUNT)

#endif					// set ramp up time
//----------------------------------------------------------------------------
#endif					//	SPEED_CTRL END
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_ControlAlgoKBS.SPEED_CURRENT_CTRL 
#if DAVECG_ControlPanelKBS.Set_Start_Speed_Ref
//****************************************************************************
// @Function        Set_Start_Speed_Ref(int spd_start_ref)
// Description		This Function sets start speed reference value for the motor to run.
//					This function effects the global var g_nv_start_ref as specified by
//					the user through 'spd_start_ref' var. This is valid only in case of
//					speed/speed_current control algorithm. In case of invalid user entry
//					the functions assigns the default value for that particular motor.
//----------------------------------------------------------------------------


#define Set_Start_Speed_Ref(val)	API_Ref=(int)(val/API_NORM_SPEED);\
									if (API_Ref>=0 && API_Ref<=(API_SPEED_MAX_TARGET)) (g_speed_start_ref=API_Ref);\
									else (g_speed_start_ref=MOTOR_START_REF)


#endif						// set start speed ref
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_End_Speed_Ref
//****************************************************************************
// @Function        Set_End_Speed_Ref(int spd_end_ref)
// Description		This Function sets start speed reference value for the motor to run.
//					This function effects the global var g_nv_end_ref as specified by
//					the user through 'spd_end_ref' var. This is valid only in case of
//					speed/speed_current control algorithm. In case of invalid user entry
//					the functions assigns the default value for that particular motor.
//----------------------------------------------------------------------------

#define  Set_End_Speed_Ref(val)		API_Ref=(int)(val/API_NORM_SPEED);\
									if (API_Ref>=0 && API_Ref<=(API_SPEED_MAX_TARGET)) (g_speed_end_ref=API_Ref);\
									else (g_speed_end_ref=MOTOR_END_REF)


#endif					//set end speed ref
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Speed_Proportional_Gain
//****************************************************************************
// @Function        Set_Speed_Proportional_Gain(float pi_prop_gain)
// Description		This Function sets the proportional gains for speed 
//					for the PI control algorithm. This function effects the global 
//					vars g_pi_speed_array.kp through var
//					pi_prop_gain as specified by the user. This is 
//					valid only in case of speed current control algorithm.In case of 
//					invalid user entry the functions assigns the default values. 
//					for that control strategy and motor.
//----------------------------------------------------------------------------

#define  Set_Speed_Proportional_Gain(val)  	API_Ref=(int)val; if (API_Ref<=64 && API_Ref>=0) (API_Ref=(int)(val*(POWER215/64)-1));\
						  				  	else  API_Ref=-2;\
									  		if (API_Ref>=0) (speed_kp=API_Ref);\
											else if(API_Ref==-1) (speed_kp=0);\
									  		else (speed_kp=INIT_SPEED_KP)


#endif					//set speed proportional gain
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Current_Proportional_Gain
//****************************************************************************
// @Function        Set_Current_Proportional_Gain(float pi_prop_gain)
// Description		This Function sets the proportional gains for current
//					for the PI control algorithm. This function effects the global 
//					vars g_pi_current_array.kp through var
//					pi_prop_gain as specified by the user. This is 
//					valid only in case of speed current control algorithm.In case of 
//					invalid user entry the functions assigns the default values. 
//					for that control strategy and motor.
//----------------------------------------------------------------------------

#define Set_Current_Proportional_Gain(val)  API_Ref=(int)val; if (API_Ref<=64 && API_Ref>=0) (API_Ref=(int)(val*(POWER215/64)-1));\
						  				  	else  API_Ref=-2;\
									  		if (API_Ref>=0) (current_kp=API_Ref);\
									  		else if(API_Ref==-1) (current_kp=0);\
											else (current_kp=INIT_CURRENT_KP)


#endif					//set proportional gain
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Speed_Integral_Gain
//****************************************************************************
// @Function        Set_Speed_Integral_Gain(float pi_integ_gain)
// Description		This Function sets the proportional gains for speed 
//					for the PI control algorithm. This function effects the global 
//					vars g_pi_speed_array.ki through var
//					pi_integ_gain as specified by the user. This is 
//					valid only in case of speed current control algorithm.In case of 
//					invalid user entry the functions assigns the default values. 
//					for that control strategy and motor.
//----------------------------------------------------------------------------

#define Set_Speed_Integral_Gain(val)	API_Ref=(int)val; if (API_Ref<=1 && API_Ref>=0) (API_Ref=(int)(val*POWER215)-1);\
						  				else  API_Ref=-2;\
									  	if (API_Ref>=0) (speed_ki=API_Ref);\
										else if(API_Ref==-1) (speed_ki=0);\
									  	else (speed_ki=INIT_SPEED_KI)

#endif							//set integral gain
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Current_Integral_Gain
//****************************************************************************
// @Function        Set_Current_Integral_Gain(float pi_integ_gain)
// Description		This Function sets the proportional gains for current
//					for the PI control algorithm. This function effects the global 
//					vars g_pi_current_array.ki through var
//					pi_integ_gain as specified by the user. This is 
//					valid only in case of speed current control algorithm.In case of 
//					invalid user entry the functions assigns the default values. 
//					for that control strategy and motor.
//----------------------------------------------------------------------------
#define Set_Current_Integral_Gain(val)	API_Ref=(int)val; if (API_Ref<=1 && API_Ref>=0) (API_Ref=(int)(val*POWER215)-1);\
						  				else  API_Ref=-2;\
									  	if (API_Ref>=0) (current_ki=API_Ref);\
										else if(API_Ref==-1) (current_ki=0);\
									  	else (current_ki=INIT_CURRENT_KI)
#endif							//set integral gain
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Ramp_Up_Time
//****************************************************************************
// @Function        Set_Ramp_Up_Time(unsigned long r_time)
// Description		This Function sets the ramp up time parameter which
//					decides at what time steps, the motor speed ramps up and
//					reaches the desired speed.
//					This function effects global vars g_ramp_up_time and is
//					dependent on global var g_pwm_freq and user entered value
//					through r_time.In case of invalid user entry the functions
//					assigns the default value for that particular motor.
//----------------------------------------------------------------------------

#define Set_Slew_Rate(val) 		API_Ref=(int)val; if (API_Ref>=API_SPEED_RAMPUP_MIN && API_Ref<=API_SPEED_RAMPUP_MAX) (API_Ref=(int)(API_NORM_SPEED*(API_FREQ/val)));\
								else   API_Ref=0;\
						  		if (API_Ref>0 && API_Ref<(POWER215)) (Rampup_Counter=API_Ref);\
						  		else (Rampup_Counter=SPEED_RAMPUP_COUNT)

#endif					//set ramp up time
//----------------------------------------------------------------------------
#endif					//SPEED_CURRENT_CTRL END
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_ControlAlgoKBS.BEMF
#if DAVECG_ControlPanelKBS.Set_Bemf_Start_Freq
//****************************************************************************
// @Function        void Set_Bemf_Start_Freq(unsigned int s_freq)
// Description		This Function sets the BEMF start freq for the motor to run.
//					This function effects the global var g_start_freq as specified by
//					the user through 's_freq' var. This is valid only in case of
//					BEMF control algorithm.In case of invalid user entry the functions
//					assigns the default value for that particular motor. 
//----------------------------------------------------------------------------

#define Set_Bemf_Start_Freq(val)	API_Ref=(int)(val*API_NORM_BEMF_FREQ);\
									if (API_Ref>=0 && API_Ref<=(100*API_NORM_BEMF_FREQ)) (g_start_freq=API_Ref);\
									else (g_start_freq=START_FREQ)

#endif					//bemf start freq
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Bemf_Check_Freq
//****************************************************************************
// @Function        void Set_Bemf_Check_Freq(unsigned int bc_freq)
// Description		This Function sets the BEMF check freq for the motor to run.
//					This function effects the global var g_bemf_check_freq as specified by
//					the user through 'bc_freq' var. This is valid only in case of
//					BEMF control algorithm.In case of invalid user entry the functions
//					assigns the default value for that particular motor. 
//----------------------------------------------------------------------------

#define  Set_Bemf_Check_Freq(val)	API_Ref=(int)(val*API_NORM_BEMF_FREQ);\
									if (API_Ref>=0 && API_Ref<=(100*API_NORM_BEMF_FREQ)) (g_bemf_check_freq=API_Ref);\
									else (g_bemf_check_freq=BEMF_CHECK_FREQ)

#endif					//bemf check freq
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Bemf_End_Freq
//****************************************************************************
// @Function        void Set_Bemf_End_Freq(unsigned int e_freq)
// Description		This Function sets the BEMF end freq for the motor to run.
//					This function effects the global var g_end_freq as specified by
//					the user through 'e_freq' var. This is valid only in case of
//					BEMF control algorithm.In case of invalid user entry the functions
//					assigns the default value for that particular motor. 
//----------------------------------------------------------------------------

#define Set_Bemf_End_Freq(val)	API_Ref=(int)(val*API_NORM_BEMF_FREQ);\
								if (API_Ref>=0 && API_Ref<=(100*API_NORM_BEMF_FREQ)) (g_bemf_end_freq=API_Ref);\
								else (g_bemf_end_freq=END_FREQUENCY)

#endif					//bemf end freq
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Bemf_Start_Duty_Cycle
//****************************************************************************
// @Function        void Set_Bemf_Start_Duty_Cycle(unsigned int start_dc)
// Description		This Function sets percentage start duty cycle for the motor.
//					This function effects the global var g_start_duty_c as specified by
//					the user through 'start_dc' var. This is valid only in case of
//					BEMF control algorithm.In case of invalid user entry the functions
//					assigns the default value for that particular motor. 
//----------------------------------------------------------------------------

#define Set_Bemf_Start_Duty_Cycle(val)	API_Ref=(int)(val*PULSE_PERIOD);\
										if (API_Ref>=0 && API_Ref<=(PULSE_PERIOD)) (g_start_duty_c=API_Ref);\
										else (g_start_duty_c=START_DUTY_CYC)

#endif					//bemf start duty cycle
//----------------------------------------------------------------------------
#if DAVECG_ControlAlgoKBS.KEIL
#if DAVECG_ControlPanelKBS.Set_Bemf_Freq_Inc
//****************************************************************************
// @Function        void Set_Bemf_Freq_Inc(int freq_inc)
// Description		This Function sets increment step for the frequency in BEMF mode.
//					This function effects the global var g_freq_increment as specified by
//					the user through 'freq_inc' var. This is valid only in case of
//					BEMF control algorithm.In case of invalid user entry the functions
//					assigns the default value for that particular motor. 
//----------------------------------------------------------------------------
#if DAVECG_ControlAlgoKBS.START_RAMP  // Linear ramp

#define  Set_Bemf_Freq_Inc(val)  API_Ref=(int)val; if (API_Ref>=API_FREQ_INC_MIN && API_Ref<=API_FREQ_INC_MAX) (API_Ref=1);\
									else   API_Ref=-2;\
									if (API_Ref==1) (g_freq_increment=(int)(64/val));\
						  			else (g_freq_increment=FREQ_INC)


#else  // QUAD ramp

#define Set_Bemf_Freq_Inc(val)  API_Ref=(int)val; if (API_Ref>=API_FREQ_INC_MIN && API_Ref<=API_FREQ_INC_MAX) (API_Ref=1);\
								else   API_Ref=-2;\
								if (API_Ref==1) (g_freq_increment=(int)(API_NORM_BEMF_FREQ_INC/(sqrt(val))));\
						  		else (g_freq_increment=FREQ_INC)

#endif



#endif   // QUAD or Linear ramp
#endif					//bemf freq inc


#if DAVECG_ControlAlgoKBS.SDCC
#if DAVECG_ControlPanelKBS.Set_Bemf_Freq_Inc
//****************************************************************************
// @Function        void Set_Bemf_Freq_Inc(int freq_inc)
// Description		This Function sets increment step for the frequency in BEMF mode.
//					This function effects the global var g_freq_increment as specified by
//					the user through 'freq_inc' var. This is valid only in case of
//					BEMF control algorithm.In case of invalid user entry the functions
//					assigns the default value for that particular motor. 
//----------------------------------------------------------------------------
#if DAVECG_ControlAlgoKBS.START_RAMP  // Linear ramp

#define  Set_Bemf_Freq_Inc(val)  API_Ref=(int)val; if (API_Ref>=API_FREQ_INC_MIN && API_Ref<=API_FREQ_INC_MAX) (API_Ref=1);\
									else   API_Ref=-2;\
									if (API_Ref==1) (g_freq_increment=(int)(64/val));\
						  			else (g_freq_increment=FREQ_INC)


#else  // QUAD ramp

#define Set_Bemf_Freq_Inc(val)  API_Ref=(int)val; if (API_Ref>=API_FREQ_INC_MIN && API_Ref<=64) (API_Ref=1);\
								else   API_Ref=-2;\
								if (API_Ref==1) (g_freq_increment=(int)(API_NORM_BEMF_FREQ_INC/(val)));\
						  		else (g_freq_increment=FREQ_INC)
#endif

#endif   // QUAD or Linear ramp
#endif	//bemf freq inc


//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.Set_Bemf_Voltage_Inc
//****************************************************************************
// @Function        void Set_Bemf_Voltage_Inc(int volt_inc)
// Description		This Function sets increment step for the voltage in BEMF mode.
//					This function effects the global var g_volt_increment as specified by
//					the user through 'volt_inc' var. This is valid only in case of
//					BEMF control algorithm.In case of invalid user entry the functions
//					assigns the default value for that particular motor. 
//----------------------------------------------------------------------------

#define  Set_Bemf_Voltage_Inc(val)  API_Ref=(int)val; if (API_Ref>=API_VOLTAGE_INC_MIN && API_Ref<=API_VOLTAGE_INC_MAX) (API_Ref=1);\
									else API_Ref=-2;\
						  			if (API_Ref==1) (g_volt_increment=(int)(API_NORM_VOLTAGE_INC/val));\
						  			else (g_volt_increment=VOLT_INC)

#endif					//bemf volt inc
//----------------------------------------------------------------------------
#endif					//BEMF CTRL END
//----------------------------------------------------------------------------
//


//****************************************************************************
#endif					//END OF FILE
//****************************************************************************
