Writing S-Functions    

The MEX S-Function Wrapper

Creating S-functions using an S-function wrapper allows you to insert your C code algorithms in Simulink and the Real-Time Workshop with little or no change to your original C code function. A MEX S-function wrapper is an S-function that calls code that resides in another module. In effect, the wrapper binds your code to Simulink. A TLC S-function wrapper is a TLC file that specifies how the Real-Time Workshop should call your code (the same code that was called from the C-MEX S-function wrapper).

Suppose you have an algorithm (i.e., a C function), called my_alg that resides in the file my_alg.c. You can integrate my_alg into Simulink by creating a MEX S-function wrapper (e.g., wrapsfcn.c). Once this is done, Simulink will be able to call my_alg from an S-function block. However, the Simulink S-function contains a set of empty functions that Simulink requires for various API-related purposes. For example, although only mdlOutputs calls my_alg, Simulink calls mdlTerminate as well, even though this S-function routine performs no action.

You can integrate my_alg into the Real-Time Workshop generated code (i.e., embed the call to my_alg in the generated code) by creating a TLC S-function wrapper (e.g., wrapsfcn.tlc). The advantage of creating a TLC S-function wrapper is that the empty function calls can be eliminated and the overhead of executing the mdlOutputs function and then the my_alg function can be eliminated.

Wrapper S-functions are useful when creating new algorithms that are procedural in nature or when integrating legacy code into Simulink. However, if you want to create code that is:

then you must create a fully inlined TLC file for your S-function.

This figure illustrates the wrapper S-function concept.

Figure 8-1: How S-Functions Interface with Hand-Written Code

Using an S-function wrapper to import algorithms in your Simulink model means that the S-function serves as an interface that calls your C code algorithms from mdlOutputs. S-function wrappers have the advantage that you can quickly integrate large stand-alone C code into your model without having to make changes to the code.

This is an example of a model that includes an S-function wrapper.

Figure 8-1: An Example Model That Includes an S-Function Wrapper

There are two files associated with wrapsfcn block, the S-function wrapper and the C code that contains the algorithm. This is the S-function wrapper code for this example, called wrapsfcn.c.

The S-function routine mdlOutputs contains a function call to my_alg, which is the C function that contains the algorithm that the S-function performs. This is the code for my_alg.c.

The wrapper S-function (wrapsfcn) calls my_alg, which computes u * 2.0. To build wrapsfcn.mex, use the following command.


 Writing Wrapper S-Functions The TLC S-Function Wrapper