Добавлена модель МК матлаб с компилятором MinGW

Но по какой-то приниче запуск модели лочит MCU.mexw64 и его нельзя удалить. Но при этом можно переименовать... непонятно крч
This commit is contained in:
2025-11-08 18:18:17 +03:00
parent 67c8d0e039
commit afc4a114f6
412 changed files with 863427 additions and 120 deletions

237
MATLAB/MCU_Wrapper/MCU.c Normal file
View File

@@ -0,0 +1,237 @@
/**
**************************************************************************
* @file MCU.c
* @brief Исходный код S-Function.
**************************************************************************
@details
Данный файл содержит функции S-Function, который вызывает MATLAB.
**************************************************************************
@note
Описание функций по большей части сгенерировано MATLAB'ом, поэтому на английском
**************************************************************************/
/**
* @addtogroup WRAPPER_SFUNC S-Function funtions
* @ingroup MCU_WRAPPER
* @brief Дефайны и функции блока S-Function
* @details Здесь собраны функции, с которыми непосредственно работает S-Function
* @note Описание функций по большей части сгенерировано MATLAB'ом, поэтому на английском
* @{
*/
#define S_FUNCTION_NAME MCU
#define S_FUNCTION_LEVEL 2
#include "mcu_wrapper_conf.h"
#include <windows.h>
#include <process.h>
#include "mex.h"
#define MDL_UPDATE ///< для подключения mdlUpdate()
/**
* @brief Update S-Function at every step of simulation
* @param S - pointer to S-Function (library struct from "simstruc.h")
* @details Abstract:
* This function is called once for every major integration time step.
* Discrete states are typically updated here, but this function is useful
* for performing any tasks that should only take place once per
* integration step.
*/
static void mdlUpdate(SimStruct* S, int_T tid)
{
// get time of simulation
time_T TIME = ssGetT(S);
//---------------SIMULATE MCU---------------
MCU_Step_Simulation(S, TIME); // SIMULATE MCU
//------------------------------------------
}//end mdlUpdate
/**
* @brief Writting outputs of S-Function
* @param S - pointer to S-Function (library struct from "simstruc.h")
* @details Abstract:
* In this function, you compute the outputs of your S-function
* block. Generally outputs are placed in the output vector(s),
* ssGetOutputPortSignal.
*/
static void mdlOutputs(SimStruct* S, int_T tid)
{
SIM_writeOutputs(S);
}//end mdlOutputs
#define MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
static void mdlCheckParameters(SimStruct* S)
{
int i;
// Проверяем и принимаем параметры и разрешаем или запрещаем их менять
// в процессе моделирования
for (i = 0; i < 1; i++)
{
// Input parameter must be scalar or vector of type double
if (!mxIsDouble(ssGetSFcnParam(S, i)) || mxIsComplex(ssGetSFcnParam(S, i)) ||
mxIsEmpty(ssGetSFcnParam(S, i)))
{
ssSetErrorStatus(S, "Input parameter must be of type double");
return;
}
// Параметр м.б. только скаляром, вектором или матрицей
if (mxGetNumberOfDimensions(ssGetSFcnParam(S, i)) > 2)
{
ssSetErrorStatus(S, "Параметр м.б. только скаляром, вектором или матрицей");
return;
}
// sim_dt = mxGetPr(ssGetSFcnParam(S,0))[0];
// Parameter not tunable
// ssSetSFcnParamTunable(S, i, SS_PRM_NOT_TUNABLE);
// Parameter tunable (we must create a corresponding run-time parameter)
ssSetSFcnParamTunable(S, i, SS_PRM_TUNABLE);
// Parameter tunable only during simulation
// ssSetSFcnParamTunable(S, i, SS_PRM_SIM_ONLY_TUNABLE);
}//for (i=0; i<NPARAMS; i++)
}//end mdlCheckParameters
#endif //MDL_CHECK_PARAMETERS
static void mdlInitializeSizes(SimStruct* S)
{
ssSetNumSFcnParams(S, 1);
// Кол-во ожидаемых и фактических параметров должно совпадать
if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S))
{
// Проверяем и принимаем параметры
mdlCheckParameters(S);
}
else
{
return;// Parameter mismatch will be reported by Simulink
}
// set up discrete states
ssSetNumContStates(S, 0); // number of continuous states
ssSetNumDiscStates(S, DISC_STATES_WIDTH); // number of discrete states
// set up input port
if (!ssSetNumInputPorts(S, IN_PORT_NUMB)) return;
for (int i = 0; i < IN_PORT_NUMB; i++)
{
ssSetInputPortWidth(S, i, inLengths[i]);
ssSetInputPortDirectFeedThrough(S, i, 0);
ssSetInputPortRequiredContiguous(S, i, 1);
}
// set up output port
if (!ssSetNumOutputPorts(S, OUT_PORT_NUMB)) return;
for (int i = 0; i < OUT_PORT_NUMB; i++)
ssSetOutputPortWidth(S, i, outLengths[i]);
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 5); // number of real work vector elements
ssSetNumIWork(S, 5); // number of integer work vector elements
ssSetNumPWork(S, 0); // number of pointer work vector elements
ssSetNumModes(S, 0); // number of mode work vector elements
ssSetNumNonsampledZCs(S, 0); // number of nonsampled zero crossings
ssSetRuntimeThreadSafetyCompliance(S, RUNTIME_THREAD_SAFETY_COMPLIANCE_TRUE);
/* Take care when specifying exception free code - see sfuntmpl.doc */
ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
}
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/**
* @brief Initialize S-Function at start of simulation
* @param S - pointer to S-Function (library struct from "simstruc.h")
* @details Abstract:
* This function is called once at start of model execution. If you
* have states that should be initialized once, this is the place
* to do it.
*/
static void mdlStart(SimStruct* S)
{
SIM_Initialize_Simulation(S);
}
#endif // MDL_START
/**
* @brief Initialize Sample Time of Simulation
* @param S - pointer to S-Function (library struct from "simstruc.h")
* @details Abstract:
* This function is used to specify the sample time(s) for your
* S-function. You must register the same number of sample times as
* specified in ssSetNumSampleTimes.
*/
static void mdlInitializeSampleTimes(SimStruct* S)
{
// Шаг дискретизации
hmcu.sSimSampleTime = mxGetPr(ssGetSFcnParam(S, NPARAMS - 1))[0];
// Register one pair for each sample time
ssSetSampleTime(S, 0, hmcu.sSimSampleTime);
ssSetOffsetTime(S, 0, 0.0);
}
/**
* @brief Terminate S-Function at the end of simulation
* @param S - pointer to S-Function (library struct from "simstruc.h")
* @details Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was
* allocated in mdlStart, this is the place to free it.
*/
static void mdlTerminate(SimStruct* S)
{
hmcu.fMCU_Stop = 1;
#ifdef RUN_APP_MAIN_FUNC_THREAD
if (hmcu.hMCUThread != NULL) {
ResumeThread(hmcu.hMCUThread);
//// Простая версия - ждем завершения потока
// Ждем до 5 секунд
for (int i = 0; i < 50; i++) {
// Проверяем, завершился ли поток (упрощенная проверка)
DWORD exitCode;
if (GetExitCodeThread(hmcu.hMCUThread, &exitCode) && exitCode != STILL_ACTIVE) {
break; // поток завершился
}
Sleep(100); // ждем 100ms
}
//// Даем потоку шанс завершиться нормально
//DWORD waitResult = WaitForSingleObject(hmcu.hMCUThread, 3000);
//if (waitResult == WAIT_TIMEOUT) {
// // Поток не ответил - завершаем принудительно
// TerminateThread(hmcu.hMCUThread, 0);
//}
CloseHandle(hmcu.hMCUThread);
hmcu.hMCUThread = NULL;
}
#endif
SIM_deInitialize_Simulation(S);
mexUnlock();
}
/** WRAPPER_SFUNC
* @}
*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a
MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration
function */
#endif

