//*****************************************************************************/
// @Module         COMMON APPLICATION INTERFACE
// @Filename       API_LIB.H
// @Controller     DAVECG_ControlAlgoKBS.Target@
//
//
// @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"


#include "MAIN.H"


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





#if DAVECG_ControlAlgoKBS.KEIL

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

extern void ExecuteCANCmd(void);

extern data 	unsigned char 	ucNewCMD0;
extern idata 	PI_parameter 	gt_Id_control;
extern idata 	PI_parameter 	gt_Iq_control;
extern idata 	PI_parameter 	gt_Speed_control;
extern data 	int 			gi_Iq_startup;
extern data 	int 			gi_Speed_reference_user;
extern data 	int 			gi_Speed_startup;	
extern bdata 	unsigned char 	Status_word;
extern data 	int 			gi2_Speed[2];
extern data		int				gi_I_d;
extern data		int				gi_I_q;
extern 			bit 			gb_Trap;
extern 			bit 			gb_On;
extern 			bit 			gb_Off;

#endif //end of KEIL

#if DAVECG_ControlAlgoKBS.SDCC

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


extern void ExecuteCANCmd(void);

extern data at 0x002a unsigned char 	ucNewCMD0;
extern idata at 0x0080	PI_parameter 	gt_Id_control;		// char y[3] @ 0x80, int k.p @ 0x83, int k.i @ 0x85
extern idata at 0x0088	PI_parameter 	gt_Iq_control; 		// char y[3] @ 0x88, int k.p @ 0x8B, int k.i @ 0x8D
extern idata at 0x0090	PI_parameter 	gt_Speed_control; 	// char y[3] @ 0x90, int k.p @ 0x93, int k.i @ 0x95
extern data at 0x0038 	int 			gi_Iq_startup;
extern data at 0x0032 	int 			gi_Speed_reference_user;	
extern data at 0x0036 	int 			gi_Speed_startup;
extern data at 0x0020	unsigned char 	Status_word; 

extern data 			int 			gi2_Speed[2];
extern data				int				gi_I_d;
extern data				int				gi_I_q;
extern 			bit 			gb_Trap;
extern 			bit 			gb_On;
extern 			bit 			gb_Off;

#endif   //end of SDCC

extern int API_Ref;
extern char MotorDirection;

#if DAVECG_ControlPanelKBS.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.
//----------------------------------------------------------------------------

#define Set_Motor_Start() ucNewCMD0=CMDSTART;	ExecuteCANCmd()

#endif

#if DAVECG_ControlPanelKBS.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.
//----------------------------------------------------------------------------

#define Set_Motor_Stop() ucNewCMD0=CMDSTOP;	ExecuteCANCmd()

#endif

#if DAVECG_ControlPanelKBS.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.
//----------------------------------------------------------------------------

#define Set_Direction_Forward()	if(gi_Speed_reference_user<0) gi_Speed_reference_user = -gi_Speed_reference_user

#endif					//Motor Forward Direction
//----------------------------------------------------------------------------
#if DAVECG_ControlPanelKBS.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.
//----------------------------------------------------------------------------

#define Set_Direction_Reverse()	if(gi_Speed_reference_user>0) gi_Speed_reference_user = -gi_Speed_reference_user

#endif					//Motor Reverse Direction


#if DAVECG_ControlPanelKBS.CPSet_Proportional_Gain_iq
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Set_Proportional_Gain_Iq(float)
// Description		This Function sets his Function sets the proportional gain for IQ
//----------------------------------------------------------------------------

#define Set_Proportional_Gain_Iq(val) API_Ref=(int)val; if (API_Ref<=MAX_KP && API_Ref>=0) (API_Ref=(int)((val-1)*(NORM_KP/16)+(NORM_KP/16)-1));\
									  else  API_Ref=0;\
									  if (API_Ref>0) (gt_Iq_control.kp=API_Ref);\
									  else (gt_Iq_control.kp=DEFAULT_IQ_KP)

#endif

