/*
 * driver.h
 *
 * Created: 4.3.2013 18:51:04
 *  Author: Jirka
 */ 

enum reg_name {ABS_POS, EL_POS, MARK, SPEED, ACC, DEC, MAX_SPEED, MIN_SPEED,		//Register names
	KVAL_HOLD, KVAL_RUN, KVAL_ACC, KVAL_DEC, INT_SPEED, ST_SLP, FN_SLP_ACC,
	FN_SLP_DEC, K_THERM, ADC_OUT, OCD_TH, STALL_TH, FS_SPD, STEP_MODE,
	ALARM_EN, CONFIG, STATUS};
enum direction {BACKWARD, FORWARD};													//Direction
enum action {RESET, MARK_reg};
	

//---------REGISTER  ADDRESSES---------//
#define adr_ABS_POS		0x01
#define adr_EL_POS		0x02
#define adr_MARK		0x03
#define adr_SPEED		0x04
#define adr_ACC			0x05
#define adr_DEC			0x06
#define adr_MAX_SPEED	0x07
#define adr_MIN_SPEED	0x08
#define adr_KVAL_HOLD	0x09
#define adr_KVAL_RUN	0x0A
#define adr_KVAL_ACC	0x0B
#define adr_KVAL_DEC	0x0C
#define adr_INT_SPEED	0x0D
#define adr_ST_SLP		0x0E
#define adr_FN_SLP_ACC	0x0F
#define adr_FN_SLP_DEC	0x10
#define adr_K_THERM		0x11
#define adr_ADC_OUT		0x12
#define adr_OCD_TH		0x13
#define adr_STALL_TH	0x14
#define adr_FS_SPD		0x15
#define adr_STEP_MODE	0x16
#define adr_ALARM_EN	0x17
#define adr_CONFIG		0x18
#define adr_STATUS		0x19
//-------------------------------------//

//---------------------------------------------PRESETS---------------------------------------------//
//ALL registers
#define NO_PRESETS					0x00000000
#define VALUE_NULL					0x00000000

//MIN_SPEED register
#define LSPD_OPT_ON					0x10			//Low speed optimalization ON
#define LSPD_OPT_OFF				0x00			//Low speed optimalization OFF

//ALARM_EN register
#define ALARM_OVERCURRENT			0x01			//Over-current alarm
#define ALARM_THERMAL_SHUTDOWN		0x02			//Thermal shutdown alarm
#define ALARM_THERMAL_WARNING		0x04			//Thermal warning alarm
#define ALARM_UNDERVOLTAGE			0x08			//Under-voltage alarm
#define ALARM_STALL_DETECTION_A		0x10			//Stall detection (Bridge A) alarm
#define ALARM_STALL_DETECTION_B		0x20			//Stall detection (Bridge B) alarm
#define ALARM_SWITCH_ON				0x40			//Switch turn-on event alarm
#define ALARM_WRONG_CMD				0x80			//wrong or non-performable command alarm

//STEP_MODE register
#define STEP_SEL_FULL_STEP			0x00			//Step mode = Full step
#define STEP_SEL_HALF_STEP			0x01			//Step mode = Half step
#define STEP_SEL_MICROSTEP_1_4		0x02			//Step mode = 1/4 micro-step
#define STEP_SEL_MICROSTEP_1_8		0x03			//Step mode = 1/8 micro-step
#define STEP_SEL_MICROSTEP_1_16		0x04			//Step mode = 1/16 micro-step
#define STEP_SEL_MICROSTEP_1_32		0x05			//Step mode = 1/32 micro-step
#define STEP_SEL_MICROSTEP_1_64		0x06			//Step mode = 1/64 micro-step
#define STEP_SEL_MICROSTEP_1_128	0x07			//Step mode = 1/128 micro-step
#define SYNC_SEL_EL_POS_7			0x00			//SYNC signal source = EL_POS[7]
#define SYNC_SEL_EL_POS_6			0x10			//SYNC signal source = EL_POS[6]
#define SYNC_SEL_EL_POS_5			0x20			//SYNC signal source = EL_POS[5]
#define SYNC_SEL_EL_POS_4			0x30			//SYNC signal source = EL_POS[4]
#define SYNC_SEL_EL_POS_3			0x40			//SYNC signal source = EL_POS[3]
#define SYNC_SEL_EL_POS_2			0x50			//SYNC signal source = EL_POS[2]
#define SYNC_SEL_EL_POS_1			0x60			//SYNC signal source = EL_POS[1]
#define SYNC_SEL_EL_POS_0			0x70			//SYNC signal source = EL_POS[0]
#define SYNC_EN_SYNC				0x80			//BUSY/SYNC pin = SYNC operation mode
#define SYNC_EN_BUSY				0x00			//BUSY/SYNC pin = BUSY operation mode

