diff --git a/App_Wrapper/app_io.c b/App_Wrapper/app_io.c index 8df1cdf..d97aaf9 100644 --- a/App_Wrapper/app_io.c +++ b/App_Wrapper/app_io.c @@ -2,6 +2,20 @@ #include "upp.h" +#define detect_front(_in_numb_, _var_, _val_) { \ +if ((in[_in_numb_] > 0.5) && (prev_in[_in_numb_] <= 0.5)) \ +{ \ + _var_ = _val_; \ +} } + +#define detect_rise(_in_numb_, _var_, _val_) { \ +if ((in[_in_numb_] < 0.5) && (prev_in[_in_numb_] >= 0.5)) \ +{ \ + _var_ = _val_; \ +} } + + + void WriteFromSFunc(real_T* disc) { @@ -17,49 +31,43 @@ void WriteFromSFunc(real_T* disc) disc[PORT_WIDTH + i] = 1; } } - disc[2 * PORT_WIDTH + 0] = phase_A.ctrl.f.EnableTiristor; - disc[2 * PORT_WIDTH + 1] = phase_A.ctrl.f.TiristorIsEnable; - disc[2 * PORT_WIDTH + 2] = uwTick - phase_A.ctrl.enable_start_tick; - disc[2 * PORT_WIDTH + 3] = phase_A.ctrl.angle.start_delay_us; - disc[2 * PORT_WIDTH + 4] = (uint16_t)((uint16_t)TIMER->CNT - phase_A.ctrl.angle.start_delay_us); + disc[2 * PORT_WIDTH + 0] = phase_A.ctrl.angle.delay_us; + disc[2 * PORT_WIDTH + 1] = (uint16_t)((uint16_t)TIMER->CNT - phase_A.ctrl.angle.start_delay_tick); + disc[2 * PORT_WIDTH + 2] = phase_A.ctrl.angle.start_delay_tick; + disc[2 * PORT_WIDTH + 3] = TIMER->CNT; } void ReadToSFunc(real_T* in) { - static real_T in_prev[3]; + static real_T prev_in[IN_PORT_WIDTH]; - if ((in[0] > 0.5) && (in_prev[0] <= 0.5)) + detect_front(0, phase_A.zc_detector.f.EXTIZeroCrossDetected, 1); + detect_rise(0, phase_A.zc_detector.f.EXTIZeroCrossDetected, 1); + + detect_front(1, phase_B.zc_detector.f.EXTIZeroCrossDetected, 1); + detect_rise(1, phase_B.zc_detector.f.EXTIZeroCrossDetected, 1); + + detect_front(2, phase_C.zc_detector.f.EXTIZeroCrossDetected, 1); + detect_rise(2, phase_C.zc_detector.f.EXTIZeroCrossDetected, 1); + + detect_front(3, Upp.GoSafe, 1); + detect_rise(3, Upp.GoSafe, 0); + + detect_front(4, Upp.Prepare, 1); + detect_rise(4, Upp.Prepare, 0); + + detect_front(5, Upp.ForceStop, 1); + detect_rise(5, Upp.ForceStop, 0); + + + Upp.sine_freq = in[6]; + Upp.Duration = in[7]; + + + for (int i = 0; i < IN_PORT_WIDTH; i++) { - phase_A.zc_detector.f.EXTIZeroCrossDetected = 1; + prev_in[i] = in[i]; } - else if ((in[0] < 0.5) && (in_prev[0] >= 0.5)) - { - phase_A.zc_detector.f.EXTIZeroCrossDetected = 1; - } - - - if ((in[1] > 0.5) && (in_prev[1] <= 0.5)) - { - phase_B.zc_detector.f.EXTIZeroCrossDetected = 1; - } - else if ((in[1] < 0.5) && (in_prev[1] >= 0.5)) - { - phase_B.zc_detector.f.EXTIZeroCrossDetected = 1; - } - - - if ((in[2] > 0.5) && (in_prev[2] <= 0.5)) - { - phase_C.zc_detector.f.EXTIZeroCrossDetected = 1; - } - else if ((in[2] < 0.5) && (in_prev[2] >= 0.5)) - { - phase_C.zc_detector.f.EXTIZeroCrossDetected = 1; - } - - in_prev[0] = in[0]; - in_prev[1] = in[1]; - in_prev[2] = in[2]; } \ No newline at end of file diff --git a/MCU_Wrapper/MCU.c b/MCU_Wrapper/MCU.c index 9dd552a..05ad921 100644 --- a/MCU_Wrapper/MCU.c +++ b/MCU_Wrapper/MCU.c @@ -185,8 +185,10 @@ static void mdlInitializeSampleTimes(SimStruct* S) static void mdlTerminate(SimStruct* S) { hmcu.fMCU_Stop = 1; +#ifdef RUN_APP_MAIN_FUNC_THREAD ResumeThread(hmcu.hMCUThread); WaitForSingleObject(hmcu.hMCUThread, 10000); +#endif SIM_deInitialize_Simulation(); mexUnlock(); } diff --git a/MCU_Wrapper/mcu_wrapper.c b/MCU_Wrapper/mcu_wrapper.c index ef08569..f7e212d 100644 --- a/MCU_Wrapper/mcu_wrapper.c +++ b/MCU_Wrapper/mcu_wrapper.c @@ -108,7 +108,7 @@ void MCU_writeOutputs(SimStruct* S) /* Get S-Function descrete array */ real_T* Out_Buff = ssGetDiscStates(S); - //Simulate_GPIO_BSRR(); + Simulate_GPIO_BSRR(); WriteFromSFunc(Out_Buff); } //-----------------CONTROLLER SIMULATE FUNCTIONS---------------// @@ -169,9 +169,13 @@ void SIM_Initialize_Simulation(void) */ void SIM_deInitialize_Simulation(void) { +//#ifdef DEINITIALIZE_AFTER_SIM +#include "upp.h" + memset(&Upp, 0, sizeof(Upp)); // simulate structures of peripheral deinitialization deInitialize_Periph_Sim(); // mcu peripheral memory deinitialization deInitialize_MCU(); +//#endif } //-------------------------------------------------------------// diff --git a/MCU_Wrapper/mcu_wrapper_conf.h b/MCU_Wrapper/mcu_wrapper_conf.h index 367ae87..816ac09 100644 --- a/MCU_Wrapper/mcu_wrapper_conf.h +++ b/MCU_Wrapper/mcu_wrapper_conf.h @@ -49,11 +49,14 @@ #define MCU_CORE_CLOCK 72000000 +//#define DEINITIALIZE_AFTER_SIM ///< Enable deinitializing structures at simulation ends + + #define PORT_WIDTH 16 ///< width of one port #define PORT_NUMB 3 ///< amount of ports // Parameters of S_Function #define NPARAMS 1 ///< number of input parametrs (only Ts) -#define IN_PORT_WIDTH (3) ///< width of input ports +#define IN_PORT_WIDTH (8) ///< width of input ports #define IN_PORT_NUMB 1 ///< number of input ports #define OUT_PORT_WIDTH PORT_WIDTH ///< width of output ports #define OUT_PORT_NUMB PORT_NUMB ///< number of output ports diff --git a/MCU_Wrapper/run_mex.bat b/MCU_Wrapper/run_mex.bat index 8317291..64dd87f 100644 --- a/MCU_Wrapper/run_mex.bat +++ b/MCU_Wrapper/run_mex.bat @@ -81,3 +81,4 @@ IF [%1]==[debug] (set debug= -g) ::------START COMPILING------- echo Compiling... mex %output% %defines% %includes% %codes% %debug% +echo %DATE% %TIME% \ No newline at end of file diff --git a/mcu_test_r2023.slx b/mcu_test_r2023.slx index 24025c0..675cc8c 100644 Binary files a/mcu_test_r2023.slx and b/mcu_test_r2023.slx differ diff --git a/mexing.m b/mexing.m index 26713cc..f524873 100644 --- a/mexing.m +++ b/mexing.m @@ -3,7 +3,7 @@ clear, clc, set = mex.getCompilerConfigurations('C', 'Selected'); -Ts = 0.000001; +Ts = 0.00001; delete("*.mexw64") delete("*.mexw64.pdb")