#include "intrinsics_56800E.h" 
#include "types.h" 

/* Used in function: main	- constant in a case of constant flux excitation */
#define 	PSI_S_LENGTH_REQ 	(19333)     /* Psi_req / scale_psi = 0.59 / 0.000030518 */

/* Used in function: rotate_Psi_S */
#define 	K_COS 			(16380)			/* (1-cos(alpha_n)) / alpha_n^2 * 2^15 */
#define		K_SIN 			(32757)			/* sin(alpha_n) / alpha_n * 2^15 */ 

/* Used in function: compute_stator_voltage */
#define		U_S_LENGTH_MAX 	(14344)			/* U_max / scale_u  */
#define		SAMPLE_FREQ 	(30722)			/* 1/(Ts / (scale_psi / scale_u))*2^15 / 2^5 */
#define 	SHIFT_U_S 		(5)				/* number of shifts to normalize computed stator voltage 2^5 */
#define		R_S 			(69)			/* Rs * (scale_i / scale_u) * 2^15 / 2^5 */
#define  	U_S_LENGTH_MAX_SQ 	(0x00062C10)/* (U_S_LENGTH_MAX / 2^5)^2  * 2 */ 	/* *2 due to fractional multiplication */
#define  	U_S_LENGTH_MAX_SQ_HI 	(0x0006)/* extract the 16 MSBs of a 32-bit value U_S_LENGTH_MAX_SQ*/
#define  	U_S_LENGTH_MAX_SQ_LO 	(0x2C10)/* extract the 16 LSBs of a 32-bit value U_S_LENGTH_MAX_SQ*/

/* Used in function: select_alpha */
#define  	PP_TS 				(4716)		/* pp * Ts * scale_omega / scale_alpha * 2^15 */

typedef struct
{
	Frac16 	alpha;
	Frac16	beta;
} mc_sPhase;

extern mc_sPhase Psi_S, Psi_Sn, i_S, u_S;

//header file for function 'PDSFC.c'

/* PDSFC routines in C language*/
void rotate_Psi_S(mc_sPhase *Psi_S, mc_sPhase *Psi_Sn, Frac16 alpha);
void adjust_module_Psi_S(mc_sPhase 	*Psi_Sn, Frac16 Psi_S_length_req, Frac16 I);
void compute_stator_voltage(mc_sPhase *Psi_Sn, mc_sPhase *Psi_S,mc_sPhase *i_S, mc_sPhase *u_S);
Frac16 select_alpha(Frac16 omega_est, Frac16 omega_req, Frac16 Deltaalpha, Frac16 I);
Frac16 test_current(mc_sPhase *i_S, Frac16 I_max);
void limit_stator_voltage(mc_sPhase *u_S, Frac16 U_max);

/* PDSFC routines in assembly language*/
asm void a_rotate_Psi_S(mc_sPhase 	*Psi_S, mc_sPhase 	*Psi_Sn, Frac16 alpha);
asm void a_adjust_module_Psi_S(mc_sPhase 	*Psi_Sn, Frac16 Psi_S_length_req, Frac16 I);
asm void a_compute_stator_voltage(mc_sPhase *Psi_Sn, mc_sPhase *i_S, mc_sPhase *Psi_S, mc_sPhase *u_S);
asm short a_select_alpha(Frac16 omega_est, Frac16 omega_req, Frac16 Deltaalpha, Frac16 I);
asm short a_test_current(mc_sPhase *i_S, Frac16 I_max);
asm void a_limit_stator_voltage(mc_sPhase *u_S, Frac16 U_max);