//CONFIG register
#define OSC_SEL_000					0x0000			//OSC_SEL and EXT_CLK bits set the system clock source
#define OSC_SEL_001					0x0001			//OSC_SEL and EXT_CLK bits set the system clock source
#define OSC_SEL_010					0x0002			//OSC_SEL and EXT_CLK bits set the system clock source
#define OSC_SEL_011					0x0003			//OSC_SEL and EXT_CLK bits set the system clock source
#define OSC_SEL_100					0x0004			//OSC_SEL and EXT_CLK bits set the system clock source
#define OSC_SEL_101					0x0005			//OSC_SEL and EXT_CLK bits set the system clock source
#define OSC_SEL_110					0x0006			//OSC_SEL and EXT_CLK bits set the system clock source
#define OSC_SEL_111					0x0007			//OSC_SEL and EXT_CLK bits set the system clock source
#define EXT_CLK_SET					0x0008			//OSC_SEL and EXT_CLK bits set the system clock source
#define SW_MODE_USER				0x0010			//External switch - HardStop interrupt
#define SW_MODE_INTERRUPT			0x0000			//External switch - For user disposal
#define EN_VSCOMP_EN				0x0020			//Motor supply voltage compensation enable
#define EN_VSCOMP_DIS				0x0000			//Motor supply voltage compensation disable
#define OC_SD_SHUT_DOWN				0x0080			//Over-current even shut down bridges
#define OC_SD_NOT_SHUT_DOWN			0x0000			//Over-current even do not shut down bridges
#define POW_SR_180					0x0100			//Power bridge output slew rate = 180
#define POW_SR_290					0x0200			//Power bridge output slew rate = 290
#define POW_SR_530					0x0300			//Power bridge output slew rate = 530
#define F_PWM_DEC_0625				0x0000			//PWM frequency: Multiplication factor = 0.625
#define F_PWM_DEC_0750				0x0400			//PWM frequency: Multiplication factor = 0.750
#define F_PWM_DEC_0875				0x0800			//PWM frequency: Multiplication factor = 0.875
#define F_PWM_DEC_1000				0x0C00			//PWM frequency: Multiplication factor = 1.000
#define F_PWM_DEC_1250				0x1000			//PWM frequency: Multiplication factor = 1.250
#define F_PWM_DEC_1500				0x1400			//PWM frequency: Multiplication factor = 1.500
#define F_PWM_DEC_1750				0x1800			//PWM frequency: Multiplication factor = 1.750
#define F_PWM_DEC_2000				0x1C00			//PWM frequency: Multiplication factor = 2.000
#define F_PWM_INT_1					0x0000			//PWM frequency: Integer division factor = 1
#define F_PWM_INT_2					0x2000			//PWM frequency: Integer division factor = 2
#define F_PWM_INT_3					0x4000			//PWM frequency: Integer division factor = 3
#define F_PWM_INT_4					0x6000			//PWM frequency: Integer division factor = 4
#define F_PWM_INT_5					0x8000			//PWM frequency: Integer division factor = 5
#define F_PWM_INT_6					0xA000			//PWM frequency: Integer division factor = 6
#define F_PWM_INT_7					0xC000			//PWM frequency: Integer division factor = 7
//-------------------------------------------------------------------------------------------------//