View File

@@ -0,0 +1,188 @@
{
"wrapperPath": {
"Prompt": "",
"Type": "edit",
"Default": ".\\MCU_Wrapper"
},
"enableDebug": {
"Prompt": "Enable Debug",
"Type": "checkbox",
"Default": true
},
"mcuClk": {
"Prompt": "MCU Core Clock (MHz)",
"Type": "edit",
"Default": "72*10^6",
"Def": "MCU_CORE_CLOCK"
},
"threadCycles": {
"Prompt": "Deckstop cycles per MCU simulation step",
"Type": "edit",
"Default": 255,
"Def": "DEKSTOP_CYCLES_FOR_MCU_APP"
},
"enableThreading": {
"Prompt": "Run MCU main() in a separate thread",
"Type": "checkbox",
"Default": true,
"Def": "RUN_APP_MAIN_FUNC_THREAD"
},
"enableDeinit": {
"Prompt": "Deinitialize model after simulation",
"Type": "checkbox",
"Default": true,
"Def": "DEINITIALIZE_AFTER_SIM"
},
"periphPath": {
"Prompt": "",
"Type": "edit",
"Default": ".\\MCU_STM32_Matlab\\stm32f4xx_matlab_conf.json"
},
"inNumb": {
"Prompt": "Number of Input Ports",
"Type": "spinbox",
"Default": 2
},
"in_port_1_name": {
"Prompt": "",
"Type": "edit",
"Default": "ADC"
},
"in_port_1_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 6
},
"in_port_2_name": {
"Prompt": "",
"Type": "edit",
"Default": "IN"
},
"in_port_2_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 16
},
"in_port_3_name": {
"Prompt": "",
"Type": "edit",
"Default": "IN"
},
"in_port_3_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 16
},
"in_port_4_name": {
"Prompt": "",
"Type": "edit",
"Default": "IN"
},
"in_port_4_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 16
},
"in_port_5_name": {
"Prompt": "",
"Type": "edit",
"Default": "IN"
},
"in_port_5_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 16
},
"outNumb": {
"Prompt": "Number of Output Ports",
"Type": "spinbox",
"Default": 2
},
"out_port_1_name": {
"Prompt": "",
"Type": "edit",
"Default": "THYR"
},
"out_port_1_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 6
},
"out_port_2_name": {
"Prompt": "",
"Type": "edit",
"Default": "OUT"
},
"out_port_2_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 16
},
"out_port_3_name": {
"Prompt": "",
"Type": "edit",
"Default": "OUT"
},
"out_port_3_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 16
},
"out_port_4_name": {
"Prompt": "",
"Type": "edit",
"Default": "OUT"
},
"out_port_4_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 16
},
"out_port_5_name": {
"Prompt": "",
"Type": "edit",
"Default": "OUT"
},
"out_port_5_width": {
"Prompt": "",
"Type": "spinbox",
"Default": 16
},
"appWrapperPath": {
"Prompt": "",
"Type": "edit",
"Default": ".\\app_wrapper"
},
"srcTable": {
"Prompt": "",
"Type": "customtable",
"Default": [
"..\\UPP\\Core\\Src\\main.c",
"..\\UPP\\Core\\Src\\stm32f4xx_hal_msp.c",
"..\\UPP\\Core\\Src\\adc.c",
"..\\UPP\\Core\\Src\\gpio.c",
"..\\UPP\\Core\\Src\\stm32f4xx_it.c",
"..\\UPP\\Core\\Src\\system_stm32f4xx.c",
"..\\UPP\\Core\\Src\\tim.c"
],
"Def": "Files"
},
"incTable": {
"Prompt": "",
"Type": "customtable",
"Default": [
"..\\UPP\\Core\\Inc",
"..\\UPP\\Core\\ZeroCross",
"..\\UPP\\Core\\UPP",
"..\\UPP\\Core\\Thyristors",
"..\\UPP\\Core\\Configs",
"..\\UPP\\AllLibs\\MyLibs\\MyLibs\\Inc"
],
"Def": "Paths"
},
"userDefs": {
"Prompt": "User Defines",
"Type": "textarea",
"Default": ""
}
}