#if DAVECG_ControlPanelKBS.CPSet_Integral_Gain_iq
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Set_integral_gain_Iq(float)
// Description		This Function sets his Function sets the integral gain for IQ.
//----------------------------------------------------------------------------

#define Set_integral_gain_Iq(val) API_Ref=(int)val; if (API_Ref<=MAX_KI && API_Ref>=0)(API_Ref=(int)(val*(NORM_KI/16)));\
								  else  API_Ref=0;\
								  if (API_Ref>0) (gt_Iq_control.ki=API_Ref);\
								  else (gt_Iq_control.ki=DEFAULT_IQ_KI)

#endif


#if DAVECG_ControlPanelKBS.CPSet_Proportional_Gain_id
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Set_Proportional_Gain_Id(float)
// Description		This Function sets his Function sets the proportional gain for Id
//----------------------------------------------------------------------------

#define Set_Proportional_Gain_Id(val) API_Ref=(int)val; if (API_Ref<=MAX_KP && API_Ref>=0) (API_Ref=(int)((val-1)*(NORM_KP/16)+(NORM_KP/16)-1));\
									  else  API_Ref=0;\
									  if (API_Ref>0) (gt_Id_control.kp=API_Ref);\
									  else (gt_Id_control.kp=DEFAULT_ID_KP)

#endif


#if DAVECG_ControlPanelKBS.CPSet_Integral_Gain_id
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Set_integral_gain_Id(float)
// Description		This Function sets his Function sets the integral gain for Id
//----------------------------------------------------------------------------

#define Set_integral_gain_Id(val) API_Ref=(int)val; if (API_Ref<=MAX_KI && API_Ref>=0) (API_Ref=(int)(val*(NORM_KI/16)));\
								  else  API_Ref=0;\
								  if (API_Ref>0 ) (gt_Id_control.ki=API_Ref);\
								  else (gt_Id_control.ki=DEFAULT_ID_KI)

#endif

#if DAVECG_ControlPanelKBS.CPSet_Proportional_Gain_speed
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Set_Proportional_Gain_speed(float)
// Description		This Function sets his Function sets the proportional gain for Speed
//----------------------------------------------------------------------------

#define Set_Proportional_Gain_speed(val) API_Ref=(int)val; if (API_Ref<=MAX_KP && API_Ref>=0) (API_Ref=(int)((val-1)*(NORM_KP/16)+(NORM_KP/16)-1));\
										else  API_Ref=0;\
									  	if (API_Ref>0) (gt_Speed_control.kp=API_Ref);\
									  	else (gt_Speed_control.kp=DEFAULT_SPEED_KP)

#endif


#if DAVECG_ControlPanelKBS.CPSet_Integral_Gain_speed
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Set_integral_Gain_speed(float)
// Description		This Function sets his Function sets the integral gain for speed
//----------------------------------------------------------------------------

#define Set_integral_Gain_speed(val) API_Ref=(int)val; if (API_Ref<=MAX_KI && API_Ref>=0) (API_Ref=(int)(val*(NORM_KI/16)));\
									else  API_Ref=0;\
								  	if (API_Ref>0) (gt_Speed_control.ki=API_Ref);\
								  	else (gt_Speed_control.ki=DEFAULT_SPEED_KI)

#endif

#if DAVECG_ControlPanelKBS.CPSet_IqStartup
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Set_Iq_Startup(float)
// Description		This Function sets his Function sets the IQ startup value
//----------------------------------------------------------------------------

#define Set_Iq_Startup(val) API_Ref=(int)val; if (API_Ref<=MAX_IQ_REF && API_Ref>=0) (API_Ref=(int)(val*(NORM_IQ_REF/16)));\
							else  API_Ref=0;\
							if (API_Ref>0) (gi_Iq_startup=API_Ref);\
							else (gi_Iq_startup=DEFAULT_IQ_STARTUP)

#endif


#if DAVECG_ControlPanelKBS.CPSet_Motor_Speed
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Set_Motor_Speed(int)
// Description		This Function sets the motor speed 
//----------------------------------------------------------------------------

