主要内容

Fixed Point Signals in Legacy Functions

This example shows you how to use the Legacy Code Tool to integrate legacy C functions that pass their inputs and outputs using signals of fixed point data type.

遗留代码工具允许您:

  • Provide the legacy function specification,

  • Generate a C-MEX S-function that is used during simulation to call the legacy code, and

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

Providing the Legacy Function Specification

Functions provided with the Legacy Code Tool take a specific data structure or array of structures as the argument. The data structure is initialized by calling the function legacy_code() using 'initialize' as the first input. After initializing the structure, you have to assign its properties to values corresponding to the legacy code being integrated. The prototype of the legacy functions being called in this example is:

myfixpt timess16(const myfixptin1,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';

The legacy source code is found in the filesTimesFixpt.h.Timess16.c.

%sldemo_sfun_times_s16.def = letacacy_code('初始化'); def.SFunctionName ='sldemo_sfun_times_s16';def.outputfcnspec =.'myfixpt y1 = timess16(myfixpt u1,myfixpt u2,uint8 p1)';def.HeaderFiles = {'timesFixpt.h'};def.SourceFiles = {'timesS16.c'};def.incpaths = {'sldemo_lct_src'};def.srcpaths = {'sldemo_lct_src'};

生成和编译S函数以在仿真期间使用

The function legacy_code() is called again with the first input set to 'generate_for_sim' in order to automatically generate and compile the C-MEX S-function according to the description provided by the input argument 'def'. This S-function is used to call the legacy functions in simulation. The source code for the S-function is found in the filesldemo_sfun_times_s16.c..

legacy_code('generate_for_sim', def);
### Start Compiling sldemo_sfun_times_s16 mex('-I/tmp/Bdoc22a_1891349_108461/tp5e3db00b/ex15473442/sldemo_lct_src', '-I/tmp/Bdoc22a_1891349_108461/tp5e3db00b/ex15473442', '-c', '-outdir', '/tmp/Bdoc22a_1891349_108461/tpf38d8acd_3183_4d94_9b80_7a5a01a5e15e', '/tmp/Bdoc22a_1891349_108461/tp5e3db00b/ex15473442/sldemo_lct_src/timesS16.c') Building with 'gcc'. MEX completed successfully. mex('sldemo_sfun_times_s16.c', '-I/tmp/Bdoc22a_1891349_108461/tp5e3db00b/ex15473442/sldemo_lct_src', '-I/tmp/Bdoc22a_1891349_108461/tp5e3db00b/ex15473442', '/tmp/Bdoc22a_1891349_108461/tpf38d8acd_3183_4d94_9b80_7a5a01a5e15e/timesS16.o') Building with 'gcc'. MEX completed successfully. ### Finish Compiling sldemo_sfun_times_s16 ### Exit

Generating an rtwmakecfg.m File for Code Generation

After the TLC block file is created, the function legacy_code() can be called again with the first input set to 'rtwmakecfg_generate' in order to generate an rtwmakecfg.m file to support code generation through Simulink® Coder™. Generate the rtwmakecfg.m file if the required source and header files for the S-functions are not in the same directory as the S-functions, and you want to add these dependencies in the makefile produced during code generation.

Note: Complete this step only if you are going to simulate the model in accelerated mode.

legacy_code('rtwmakecfg_generate', def);

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

编译C-MEX S函数源后,可以使用第一个输入设置为“slblock_generate”来调用函数legacy_code(),以便生成被配置为调用该函数的屏蔽的S函数块。该块放置在一个新模型中,可以复制到现有模型。

%Legacy_code('slblock_generate',def);

Integration with Legacy Code

The modelsldemo_lct_fixpt_signals显示与遗留代码的集成。子系统TestFixpt用作通过所生成的S函数呼叫传统C函数的线束,范围将功能的输出与内置Simulink®产品块的输出进行比较;金宝app结果是相同的。

Open_System('sldemo_lct_fixpt_signals') open_system('sldemo_lct_fixpt_signals/TestFixpt') sim('sldemo_lct_fixpt_signals')
ans = 金宝appsimulink.simulationoutput:yout:[101x2 double] simulation meta数据:[1x1 simulink.simulation metaData] errormessage:[0x0 char]

See Also