#include "controller.h"
//#include "edrk_main.h"
//#include "vector.h"
//#include "vector_control.h"
//
//#include "xp_project.h"
//#include "xp_write_xpwm_time.h"
//#include "edrk_main.h"
//#include "vector.h"
//#include "vector_control.h"
//#include "v_rotor.h"



T_project project = {0};


WINDING a;
EDRK edrk = EDRK_DEFAULT;
FLAG f = FLAG_DEFAULTS;

WRotorValues WRotor = WRotorValues_DEFAULTS;




void mcu_simulate_step(void)
{
	int ff = 0;
	static _iq Uzad1 = 0, Fzad = 0, Uzad2 = 0, Izad_out = 0, Uzad_from_master = 0;
	int pwm_enable_calc_main;
	_iq wd;

	if (edrk.flag_second_PCH == 0) {
		wd = uf_alg.winding_displacement_bs1;
	}
	else {
		wd = uf_alg.winding_displacement_bs2;
	}

	detect_level_interrupt(edrk.flag_second_PCH);
	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;
	}
	else
	{
		pwm_enable_calc_main = 0;
	}


	ramp_all_zadanie(0);  // тут все по штатному

	calc_norm_ADC_0(0);

	vectorControlConstId(edrk.zadanie.iq_power_zad_rmp, edrk.zadanie.iq_oborots_zad_hz_rmp,
		WRotor.RotorDirectionSlow, WRotor.iqWRotorSumFilter,
		edrk.Mode_ScalarVectorUFConst,
		edrk.MasterSlave, edrk.zadanie.iq_Izad, wd,
		edrk.master_theta, edrk.master_Iq, edrk.iq_power_kw_another_bs,
		&edrk.tetta_to_slave, &edrk.Iq_to_slave, &edrk.P_to_master,
		0, 0);

	test_calc_vect_dq_pwm24_Ing(vect_control.iqTheta, vect_control.iqUdKm, vect_control.iqUqKm,
		edrk.disable_alg_u_disbalance,
		edrk.zadanie.iq_kplus_u_disbalance_rmp, edrk.zadanie.iq_k_u_disbalance_rmp,
		filter.iqU_1_fast, filter.iqU_2_fast,
		0,
		edrk.Uzad_max,
		edrk.MasterSlave,
		edrk.flag_second_PCH,
		&edrk.Kplus, &edrk.Uzad_to_slave);
	analog.PowerFOC = edrk.P_to_master;
	Fzad = vect_control.iqFstator;
	Izad_out = edrk.Iq_to_slave;

	if (xpwm_time.one_or_two_interrupts_run == PWM_ONE_INTERRUPT_RUN)
		write_swgen_pwm_times(PWM_MODE_RELOAD_FORCE);
	else
	{
		if (pwm_enable_calc_main)
			write_swgen_pwm_times(PWM_MODE_RELOAD_LEVEL_HIGH);
		else
			write_swgen_pwm_times(PWM_MODE_RELOAD_LEVEL_LOW);
	}
}








void project_read_all_pbus2()
{

}

#pragma DATA_SECTION(break_result_1,".fast_vars");
_iq break_result_1 = 0;

#pragma DATA_SECTION(break_result_2,".fast_vars");
_iq break_result_2 = 0;

#pragma DATA_SECTION(break_result_3,".fast_vars");
_iq break_result_3 = 0;

#pragma DATA_SECTION(break_result_4,".fast_vars");
_iq break_result_4 = 0;

//void read_in_sensor_line1(T_cds_in_rotation_sensor *rs){
//
//}
//
//void read_in_sensor_line2(T_cds_in_rotation_sensor *rs){
//
//}
//void read_command_reg(T_cds_in_rotation_sensor *rs){
//
//}
//void write_command_reg(T_cds_in_rotation_sensor *rs){
//
//}
//void tune_sampling_time(T_rotation_sensor *rs){
//
//}
//void wait_for_registers_updated(T_cds_in_rotation_sensor *rs){
//
//}
//void read_direction_in_plane(T_cds_in_rotation_sensor *rs){
//
//}
//void sensor_set(T_rotation_sensor *rs)
//{
//
//}
//void sensor_read(T_rotation_sensor *rs)
//{
//
//}
//void update_sensors_data(T_rotation_sensor *rs)
//{
// //   rs->in_plane.write.regs.comand_reg.bit.update_registers = 1;
// //   write_command_reg(&rs->in_plane);
////    rs->in_plane.write.regs.comand_reg.bit.update_registers = 0;
//}
//void angle_sensor_read(T_cds_angle_sensor *as)
//{}
//
//void angle_plane_set(T_cds_angle_sensor *rs)
//{}
//void in_plane_set(T_cds_in_rotation_sensor* rs)
//{}
//
//void in_sensor_read1(T_cds_in_rotation_sensor *rs)
//{}
//
//void in_sensor_read2(T_cds_in_rotation_sensor *rs)
//{}




// unsigned int BWC_Started()
// {

// }

void update_uom(void)
{

}

void inc_RS_timeout_cicle(void)
{

}

void inc_CAN_timeout_cicle(void)
{

}

void pause_1000(void)
{

}

int xerror(unsigned int er_ID, void* CallBackRef)
{
};
unsigned int ReadMemory(unsigned long addr)
{
   //return (*(volatile int *)(addr));
    return 0;
}


void WriteMemory(unsigned long addr, unsigned int data)
{
   //(*(volatile int *)( addr )) = data;
}

void func_unpack_answer_from_Ingeteam(unsigned int a) {}
void unpack_answer_from_MPU_SVU_CAN(unsigned int a) {}
int get_real_in_mbox(int a, int b) {}

void start_pwm(void)
{
	// mPWM_a = 1;
	// mPWM_b = 1;
}

void stop_pwm(void)    
{
	// mPWM_a = 0;
	// mPWM_b = 0;

	// svgen_set_time_keys_closed(&svgen_pwm24_1);
	// svgen_set_time_keys_closed(&svgen_pwm24_2);
	// WriteMemory(ADR_TK_MASK_0, 0xFFFF);
	// WriteMemory(ADR_PWM_START_STOP, 0x8000);
}
void start_break_pwm() {

}


void stop_break_pwm() {

}

void stop_wdog() {

}

void start_pwm_b() {

}
void start_pwm_a() {

}
void stop_pwm_b() {

}
void stop_pwm_a() {

}

void fillADClogs() {

}
void break_resistor_managment_calc(){

}


void break_resistor_managment_init(){

}


void break_resistor_managment_update(){

}


void break_resistor_recup_calc(){

}


void break_resistor_set_closed(){

}


void DetectI_Out_BreakFase(){

}


void test_mem_limit(){

}


void set_start_mem(){

}


void getFastLogs(){

}


//void detect_I_M_overload{
//
// }


void sync_inc_error(){

}


void optical_bus_read(){

}


void optical_bus_write(void){

}


void init_flag_a(void)
{

    unsigned int i = 0;
    int *pStr = (int*)&f;
    for (i = 0; i < sizeof(f) / sizeof(int); i++) {
        *(pStr + i) = 0;
    }

    *pStr = (int*)&a;
    for (i = 0; i < sizeof(a) / sizeof(int); i++) {
        *(pStr + i) = 0;
    }


}