#define Set_Motor_Speed(val) API_Ref=(int)(val*NORM_SPEED_API);\
							 if (API_Ref>0 && API_Ref<=(SPEED_MAX_TARGET)) (gi_Speed_reference_user=API_Ref);\
							 else (gi_Speed_reference_user=DEFAULT_SPEED_REFERENCE)


#endif

#if DAVECG_ControlPanelKBS.CPSet_Speed_Startup
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Set_Speed_Startup(int)
// Description		This Function sets the motor startup speed
//----------------------------------------------------------------------------

#define Set_Speed_Startup(val) API_Ref=(int)(val*NORM_SPEED_API);\
							   if (API_Ref>0 && API_Ref<(SPEED_MAX_TARGET)) (gi_Speed_startup=API_Ref);\
							   else (gi_Speed_startup=DEFAULT_SPEED_STARTUP)
#endif

#if DAVECG_ControlPanelKBS.CPGet_Motor_Speed
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Get_Motor_Speed()
// Description		This Function returns motor speed
//----------------------------------------------------------------------------

#define Get_Motor_Speed() gi2_Speed[0]

#endif

#if DAVECG_ControlPanelKBS.CPGet_Iq
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Get_Iq()
// Description		This Function returns Iq value
//----------------------------------------------------------------------------

#define Get_Iq() gi_I_q

#endif

#if DAVECG_ControlPanelKBS.CPGet_Id
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Get_Id()
// Description		This Function returns Id value
//----------------------------------------------------------------------------

#define Get_Id() gi_I_d

#endif


#if DAVECG_ControlPanelKBS.CPGet_Motor_Status
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Get_Motor_Status()
// Description		This Function returns motor staus 
//----------------------------------------------------------------------------

#define Get_Motor_Status() Status_word

#endif

#if DAVECG_ControlPanelKBS.CPGet_Motor_Start_Status
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Get_Motor_Start_Status()
// Description		This Function returns the bit for motor run status
//----------------------------------------------------------------------------

#define Get_Motor_Start_Status() gb_On

#endif

#if DAVECG_ControlPanelKBS.CPGet_Motor_Stop_Status
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Get_Motor_Stop_Status()
// Description		This Function returns the bit for motor stop status
//----------------------------------------------------------------------------

#define Get_Motor_Stop_Status()	gb_Off

#endif


#if DAVECG_ControlPanelKBS.CPGet_Ctrap_Status
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Get_Ctrap_Status()
// Description		This Function returns ctrap staus
//----------------------------------------------------------------------------

#define Get_Ctrap_Status() gb_Trap

#endif


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

#define Reset_Ctrap_Req() gb_Trap=0

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

#if DAVECG_ControlPanelKBS.CPGet_ADC_AuxA
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Get_ADC_AuxA()
// Description		This Function to read 10-bit conversion results
//----------------------------------------------------------------------------

static uword Get_ADC_AuxA(void)
{
	uword uwResult = 0;

	SFR_PAGE(_ad2, SST1);          // switch to page 2

	// 10-bit conversion 
	uwResult = (((ADC_RESR3L >> 6) & 0x03)+(uword)(ADC_RESR3H << 2))&0x03FF; 
	
	SFR_PAGE(_ad0, RST1);          // restore the old ADC page

	return(uwResult);
} 
#endif
//----------------------------------------------------------------------------

#if DAVECG_ControlPanelKBS.CPGet_ADC_AuxB
//----------------------------------------------------------------------------
//****************************************************************************
// @Function        Get_ADC_AuxB()
// Description		This Function to read 10-bit conversion results
//----------------------------------------------------------------------------

static uword Get_ADC_AuxB(void)
{
	int uwResult = 0;

	SFR_PAGE(_ad2, SST1);          // switch to page 2
	
	// 10-bit conversion 
	uwResult = (((ADC_RESR2L >> 6) & 0x03)+(uword)(ADC_RESR2H << 2))&0x03FF; 
	
	SFR_PAGE(_ad0, RST1);          // restore the old ADC page

	return(uwResult);
} 
#endif
//----------------------------------------------------------------------------


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