View File

@@ -0,0 +1,281 @@
/**
**************************************************************************
* @file mcu_wrapper.c
* @brief Исходный код оболочки МК.
**************************************************************************
@details
Данный файл содержит функции для симуляции МК в Simulink (S-Function).
**************************************************************************/
#include "mcu_wrapper_conf.h"
#include "app_wrapper.h"
/**
* @addtogroup WRAPPER_CONF
* @{
*/
SIM__MCUHandleTypeDef hmcu; ///< Хендл для управления потоком программы МК
// INPUT/OUTPUTS AUTO-PARAMS START
/**
* @brief Таблица длин массивов IN
*/
const int inLengths[IN_PORT_NUMB] = {
ADC_PORT_1_WIDTH,
IN_PORT_2_WIDTH
};
/**
* @brief Таблица смещений в выходном массиве IN
*/
const int inOffsets[IN_PORT_NUMB] = {
OFFSET_IN_ARRAY_1,
OFFSET_IN_ARRAY_2
};
/**
* @brief Таблица длин массивов OUT
*/
const int outLengths[OUT_PORT_NUMB] = {
THYR_PORT_1_WIDTH,
OUT_PORT_2_WIDTH
};
/**
* @brief Таблица смещений в выходном массиве OUT
*/
const int outOffsets[OUT_PORT_NUMB] = {
OFFSET_OUT_ARRAY_1,
OFFSET_OUT_ARRAY_2
};
// INPUT/OUTPUTS AUTO-PARAMS END
/** MCU_WRAPPER
* @}
*/
//-------------------------------------------------------------//
//-----------------CONTROLLER SIMULATE FUNCTIONS---------------//
/* THREAD FOR MCU APP */
#ifdef RUN_APP_MAIN_FUNC_THREAD
/**
* @brief Главная функция приложения МК.
* @details Функция с которой начинается выполнение кода МК. Выход из данной функции происходит только в конце симуляции @ref mdlTerminate
*/
extern int main(void); // extern while from main.c
/**
* @brief Поток приложения МК.
* @details Поток, который запускает и выполняет код МК (@ref main).
*/
unsigned __stdcall MCU_App_Thread(void) {
main(); // run MCU code
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 Симуляция МК на один такт симуляции.
* @param S - указатель на структуру S-Function из "simstruc.h"
* @param time - текущее время симуляции.
* @details Запускает поток, который выполняет код МК и управляет ходом потока:
* Если прошел таймаут, поток прерывается, симулируется периферия
* и на следующем шаге поток возобнавляется.
*
* Вызывается из mdlUpdate()
*/
void MCU_Step_Simulation(SimStruct* S, time_T time)
{
hmcu.SystemClockDouble += hmcu.sSystemClock_step; // emulate core clock
hmcu.SystemClock = hmcu.SystemClockDouble;
hmcu.SimTime = time;
MCU_readInputs(S); // считывание портов
MCU_Periph_Simulation(S); // simulate peripheral
#ifdef RUN_APP_MAIN_FUNC_THREAD
ResumeThread(hmcu.hMCUThread);
for (int i = DEKSTOP_CYCLES_FOR_MCU_APP; i > 0; i--)
{
}
SuspendThread(hmcu.hMCUThread);
#else
app_step();
#endif //RUN_APP_MAIN_FUNC_THREAD
MCU_writeOutputs(S); // запись портов (по факту запись в буфер. запись в порты в mdlOutputs)
}
/* SIMULATE MCU PERIPHERAL */
/**
* @brief Симуляция периферии МК
* @details Пользовательский код, который симулирует работу периферии МК.
*/
void MCU_Periph_Simulation(SimStruct* S)
{
// PERIPH SIM START
uwTick = hmcu.SystemClock / (MCU_CORE_CLOCK / 1000);
Simulate_TIMs();
Simulate_GPIO_BSRR();
// PERIPH SIM END
}
/* READ INPUTS S-FUNCTION TO MCU REGS */
/**
* @brief Считывание входов S-Function в порты ввода-вывода.
* @param S - указатель на структуру S-Function из "simstruc.h"
* @details Пользовательский код, который записывает входы МК из входов S-Function.
*/
void MCU_readInputs(SimStruct* S)
{
SIM_readInputs(S);
/* Get S-Function descrete array (IO buffer) */
real_T* In_Buff = ssGetDiscStates(S);
app_readInputs(In_Buff);
}
/* WRITE OUTPUTS BUFFER S-FUNCTION FROM MCU REGS*/
/**
* @brief Запись портов ввода-вывода в буфер выхода S-Function
* @param S - указатель на структуру S-Function из "simstruc.h"
* @details Пользовательский код, который записывает буфер выходов S-Function из портов ввода-вывода.
*/
void MCU_writeOutputs(SimStruct* S)
{
/* Get S-Function descrete array (IO buffer) */
real_T* Out_Buff = ssGetDiscStates(S);
app_writeOutputBuffer(Out_Buff);
}
//-----------------CONTROLLER SIMULATE FUNCTIONS---------------//
//-------------------------------------------------------------//
//-------------------------------------------------------------//
//----------------------SIMULINK FUNCTIONS---------------------//
/* MCU WRAPPER DEINITIALIZATION */
/**
* @brief Инициализация симуляции МК.
* @details Пользовательский код, который создает поток для приложения МК
и настраивает симулятор МК для симуляции.
*/
void SIM_Initialize_Simulation(SimStruct* S)
{
#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 */
app_init();
// PERIPH INIT START
Initialize_Periph_Sim();
// PERIPH INIT END
/* clock step initialization */
hmcu.sSystemClock_step = MCU_CORE_CLOCK * hmcu.sSimSampleTime; // set system clock step
hmcu.fInitDone = 1;
}
/* MCU WRAPPER DEINITIALIZATION */
/**
* @brief Деинициализация симуляции МК.
* @details Пользовательский код, который будет очищать все структуры после окончания симуляции.
*/
void SIM_deInitialize_Simulation(SimStruct* S)
{
#ifdef DEINITIALIZE_AFTER_SIM
// deinitialize app
app_deinit();
// PERIPH DEINIT START
deInitialize_Periph_Sim();
// PERIPH DEINIT END
#endif// DEINITIALIZE_AFTER_SIM
}
/* WORK WITH IN/OUT BUFFER OF S-BLOCK */
/**
* @brief Функция для записи переменной в буфер выходов в определенный массив
* @param xD - указатель на буфер состояний
* @param value - значение для записи
* @param array_index - индекс выходного массива
* @param value_index - индекс внутри массива
*/
void __WriteOutputArray(real_T* xD, float value, int array_index, int value_index)
{
if (array_index >= OUT_PORT_NUMB)
return;
if (value_index >= outLengths[array_index])
return;
int global_index = XD_OUTPUT_START + outOffsets[array_index] + value_index;
xD[global_index] = value;
}
/**
* @brief Функция для чтения значения из буфера входов из определенного массива
* @param xD - указатель на буфер состояний
* @param array_index - индекс входного массива
* @param value_index - индекс внутри массива
* @return - считанное значение или 0.0 при выходе за границы
*/
float __ReadInputArray(const real_T* xD, int array_index, int value_index)
{
if (array_index >= IN_PORT_NUMB)
return 0.0f;
if (value_index >= inLengths[array_index])
return 0.0f;
int global_index = XD_INPUT_START + inOffsets[array_index] + value_index;
return xD[global_index];
}
/**
* @brief Формирование выходов S-Function.
* @param S - указатель на структуру S-Function из "simstruc.h"
* @details Пользовательский код, который записывает выходы S-Function из буфера дискретных состояний.
*/
void SIM_writeOutputs(SimStruct* S)
{
real_T* Output = ssGetOutputPortRealSignal(S,0);
real_T* Out_Buff = ssGetDiscStates(S);
int global_index;
//-------------WRITTING OUTPUT--------------
for (int arr_ind = 0; arr_ind < OUT_PORT_NUMB; arr_ind++)
{
Output = ssGetOutputPortRealSignal(S, arr_ind);
for (int val_ind = 0; val_ind < outLengths[arr_ind]; val_ind++)
{
global_index = XD_OUTPUT_START + outOffsets[arr_ind] + val_ind;
Output[val_ind] = Out_Buff[global_index];
Out_Buff[global_index] = 0;
}
}
//------------------------------------------
}
/**
* @brief Формирование входов S-Function.
* @param S - указатель на структуру S-Function из "simstruc.h"
* @details Пользовательский код, который считывает входы S-Function в буфер дискретных состояний.
*/
void SIM_readInputs(SimStruct* S)
{
real_T* Input = ssGetInputPortRealSignal(S, 0);
real_T* In_Buff = ssGetDiscStates(S);
int global_index;
//-------------READING INPUTS---------------
for (int arr_ind = 0; arr_ind < IN_PORT_NUMB; arr_ind++)
{
Input = ssGetInputPortRealSignal(S, arr_ind);
for (int val_ind = 0; val_ind < inLengths[arr_ind]; val_ind++)
{
global_index = XD_INPUT_START + inOffsets[arr_ind] + val_ind;
In_Buff[global_index] = Input[val_ind];
}
}
//------------------------------------------
}
//-------------------------------------------------------------//

View File

@@ -0,0 +1,237 @@
/**
**************************************************************************
* @dir ../MCU_Wrapper
* @brief <b> Папка с исходным кодом оболочки МК. </b>
* @details
В этой папке содержаться оболочка(англ. wrapper) для запуска и контроля
эмуляции микроконтроллеров в MATLAB (любого МК, не только STM).
Оболочка представляет собой S-Function - блок в Simulink, который работает
по скомпилированому коду. Компиляция происходит с помощью MSVC-компилятора.
**************************************************************************/
/**
**************************************************************************
* @file mcu_wrapper_conf.h
* @brief Заголовочный файл для оболочки МК.
**************************************************************************
@details
Главный заголовочный файл для матлаба. Включает дейфайны для S-Function,
объявляет базовые функции для симуляции МК и подключает базовые библиотеки:
- для симуляции "stm32fxxx_matlab_conf.h"
- для S-Function "simstruc.h"
- для потоков <process.h>
**************************************************************************/
#ifndef _WRAPPER_CONF_H_
#define _WRAPPER_CONF_H_
// Includes
#include "simstruc.h" // For S-Function variables
#include <process.h> // For threads
/**
* @defgroup MCU_WRAPPER MCU Wrapper
* @brief Всякое для оболочки МК
*/
/**
* @addtogroup WRAPPER_CONF Wrapper Configuration
* @ingroup MCU_WRAPPER
* @brief Параметры конфигурации для оболочки МК
* @details Здесь дефайнами задается параметры оболочки, которые определяют как она будет работать
* @{
*/
// Parametrs of MCU simulator
//#define RUN_APP_MAIN_FUNC_THREAD ///< Enable using thread for MCU main() func
//#define DEKSTOP_CYCLES_FOR_MCU_APP 0xFFFF ///< number of for() cycles after which MCU thread would be suspended
//#define MCU_CORE_CLOCK 150000000 ///< MCU clock rate for simulation
// Parameters of S_Function
// INPUT/OUTPUTS PARAMS START
#define IN_PORT_NUMB 2
#define ADC_PORT_1_WIDTH 6
#define IN_PORT_2_WIDTH 16
#define OUT_PORT_NUMB 2
#define THYR_PORT_1_WIDTH 6
#define OUT_PORT_2_WIDTH 16
// INPUT/OUTPUTS PARAMS END
/** WRAPPER_CONF
* @}
*/
/**
* @addtogroup MCU_WRAPPER
* @{
*/
/** @brief Записывает значение в выходной массив блока S-Function
* @param _var_ Значение, которое необходимо записать (будет преобразовано в float)
* @param _arr_ind_ Индекс выходного порта
* @param _val_ind_ Индекс элемента в выходном массиве
*/
#define WriteOutputArray(_var_, _arr_ind_, _val_ind_) __WriteOutputArray(Buffer, (float)_var_, _arr_ind_, _val_ind_)
/** @brief Считывает значение из входного массива блока S-Function
* @param _var_ Значение, которое необходимо записать (будет преобразовано в float)
* @param _arr_ind_ Индекс входного порта
* @param _val_ind_ Индекс элемента во входном массиве
*/
#define ReadInputArray(_arr_ind_, _val_ind_) __ReadInputArray(Buffer, _arr_ind_, _val_ind_)
// INPUT/OUTPUTS AUTO-PARAMS START
/// === Полный размер буфера ===
#define TOTAL_IN_SIZE (ADC_PORT_1_WIDTH + IN_PORT_2_WIDTH)
/// === Смещения массивов (внутри общего буфера) ===
#define OFFSET_IN_ARRAY_1 0
#define OFFSET_IN_ARRAY_2 (OFFSET_IN_ARRAY_1 + ADC_PORT_1_WIDTH)
/// === Полный размер буфера ===
#define TOTAL_OUT_SIZE (THYR_PORT_1_WIDTH + OUT_PORT_2_WIDTH)
/// === Смещения массивов (внутри общего буфера) ===
#define OFFSET_OUT_ARRAY_1 0
#define OFFSET_OUT_ARRAY_2 (OFFSET_OUT_ARRAY_1 + THYR_PORT_1_WIDTH)
// INPUT/OUTPUTS AUTO-PARAMS END
extern const int outLengths[OUT_PORT_NUMB];
extern const int outOffsets[OUT_PORT_NUMB];
extern const int inLengths[IN_PORT_NUMB];
extern const int inOffsets[IN_PORT_NUMB];
#define TOTAL_XD_SIZE (TOTAL_IN_SIZE + TOTAL_OUT_SIZE)
#define XD_INPUT_START 0
#define XD_OUTPUT_START (XD_INPUT_START + TOTAL_IN_SIZE)
// Fixed parameters(?) of S_Function
#define NPARAMS 1 ///< number of input parametrs (only Ts)
#define DISC_STATES_WIDTH TOTAL_XD_SIZE ///< 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
/**
* @brief MCU handle Structure definition.
* @note Prefixes: h - handle, s - settings, f - flag
*/
typedef struct {
// MCU Thread
HANDLE hMCUThread; ///< Хендл для потока МК
int idMCUThread; ///< id потока МК (unused)
// Flags
unsigned fMCU_Stop : 1; ///< флаг для выхода из потока программы МК
unsigned fInitDone : 1; ///< флаг для выхода из потока программы МК
double SimTime; ///< Текущее время симуляции
long SystemClock; ///< Счетчик тактов для симуляции системных тиков (в целочисленном формате)
double SystemClockDouble; ///< Счетчик в формате double для точной симуляции системных тиков С промежуточными значений
double sSystemClock_step; ///< Шаг тиков для их симуляции, в формате double
double sSimSampleTime; ///< Период дискретизации симуляции
}SIM__MCUHandleTypeDef;
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.
* @param _expression_ - expression for while.
* @details Это while который будет использоваться в симулинке @ref sim_while для подробностей.
*/
#define while(_expression_) sim_while(_expression_)
#endif
/* SIMULINK WHILE */
/**
* @brief While statement for emulate MCU code in Simulink.
* @param _expression_ - expression for while.
* @details Данный while необходим, чтобы в конце симуляции, завершить поток МК:
* При выставлении флага окончания симуляции, все while будут пропускаться
* и поток сможет дойти до конца функции main и завершить себя.
*/
#define sim_while(_expression_) while((_expression_)&&(hmcu.fMCU_Stop == 0))
/* DEFAULT WHILE */
/**
* @brief Default/Native C while statement.
* @param _expression_ - expression for while.
* @details Данный while - аналог обычного while, без дополнительного функционала.
*/
#define native_while(_expression_) for(; (_expression_); )
/***************************************************************/
//------------------ SIMULINK WHILE DEFINES -----------------//
//-------------------------------------------------------------//
//-------------------------------------------------------------//
//---------------- SIMULATE FUNCTIONS PROTOTYPES -------------//
/* Step simulation */
void MCU_Step_Simulation(SimStruct *S, time_T time);
/* MCU peripheral simulation */
void MCU_Periph_Simulation(SimStruct* S);
/* Initialize MCU simulation */
void SIM_Initialize_Simulation(SimStruct* S);
/* Deinitialize MCU simulation */
void SIM_deInitialize_Simulation(SimStruct* S);
/* Read inputs S-function */
void MCU_readInputs(SimStruct* S);
/* Write pre-outputs S-function (out_buff states) */
void MCU_writeOutputs(SimStruct* S);
/* Write outputs of block of S-Function*/
void SIM_writeOutputs(SimStruct* S);
/* Write inputs of block of S-Function*/
void SIM_readInputs(SimStruct* S);
/* Set output of block of S-Function*/
void __WriteOutputArray(real_T* xD, float value, int array_index, int value_index);
/* Get input of block of S-Function*/
float __ReadInputArray(const real_T* xD, int array_index, int value_index);
//---------------- SIMULATE FUNCTIONS PROTOTYPES -------------//
//-------------------------------------------------------------//
/** MCU_WRAPPER
* @}
*/
#endif // _WRAPPER_CONF_H_
//-------------------------------------------------------------//
//---------------------BAT FILE DESCRIBTION--------------------//
/**
* @file run_mex.bat
* @brief Батник для компиляции оболочки МК.
* @details
* Вызывается в матлабе из allmex.m.
*
* Исходный код батника:
* @include run_mex.bat
*/