User`s guide

4 Guidelines for Writing C MEX S-Functions
4-8
Writing Wrapper S-Functions
This section describes how to create S-functions that work seaml essly with
Simulink and the Real-time Workshop using the wrapper concept. This section
begins by describing how to interface your algorithms in Simulink by writing
MEX S-function wrappers (
sfunction.mex). It finishes with a description of
how to direct the Real-Time Workshop to insert your algorithm into the
generated code by creating a TLC S-function wrapper (
sfunction.tlc).
The MEX S-Function Wrapper
Creating S-functions using an S-function wrapper allows you to insert your C
code algor ithms in Simulink and the Real- T ime Worksho p with littl e or no
change to your original C code function. A MEX S-function wrapper is an
S-function thatcallscodethat resides in an other module. In effect,thewrapper
binds your code t o Simulink. A TLC S-function wrapper is a TLC file that
specifies h o w the Real-Time W o rks h op should ca ll y o ur cod e (the same co de
that was called from t he C-MEX S-functio n wrapper).
Suppose you have an al go rithm (i.e., a C function), ca lle d
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, S imulink will
be able t o ca ll
my_alg from an S-f unctio n block. Howeve r, the Simulink
S-functioncontains a set of empty functions that Simulink requires for various
API-related purposes. For example, although only
mdlOutputs calls my_alg,
Simulink cal ls
mdlTerminate as well, even though this S-function routine
performs no acti on.
You can integrat e
my_alg into the Real-Time W orksh op generated code (i.e.,
embed the call to
my_alg in the generated code) by creating a TLC S-function
wra pper (e.g.,
wrapsfcn.tlc). The adv ant ag e of cre ating a TLC S -function
wrapper is that the empty function calls can be eliminat e d and the o v erhe ad of
executing the
mdlOutputs functionandthenthemy_alg function can be
eliminated.