Simulink代码生成(二十)——TSP开发之系统目标文件
一、系统目标文件解读
tlc文件通常分为两个等价,系统级和模块级。系统目标文件(system target file),例如ert.tlc就是常用的系统目标文件
系统目标文件非常特殊,%%也不都是注释,像这里告诉了Simulink环境,目标文件支持哪些功能,例如生成
- TMF: arduino_ec.tmf 生成tmf文件的模板
- MAKE: make_rtw 支持MAKE命令
- EXTMODE: ext_comm 支持外部模式
- 函数级或者非脚本类型的tlc,需要包含NULL_FILE
- assign CodeFormat = “Embedded-C” 嵌入式代码支持的生成代码类型 Embedded-C
- assign TargetType = “RT” 单片机都是实时的硬件,所以用RT(real time)
- assign AutoBuildProcedure = !GenerateSampleERTMain ,AutoBuildProcedure 是系统内部的函数,这里指的是不生成Main文件,在此处也可以不写,自己去configuration里面选。
%assign SuppressSetEventsForThisBaseRateFcn = (TargetOS == “VxWorksExample”)
%assign InlineSetEventsForThisBaseRateFcn = TLC_TRUE
%assign SuppressMultiTaskScheduler = TLC_TRUE
%assign UseRTOS = TLC_FALSE
这4行是关于VxWork的设置,单片机一般是裸机运行,不带操作系统,不需要修改
%include “commontargetlib.tlc”
%include “codegenentry.tlc”
tlc里面的include和c语言不同,C语言是告诉程序包含了哪些头文件,可以去头文件里面寻找函数等,tlc的include相当于inline, 内联的功能,相当于直接展开到文件中。
这里继承了erg.tlc的内容,只是在基础上做了一些修改,添加了一些自己的东西。
BEGIN_RTW_OPTIONS和END_RTW_OPTIONS中间这部分也是有用的,是对关于GUI的一些配置。选择arduio会比选择erg.tlc多一个arduio options的选项。
- rtwoptions(oIdx).type = ‘Category’; 表示这个会显示在code generation下面
- rtwoptions(oIdx).default = 4; % number of items under this category 表示这个选项下面有4个控件
- oIdx = oIdx + 1; 会显示成子控件
- if false % deactivated for the moment 最后面的if false表示不会显示这个RTOS相关的控件
根据字面意思大概能够知道每行代码的意思
%------------------------------------%
% Configure code generation settings %
%------------------------------------%
rtwgensettings.BuildDirSuffix = ‘_arduino’;
rtwgensettings.Version = ‘1’;
rtwgensettings.DerivedFrom = ‘ert.tlc’;
%% rtwgensettings.SelectCallback = ‘arduino_ec_select_callback_handler(hDlg, hSrc)’;
rtwgensettings.SelectCallback = ‘arduino_hyo_select_callback_handler(hDlg, hSrc)’;
- 这部分是用来进行模型的一些配置
- rtwgensettings.BuildDirSuffix = ‘_arduino’; 是用来配置生成代码的文件夹的名字,这里的文件夹名是-> 模型名+_arduino
- rtwgensettings.Version = ‘1’;
rtwgensettings.DerivedFrom = ‘ert.tlc’;
这两个是一起的,表示继承自 ert.tlc- rtwgensettings.SelectCallback = ‘arduino_hyo_select_callback_handler(hDlg, hSrc)’; 选择系统目标文件的时候,会触发一个回调函数,该回调函数用来对模型进行一些配置,例如定步长,仿真时间等,详细如下。
%% SYSTLC: Arduino EC Target TMF: arduino_ec.tmf MAKE: make_rtw \
%% EXTMODE: ext_comm
%% Copyright 2013-2014 The MathWorks, Inc.
%selectfile NULL_FILE
%assign CodeFormat = "Embedded-C"
%assign TargetType = "RT"
%assign Language = "C"
%assign AutoBuildProcedure = !GenerateSampleERTMain
%% The model_SetEventsForThisBaseRate function is not required for the
%% VxWorks environment, i.e., when using an operating system.
%assign SuppressSetEventsForThisBaseRateFcn = (TargetOS == "VxWorksExample")
%assign InlineSetEventsForThisBaseRateFcn = TLC_TRUE
%assign SuppressMultiTaskScheduler = TLC_TRUE
%assign UseRTOS = TLC_FALSE
%include "commontargetlib.tlc"
%include "codegenentry.tlc"
%% The contents between 'BEGIN_RTW_OPTIONS' and 'END_RTW_OPTIONS' in this file
%% are used to maintain backward compatibility to R13 and preR13 custom target
%% file only. If you want to use this file as a template to develop your
%% own system target file, you need to remove the 'CONFIGSET_TARGET_COMPONENT'
%% section at the end of this file.
%%
/%
BEGIN_RTW_OPTIONS
oIdx = 1;
rtwoptions(oIdx).prompt = 'Arduino options';
rtwoptions(oIdx).type = 'Category';
rtwoptions(oIdx).enable = 'on';
rtwoptions(oIdx).default = 4; % number of items under this category
% excluding this one.
rtwoptions(oIdx).popupstrings = '';
rtwoptions(oIdx).tlcvariable = '';
rtwoptions(oIdx).tooltip = '';
rtwoptions(oIdx).callback = '';
rtwoptions(oIdx).opencallback = '';
rtwoptions(oIdx).closecallback = '';
rtwoptions(oIdx).makevariable = '';
oIdx = oIdx + 1;
rtwoptions(oIdx).prompt = 'Download to board';
rtwoptions(oIdx).type = 'Checkbox';
rtwoptions(oIdx).default = 'off';
rtwoptions(oIdx).tlcvariable = 'DownloadToArduino';
rtwoptions(oIdx).makevariable = 'DOWNLOAD_TO_ARDUINO';
rtwoptions(oIdx).callback = '';
rtwoptions(oIdx).tooltip = sprintf(['Select this if you want to download to Arduino board']);
oIdx = oIdx + 1;
rtwoptions(oIdx).prompt = 'makefile Parallel Execution';
rtwoptions(oIdx).type = 'Checkbox';
rtwoptions(oIdx).default = 'off';
rtwoptions(oIdx).tlcvariable = 'ParallelExecution';
rtwoptions(oIdx).makevariable = '';
rtwoptions(oIdx).callback = '';
rtwoptions(oIdx).tooltip = sprintf(['Uses ''-j'' option for the makefile.']);
oIdx = oIdx + 1;
rtwoptions(oIdx).prompt = 'avr-gcc options';
rtwoptions(oIdx).type = 'Edit';
rtwoptions(oIdx).default = '';
rtwoptions(oIdx).tlcvariable = 'DialogOptions';
rtwoptions(oIdx).makevariable = 'DIALOG_OPTIONS';
rtwoptions(oIdx).callback = '';
rtwoptions(oIdx).tooltip = sprintf(['Options passed directly to avr-gcc compiler\nExample: -O2 -flto\nNote: Link Time Optimization (-flto) is available first\nin gcc 4.7 series and requires to use alternative compiler.']);
oIdx = oIdx + 1;
rtwoptions(oIdx).prompt = 'Alternative gcc compiler';
rtwoptions(oIdx).type = 'Edit';
rtwoptions(oIdx).default = '';
rtwoptions(oIdx).tlcvariable = 'AlternativeGCC';
rtwoptions(oIdx).makevariable = 'ALTERNATIVE_GCC';
rtwoptions(oIdx).callback = '';
rtwoptions(oIdx).tooltip = sprintf(['Newer avr-gcc compiler as one comming with Arduino software\nUse it like: c:\\avr-gcc-4.8-mingw32\\bin']);
if false % deactivated for the moment
oIdx = oIdx + 1;
rtwoptions(oIdx).prompt = 'Use RTOS';
rtwoptions(oIdx).type = 'Checkbox';
rtwoptions(oIdx).default = 'off';
rtwoptions(oIdx).tlcvariable = 'UseRTOS';
rtwoptions(oIdx).makevariable = 'USERTOS';
rtwoptions(oIdx).callback = '';
rtwoptions(oIdx).tooltip = sprintf(['Use RTOS operating system instead of bare board system.']);
end
%------------------------------------%
% Configure code generation settings %
%------------------------------------%
rtwgensettings.BuildDirSuffix = '_arduino';
rtwgensettings.Version = '1';
rtwgensettings.DerivedFrom = 'ert.tlc';
%% rtwgensettings.SelectCallback = 'arduino_ec_select_callback_handler(hDlg, hSrc)';
rtwgensettings.SelectCallback = 'arduino_hyo_select_callback_handler(hDlg, hSrc)';
END_RTW_OPTIONS
%/
二、Configuration的设置
matlab对于Configuration的设置如下
- hDlg, hSrc两个参数是必须的,hDlg指的是Configuration这个GUI的句柄,hSrc指的是对应的模型的句柄。
function arduino_ec_select_callback_handler(hDlg, hSrc)
%ARDUINO_EC_SELECT_CALLBACK_HANDLER callback handler for Arduino target
% Copyright 2009-2014 The MathWorks, Inc.
% Modified by Hyo @20150810
% The target is model reference compliant
slConfigUISetVal(hDlg, hSrc, 'ModelReferenceCompliant', 'on');
slConfigUISetEnabled(hDlg, hSrc, 'ModelReferenceCompliant', false);
% Hardware being used is the production hardware
slConfigUISetVal(hDlg, hSrc, 'ProdEqTarget', 'on');
% Setup C++ as default language
slConfigUISetVal(hDlg, hSrc, 'TargetLang', 'C++');
% Setup the hardware configuration
slConfigUISetVal(hDlg, hSrc, 'ProdHWDeviceType', 'Atmel->AVR');
% Set the TargetLibSuffix
slConfigUISetVal(hDlg, hSrc, 'TargetLibSuffix', '.a');
% For real-time builds, we must generate ert_main.c
slConfigUISetVal(hDlg, hSrc, 'ERTCustomFileTemplate', 'arduino_ec_file_process.tlc');
%slConfigUISetVal(hDlg, hSrc, 'ConcurrentExecutionCompliant', 'on');
% Hyo customization section to set correct configration automatically.
slConfigUISetVal(hDlg, hSrc, 'SolverType', 'Fixed-step');
slConfigUISetEnabled(hDlg, hSrc, 'SolverType', 'off');
slConfigUISetVal(hDlg, hSrc, 'Solver', 'FixedStepDiscrete');
slConfigUISetVal(hDlg, hSrc, 'FixedStep', '0.1');
slConfigUISetVal(hDlg, hSrc, 'InlineParams', 'on');
slConfigUISetVal(hDlg, hSrc, 'GenerateReport', 'on');
slConfigUISetVal(hDlg, hSrc, 'LaunchReport', 'on');
slConfigUISetVal(hDlg, hSrc, 'GenCodeOnly', 'on');
slConfigUISetVal(hDlg, hSrc, 'GenerateMakefile', 'off');
slConfigUISetVal(hDlg, hSrc, 'DownloadToArduino', 'off');
end
采用循环进行配置会更方便更简洁,修改后的配置
function arduino_hyo_select_callback_handler(hDlg, hSrc)
% Modified by Hyo @20150810
configs = {'ModelReferenceCompliant', 'on';
'ProdEqTarget', 'on';
'TargetLang', 'C++';
'ProdHWDeviceType', 'Atmel->AVR';
'TargetLibSuffix', '.a';
'ERTCustomFileTemplate', 'arduino_ec_file_process.tlc';
'SolverType', 'Fixed-step';
'Solver', 'FixedStepDiscrete';
'FixedStep', '0.1';
'InlineParams', 'on';
'GenerateReport', 'on';
'LaunchReport', 'on';
'GenCodeOnly', 'on';
'GenerateMakefile', 'off';
'DownloadToArduino', 'off';
};
for ii = 1:length(configs)
slConfigUISetVal(hDlg, hSrc, configs{ii,1}, configs{ii,2});
end
slConfigUISetEnabled(hDlg, hSrc, 'SolverType', 'off');
end
上述有些配置,不知道关键字,那么就需要获取所有configuration的配置,从里面找即可。
将配置导出成m文件,即可知道所有的配置。