//*****************************************************************************/
// @Module         COMMON APPLICATION INTERFACE
// @Filename       API_LIB.H
// @Controller     XC16x Series
//
//
// @Description    This file contains all API function calls for general application
//					development based on motor control.
//*****************************************************************************/
//
#ifndef _API_LIB_H_
#define _API_LIB_H_
//
//----------------------------------------------------------------------------
#include "CONFIG.H"
//----------------------------------------------------------------------------
//
//****************************************************************************
// @Defines
//****************************************************************************
// NOTE:	0-x value of any int var => 0 - 32767 => pow(2,15)
//----------------------------------------------------------------------------

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

#if DAVECG_KEIL
#define C_SDATA	sdata
#define C_BDATA bdata
#define BIT volatile bit
#define INLINE static __inline
#elif DAVECG_TASKING
#define C_SDATA system
#define C_BDATA _bitword
#define BIT bit
#define INLINE _inline
#endif
//----------------------------------------------------------------------------


//----------------------------------------------------------------------------
#if DAVECG_BLOCK_COMMUTATION
//----------------------------------------------------------------------------

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

extern int C_SDATA g_trap_reset;
extern BIT gb_motor_status;
extern BIT gb_on;
extern BIT gb_off;
extern BIT gb_hs_error;
extern BIT gb_ctrap;
extern BIT gb_overcurrent;
extern BIT gb_time_out;
extern int C_SDATA g_direction;
extern int C_SDATA g_direction_user;
extern unsigned long C_SDATA g_ramp_up_time;  
extern unsigned long C_SDATA g_pwm_freq;    
extern unsigned int C_SDATA g_nv_end_ref;   
extern unsigned int C_SDATA g_nv_start_ref; 
extern unsigned int C_SDATA g_duty_cycle;
extern int C_SDATA g_current;
extern C_BDATA g_status_word;         
//----------------------------------------------------------------------------

#if DAVECG_SPEED_CURRENT_CTRL
extern struct pi_controller C_SDATA g_pi_speed_array;
extern struct pi_controller C_SDATA g_pi_current_array;
#elif DAVECG_CURRENT_CTRL
extern struct pi_controller C_SDATA g_pi_array;
#elif DAVECG_SPEED_CTRL
extern struct pi_controller C_SDATA g_pi_array;
#else //voltage_control
#endif
//----------------------------------------------------------------------------

#if DAVECG_BEMF
extern int C_SDATA g_volt_increment;                   // Voltage increment
extern int C_SDATA g_freq_increment;                   // Frequency increment
extern unsigned int C_SDATA g_speed_CC61;              // Actual speed - bemf mode
extern unsigned int C_SDATA g_start_freq;              // Start frequency
extern unsigned int C_SDATA g_start_duty_c;            // Start duty cycle
extern unsigned int C_SDATA g_bemf_check_freq;         // BEMF check frequency
extern unsigned int C_SDATA g_bemf_end_freq;           // BEMF end frequency
#endif

#if DAVECG_HALL
extern unsigned int C_SDATA g_speed_CC60;              // Actual speed - hall mode
#endif
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
#if DAVECG_CPReset_Ctrap_Req
//****************************************************************************
// @Function        Reset_Ctrap_Req(void)
// Description		This Function resets the global variable g_trap_reset.
//					This global var in turn resets the CTrap of the controller.	
//					For motor to run, this var should be 0.	This is set to 1
//					if there is trap occurance on the hardware side.
//----------------------------------------------------------------------------

INLINE void Reset_Ctrap_Req(void)
{
	g_trap_reset	=	RESET;
}
#endif						//reset ctrap
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Motor_Start(void)
{
	gb_motor_status	=	START;
}
#endif					//Motor start
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Motor_Stop(void)
{
	gb_motor_status	=	STOP;
}
#endif					//Motor stop
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Direction_Forward(void)
{
	g_direction_user = MOTOR_DIRECTION;
}
#endif					//Motor Forward Direction
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Direction_Reverse(void)
{
	g_direction_user = (~MOTOR_DIRECTION)&0x0001;
}
#endif					//Motor Reverse Direction
//----------------------------------------------------------------------------
#if DAVECG_CPGet_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_on
//					Bit 1 --> gb_off
//					Bit 2 --> gb_idle_state
//					Bit 3 --> gb_hs_error
//					Bit 4 --> gb_time_out
//					Bit 5 --> gb_ctrap
//					Bit 6 --> gb_overcurrent
//
//					Remaining bits in this var can be used for futher more 
//					status flags if required.
//					Returns '0x0001' if the motor is in normal run mode.
//					It may return may other values under abnormal condition.
//----------------------------------------------------------------------------

