#include <math.h>
#include <stdlib.h>
//#include "project.h"

#include "IQmathLib.h"
//#include "f281xpwm.h"
//
////#include "SpaceVectorPWM.h"
//#include "MemoryFunctions.h"
//#include "PWMTools.h"
//#include "pwm_vector_regul.h"
//#include "PWMTMSHandle.h"
//#include "TuneUpPlane.h"
//#include "RS_Functions.h"
//#include "CAN_setup.h"
//#include "global_time.h"
#include "params.h"
#include "vector.h"
//#include "rmp_cntl_my1.h"
//#include "vhzprof.h"
//#include "adc_tools.h"
//#include "v_pwm24.h"
//#include "break_regul.h"
//#include "break_tools.h"
//#include "detect_phase.h"
//#include "mathlib.h"
//#include "project.h"
//#include "log_to_memory.h"
//#include "rotation_speed.h"
//#include "detect_overload.h"
//#include "xp_write_xpwm_time.h"
//#include "errors.h"
//#include "sync_tools.h"
//#include "optical_bus.h"
//#include "IQmathLib.h"

#define DEF_FREQ_PWM_XTICS (3750000 / FREQ_PWM / 2)
#define DEF_PERIOD_MIN_XTICS 400	//375 ~ 100mks	//315 ~ 84 mks	//460//(3750000 * mks / 1000000)
#define DEF_PERIOD_MIN_BR_XTICS 165

#define DEF_FREQ_PWM_XTICS_MIN = 4261
#define DEF_FREQ_PWM_XTICS_MAX = 4687

#define STOP_ROTOR_LIMIT	27962	//2 rpm
#define STOP_ROTOR_MIN_CURRENT	4194304	//750A	//3355443	//600A

#define K_MODUL_MAX 15770583	//13421772	//80%	//10066329	//60%	//5033164	//30%	15099494 ~ 90%	//15435038 ~ 0.92%
								//15770583 ~ 0.94%
#define MIN_Fsl_WHEN_STOPED 41943   //0.05 //67108	//0.08Hz

#define PWM_ONE_INTERRUPT_RUN       1
#define PWM_TWICE_INTERRUPT_RUN     0


//4464
// Частота ШИМ в xilinx тиках (60000000 / 16 / FREQ_PWM = 3750000 / FREQ_PWM)
#pragma DATA_SECTION(VAR_FREQ_PWM_XTICS,".fast_vars1");
int VAR_FREQ_PWM_XTICS = DEF_FREQ_PWM_XTICS;

// Минимальное значение ШИМа в xilinx тиках
#pragma DATA_SECTION(VAR_PERIOD_MAX_XTICS,".fast_vars1");
int VAR_PERIOD_MAX_XTICS = DEF_FREQ_PWM_XTICS - DEF_PERIOD_MIN_XTICS;

// Минимальное значение ШИМа в xilinx тиках (mintime+deadtime) (Fпилы * Tмин.ключа.сек = (60 / 16 / 2) * Tмкс = (60  * Tмкс / 16 / 2))
#pragma DATA_SECTION(VAR_PERIOD_MIN_XTICS,".fast_vars1");
int VAR_PERIOD_MIN_XTICS = DEF_PERIOD_MIN_XTICS;// 

// Минимальное значение ШИМа в xilinx тиках для тормозных ключей (mintime) (Fпилы * Tмин.ключа.сек = (60 / 16 / 2) * Tмкс = (60  * Tмкс / 16 / 2))
#pragma DATA_SECTION(VAR_PERIOD_MIN_BR_XTICS,".fast_vars1");
int VAR_PERIOD_MIN_BR_XTICS = DEF_PERIOD_MIN_BR_XTICS;// 

#pragma DATA_SECTION(freq1,".fast_vars1");
_iq freq1;

#pragma DATA_SECTION(k1,".fast_vars1");
_iq k1 = 0;

RMP_MY1 rmp_freq = RMP_MY1_DEFAULTS; 
//VHZPROF vhz1 = VHZPROF_DEFAULTS;

// WINDING a;
// FLAG f;

#define COUNT_SAVE_LOG_OFF 100 //Сколько тактов ШИМ сохраняется после остановки
#define COUNT_START_IMP  2

