主要内容

集成外部C函数,将输入和输出参数作为参数与定点数据类型

Integrate legacy C functions that pass their inputs and outputs by using parameters of a fixed-point data type with the Legacy Code Tool.

使用旧版代码工具,您可以:

  • Provide the legacy function specification.

  • 生成一个C-MEX S功能,该功能在模拟过程中调用旧版代码。

  • Compile and build the generated S-function for simulation.

  • Generate a TLC block file and optional rtwmakecfg.m file that specifies how the generated code for a model calls the legacy code.

提供旧功能规范

传统代码工具功能以特定的数据结构或一系列结构作为参数。您可以使用'initialize'作为第一个输入来调用函数lacacy_code()来初始化数据结构。初始化结构后,将其属性分配给对应于要集成的遗留代码的值。有关属性的详细帮助,请致电legacy_code('help'). The prototype of the legacy functions being called in this example is:

myFixpt timess16(const myfixpt in1,const myfixpt in2,const uint8_t fraclength)

从逻辑上讲,myFixpt是固定点数据类型,从物理上讲是16位整数的类型:

myfixpt = 金宝appsimulink.numerictype;myfixpt.datatypemode ='Fixed-point: binary point scaling';myFixpt.Signed = true; myFixpt.WordLength = 16; myFixpt.FractionLength = 10; myFixpt.IsAlias = true; myFixpt.HeaderFile ='timesFixpt.h';

传统源代码在文件中timesfixpt.h, 和Timess16.c.

%rtwdemo_sfun_gain_fixptdef = legacy_code('初始化'); def.SFunctionName ='rtwdemo_sfun_gain_fixpt';def.outputfcnspec ='myFixpt y1 = timesS16(myFixpt u1, myFixpt p1, uint8 p2)';def.headerfiles = {'timesFixpt.h'};def.SourceFiles = {'timesS16.c'};def.incpaths = {'rtwdemo_lct_src'};def.srcpaths = {'rtwdemo_lct_src'};

生成s功能以进行仿真

To generate a C-MEX S-function according to the description provided by the input argument 'def', call the function legacy_code() again with the first input set to 'sfcn_cmex_generate'. The S-function calls the legacy functions during simulation. The source code for the S-function is in the filertwdemo_sfun_gain_fixpt.c.

legacy_code('sfcn_cmex_generate', def);

编译生成的S功能以进行仿真

After you generate the C-MEX S-function source file, to compile the S-function for simulation with Simulink®, call the function legacy_code() again with the first input set to 'compile'.

legacy_code(“编译”, def);
### Start Compiling rtwdemo_sfun_gain_fixpt mex('-I/mathworks/devel/bat/Bdoc20b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/Bdoc20b_1465442_59938/tp8eb0d42c/ex05928773', '-c', '-outdir', '/tmp/Bdoc20b_1465442_59938/tp644ad607_18ab_4194_b113_c281e4a6b524', '/mathworks/devel/bat/Bdoc20b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src/timesS16.c') Building with 'gcc'. MEX completed successfully. mex('rtwdemo_sfun_gain_fixpt.c', '-I/mathworks/devel/bat/Bdoc20b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/Bdoc20b_1465442_59938/tp8eb0d42c/ex05928773', '/tmp/Bdoc20b_1465442_59938/tp644ad607_18ab_4194_b113_c281e4a6b524/timesS16.o') Building with 'gcc'. MEX completed successfully. ### Finish Compiling rtwdemo_sfun_gain_fixpt ### Exit

生成一个用于代码生成的TLC块文件

编译S功能并在模拟中使用它后,您可以再次调用函数legacy_code()。将第一个输入设置为“ sfcn_tlc_generate”以生成一个TLC块文件。块文件指定模型生成的代码如何调用旧版代码。如果您不生成TLC块文件,并且尝试为包括s功能的模型生成代码,则代码生成失败。S函数的TLC块文件是:rtwdemo_sfun_gain_fixpt.tlc.

legacy_code('sfcn_tlc_generate', def);

Generate an rtwmakecfg.m File for Code Generation

After you create the TLC block file, you can call the function legacy_code() again. Set the first input to 'rtwmakecfg_generate' to generate an rtwmakecfg.m file that supports code generation. If the required source and header files for the S-function are not in the same folder as the S-function, and you want to add these dependencies in the makefile produced during code generation, generate the rtwmakecfg.m file.

legacy_code('rtwmakecfg_generate', def);

Generate a Masked S-Function Block for Calling the Generated S-Function

编译C-MEX S功能源后,您可以再次调用函数lecacy_code()。将第一个输入设置为“ slblock_generate”,以生成调用该s功能的蒙版s功能块。该软件将块置于新型号中。您可以将块复制到现有模型。

legacy_code('slblock_generate', def);

显示与旧代码的生成集成

The modelrtwdemo_lct_fixpt_paramsshows integration of the model with the legacy code. The subsystem TestFixpt serves as a harness for the call to the legacy C function via the generated S-function.

open_system('rtwdemo_lct_fixpt_params')open_system('rtwdemo_lct_fixpt_params/TestFixpt') sim('rtwdemo_lct_fixpt_params')

See Also

相关话题