INLINE int Get_Motor_Target_Status(void)
{
	return g_status_word;
}
#endif					//Get motor target status
//----------------------------------------------------------------------------
#if DAVECG_CPGet_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 0 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.
//----------------------------------------------------------------------------

INLINE int Get_Start_Condition_Status(void)
{
	return gb_on;
}
#endif					//Get start condition status
//----------------------------------------------------------------------------
#if DAVECG_CPGet_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 1 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..
//----------------------------------------------------------------------------

INLINE int Get_Stop_Condition_Status(void)
{
	return gb_off;
}
#endif					//Get stop condition status
//----------------------------------------------------------------------------
#if DAVECG_CPGet_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 3 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.
//----------------------------------------------------------------------------

INLINE int Get_Hall_Error_Status(void)
{
	return gb_hs_error;
}
#endif					//Get Hall Error status
//----------------------------------------------------------------------------
#if DAVECG_CPGet_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 4 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.
//----------------------------------------------------------------------------

INLINE int Get_Time_Out_Status(void)
{
	return gb_time_out;
}
#endif					//Time out
//----------------------------------------------------------------------------
#if DAVECG_CPGet_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 5 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.
//----------------------------------------------------------------------------

INLINE int Get_Ctrap_Status(void)
{
	return gb_ctrap;
}
#endif					//ctrap status
//----------------------------------------------------------------------------
#if DAVECG_CPGet_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 6 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.
//----------------------------------------------------------------------------

INLINE int Get_Over_Current_Status(void)
{
	return gb_overcurrent;
}
#endif					//over current
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#if DAVECG_CPRead_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.
//----------------------------------------------------------------------------

INLINE int Read_Motor_Current(void)
{
	return ((g_current/POWER15)*KCCONST);
}
#endif					//read motor current
//----------------------------------------------------------------------------
#if DAVECG_CPRead_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 g_speed_CC61 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.
//----------------------------------------------------------------------------

INLINE unsigned int Read_Motor_Speed(void)
{
#if DAVECG_HALL
    return g_speed_CC60*KSPEED;
#elif DAVECG_BEMF
    return g_speed_CC61*KSPEED;
#endif
}
#endif					//read motor speed
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_VOLTAGE_CTRL 
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Voltage_Start_Ref(float v_start_ref)
{
	if (v_start_ref>VOLTAGE_LOWER_LIMIT && v_start_ref<=VOLTAGE_UPPER_LIMIT)
	{
		g_nv_start_ref = ((v_start_ref/Kvolt)*POWER15);
	}
	else
	{
		g_nv_start_ref = ((DEFAULT_START_VOLT/Kvolt)*POWER15);
	}
}
#endif						//voltage start ref
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Voltage_End_Ref(float v_end_ref)
{
	if (v_end_ref>VOLTAGE_LOWER_LIMIT && v_end_ref<=VOLTAGE_UPPER_LIMIT)
	{
		g_nv_end_ref = ((v_end_ref/Kvolt)*POWER15);
	}
	else
	{
		g_nv_end_ref = ((DEFAULT_END_VOLT/Kvolt)*POWER15);
	}
}
#endif						//voltage end ref
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Ramp_Up_Time(float r_time)
{
	if(r_time>RAMP_TIME_LOWER_LIMIT && r_time<=RAMP_TIME_UP_LIMIT)
	{
		g_ramp_up_time = ((r_time*POWER6)*(g_pwm_freq));
	}
	else
	{
		g_ramp_up_time = ((RAMP_TIME_DEFAULT*POWER6)*(g_pwm_freq));
	}
}
#endif					//ramp up time
//----------------------------------------------------------------------------
#endif					//VOLTAGE_CTRL END
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_CURRENT_CTRL  
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Motor_Current(float current)
{
	if (current>CURRENT_LOWER_LIMIT && current<=CURRENT_UPPER_LIMIT)
	{
		g_current_ref = ((current/KCCONST)*POWER15);
	}
	else
	{
		g_current_ref = ((DEFAULT_CURRENT_VALUE/KCCONST)*POWER15);
	}
}
#endif					//set motor current
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Proportional_Gain(float pi_prop_gain)
{
	if (pi_prop_gain>PROP_GAIN_LOW_LIMIT && pi_prop_gain<PROP_GAIN_HIGH_LIMIT)
	{
		g_pi_array.kp = ((pi_prop_gain/PROP_GAIN_HIGH_LIMIT)*POWER15);
	}
	else
	{
		g_pi_array.kp = ((PROP_GAIN_DEFAULT/PROP_GAIN_HIGH_LIMIT)*POWER15);
	}
}
#endif					//set proportional gain
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Integral_Gain(float pi_integ_gain)
{
	if (pi_integ_gain>INT_GAIN_LOW_LIMIT && pi_integ_gain<INT_GAIN_HIGH_LIMIT)
	{
		g_pi_array.ki = ((pi_integ_gain/INT_GAIN_HIGH_LIMIT)*POWER15);
	}
	else
	{
		g_pi_array.ki = ((INT_GAIN_DEFAULT/INT_GAIN_HIGH_LIMIT)*POWER15);
	}
}
#endif					//set integral gain
//----------------------------------------------------------------------------
#endif					//CURRENT_CTRL END
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_SPEED_CTRL 
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Start_Speed_Ref(int spd_start_ref)
{
	if (spd_start_ref>SPEED_LOWER_LIMIT && spd_start_ref<=SPEED_UPPER_LIMIT)
	{
		g_nv_start_ref = (spd_start_ref * POWER15)/KSPEED;
	}
	else
	{
		g_nv_start_ref = (DEFAULT_START_SPEED * POWER15)/KSPEED;
	}
}
#endif					//set speed start ref
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_End_Speed_Ref(int spd_end_ref)
{
	if (spd_end_ref>SPEED_LOWER_LIMIT && spd_end_ref<=SPEED_UPPER_LIMIT)
	{
		g_nv_end_ref = (spd_end_ref * POWER15)/KSPEED;
	}
	else
	{
		g_nv_end_ref = (DEFAULT_END_SPEED * POWER15)/KSPEED;
	}
}
#endif					//set end speed ref
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Proportional_Gain(float pi_prop_gain)
{
	if (pi_prop_gain>PROP_GAIN_LOW_LIMIT && pi_prop_gain<PROP_GAIN_HIGH_LIMIT)
	{
		g_pi_array.kp = ((pi_prop_gain/PROP_GAIN_HIGH_LIMIT)*POWER15);
	}
	else
	{
		g_pi_array.kp = ((PROP_GAIN_DEFAULT/PROP_GAIN_HIGH_LIMIT)*POWER15);
	}
}
#endif					//set Proportional gain
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Integral_Gain(float pi_integ_gain)
{
	if (pi_integ_gain>INT_GAIN_LOW_LIMIT && pi_integ_gain<INT_GAIN_HIGH_LIMIT)
	{
		g_pi_array.ki = ((pi_integ_gain/INT_GAIN_HIGH_LIMIT)*POWER15);
	}
	else
	{
		g_pi_array.ki = ((INT_GAIN_DEFAULT/INT_GAIN_HIGH_LIMIT)*POWER15);
	}
}
#endif					//set integral gain
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Ramp_Up_Time(float r_time)
{
	if(r_time>RAMP_TIME_LOWER_LIMIT && r_time<=RAMP_TIME_UP_LIMIT)
	{
		g_ramp_up_time = ((r_time*POWER6)*(g_pwm_freq));
	}
	else
	{
		g_ramp_up_time = ((RAMP_TIME_DEFAULT*POWER6)*(g_pwm_freq));
	}
}
#endif					// set ramp up time
//----------------------------------------------------------------------------
#endif					//	SPEED_CTRL END
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_SPEED_CURRENT_CTRL 
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------