int i = 0;
/*void InitPWM()
{
		WriteMemory(ADR_PWM_MODE_0, 0x0000); //Выбираем в качестве источника ШИМ TMS
}*/

static void write_swgen_pwm_times();
void write_swgen_pwm_times_split_eages(unsigned int mode_reload);
void fix_pwm_freq_synchro_ain();
void detect_level_interrupt(void);
void detect_current_saw_val(void);

void InitXPWM(void)
{

	#ifdef XPWMGEN
	/* Start of PWM Generator initialization*/ 
	for(i = 0; i < 16; i++) // Обнуляем
	{
		WriteMemory(ADR_PWM_KEY_NUMBER, i);
		WriteMemory(ADR_PWM_TIMING, 0);

	}
	WriteMemory(ADR_PWM_DIRECT, 0xffff);
    WriteMemory(ADR_PWM_DRIVE_MODE, 0); //Choose PWM sourse PWMGenerator on Spartan 200e
    WriteMemory(ADR_PWM_DEAD_TIME, 360); //Dead time in tics. 1 tic = 16.67 nsec
#ifndef _test_without_power
    // OFF WDOG
    WriteMemory(ADR_PWM_WDOG, 0x8005);	//TODO turn on
#else
    // ON WDOG
    WriteMemory(ADR_PWM_WDOG, 0x0005);  //TODO turn on
#endif
    WriteMemory(ADR_PWM_PERIOD, VAR_FREQ_PWM_XTICS); // Saw period in tics. 1 tic = 16.67 nsec
    WriteMemory(ADR_PWM_SAW_DIRECT, 0x0555);
	WriteMemory(ADR_TK_MASK_0, 0);
    WriteMemory(ADR_TK_MASK_1, 0xffff);	//Turn off additional 16 tk lines
#if C_PROJECT_TYPE == PROJECT_BALZAM
    WriteMemory(ADR_PWM_IT_TYPE, 1);	//1 interrupt per PWM period
#else
	WriteMemory(ADR_PWM_IT_TYPE, 0);	//interrupt on each counter twist
#endif
//	WriteMemory(ADR_PWM_WDOG, 0x8008);//разрешение ошибки по PWM
	#endif

	#ifdef TMSPWMGEN

		WriteMemory(ADR_PWM_MODE_0, 0x0000); //Выбираем в качестве источника ШИМ TMS

	#endif
	
/* End оf PWM Gen init */	
}

void initPWM_Variables(void)
{
    //Для первой пилы закрыто, когда выше заданного уровня, для второй ниже
//    xpwm_time.Tclosed_0 = 0;
//    xpwm_time.Tclosed_1 = VAR_FREQ_PWM_XTICS + 1;
//    xpwm_time.pwm_tics = VAR_FREQ_PWM_XTICS;
//    xpwm_time.saw_direct.all = 0;//0x0555;
//    xpwm_time.one_or_two_interrupts_run = PWM_TWICE_INTERRUPT_RUN;


	init_alpha_pwm24(VAR_FREQ_PWM_XTICS);
	InitVariablesSvgen(VAR_FREQ_PWM_XTICS);
	init_DQ_pid();
	break_resistor_managment_init();

    rmp_freq.RampLowLimit = _IQ(-4); //0
	rmp_freq.RampHighLimit = _IQ(4);

	rmp_freq.RampPlus = _IQ(0.00005);	//_IQ(0.0002);_IQ(0.000005);

	rmp_freq.RampMinus = _IQ(-0.00005);	//_IQ(-0.000005);
	rmp_freq.DesiredInput = 0;
	rmp_freq.Out = 0;

	a.k = 0;
	a.k1 = 0;
	a.k2 = 0;

	k1 = 0;
	freq1 = 0;
}

#pragma CODE_SECTION(start_PWM24,".fast_run2")
void start_PWM24(int O1, int O2)
{
	if ((O1 == 1) && (O2 == 1))
	{
		start_pwm();
	}
	else
	{
		if ((O1 == 0) && (O2 == 1))
		{
			start_pwm_b();
		}
		if ((O1 == 1) && (O2 == 0))
		{
			start_pwm_a();
		}
	}
}

