Skip to Main Content Skip to Search
Product Documentation

Import Custom Code into Model

Generate S-Function Wrappers

About S-Function Wrapper Generation

An S-function wrapper is an S-function that calls your C or C++ code from within Simulink. S-function wrappers provide a standard interface between Simulink and externally written code, allowing you to integrate your code into a model with minimal modification. This is useful for software-in-the-loop (SIL) code verification (validating your generated code in Simulink), as well as for simulation acceleration purposes (see Use a SIL or PIL Block). For a complete description of wrapper S-functions, see Developing S-Functions in the Simulink documentation.

Using the Create block parameter, on the Code Generation > SIL and PIL Verification pane, you can build in one automated step:

When the Create block parameter is set to SIL, the build process generates an additional source code file, model_sf.c or .cpp, in the build folder. This module contains the S-function that calls the generated code that you deploy. You can use this S-function within Simulink.

The build process then compiles and links model_sf.c or .cpp with model.c or .cpp and the other generated code modules, building a MEX-file. The MEX-file is named model_sf.mexext. (mexext is the file extension for MEX-files on your platform, as given by the MATLAB mexext command.) The MEX-file is stored in your working folder. Finally, the build process creates and opens an untitled model containing the SIL block with the generated S-function.

Create a SIL Block

To create a SIL block with the S-function wrapper for your generated code, open your ERT-based Simulink model and do the following:

  1. Open the Configuration Parameters dialog box.

  2. Select the Code Generation > SIL and PIL Verification pane.

  3. From the Create block drop-down list, select SIL.

  4. Configure the other code generation options as required.

  5. To initialize the memory for the S-function to zero, you must clear the following options in the Data initialization section of the Optimization > General pane:

  6. Select the Code Generation pane, and click Build.

  7. When the build process completes, an untitled model window opens. This model contains a SIL block with the generated S-function.

  8. Save the new model.

  9. You can now use the SIL block with other blocks or models in Simulink.

S-Function Wrapper Generation Limitations

The following limitations apply to Embedded Coder S-function wrapper generation:

  


Related Products & Applications

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

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