Products & Services Industries Academia Support User Community Company

Learn more about Real-Time Workshop   

Automating the Generation of Files for Fully Inlined S-Functions Using Legacy Code Tool

Legacy Code Tool and Code Generation

The Simulink Legacy Code Tool provides a way to automatically generate fully inlined C MEX S-functions for legacy or custom code that is optimized for embedded components such as device drivers and lookup tables, that call existing C or C++ functions.

In addition, you can use the tool to

If you want to include such S-functions in models for which you intend to generate code, you must use the tool to generate a TLC block file for the generated S-function. The TLC block file specifies how the generated code for a model calls the existing C or C++ function.

If the S-function depends on files in directories other than the directory containing the S-function dynamically loadable executable file and you want to maintain those dependencies for building a model that includes the S-function, you need the tool to also generate an rtwmakecfg.m file for the S-function. For example, for some applications, such as custom targets, it might be preferable to locate files in a target-specific location. The Real-Time Workshop build process looks for the generated rtwmakecfg.m file in the same directory as the S-function's dynamically loadable executable and calls the rtwmakecfg function if the file is found.

For background information and to get started using the Legacy Code Tool, see Integrating Existing C Functions into Simulink Models with the Legacy Code Tool in the Simulink Writing S-Functions documentation.

Generating Inlined S-Function Files for Code Generation Support

Depending on your application's code generation needs, you can choose to do either of the following to generate code for a model that uses the S-function:

singleCPPMexFile Limitations

You cannot set the singleCPPMexFile field to true (1) if

Applying Model Code Style Settings to Legacy Functions

To apply the code style specified by a model's configuration parameters to a legacy function,

  1. Initialize the Legacy Code Tool data structure. For example,

    def = legacy_code('initialize');
    
  2. Set the value of the Options.singleCPPMexFile field of the data structure to true (1). For example,

    def.Options.singleCPPMexFile = 1;

To verify the setting, enter

def.Options.singleCPPMexFile

singleCPPMexFile Limitations

You cannot set the singleCPPMexFile field to true (1) if

Addressing Dependencies on Files in Different Locations

By default, the Legacy Code Tool assumes that all files on which an S-function depends reside in the same directory as the S-function's dynamically loadable executable file. If your S-function depends on files that reside elsewhere and you are using the Real-Time Workshop template makefile build process, you must generate an rtwmakecfg.m file for the S-function. For example, it is likely that you need to generate this file if your Legacy Code Tool data structure defines compilation resources as path names.

To generate the rtwmakecfg.m file, call the legacy_code function with 'rtwmakecfg_generate' as the first argument and the name of the Legacy Code Tool data structure as the second argument.

legacy_code('rtwmakecfg_generate', lct_spec);

If you use multiple registration files in the same directory and generate an S-function for each file with a single call to legacy_code, as explained in Handling Multiple Registration Files in the Simulink Writing S-Functions documentation, the call to legacy_code that specifies 'rtwmakecfg_generate' must be common to all registration files.

For example, if defs is defined as an array of Legacy Code Tool structures, you call legacy_code with 'rtwmakecfg_generate' once.

defs = [defs1(:);defs2(:);defs3(:)];
legacy_code('rtwmakecfg_generate', defs);

For more information, see Build Support for S-Functions.

Deploying Generated S-Functions for Simulation and Code Generation

You can deploy the S-functions that you generate with the Legacy Code Tool for use by others. To deploy an S-function for simulation and code generation, you need to share the following files:

Users of the deployed files might need to:

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS