#ifndef _V_PWM24_H
#define _V_PWM24_H

#ifdef __cplusplus
	extern "C" {  
#endif

#include "IQmathLib.h"
#include "DSP281x_Device.h"
#include "word_structurs.h"


#include "vhzprof.h"
#include "rmp_cntl_v1.h"

enum {  V_PWM24_PREV_PWM_CLOSE = 1,
        V_PWM24_PREV_PWM_MIDDLE,
        V_PWM24_PREV_PWM_WORK_KM0,
        V_PWM24_PREV_PWM_WORK
};

enum {  V_PWM24_PHASE_SEQ_NORMAL_ABC = 1,
        V_PWM24_PHASE_SEQ_NORMAL_BCA,
        V_PWM24_PHASE_SEQ_NORMAL_CAB,
        V_PWM24_PHASE_SEQ_REVERS_ACB,
        V_PWM24_PHASE_SEQ_REVERS_CBA,
        V_PWM24_PHASE_SEQ_REVERS_BAC
};




typedef struct  {       _iq freq1;
                        _iq k1;
                        _iq k2;
	                    } ALG_PWM24;

typedef ALG_PWM24 *ALG_PWM24_handle;


#define ALG_PWM24_DEFAULTS  {0,0,0}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////


typedef struct 	{ //  _iq  Gain;						// Input: reference gain voltage (pu)
					//_iq  Offset;				// Input: reference offset voltage (pu)
//					_iq  Freq;					// Input: reference frequency (pu)
//					_iq  FreqMax;				// Parameter: Maximum step angle = 6*base_freq*T (pu)
//					_iq  Alpha;					// History: Sector angle (pu)
					//_iq  Full_Alpha;
					//_iq  NewEntry;			// History: Sine (angular) look-up pointer (pu)
//					_iq delta_U;
//					_iq delta_t;
					//int16 Periodmax;
					//int16 PeriodMin;
					int XilinxFreq;	// Xilinx freq in TIC

					unsigned int pwm_minimal_impuls_zero_plus;
					unsigned int pwm_minimal_impuls_zero;
					unsigned int pwm_minimal_impuls_zero_minus;

					WORD_UINT2BITS_STRUCT saw_direct;

					//int region;
					//Uint32  SectorPointer;   	// History: Sector number (Q0) - independently with global Q
					//PIDREG3 delta_t;
//					_iq Ia;
//					_iq	Ib;
//					_iq Ic;
					unsigned int number_svgen;
					unsigned int phase_sequence; // номер ПЧ для задания нужного направления чередования
					int prev_level; // предыдущее состояние ШИМа, для перехода из middle или close в рабочее
					unsigned int  Tclosed_high;
				    unsigned int Tclosed_saw_direct_0;
				    unsigned int Tclosed_saw_direct_1;


					unsigned int   Ta_0;
					unsigned int   Ta_1;

					int   Ta_imp;

					unsigned int   Tb_0;
					unsigned int   Tb_1;

					int   Tb_imp;

					unsigned int   Tc_0;
					unsigned int   Tc_1;

                    int   Tc_imp;

//					void   (*calc)();	    	// Pointer to calculation function
//					void   (*calc_dq)();	    // Pointer to calculation function which don`t calculate angle from freq
					} SVGEN_PWM24;
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																				
typedef SVGEN_PWM24 *SVGEN_PWM24_handle;


//#define SVGEN_PWM24_TIME_DEFAULTS { 0,0,0,0 }

                     
#define SVGEN_PWM24_DEFAULTS { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
//								(void (*)(unsigned int))svgen_pwm24_calc }

//extern int ar_sa_a[3][4][7];

extern SVGEN_PWM24 svgen_pwm24_1; 
extern SVGEN_PWM24 svgen_pwm24_2;

extern _iq pidCur_Kp;
extern _iq pidCur_Ki;

extern _iq iq_alfa_coef;

extern _iq iq_koef_mod_korrect_1;
extern _iq iq_koef_mod_korrect_2;



void write_swgen_pwm_times(unsigned int mode_reload);

//void change_freq_pwm(_iq FreqMax, int freq_pwm_xtics, _iq XilinxFreq);

unsigned int detect_level_interrupt(int flag_second_PCH);


void svgen_set_time_keys_closed(SVGEN_PWM24 *vt);
void svgen_set_time_middle_keys_open(SVGEN_PWM24 *vt);


_iq correct_balance_uzpt_pwm24(_iq Tinput, _iq Kplus);

void recalc_time_pwm_minimal_2_xilinx_pwm24(SVGEN_PWM24 *pwm24,
                                            unsigned int *T0, unsigned int *T1,
                                            int *T_imp,
                                            _iq timpuls_corr );



////////////////////////////////////////////////////////////

void InitXPWM(unsigned int freq_pwm);

void start_PWM24(int O1, int O2);

void InitPWM_Variables(int n_pch);

//////////////////////////////////////////////

extern ALG_PWM24    alg_pwm24;
extern RMP_V1      rmp_freq;
extern VHZPROF      vhz1;


#ifdef __cplusplus
	}
#endif

#endif /* _V_PWM24_H */
