Documentation Center

  • Trial Software
  • Product Updates

Specification of Block Sample Time

This example shows you how to use the Legacy Code Tool to integrate legacy C functions with the block's sample time specified, inherited and parameterized.

The Legacy Code Tool allows you to:

  • Provide the legacy function specification,

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

  • Generate a block TLC file and optional rtwmakecfg.m file that is used during code generation to call the legacy code.

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. For detailed help on the properties, call legacy_code('help')legacy_code('help'). The prototypes of the legacy functions being called in this example are:

FLT gainScalar(const FLT in, const FLT gain)

where FLT is a typedef to float. The legacy source code is found in the files your_types.hyour_types.h, gain.hgain.h, and gainScalar.cgainScalar.c.

defs = [];

% rtwdemo_sfun_st_inherited
def = legacy_code('initialize');
def.SFunctionName = 'rtwdemo_sfun_st_inherited';
def.OutputFcnSpec = 'single y1 = gainScalar(single u1, single p1)';
def.HeaderFiles   = {'gain.h'};
def.SourceFiles   = {'gainScalar.c'};
def.IncPaths      = {'rtwdemo_lct_src'};
def.SrcPaths      = {'rtwdemo_lct_src'};
defs = [defs; def];

% rtwdemo_sfun_st_fixed
def = legacy_code('initialize');
def.SFunctionName = 'rtwdemo_sfun_st_fixed';
def.OutputFcnSpec = 'single y1 = gainScalar(single u1, single p1)';
def.HeaderFiles   = {'gain.h'};
def.SourceFiles   = {'gainScalar.c'};
def.IncPaths      = {'rtwdemo_lct_src'};
def.SrcPaths      = {'rtwdemo_lct_src'};
def.SampleTime    = [2 1];
defs = [defs; def];

% rtwdemo_sfun_st_parameterized
def = legacy_code('initialize');
def.SFunctionName = 'rtwdemo_sfun_st_parameterized';
def.OutputFcnSpec = 'single y1 = gainScalar(single u1, single p1)';
def.HeaderFiles   = {'gain.h'};
def.SourceFiles   = {'gainScalar.c'};
def.IncPaths      = {'rtwdemo_lct_src'};
def.SrcPaths      = {'rtwdemo_lct_src'};
def.SampleTime    = 'parameterized';
defs = [defs; def];

Generating S-Functions for Use During Simulation

The function legacy_code() is called again with the first input set to 'sfcn_cmex_generate' in order to automatically generate C-MEX S-functions according to the description provided by the input argument 'defs'. The S-functions are used to call the legacy functions in simulation. The source code for the S-functions is found in the files rtwdemo_sfun_st_inherited.crtwdemo_sfun_st_inherited.c and rtwdemo_sfun_st_fixed.crtwdemo_sfun_st_fixed.c. rtwdemo_sfun_st_parameterized.crtwdemo_sfun_st_parameterized.c.

legacy_code('sfcn_cmex_generate', defs);

Compiling the Generated S-Function for Simulation

After the C-MEX S-function source files are generated, the function legacy_code() is called again with the first input set to 'compile' in order to compile the S-function for simulation with Simulink®.

legacy_code('compile', defs);
### Start Compiling rtwdemo_sfun_st_inherited
    mex('-I/mathworks/devel/bat/R2014ad/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/R2014ad_522_6358/tp10b125dd_ecf4_422b_a4a4_4084ef3601b3', '-c', '-outdir', '/tmp/R2014ad_522_6358/tp613c2118_1e88_473b_ac46_e4a436bbc166', '/mathworks/devel/bat/R2014ad/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src/gainScalar.c')
Building with 'gcc'.
MEX completed successfully.
    mex('rtwdemo_sfun_st_inherited.c', '-I/mathworks/devel/bat/R2014ad/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/R2014ad_522_6358/tp10b125dd_ecf4_422b_a4a4_4084ef3601b3', '/tmp/R2014ad_522_6358/tp613c2118_1e88_473b_ac46_e4a436bbc166/gainScalar.o')
Building with 'gcc'.
MEX completed successfully.
### Finish Compiling rtwdemo_sfun_st_inherited
### Exit

### Start Compiling rtwdemo_sfun_st_fixed
    mex('-I/mathworks/devel/bat/R2014ad/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/R2014ad_522_6358/tp10b125dd_ecf4_422b_a4a4_4084ef3601b3', '-c', '-outdir', '/tmp/R2014ad_522_6358/tp5f540b1d_0d40_4f3c_9cf9_22e320f576cf', '/mathworks/devel/bat/R2014ad/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src/gainScalar.c')
Building with 'gcc'.
MEX completed successfully.
    mex('rtwdemo_sfun_st_fixed.c', '-I/mathworks/devel/bat/R2014ad/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/R2014ad_522_6358/tp10b125dd_ecf4_422b_a4a4_4084ef3601b3', '/tmp/R2014ad_522_6358/tp5f540b1d_0d40_4f3c_9cf9_22e320f576cf/gainScalar.o')
Building with 'gcc'.
MEX completed successfully.
### Finish Compiling rtwdemo_sfun_st_fixed
### Exit

### Start Compiling rtwdemo_sfun_st_parameterized
    mex('-I/mathworks/devel/bat/R2014ad/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/R2014ad_522_6358/tp10b125dd_ecf4_422b_a4a4_4084ef3601b3', '-c', '-outdir', '/tmp/R2014ad_522_6358/tpfe4e2f06_26dd_4307_9c0e_af0098303e5b', '/mathworks/devel/bat/R2014ad/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src/gainScalar.c')
Building with 'gcc'.
MEX completed successfully.
    mex('rtwdemo_sfun_st_parameterized.c', '-I/mathworks/devel/bat/R2014ad/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/tmp/R2014ad_522_6358/tp10b125dd_ecf4_422b_a4a4_4084ef3601b3', '/tmp/R2014ad_522_6358/tpfe4e2f06_26dd_4307_9c0e_af0098303e5b/gainScalar.o')
Building with 'gcc'.
MEX completed successfully.
### Finish Compiling rtwdemo_sfun_st_parameterized
### Exit

Generating a TLC Block File for Code Generation

After the S-function is compiled and used in simulation, the function legacy_code() can be called again with the first input set to 'sfcn_tlc_generate' in order to generate a TLC block file to support code generation through Simulink® Coder™. Code generation will fail if the TLC block file is not created and you try to generate code for a model that includes the S-function. The TLC block file for the S-function is: rtwdemo_sfun_st_inherited.tlcrtwdemo_sfun_st_inherited.tlc and rtwdemo_sfun_st_fixed.tlcrtwdemo_sfun_st_fixed.tlc. rtwdemo_sfun_st_parameterized.tlcrtwdemo_sfun_st_parameterized.tlc.

legacy_code('sfcn_tlc_generate', defs);

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.

legacy_code('rtwmakecfg_generate', defs);

Generating a masked S-Function block for calling the generated S-Function

After the C-MEX S-function source is compiled, the function legacy_code() can be called again with the first input set to 'slblock_generate' in order to generate a masked S-function block that is configured to call that S-function. The block is placed in a new model and can be copied to an existing model.

% legacy_code('slblock_generate', defs);

Showing the Generated Integration with Legacy Code

The model rtwdemo_lct_sampletimertwdemo_lct_sampletime shows integration with the legacy code. The subsystem sample_time serves as a harness for the calls to the legacy C functions, with unit delays serving to store the previous output values.

open_system('rtwdemo_lct_sampletime')
open_system('rtwdemo_lct_sampletime/sample_time')
sim('rtwdemo_lct_sampletime')

Was this topic helpful?