inline void init_regulators()
{
	if(f.Mode != 0)
	{
		pwm_vector_model_titov(f.iq_p_zad, f.iq_fzad, rotor.direct_rotor,
				rotor.iqFout, f.Mode, 1, 1);
	}
}

#define select_working_channels(go_a, go_b)		go_a = !f.Obmotka1; \
												go_b = !f.Obmotka2;

void PWM_interrupt()
{
	static unsigned int pwm_run = 0;
	static _iq Uzad1, Fzad, Uzad2;
	static int count_step=0;
	static int count_step_ram_off = 0;
	static int count_start_impuls = 0;
	static int flag_record_log = 0;
	static int log_saved_to_const_mem = 0;
	static int prevGo = -1;
	static volatile unsigned int go_a = 0;
	static volatile unsigned int go_b = 0;
	
	static int stop_rotor_counter = 0;
	
	static int prev_go_a = 1;
	static int prev_go_b = 1;

	int pwm_enable_calc_main = 0;

	int start_int_xtics = 0, end_int_xtics = 0;

//	i_led1_on_off(1);
	if (pwm_run == 1)
	{
//		stop_pwm();
//		errors.slow_stop.bit.PWM_interrupt_to_long |= 1;
		return;
	}
	pwm_run = 1;


//	detect_level_interrupt();
//	start_int_xtics = xpwm_time.current_period;
    if (xpwm_time.where_interrupt == PWM_LOW_LEVEL_INTERRUPT
            || xpwm_time.one_or_two_interrupts_run == PWM_ONE_INTERRUPT_RUN)
    {
        pwm_enable_calc_main = 1;
        if (f.flag_second_PCH) {
			fix_pwm_freq_synchro_ain();
		}
    }
    else {
//    	i_sync_pin_on();
    	pwm_enable_calc_main = 0;
    }


//   	project.cds_in[0].read_pbus(&project.cds_in[0]);    //read direction
//   	project.read_all_pbus();
//    optical_bus_read();

	update_rot_sensors();
	global_time.calc(&global_time);
//
	inc_RS_timeout_cicle();
	inc_CAN_timeout_cicle();
	detect_I_M_overload();
	DetectI_Out_BreakFase();
	Rotor_measure();

	if ((f.Go == 1) && (f.Stop == 0)
			&& (f.rotor_stopped == 0)
//			&& (faults.faults5.bit.rotor_stopped == 0)
			/* && (f.Ready2 == 1)*/)
	{
		if (f.Ready2) {f.flag_turn_On_Pump = 1;} //На вс-кий случай
		if (f.Go != prevGo) {
			set_start_mem(FAST_LOG);
//				clear_mem(FAST_LOG);
//				count_start_impuls = 0;
			count_step = 0;
			count_step_ram_off = COUNT_SAVE_LOG_OFF;

			init_regulators();
			stop_rotor_counter = 0;
		} else {
			if (f.Mode == 0) {
				rmp_freq.DesiredInput = freq1;
				rmp_freq.calc(&rmp_freq);
//				Fzad = rmp_freq.Out;
				Fzad = freq1;
//					if(k1 < 87772)
//					{	k1 = 87772;}
				Uzad1 = k1;
				Uzad2 = k1;
			}

			select_working_channels(go_a, go_b);

			if (go_a == 0 && prev_go_a != go_a) {
				stop_pwm_a();
			}
			if (go_a == 1 && prev_go_a != go_a) {
				start_pwm_a();
			}
			if (go_b == 0 && prev_go_b != go_b) {
				stop_pwm_b();
			}
			if (go_b == 1 && prev_go_b != go_b) {
				start_pwm_b();
			}

			prev_go_a = go_a;
			prev_go_b = go_b;

			if (count_start_impuls < COUNT_START_IMP) {
				count_start_impuls++;

				Fzad = 0;
				rmp_freq.Out = 0;

//					set_start_mem(FAST_LOG);
//					set_start_mem(SLOW_LOG);
			} else {

				if (count_start_impuls==2)
				{
					if (go_a == 1 && go_b == 1) {
					 // тут выставлено middle состояние ключей перед разрешением выходов pwm
					 // start_pwm(); должен вызваться один раз за изменение f.Go
						start_pwm();
					} else if (go_a == 1) {
						write_swgen_pwm_times();	//TODO: Check with new PWM
						start_pwm_a();
					} else if (go_b == 1) {
						write_swgen_pwm_times();
						start_pwm_b();
					}
				} // end if (count_start_impuls==1)

				count_start_impuls++;
                if (count_start_impuls > 2 * COUNT_START_IMP) {
                    count_start_impuls = 2 * COUNT_START_IMP;
                }


			}
		}
		flag_record_log = 1;
		log_saved_to_const_mem = 0;
	} else {
		if (f.Discharge
				&& (errors.slow_stop2.bit.Break_Resistor == 0)
				&& (errors.plains_and_others.bit.er0_setted == 0)
				&& (errors.umu_errors.bit.Voltage380_BSU_Off == 0))
//				&& !f.Stop)
		{
			start_break_pwm();
			break_resistor_managment_calc();
		} else {
			//Do not stop, when come for the first time, to write to xilinx times to close keys.
			//Otherwise mintime error can occure.
			//Also we do not stop pwm wile break_resistor keys working
			if (!count_start_impuls) {
			 // это произойдет только на втором такте выключения
				stop_pwm();
			}
			break_resistor_set_closed();
		}
		if (count_step_ram_off > 0) {
			count_step_ram_off--;
			flag_record_log = 1;
		} else {
			flag_record_log = 0;
		}

		pwm_vector_model_titov(f.iq_p_zad, f.iq_fzad, rotor.direct_rotor,
				rotor.iqFout, 0, 1, 1);

		if (count_start_impuls > 0) {
			count_start_impuls -= 1;
		} else {
			count_start_impuls = 0;
		}

		Uzad1 = 87772;	//0.5%
		Uzad2 = 87772;
		k1 = Uzad1;
		svgen_pwm24_1.Gain = Uzad1;
		svgen_pwm24_2.Gain = Uzad1;
		svgen_dq_1.Ualpha = 0;
		svgen_dq_1.Ubeta = 0;
		svgen_dq_2.Ualpha = 0;
		svgen_dq_2.Ubeta = 0;
		a.iqk = Uzad1;
	}
//		a.iqk1 = Uzad1;
//		a.iqk2 = Uzad2;
//		a.iqk = (a.iqk1 + a.iqk2) >> 1;
//		a.iqf = Fzad;
	prevGo = f.Go;

	break_resistor_recup_calc();
	break_resistor_managment_update();

	if (count_start_impuls >= (2 * COUNT_START_IMP) ) {

		if (f.Mode == 0) {
//			test_calc_pwm24(Uzad1, Uzad2, Fzad);
		    if (pwm_enable_calc_main) {
                test_calc_simple_dq_pwm24(Uzad1, Uzad2, Fzad, Fzad, K_MODUL_MAX);
		    }
			analog_dq_calc_const();
		} else {
			if ((_IQabs(rotor.iqFout) < STOP_ROTOR_LIMIT)
					&& (_IQabs(analog.iqIq1) > STOP_ROTOR_MIN_CURRENT)) {
				if ((stop_rotor_counter >= 2520)) {
					stop_rotor_counter = 2520;
//					faults.faults5.bit.rotor_stopped |= 1;
					f.rotor_stopped |= 1;
				} else {
					stop_rotor_counter += 1;
				}
				if (_IQabs(analog.Fsl) < MIN_Fsl_WHEN_STOPED) {
//					faults.faults5.bit.rotor_stopped |= 1;
					f.rotor_stopped |= 1;
				}
			} else {
				if (stop_rotor_counter > 0) {
					stop_rotor_counter -= 1;
				} else {
					stop_rotor_counter = 0;
				}
			}

			pwm_vector_model_titov(f.iq_p_zad, f.iq_fzad, rotor.direct_rotor,
					rotor.iqFout, f.Mode, 0, pwm_enable_calc_main);
		}

	} else {
		// тут опять middle состояние перед выключением ключей
		if (count_start_impuls)
		{
//            svgen_set_time_keys_closed(&svgen_pwm24_1);
//            svgen_set_time_keys_closed(&svgen_pwm24_2);
			svgen_set_time_middle_keys_open(&svgen_pwm24_1);
			svgen_set_time_middle_keys_open(&svgen_pwm24_2);
		}
		else
		// а тут мы уже выключились
		{
			svgen_set_time_keys_closed(&svgen_pwm24_1);
			svgen_set_time_keys_closed(&svgen_pwm24_2);
			//Снимаем запрет ШИМа
//			if (faults.faults5.bit.rotor_stopped == 1) {
			if (f.rotor_stopped == 1) {
			    if (stop_rotor_counter > 0) {
			        stop_rotor_counter -= 1;
			    } else {
//			        faults.faults5.bit.rotor_stopped = 0;
			        f.rotor_stopped = 0;
			        stop_rotor_counter = 0;
			    }
			} else {
			    stop_rotor_counter = 0;
			}
		}
	}

	if (f.Mode) {
		a.iqf = analog.iqFstator;
	} else {
		a.iqf = Fzad;
		analog.iqFstator = Fzad;
		a.iqk1 = _IQsqrt(_IQmpy(svgen_dq_1.Ualpha, svgen_dq_1.Ualpha) + _IQmpy(svgen_dq_1.Ubeta, svgen_dq_1.Ubeta));    //For output Kmodul to terminal
		a.iqk2 = _IQsqrt(_IQmpy(svgen_dq_2.Ualpha, svgen_dq_2.Ualpha) + _IQmpy(svgen_dq_2.Ubeta, svgen_dq_2.Ubeta));    //end counting Uout
	}
	a.iqk = (a.iqk1 + a.iqk2) / 2;

//	write_swgen_pwm_times();

	if (xpwm_time.one_or_two_interrupts_run == PWM_ONE_INTERRUPT_RUN)
		write_swgen_pwm_times_split_eages(PWM_MODE_RELOAD_FORCE);
	else
	{
		if (f.Go == 1)
		{
			if (count_start_impuls == (2 * COUNT_START_IMP))
			{
				if (pwm_enable_calc_main)
					write_swgen_pwm_times_split_eages(PWM_MODE_RELOAD_LEVEL_HIGH);
				else
					write_swgen_pwm_times_split_eages(PWM_MODE_RELOAD_LEVEL_LOW);
			}
			else
//	              if (pwm_enable_calc_main)
				write_swgen_pwm_times_split_eages(PWM_MODE_RELOAD_FORCE);
		}
		else
		{
			if (count_start_impuls == (2 * COUNT_START_IMP) - 1)
			{
				if (pwm_enable_calc_main)
					write_swgen_pwm_times_split_eages(PWM_MODE_RELOAD_LEVEL_HIGH);
				else
					write_swgen_pwm_times_split_eages(PWM_MODE_RELOAD_LEVEL_LOW);

			}
			else
				write_swgen_pwm_times_split_eages(PWM_MODE_RELOAD_FORCE);
		}

		//      if (pwm_enable_calc_main)
		//          prev_run_calc_uf = run_calc_uf;

	}





	// logs recording
//	if ((flag_record_log && !f.stop_Log) || f.Startstoplog)
//	//if (f.Log1_Log2 == 1)// && f.Go == 1 && (!f.Stop))
//	//if(f.Go == 1)
//			{
//		test_mem_limit(FAST_LOG, !f.Ciclelog);
//		count_step++;
//
//		if (count_step >= 0) {
//			fillADClogs();
////				logpar.log13 = flag_record_log;
////				logpar.log14 = count_start_impuls;
////			logpar.log10 = (int16)_IQtoIQ15(analog.iqIq1_filter);
////			logpar.log11 = (int16)_IQtoIQ15(rotor.iqFrotFromOptica);
//			logpar.log15 = (int16) _IQtoIQ15(analog.iqIq2);
//			logpar.log16 = (int16) _IQtoIQ15(a.iqk1);
//			logpar.log17 = (int16) _IQtoIQ15(analog.iqId1);
//			logpar.log18 = (int16) _IQtoIQ15(analog.iqIq1);
//
////			logpar.log24 = (int16) break_result_1;
////			logpar.log25 = (int16) break_result_2;
//			logpar.log27 = (int16)(_IQtoIQ15(analog.iqIbtr1_1));
//			logpar.log28 = (int16)(_IQtoIQ15(analog.iqIbtr2_1));
//
//			getFastLogs(!f.Ciclelog);
//			count_step = 0;
//		}
//	} else {
//	    if (f.Stop && log_saved_to_const_mem == 0) {
//	        logpar.copy_log_to_const_memory = 1;
//	        log_saved_to_const_mem = 1;
//	    }
//	}

//	optical_bus_write();

//	detect_current_saw_val();
	end_int_xtics = xpwm_time.current_period;
	f.PWMcounterVal = labs(start_int_xtics - end_int_xtics);

	pwm_run = 0;

	i_sync_pin_off();
//	i_led1_on_off(0);

}

