153 lines
4.7 KiB
Matlab
153 lines
4.7 KiB
Matlab
% Компилирует S-function
|
||
clear, clc
|
||
|
||
Ts = 0.00001;
|
||
|
||
% Флаг режима отладки
|
||
definesArg = buildDefinesString();
|
||
|
||
maskNames = get_param(gcbh, 'MaskNames');
|
||
isDebug = find(strcmp(maskNames, 'enableDebug'));
|
||
if strcmpi(isDebug, 'on')
|
||
modeArg = "release";
|
||
else
|
||
modeArg = "debug";
|
||
end
|
||
|
||
[includesArg, codeArg] = make_mex_arguments('incTable', 'srcTable');
|
||
|
||
|
||
% Вызов батника с двумя параметрами: includes и code
|
||
cmd = sprintf('.\\MCU_Wrapper\\run_mex.bat "%s" "%s" "%s" %s', includesArg, codeArg, definesArg, modeArg);
|
||
|
||
status = system(cmd);
|
||
|
||
if status == 0
|
||
beep
|
||
else
|
||
error('Error during compilation');
|
||
end
|
||
|
||
|
||
function [includesArg, codeArg] = make_mex_arguments(incTableName, srcTableame)
|
||
%MAKE_MEX_ARGUMENTS Формирует строки аргументов для вызова mex-компиляции через батник
|
||
%
|
||
% [includesArg, codeArg] = make_mex_arguments(includesCell, codeCell)
|
||
%
|
||
% Вход:
|
||
% includesCell — ячейковый массив путей к директориям include
|
||
% codeCell — ячейковый массив исходных файлов
|
||
%
|
||
% Выход:
|
||
% includesArg — строка для передачи в батник, например: "-I"inc1" -I"inc2""
|
||
% codeArg — строка с исходниками, например: ""src1.c" "src2.cpp""
|
||
|
||
|
||
% Здесь пример получения из маски текущего блока (замени по своему)
|
||
blockHandle = gcbh; % или замени на нужный блок
|
||
|
||
includesCell = parseCellString(get_param(blockHandle, incTableName));
|
||
codeCell = parseCellString(get_param(blockHandle, srcTableame));
|
||
|
||
% Оборачиваем пути в кавычки и добавляем -I
|
||
includesStr = strjoin(cellfun(@(f) ['-I"' f '"'], includesCell, 'UniformOutput', false), ' ');
|
||
|
||
% Оборачиваем имена файлов в кавычки
|
||
codeStr = strjoin(cellfun(@(f) ['"' f '"'], codeCell, 'UniformOutput', false), ' ');
|
||
|
||
% Удаляем символ переноса строки и пробел в конце, если вдруг попал
|
||
codeStr = strtrim(codeStr);
|
||
includesStr = strtrim(includesStr);
|
||
|
||
% Оборачиваем всю строку в кавычки, чтобы батник корректно понял
|
||
% includesArg = ['"' includesStr '"'];
|
||
% codeArg = ['"' codeStr '"'];
|
||
includesArg = includesStr;
|
||
codeArg = codeStr;
|
||
|
||
end
|
||
|
||
function definesArg = buildDefinesString()
|
||
blockHandle = gcbh;
|
||
|
||
% Получаем MaskValues и MaskNames
|
||
maskValues = get_param(blockHandle, 'MaskValues');
|
||
paramNames = get_param(blockHandle, 'MaskNames');
|
||
|
||
% Индексы параметров
|
||
idxEnable = find(strcmp(paramNames, 'enableThreading'));
|
||
idxCycles = find(strcmp(paramNames, 'threadCycles'));
|
||
idxClk = find(strcmp(paramNames, 'mcuClk'));
|
||
|
||
if any([isempty(idxEnable), isempty(idxCycles), isempty(idxClk)])
|
||
error('Один или несколько параметров не найдены в маске');
|
||
end
|
||
|
||
% Значения
|
||
enableVal = maskValues{idxEnable};
|
||
cyclesVal = maskValues{idxCycles};
|
||
clkMHz = str2double(maskValues{idxClk});
|
||
clkHz = round(clkMHz * 1e6);
|
||
|
||
% Формируем defines в формате: -D"NAME=VALUE"
|
||
if strcmpi(enableVal, 'on')
|
||
def1 = ['-D"RUN_APP_MAIN_FUNC_THREAD"'];
|
||
else
|
||
def1 = [''];
|
||
end
|
||
def2 = ['-D"DEKSTOP_CYCLES_FOR_MCU_APP__EQ__' cyclesVal '"'];
|
||
def3 = ['-D"MCU_CORE_CLOCK__EQ__' num2str(clkHz) '"'];
|
||
|
||
definesArg = strjoin({def1, def2, def3}, ' ');
|
||
% definesArg = ['"' definesStr ''];
|
||
% definesArg = definesStr;
|
||
end
|
||
|
||
|
||
|
||
function out = parseCellString(str)
|
||
str = strtrim(str);
|
||
if startsWith(str, '{') && endsWith(str, '}')
|
||
str = str(2:end-1);
|
||
end
|
||
|
||
parts = split(str, ';');
|
||
out = cell(numel(parts), 1);
|
||
for i = 1:numel(parts)
|
||
el = strtrim(parts{i});
|
||
if startsWith(el, '''') && endsWith(el, '''')
|
||
el = el(2:end-1);
|
||
end
|
||
out{i} = el;
|
||
end
|
||
|
||
if isempty(out) || (numel(out) == 1 && isempty(out{1}))
|
||
out = {};
|
||
end
|
||
end
|
||
|
||
function str = cellArrayToString(cellArray)
|
||
quoted = cellfun(@(s) ['''' s ''''], cellArray, 'UniformOutput', false);
|
||
str = ['{' strjoin(quoted, ';') '}'];
|
||
end
|
||
|
||
% % Компилирует S-function
|
||
% clear, clc,
|
||
%
|
||
% set = mex.getCompilerConfigurations('C', 'Selected');
|
||
%
|
||
% Ts = 0.00001;
|
||
%
|
||
% delete("*.mexw64")
|
||
% delete("*.mexw64.pdb")
|
||
% delete(".\MCU_Wrapper\Outputs\*.*");
|
||
%
|
||
% status=system('.\MCU_Wrapper\run_mex.bat debug');
|
||
%
|
||
% if status==0
|
||
% beep
|
||
% else
|
||
% error('Error!');
|
||
% end
|
||
%
|