INLINE void Set_Start_Speed_Ref(int spd_start_ref)
{
	if (spd_start_ref>SPEED_LOWER_LIMIT && spd_start_ref<=SPEED_UPPER_LIMIT)
	{
		g_nv_start_ref = (spd_start_ref * POWER15)/KSPEED;
	}
	else
	{
		g_nv_start_ref = (DEFAULT_START_SPEED * POWER15)/KSPEED;
	}
}
#endif						// set start speed ref
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_End_Speed_Ref(int spd_end_ref)
{
	if (spd_end_ref>SPEED_LOWER_LIMIT && spd_end_ref<=SPEED_UPPER_LIMIT)
	{
		g_nv_end_ref = (spd_end_ref * POWER15)/KSPEED;
	}
	else
	{
		g_nv_end_ref = (DEFAULT_END_SPEED * POWER15)/KSPEED;
	}
}
#endif					//set end speed ref
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Speed_Proportional_Gain(float pi_prop_gain)
{
	if (pi_prop_gain>PROP_GAIN_LOW_LIMIT && pi_prop_gain<PROP_GAIN_HIGH_LIMIT)
	{
		g_pi_speed_array.kp = ((pi_prop_gain/PROP_GAIN_HIGH_LIMIT)*POWER15);
	}
	else
	{
		g_pi_speed_array.kp = ((SPEED_PROP_GAIN_DEFAULT/PROP_GAIN_HIGH_LIMIT)*POWER15);
	}
}
#endif					//set speed proportional gain
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Current_Proportional_Gain(float pi_prop_gain)
{
	if (pi_prop_gain>PROP_GAIN_LOW_LIMIT && pi_prop_gain<PROP_GAIN_HIGH_LIMIT)
	{
		g_pi_current_array.kp = ((pi_prop_gain/PROP_GAIN_HIGH_LIMIT)*POWER15);
	}
	else
	{
		g_pi_current_array.kp = ((CURRENT_PROP_GAIN_DEFAULT/PROP_GAIN_HIGH_LIMIT)*POWER15);
	}
}
#endif					//set proportional gain
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Speed_Integral_Gain(float pi_integ_gain)
{
	if (pi_integ_gain>INT_GAIN_LOW_LIMIT && pi_integ_gain<INT_GAIN_HIGH_LIMIT)
	{
		g_pi_speed_array.ki = ((pi_integ_gain/INT_GAIN_HIGH_LIMIT)*POWER15);
	}
	else
	{
		g_pi_speed_array.ki = ((SPEED_INT_GAIN_DEFAULT/INT_GAIN_HIGH_LIMIT)*POWER15);
	}
}
#endif							//set integral gain
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Current_Integral_Gain(float pi_integ_gain)
{
	if (pi_integ_gain>INT_GAIN_LOW_LIMIT && pi_integ_gain<INT_GAIN_HIGH_LIMIT)
	{
		g_pi_current_array.ki = ((pi_integ_gain/INT_GAIN_HIGH_LIMIT)*POWER15);
	}
	else
	{
		g_pi_current_array.ki = ((CURRENT_INT_GAIN_DEFAULT/INT_GAIN_HIGH_LIMIT)*POWER15);
	}
}
#endif							//set integral gain
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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.
//----------------------------------------------------------------------------

INLINE void Set_Ramp_Up_Time(float r_time)
{
	if(r_time>RAMP_TIME_LOWER_LIMIT && r_time<=RAMP_TIME_UP_LIMIT)
	{
		g_ramp_up_time = ((r_time*POWER6)*(g_pwm_freq));
	}
	else
	{
		g_ramp_up_time = ((RAMP_TIME_DEFAULT*POWER6)*(g_pwm_freq));
	}
}
#endif					//set ramp up time
//----------------------------------------------------------------------------
#endif					//SPEED_CURRENT_CTRL END
//----------------------------------------------------------------------------

//****************************************************************************
#if DAVECG_BEMF
#if DAVECG_CPSet_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. 
//----------------------------------------------------------------------------

INLINE void Set_Bemf_Start_Freq(unsigned int s_freq)
{
	if (s_freq> BEMF_START_FREQ_MIN && s_freq<=BEMF_START_FREQ_MAX)
	{
		g_start_freq = ((s_freq*PULSE_FREQUENCY)/64);
	}
	else
	{
		g_start_freq = ((BEMF_START_FREQ_DEFAULT*PULSE_FREQUENCY)/64);
	}
}
#endif					//bemf start freq
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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. 
//----------------------------------------------------------------------------