void slow_vector_update()
{
	_iq iqKzad = 0;
	freq1 = _IQ (f.fzad/F_STATOR_MAX);//f.iqFRotorSetHz;
	iqKzad = _IQ(f.kzad);
	k1 = zad_intensiv_q(30000, 30000, k1, iqKzad);	//20000
	
	
}

//#pragma CODE_SECTION(write_swgen_pwm_times,".fast_run");
//void write_swgen_pwm_times()
//{
//	xpwm_time.Ta0_0 = (unsigned int) svgen_pwm24_1.Tc_0.Ti;
//	xpwm_time.Ta0_1 = (unsigned int) svgen_pwm24_1.Tc_1.Ti;
//	xpwm_time.Tb0_0 = (unsigned int) svgen_pwm24_1.Tb_0.Ti;
//	xpwm_time.Tb0_1 = (unsigned int) svgen_pwm24_1.Tb_1.Ti;
//	xpwm_time.Tc0_0 = (unsigned int) svgen_pwm24_1.Ta_0.Ti;
//	xpwm_time.Tc0_1 = (unsigned int) svgen_pwm24_1.Ta_1.Ti;
//
//	xpwm_time.Ta1_0 = (unsigned int) svgen_pwm24_2.Tc_0.Ti;
//	xpwm_time.Ta1_1 = (unsigned int) svgen_pwm24_2.Tc_1.Ti;
//	xpwm_time.Tb1_0 = (unsigned int) svgen_pwm24_2.Tb_0.Ti;
//	xpwm_time.Tb1_1 = (unsigned int) svgen_pwm24_2.Tb_1.Ti;
//	xpwm_time.Tc1_0 = (unsigned int) svgen_pwm24_2.Ta_0.Ti;
//	xpwm_time.Tc1_1 = (unsigned int) svgen_pwm24_2.Ta_1.Ti;
//
//	xpwm_time.Tbr0_0 = break_result_1;
//	xpwm_time.Tbr0_1 = break_result_2;
//	xpwm_time.Tbr1_0 = break_result_3;
//	xpwm_time.Tbr1_1 = break_result_4;
//
//	xpwm_time.write_1_2_winding_break_times(&xpwm_time);
//
//
////	logpar.log29 = xpwm_time.Ta0_0;
////	logpar.log30 = xpwm_time.Ta0_1;
////	logpar.log10 = xpwm_time.Tb0_0;
////	logpar.log11 = xpwm_time.Tb0_1;
////	logpar.log12 = xpwm_time.Tc0_0;
////	logpar.log13 = xpwm_time.Tc0_1;
////	logpar.log7 = _IQtoIQ12(svgen_pwm24_1.Alpha);
////	logpar.log8 = xpwm_time.Ta0_0 - xpwm_time.Tb0_0;
////	logpar.log9 = xpwm_time.Ta0_1 - xpwm_time.Tb0_1;
////	logpar.log10 = xpwm_time.Tb0_0 - xpwm_time.Tc0_0;
////	logpar.log11 = xpwm_time.Tb0_1 - xpwm_time.Tc0_1;
////	logpar.log12 = xpwm_time.Tc0_0 - xpwm_time.Ta0_0;
////	logpar.log13 = xpwm_time.Tc0_1 - xpwm_time.Ta0_1;
//
//}

