MATLAB Examples

Integrate External C Functions That Implement Start and Terminate Actions

Integrate legacy C functions that have start and terminate actions by using the Legacy Code Tool.

With the Legacy Code Tool, you can:

  • Provide the legacy function specification.
  • Generate a C-MEX S-function that calls the legacy code during simulation.
  • 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.

Contents

Provide the Legacy Function Specification

Legacy Code Tool functions take a specific data structure or array of structures as the argument. You can initialize the data structure by calling the function legacy_code() using 'initialize' as the first input. After initializing the structure, assign its properties to values corresponding to the legacy code being integrated. For detailed help on the properties, call legacy_code('help'). The prototypes of the legacy functions being called in this example are:

  • void initFaultCounter(unsigned int *counter)
  • void openLogFile(void **fid)
  • void incAndLogFaultCounter(void *fid, unsigned int *counter, double time)
  • void closeLogFile(void **fid)

The legacy source code is in the files your_types.h, fault.h, and fault.c.

% rtwdemo_sfun_fault
def = legacy_code('initialize');
def.SFunctionName = 'rtwdemo_sfun_fault';
def.InitializeConditionsFcnSpec  = 'initFaultCounter(uint32 work2[1])';
def.StartFcnSpec                 = 'openLogFile(void **work1)';
def.OutputFcnSpec                = ...
    'incAndLogFaultCounter(void *work1, uint32 work2[1], double u1)';
def.TerminateFcnSpec             = 'closeLogFile(void **work1)';
def.HeaderFiles                  = {'fault.h'};
def.SourceFiles                  = {'fault.c'};
def.IncPaths                     = {'rtwdemo_lct_src'};
def.SrcPaths                     = {'rtwdemo_lct_src'};
def.Options.useTlcWithAccel      = false;

Generate an S-Function for Simulation

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 file rtwdemo_sfun_fault.c.

legacy_code('sfcn_cmex_generate', def);

Compile the Generated S-Function for Simulation

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('compile', def);
### Start Compiling rtwdemo_sfun_fault
    mex('-I/mathworks/devel/bat/Bdoc17b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/private/tmp/Bdoc17b_685977_71560/publish_examples6/tp4062f098/ex46254423', '-c', '-outdir', '/private/tmp/Bdoc17b_685977_71560/publish_examples6/tpf6a12168_4871_4a09_a8a5_0010827ed772', '/mathworks/devel/bat/Bdoc17b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src/fault.c')
Building with 'Xcode with Clang'.
MEX completed successfully.
    mex('rtwdemo_sfun_fault.c', '-I/mathworks/devel/bat/Bdoc17b/build/matlab/toolbox/rtw/rtwdemos/rtwdemo_lct_src', '-I/private/tmp/Bdoc17b_685977_71560/publish_examples6/tp4062f098/ex46254423', '/private/tmp/Bdoc17b_685977_71560/publish_examples6/tpf6a12168_4871_4a09_a8a5_0010827ed772/fault.o')
Building with 'Xcode with Clang'.
MEX completed successfully.
### Finish Compiling rtwdemo_sfun_fault
### Exit

Generate a TLC Block File for Code Generation

After you compile the S-function and use it in simulation, you can call the function legacy_code() again with the first input set to 'sfcn_tlc_generate' to generate a TLC block file. The block file specifies how the generated code for a model calls the legacy code. If you do not generate a TLC block file and you try to generate code for a model that includes the S-function, code generation fails. The TLC block file for the S-function is: rtwdemo_sfun_fault.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 with the first input set to 'rtwmakecfg_generate' to generate an rtwmakecfg.m file to support 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

After you compile the C-MEX S-function source, you can call the function legacy_code() again with the first input set to 'slblock_generate' to generate a masked S-function block that calls that S-function. The software places the block in a new model. From there you can copy it to an existing model.

legacy_code('slblock_generate', def);

Showing the Generated Integration with Legacy Code

The model rtwdemo_lct_start_term shows integration of the model with the legacy code. The subsystem TestFixpt serves as a harness for the call to the legacy C function, and the scope compares the output of the function with the output of the built-in Simulink® product block; the results should be identical.

open_system('rtwdemo_lct_start_term')
open_system('rtwdemo_lct_start_term/TestFault')
sim('rtwdemo_lct_start_term')