/**************************************************************************
	Description: После загрузки процессора функция вызывается один раз
	и инициализирует управляющие регистры процессора
	TMS320F28335/TMS320F28379D.

	Автор: Улитовский Д.И.
	Дата последнего обновления: 2021.10.04
**************************************************************************/


#include "init28335.h"

#define FREQ_TIMER_3    (FREQ_PWM*2)

void init28335(void) {
    init_simple_scalar();

	edrk.flag_second_PCH = 0;

	edrk_init_variables_matlab();
	init_global_time_struct(FREQ_TIMER_3);

    Init_Adc_Variables();

    //svgen_pwm24_1.phase_sequence = SIMULINK_SEQUENCE;
    //svgen_pwm24_2.phase_sequence = SIMULINK_SEQUENCE;

    edrk.zadanie.iq_Izad = _IQ(1);
    //analog_zero.iqU_1 = 2048;
    //analog_zero.iqU_2 = 2048;
} //void init28335(void)

void edrk_init_variables_matlab(void)
{

	initVectorControl();
	InitXPWM(FREQ_PWM);
	InitPWM_Variables(edrk.flag_second_PCH);

//#if(SENSOR_ALG==SENSOR_ALG_23550)
//	rotorInit();
//#endif
//#if(SENSOR_ALG==SENSOR_ALG_22220)
//	// 22220 
//	rotorInit_22220();
//#endif

	control_station.clear(&control_station);

	edrk_init_matlab();


	init_ramp_all_zadanie();
	init_all_limit_koeffs();


    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_SET_IZAD] = NOMINAL_SET_IZAD;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_SET_KM] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_SET_U_ZARYAD] = NOMINAL_U_ZARYAD;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_SET_K_U_DISBALANCE] = NOMINAL_SET_K_U_DISBALANCE;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_SET_K_PLUS_U_DISBALANCE] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_SET_LIMIT_POWER] = NOMINAL_SET_LIMIT_POWER;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_UFCONST_VECTOR] = 1;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_SCALAR_FOC] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_ENABLE_ON_CHARGE] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_GO] = 1;

    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_DISABLE_ON_PUMP] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_ENABLE_ON_CHARGE] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_DISABLE_ON_QTV] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_DISABLE_ON_UMP] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_MANUAL_DISCHARGE] = 0;

    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_ROTOR_POWER] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_DISABLE_INTERRUPT_SYNC] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_DISABLE_INTERRUPT_TIMER2] = 0;
    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_STOP_LOGS] = 0;

    control_station.array_cmd[CONTROL_STATION_INGETEAM_PULT_RS485][CONTROL_STATION_CMD_SET_U_ZARYAD] = NOMINAL_U_ZARYAD;
    control_station.array_cmd[CONTROL_STATION_MPU_SVU_CAN][CONTROL_STATION_CMD_SET_U_ZARYAD] = NOMINAL_U_ZARYAD;

    control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][CONTROL_STATION_CMD_WDOG_OFF] = 0;


    for (int i = 0; i < CONTROL_STATION_CMD_LAST; i++)
        control_station.array_cmd[CONTROL_STATION_TERMINAL_CAN][i] = control_station.array_cmd[CONTROL_STATION_TERMINAL_RS232][i];


    ramp_all_zadanie(2);

    set_zadanie_u_charge_matlab();
    init_Uin_rms();
}

void edrk_init_matlab(void)
{

	edrk.Uzad_max = _IQ(K_STATOR_MAX); // макс амплитуда в Км для минимального импульса = DEF_PERIOD_MIN_MKS
	edrk.iq_bpsi_normal = _IQ(BPSI_NORMAL / NORMA_FROTOR);
	//   edrk.iq_bpsi_max = _IQ(BPSI_MAXIMAL/NORMA_FROTOR);
   //    edrk.iq_f_provorot = _IQ(F_PROVOROT/NORMA_FROTOR);

	edrk.flag_enable_update_hmi = 1;


	edrk.zadanie.ZadanieU_Charge = NOMINAL_U_ZARYAD;
	edrk.zadanie.iq_ZadanieU_Charge = _IQ(NOMINAL_U_ZARYAD / NORMA_ACP);

	edrk.zadanie.iq_set_break_level = _IQ(NOMINAL_U_BREAK_LEVEL / NORMA_ACP);

	control_station.setup_time_detect_active[CONTROL_STATION_TERMINAL_RS232] = 50;
}

void set_zadanie_u_charge_matlab(void)
{

    //	edrk.ZadanieU_Charge = edrk.ZadanieU_Charge_RS;

    //	edrk.iq_ZadanieU_Charge = _IQ(edrk.ZadanieU_Charge/NORMA_ACP);

    if (edrk.zadanie.ZadanieU_Charge <= 100)
    {
        edrk.iqMIN_U_ZPT = _IQ(-50.0 / NORMA_ACP);
        edrk.iqMIN_U_IN = _IQ(-50.0 / NORMA_ACP);
    }
    else
    {

        edrk.iqMIN_U_ZPT = _IQ(edrk.zadanie.ZadanieU_Charge * MIN_U_PROC / NORMA_ACP);
        edrk.iqMIN_U_IN = _IQ(edrk.zadanie.ZadanieU_Charge * MIN_U_PROC / NORMA_ACP);

    }

    if (edrk.zadanie.ZadanieU_Charge < LEVEL_DETECT_U_SMALL)
    {
        edrk.iqMAX_U_ZPT_Predzaryad = _IQ(edrk.zadanie.ZadanieU_Charge * MAX_U_PROC_SMALL / NORMA_ACP);
        edrk.iqMAX_U_ZPT_Global = edrk.iqMAX_U_ZPT_Predzaryad + _IQ(ADD_U_MAX_GLOBAL_SMALL / NORMA_ACP); // +500V

    }
    else
    {
        edrk.iqMAX_U_ZPT_Predzaryad = _IQ(edrk.zadanie.ZadanieU_Charge * MAX_U_PROC / NORMA_ACP);
        edrk.iqMAX_U_ZPT_Global = edrk.iqMAX_U_ZPT_Predzaryad + _IQ(ADD_U_MAX_GLOBAL / NORMA_ACP); // +200V

        if (edrk.iqMAX_U_ZPT_Global > U_D_MAX_ERROR_GLOBAL)
            edrk.iqMAX_U_ZPT_Global = U_D_MAX_ERROR_GLOBAL;
    }

    edrk.iqMAX_U_ZPT = edrk.iqMAX_U_ZPT_Global;//_IQ(edrk.zadanie.ZadanieU_Charge*MAX_U_PROC/NORMA_ACP);
    edrk.iqMAX_U_IN = _IQ(edrk.zadanie.ZadanieU_Charge * MAX_U_PROC / NORMA_ACP);

    edrk.zadanie.iq_set_break_level = _IQ(NOMINAL_U_BREAK_LEVEL / NORMA_ACP);

}