//#pragma CODE_SECTION(write_swgen_pwm_times_split_eages,".fast_run2");
//void write_swgen_pwm_times_split_eages(unsigned int mode_reload)
//{
//
//	xpwm_time.Ta0_0 = (unsigned int) svgen_pwm24_1.Tc_0.Ti;
//	xpwm_time.Ta0_1 = (unsigned int) svgen_pwm24_1.Tc_1.Ti;
//	xpwm_time.Tb0_0 = (unsigned int) svgen_pwm24_1.Tb_0.Ti;
//	xpwm_time.Tb0_1 = (unsigned int) svgen_pwm24_1.Tb_1.Ti;
//	xpwm_time.Tc0_0 = (unsigned int) svgen_pwm24_1.Ta_0.Ti;
//	xpwm_time.Tc0_1 = (unsigned int) svgen_pwm24_1.Ta_1.Ti;
//
//	xpwm_time.Ta1_0 = (unsigned int) svgen_pwm24_2.Tc_0.Ti;
//	xpwm_time.Ta1_1 = (unsigned int) svgen_pwm24_2.Tc_1.Ti;
//	xpwm_time.Tb1_0 = (unsigned int) svgen_pwm24_2.Tb_0.Ti;
//	xpwm_time.Tb1_1 = (unsigned int) svgen_pwm24_2.Tb_1.Ti;
//	xpwm_time.Tc1_0 = (unsigned int) svgen_pwm24_2.Ta_0.Ti;
//	xpwm_time.Tc1_1 = (unsigned int) svgen_pwm24_2.Ta_1.Ti;
//
//	xpwm_time.Tbr0_0 = break_result_1;
//	xpwm_time.Tbr0_1 = break_result_2;
//	xpwm_time.Tbr1_0 = break_result_3;
//	xpwm_time.Tbr1_1 = break_result_4;
//
//	xpwm_time.mode_reload = mode_reload;
//
//    xpwm_time.write_1_2_winding_break_times_split(&xpwm_time);
//}

#define CONST_IQ_1  16777216 //1

//#pragma CODE_SECTION(fix_pwm_freq_synchro_ain,".fast_run");
//void fix_pwm_freq_synchro_ain()
//{
////	if (f.Sync_input_or_output == SYNC_INPUT)
//	{
//	 sync_inc_error();
//
//	 if (f.disable_sync || f.sync_ready == 0)
//	 {
//
//
//		return;
//	 }
//
//	 if (f.pwm_freq_plus_minus_zero==1)
//	 {
//
//
//			 //Increment xtics
//			 VAR_FREQ_PWM_XTICS  = DEF_FREQ_PWM_XTICS + 1;
//			 WriteMemory(ADR_PWM_PERIOD, VAR_FREQ_PWM_XTICS); // Saw period in tics. 1 tic = 16.67 nsec
//
//			 change_freq_pwm(VAR_FREQ_PWM_XTICS);
//
//
//	 }
//
//	 if (f.pwm_freq_plus_minus_zero==-1)
//	 {
//	//4464
//			 //Decrement xtics
//			 VAR_FREQ_PWM_XTICS = DEF_FREQ_PWM_XTICS - 1;
//			 WriteMemory(ADR_PWM_PERIOD, VAR_FREQ_PWM_XTICS); // Saw period in tics. 1 tic = 16.67 nsec
//
//			 change_freq_pwm(VAR_FREQ_PWM_XTICS);
//
//	 }
//
//	 if (f.pwm_freq_plus_minus_zero==0)
//	 {
//		 VAR_FREQ_PWM_XTICS = DEF_FREQ_PWM_XTICS - 1;
//		 WriteMemory(ADR_PWM_PERIOD, VAR_FREQ_PWM_XTICS);
//		 change_freq_pwm(VAR_FREQ_PWM_XTICS);
//	 }
//
//	}
//
//
//
//}


//void detect_level_interrupt(void)
//{
//
//   WriteMemory(ADR_SAW_REQUEST, 0x8000);
//   xpwm_time.current_period = ReadMemory(ADR_SAW_VALUE);
//
//   if (xpwm_time.current_period<xpwm_time.pwm_tics/2)
//       xpwm_time.where_interrupt = PWM_LOW_LEVEL_INTERRUPT;
//
//   if (xpwm_time.current_period>xpwm_time.pwm_tics/2)
//       xpwm_time.where_interrupt = PWM_HIGH_LEVEL_INTERRUPT;
//
//}
//
//void detect_current_saw_val(void)
//{
//	WriteMemory(ADR_SAW_REQUEST, 0x8000);
//	xpwm_time.current_period = ReadMemory(ADR_SAW_VALUE);
//}




