diff --git a/App_Wrapper/main.c b/App_Wrapper/main.c index ee8aeec..a7b229b 100644 --- a/App_Wrapper/main.c +++ b/App_Wrapper/main.c @@ -152,7 +152,7 @@ void Periph_reInit(void) * @brief The application entry point. * @retval int */ -int main(void) +int main_init(void) { /* USER CODE BEGIN 1 */ __HAL_DBGMCU_FREEZE_TIM1(); @@ -212,14 +212,14 @@ int main(void) // MODBUS_Transmit_IT(&hmodbus1, &MODBUS_MSG); //RS_Receive_IT(&hmodbus1, &MODBUS_MSG); - while (1) - { - /* USER CODE END WHILE */ - Periph_reInit(); - /* USER CODE BEGIN 3 */ -// HAL_Delay(200); -// MB_Toogle_Coil_Local(&GPIOD->ODR, COIL_GPIOD_LED3); - } +// while (1) +// { +// /* USER CODE END WHILE */ +// Periph_reInit(); +// /* USER CODE BEGIN 3 */ +//// HAL_Delay(200); +//// MB_Toogle_Coil_Local(&GPIOD->ODR, COIL_GPIOD_LED3); +// } /* USER CODE END 3 */ } diff --git a/MCU_STM32F4xx_Matlab/STM32F4xx_SIMULINK/stm32f4xx_matlab_tim.c b/MCU_STM32F4xx_Matlab/STM32F4xx_SIMULINK/stm32f4xx_matlab_tim.c index cf93d00..c3c83ee 100644 --- a/MCU_STM32F4xx_Matlab/STM32F4xx_SIMULINK/stm32f4xx_matlab_tim.c +++ b/MCU_STM32F4xx_Matlab/STM32F4xx_SIMULINK/stm32f4xx_matlab_tim.c @@ -40,7 +40,6 @@ void TIM_Simulation(TIM_TypeDef *TIMx, struct TIM_Sim *TIMS) Channels_Simulation(TIMx, TIMS); // CaptureCompare and PWM channels simulation break; - // включение слейв таймера по ивенту case(TIM_SLAVEMODE_TRIGGER): // SLAVE MODE: TRIGGER MODE Slave_Mode_Check_Source(TIMx, TIMS); @@ -83,17 +82,34 @@ void Overflow_Check(TIM_TypeDef* TIMx, struct TIM_Sim* TIMS) if ((TIMx->CR1 & TIM_CR1_UDIS) == 0) // UPDATE enable { if ((TIMx->CR1 & TIM_CR1_ARPE) == 0) TIMS->RELOAD = TIMx->ARR; // PRELOAD disable - update ARR every itteration + if (TIMS->tx_cnt > TIMS->RELOAD || TIMS->tx_cnt < 0) // OVERFLOW { - TIMS->RELOAD = TIMx->ARR; // RELOAD ARR - if (TIMS->tx_cnt > TIMx->ARR) // reset COUNTER - TIMS->tx_cnt = 0; - else if (TIMS->tx_cnt < 0) - TIMS->tx_cnt = TIMx->ARR; + switch (TIMx->CR1 & TIM_CR1_CMS) + { + case TIM_COUNTERMODE_CENTERALIGNED1: + case TIM_COUNTERMODE_CENTERALIGNED2: + case TIM_COUNTERMODE_CENTERALIGNED3: + TIMx->CR1 ^= TIM_CR1_DIR; - if(TIMx->DIER & TIM_DIER_UIE) // if update interrupt enable - call_IRQHandller(TIMx); // call HANDLER + if (TIMx->DIER & TIM_DIER_UIE) // if update interrupt enable + call_IRQHandller(TIMx); // call HANDLER + break; + + // default counting + default: + TIMS->RELOAD = TIMx->ARR; // RELOAD ARR + + if (TIMS->tx_cnt > TIMx->ARR) // reset COUNTER + TIMS->tx_cnt = 0; + else if (TIMS->tx_cnt < 0) + TIMS->tx_cnt = TIMx->ARR; + + if (TIMx->DIER & TIM_DIER_UIE) // if update interrupt enable + call_IRQHandller(TIMx); // call HANDLER + break; + } } } } diff --git a/MCU_Wrapper/mcu_wrapper.c b/MCU_Wrapper/mcu_wrapper.c index ef96489..b64b403 100644 --- a/MCU_Wrapper/mcu_wrapper.c +++ b/MCU_Wrapper/mcu_wrapper.c @@ -21,6 +21,7 @@ SIM__MCUHandleTypeDef hmcu; ///< Хендл для управления //-------------------------------------------------------------// //-----------------CONTROLLER SIMULATE FUNCTIONS---------------// /* THREAD FOR MCU APP */ +#ifdef RUN_APP_MAIN_FUNC_THREAD /** * @brief Главная функция приложения МК. * @details Функция с которой начинается выполнение кода МК. Выход из данной функции происходит только в конце симуляции @ref mdlTerminate @@ -35,6 +36,7 @@ unsigned __stdcall MCU_App_Thread(void) { return 0; // end thread // note: this return will reached only at the end of simulation, when all whiles will be skipped due to @ref sim_while } +#endif //RUN_APP_MAIN_FUNC_THREAD /* SIMULATE MCU FOR ONE SIMULATION STEP */ /** * @brief Симуляция МК на один такт симуляции. @@ -56,11 +58,15 @@ void MCU_Step_Simulation(SimStruct* S, time_T time) MCU_Periph_Simulation(); // simulate peripheral + extern void Periph_reInit(void); + Periph_reInit(); +#ifdef RUN_APP_MAIN_FUNC_THREAD ResumeThread(hmcu.hMCUThread); for (int i = DEKSTOP_CYCLES_FOR_MCU_APP; i > 0; i--) { } SuspendThread(hmcu.hMCUThread); +#endif //RUN_APP_MAIN_FUNC_THREAD MCU_writeOutputs(S); // запись портов (по факту запись в буфер. запись в порты в mdlOutputs) } @@ -142,13 +148,15 @@ void SIM_writeOutputs(SimStruct* S) */ void SIM_Initialize_Simulation(void) { +#ifdef RUN_APP_MAIN_FUNC_THREAD // инициализация потока, который будет выполнять код МК hmcu.hMCUThread = (HANDLE)CreateThread(NULL, 0, MCU_App_Thread, 0, CREATE_SUSPENDED, &hmcu.idMCUThread); - +#endif //RUN_APP_MAIN_FUNC_THREAD /* user initialization */ Initialize_Periph_Sim(); - + extern int main_init(void); + main_init(); /* wrapper initialization */ hmcu.SystemClock_step = MCU_CORE_CLOCK * hmcu.SIM_Sample_Time; // set system clock step } diff --git a/MCU_Wrapper/mcu_wrapper_conf.h b/MCU_Wrapper/mcu_wrapper_conf.h index 2a47ed8..f3d066e 100644 --- a/MCU_Wrapper/mcu_wrapper_conf.h +++ b/MCU_Wrapper/mcu_wrapper_conf.h @@ -21,8 +21,8 @@ - для S-Function "simstruc.h" - для потоков **************************************************************************/ -#ifndef _CONTROLLER_H_ -#define _CONTROLLER_H_ +#ifndef _WRAPPER_CONF_H_ +#define _WRAPPER_CONF_H_ // Includes #include "stm32f4xx_matlab_conf.h" // For stm simulate functions @@ -44,12 +44,13 @@ */ // Parametrs of MCU simulator -#define CREATE_SUSPENDED 0x00000004 ///< define from WinBase.h. We dont wanna include "Windows.h" or smth like this, because of HAL there are a lot of redefine errors. - +//#define RUN_APP_MAIN_FUNC_THREAD ///< Enable using thread for MCU main() func #define DEKSTOP_CYCLES_FOR_MCU_APP 0xFF ///< number of for() cycles after which MCU thread would be suspended +#define MCU_CORE_CLOCK 150000000 ///< MCU clock rate for simulation + + #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 (9) ///< width of input ports @@ -69,6 +70,14 @@ * @{ */ +// Fixed parameters(?) of S_Function +#define NPARAMS 1 ///< number of input parametrs (only Ts) +#define DISC_STATES_WIDTH OUT_PORT_WIDTH*OUT_PORT_NUMB ///< width of discrete states array (outbup buffer) +/** + * @brief Define for creating thread in suspended state. + * @details Define from WinBase.h. We dont wanna include "Windows.h" or smth like this, because of HAL there are a lot of redefine errors. + */ +#define CREATE_SUSPENDED 0x00000004 typedef void* HANDLE; ///< MCU handle typedef /** @@ -92,6 +101,7 @@ extern SIM__MCUHandleTypeDef hmcu; // extern для видимос //-------------------------------------------------------------// //------------------ SIMULINK WHILE DEFINES -----------------// +#ifdef RUN_APP_MAIN_FUNC_THREAD /* DEFINE TO WHILE WITH SIMULINK WHILE */ /** * @brief Redefine C while statement with sim_while() macro. @@ -99,7 +109,7 @@ extern SIM__MCUHandleTypeDef hmcu; // extern для видимос * @details Это while который будет использоваться в симулинке @ref sim_while для подробностей. */ #define while(_expression_) sim_while(_expression_) - +#endif /* SIMULINK WHILE */ /** * @brief While statement for emulate MCU code in Simulink. @@ -152,7 +162,7 @@ void SIM_writeOutput(SimStruct* S); /** MCU_WRAPPER * @} */ -#endif // _CONTROLLER_H_ +#endif // _WRAPPER_CONF_H_ //-------------------------------------------------------------// diff --git a/mcu_test_r2021a.slx b/mcu_test_r2021a.slx index 4c2ee9b..8f4752b 100644 Binary files a/mcu_test_r2021a.slx and b/mcu_test_r2021a.slx differ