#ifndef DRIVER_H_
#define DRIVER_H_

	#define FLAG_PORT		PORTB
	#define FLAG_PIN			3

	unsigned char Nop (void);
	/*
		Nothing is performed.
	*/
	void SetParam (reg_name set_param,long presets, long set_value1, long set_value2);
	/*	The SetParam command sets the SET_PARAM register value equal to PRESETS, SET_VALUE, SET_VALUE2. 
		SetParam	
			void SetParam (ABS_POS, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Current position (22b), 2's complement format;
				set_value2 - VALUE_NULL;
			
			void SetParam (EL_POS, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Electrical position, STEP (2b);
				set_value2 - Electrical position, MICROSTEP (7b);
			
			void Set_Param (MARK, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Mark position (22b);
				set_value2 - VALUE_NULL;
			
			void SetParam (SPEED, presets, set_value1, set_value2)
				presets - Read only;
				set_value1 - Read only;
				set_value2 - Read only;
			
			void SetParam (ACC, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Acceleration (12b);
				set_value2 - VALUE_NULL;
			
			void SetParam (DEC, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Deceleration (12b);
				set_value2 - VALUE_NULL;	
			
			void SetParam (MAX_SPEED, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Maximum speed (10b);
				set_value2 - VALUE_NULL;
			
			void SetParam (MIN_SPEED, presets, set_value1, set_value2)
				presets - LSPD_OPT_ON / LSPD_OPT_OFF;
				set_value1 - Minimum speed (12b);
				set_value2 - VALUE_NULL;
			
			void SetParam (KVAL_HOLD, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 -  Holding Kval(8b);
				set_value2 - VALUE_NULL;
			
			void SetParam (KVAL_RUN, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Constant speed Kval (8b);
				set_value2 - VALUE_NULL;
			
			void SetParam (KVAL_ACC, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Acceleration starting Kval (8b);
				set_value2 - VALUE_NULL;
			
			void SetParam (KVAL_DEC, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Deceleration starting Kval (8b);
				set_value2 - VALUE_NULL;
			
			void SetParam (INT_SPEED, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Intersect speed (14b);
				set_value2 - VALUE_NULL;
			
			void SetParam (ST_SLP, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Start slope (8b);
				set_value2 - VALUE_NULL;
			
			void SetParam (FN_SLP_ACC, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Acceleration final slope (8b);
				set_value2 - VALUE_NULL;
			
			void SetParam (FN_SLP_DEC, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Deceleration final slope (8b);
				set_value2 - VALUE_NULL;
			
			void SetParam (K_THERM, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Thermal compensation faactor (4b);
				set_value2 - VALUE_NULL;
			
			void SetParam (ADC_OUT, presets, set_value1, set_value2)
				presets - Read only;
				set_value1 - Read only;
				set_value2 - Read only;
			
			void SetParam (OCD_TH, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - OCD treshold (4b);
				set_value2 - VALUE_NULL;
		
			void SetParam (STALL_TH, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - STALL treshold (7b);
				set_value2 - VALUE_NULL;
			
			void SetParam (FS_SPD, presets, set_value1, set_value2)
				presets - NO_PRESETS;
				set_value1 - Full-step speed (10b);
				set_value2 - VALUE_NULL;	
		
			void SetParam (STEP_MODE, presets, set_value1, set_value2)
				presets - STEP_SEL_FULL_STEP					SYNC_SEL_EL_POS_7				SYNC_EN_SYNC
						  STEP_SEL_HALF_STEP					SYNC_SEL_EL_POS_6				SYNC_EN_BUSY
						  STEP_SEL_MICROSTEP_1_4				SYNC_SEL_EL_POS_5	
						  STEP_SEL_MICROSTEP_1_8				SYNC_SEL_EL_POS_4	
						  STEP_SEL_MICROSTEP_1_16				SYNC_SEL_EL_POS_3	
						  STEP_SEL_MICROSTEP_1_32				SYNC_SEL_EL_POS_2	
						  STEP_SEL_MICROSTEP_1_64				SYNC_SEL_EL_POS_1	
						  STEP_SEL_MICROSTEP_1_128				SYNC_SEL_EL_POS_0			  
				set_value1 - VALUE_NULL;
				set_value2 - VALUE_NULL;
			
			void SetParam (ALARM_EN, presets, set_value1, set_value2)
				presets - ALARM_OVERCURRENT		
						  ALARM_THERMAL_SHUTDOWN	
						  ALARM_THERMAL_WARNING	
						  ALARM_UNDERVOLTAGE		
						  ALARM_STALL_DETECTION_A	
						  ALARM_STALL_DETECTION_B	
						  ALARM_SWITCH_ON			
						  ALARM_WRONG_CMD			
				set_value1 - VALUE_NULL;
				set_value2 - VALUE_NULL;
		
			void SetParam (CONFIG, presets, set_value1, set_value2)
				presets - OSC_SEL_000			EXT_CLK_SET					POW_SR_180				F_PWM_DEC_0625			F_PWM_INT_1
						  OSC_SEL_001			SW_MODE_USER				POW_SR_290				F_PWM_DEC_0750			F_PWM_INT_2
						  OSC_SEL_010			SW_MODE_INTERRUPT			POW_SR_530				F_PWM_DEC_0875			F_PWM_INT_3
						  OSC_SEL_011			EN_VSCOMP_EN										F_PWM_DEC_1000			F_PWM_INT_4
						  OSC_SEL_100			EN_VSCOMP_DIS										F_PWM_DEC_1250			F_PWM_INT_5
						  OSC_SEL_101			OC_SD_SHUT_DOWN										F_PWM_DEC_1500			F_PWM_INT_6
						  OSC_SEL_110			OC_SD_NOT_SHUT_DOWN									F_PWM_DEC_1750			F_PWM_INT_7
						  OSC_SEL_111																F_PWM_DEC_2000									
				set_value1 - VALUE_NULL;
				set_value2 - VALUE_NULL;
	*/	
	long GetParam (reg_name get_param);
	/*	This command reads the current GET_PARAM register value.
		get param - Name of register (ABS_POS, EL_POS, MARK, SPEED, ACC, DEC, MAX_SPEED, MIN_SPEED,										  KVAL_HOLD, KVAL_RUN, KVAL_ACC, KVAL_DEC, INT_SPEED, ST_SLP, FN_SLP_ACC,
									  FN_SLP_DEC, K_THERM, ADC_OUT, OCD_TH, STALL_TH, FS_SPD, STEP_MODE,
									  ALARM_EN, CONFIG, STATUS)
		return - Long value of register
	*/
	void Run (direction dir, long spd);
	/*
		The Run command produces a motion at SPD speed. The direction is selected by the DIR.
		dir - FORWARD, BACKWARD
		spd - speed value (20b)
	*/
	void StepClock (direction dir);
	/*
		The StepClock command switches the device in Step-clock mode and imposes the forward or backward direction.
		dir - FORWARD, BACKWARD
	*/
	void Move (direction dir, long n_step);
	/*
		The Move command produces a motion of N_STEP microsteps. Direction is selected by DIR.
		dir - FORWARD, BACKWARD
		n_step - number of steps (22b)
	*/
	void GoTo (long abs_position);
	/*
		The GoTo command produces a motion to ABS_POSITION absolute position through the shortest path.
		abs_position - absolute position (22b)
	*/
	void GoTo_Dir (direction dir, long abs_position);
	/*
		The GoTo_DIR command produces a motion to ABS_POSITION absolute position imposing a forward or a backward rotation.
		dir - FORWARD, BACKWARD
		abs_position - absolute position (22b)
	*/
	void GoUntil (action act, direction dir, long spd);
	/*
		The GoUntil command produces a motion at SPD speed imposing a forward or a backward direction. When an external switch
		turn-on event occurs, the ABS_POS register is reset or ABS_POS register value is copied into the MARK register, then the
		system performs a SoftStop command.
		act - RESET, MARK_reg
		dir - FORWARD, BACKWARD
		spd - speed value (20b)
	*/
	void ReleaseSW (action act, direction dir);
	/*
		The ReleaseSW command produces a motion at minimum speed imposing a forward or a backward rotation. When SW is released
		(opened), the ABS_POS register is reset or the ABS_POS register value is copied into the MARK register, then the system
		performs a HardStop command.
		act - RESET, MARK_reg
		dir - FORWARD, BACKWARD
	*/
	void GoHome (void);
	/*
		The GoHome command produces a motion to the HOME position (zero position) via the shortest path.
	*/
	void GoMark (void);
	/*
		The GoMark command produces a motion to the MARK position performing the minimum path.
	*/
	void ResetPos (void);
	/*
		The ResetPos commands resets the ABS_POS register to zero. The zero position is also defined as HOME position.
	*/
	void ResetDevice (void);
	/*
		The ResetDevice command resets the device to power up conditions.
	*/
	void SoftStop (void);
	/*
		The SoftStop command causes an immediate deceleration to zero speed and a consequent motor stop. The deceleration
		value is the one stored in the DEC register.
	*/
	void HardStop (void);
	/*
		The HardStop command causes an immediate motor stop with infinite deceleration.
	*/
	void SoftHiZ (void);
	/*
		The SoftHiZ command disables the power bridges (high impedance state) after a deceleration to zero. The deceleration
		value used is the one stored in the DEC register. When bridges are disabled, the HiZ flag is raised.
	*/
	void HardHiZ (void);
	/*
		The HardHiZ command immediately disables the power bridges (high impedance state) and raises the HiZ flag.
	*/
	int GetStatus (void);
	/*
		The GetStatus command returns the STATUS register value.
	*/

#endif /* DRIVER_H_ */