INLINE void Set_Bemf_Check_Freq(unsigned int bc_freq)
{
	if (bc_freq>BEMF_CHECK_FREQ_MIN && bc_freq<=BEMF_CHECK_FREQ_MAX)
	{
		g_bemf_check_freq = ((bc_freq*PULSE_FREQUENCY)/64);
	}
	else
	{
		g_bemf_check_freq = ((BEMF_CHECK_FREQ_DEFAULT*PULSE_FREQUENCY)/64);
	}
}
#endif					//bemf check freq
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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. 
//----------------------------------------------------------------------------

INLINE void Set_Bemf_End_Freq(unsigned int e_freq)
{
	if (e_freq>BEMF_END_FREQ_MIN && e_freq<=BEMF_END_FREQ_MAX)
	{
		g_bemf_end_freq = ((e_freq*PULSE_FREQUENCY)/64);
	}
	else
	{
		g_bemf_end_freq = ((BEMF_END_FREQ_DEFAULT*PULSE_FREQUENCY)/64);
	}
}
#endif					//bemf end freq
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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. 
//----------------------------------------------------------------------------

INLINE void Set_Bemf_Start_Duty_Cycle(unsigned int start_dc)
{
	if (start_dc>BEMF_START_DUTY_CYC_MIN && start_dc<=BEMF_START_DUTY_CYC_MAX)
	{
		g_start_duty_c = (start_dc*(PULSE_PERIOD/100));
	}
	else
	{
		g_start_duty_c = (BEMF_START_DUTY_CYC_DEFAULT*(PULSE_PERIOD/100));
	}
}
#endif					//bemf start duty cycle
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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_START_RAMP  // Linear ramp
INLINE void Set_Bemf_Freq_Inc(float freq_inc)
{

	if (freq_inc>BEMF_FREQ_INC_MIN && freq_inc<=BEMF_FREQ_INC_MAX)
	{
		g_freq_increment = (BEMF_FREQ_INC_MAX/freq_inc)-1;
	}
	else
	{
		g_freq_increment = (BEMF_FREQ_INC_MAX/BEMF_FREQ_INC_DEFAULT)-1;
	}
}
#else  // QUAD ramp
INLINE void Set_Bemf_Freq_Inc(float freq_inc)
{
	static float Square_Root(float val);

	if (freq_inc>BEMF_FREQ_INC_MIN && freq_inc<=BEMF_FREQ_INC_MAX)
	{
		g_freq_increment = (Square_Root(2*PULSE_FREQUENCY/freq_inc))-1;
	}
	else
	{
		g_freq_increment = (Square_Root(2*PULSE_FREQUENCY/BEMF_FREQ_INC_DEFAULT))-1;
	}
}

static float Square_Root(float val)
{
	int limit=0;
	float result  = val / 2;
	const float Tolerance = 1.0e-3;
	float result_tolerance;

	do 
	{
	result = (result + val / result) / 2;
	
	limit++;
	if(limit>1000)
		break;
	
	result_tolerance=result * result - val;
	
	if( result_tolerance < 0 )
	
		result_tolerance=-result_tolerance;


	}while( result_tolerance > Tolerance);
	return result;
}

#endif   // QUAD or Linear ramp


#endif					//bemf freq inc
//----------------------------------------------------------------------------
#if DAVECG_CPSet_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. 
//----------------------------------------------------------------------------

INLINE void Set_Bemf_Voltage_Inc(float volt_inc)
{
	if (volt_inc>BEMF_VOLT_INC_MIN && volt_inc<=BEMF_VOLT_INC_MAX)
	{
		g_volt_increment = ((BEMF_VOLT_INC_MAX/volt_inc)-1);
	}
	else
	{
		g_volt_increment = ((BEMF_VOLT_INC_MAX/BEMF_VOLT_INC_DEFAULT)-1);
	}
}
#endif					//bemf volt inc
//----------------------------------------------------------------------------
#endif					//BEMF CTRL END
//----------------------------------------------------------------------------
//
#endif					//BLOCK COMMUTATION END
//----------------------------------------------------------------------------
//
//****************************************************************************
#endif													//END OF FILE
//****************************************************************************
