User`s guide

Implementing Device Drivers
10-19
The Public Registration Function
The include file cg_sfun.h defines a function that is the entry point for the
S-function code. This function is named by the
#define S_FUNCTION_NAME name
macro that you specified earlier in your code.
This function registers the other local functions by installing pointers to them
in the
SimStruct. This is the only public function in the S-function and is called
by the real-time program’s public registration function during program
startup. The remainder of the S-function code is then accessed via these
pointers. This scheme produces re-entrant code allowing multiple instances of
an S-function within a single program.
The
S_FUNCTION_NAME function always has the same definition and therefore is
most simply implemented by including
cg_sfun.h at the bottom of your
S-function.
Compiling the MEX-File
See the MATLAB Application Program Interface Guide for information on how
to use
mex to compile the device driver S-function into an executable MEX-file.
Note that many I/O boards supply include files as part of their software support
package. If your device driver code includes such files, you must ensure that
these files are available when you build your real-time program, as well as
when you compile the device drivers as MEX-files.
Converting the S-Function to a Block
Once you have compiled the S-function into an executable MEX-file, you can
convert it to a block and then mask the block to create its own dialog box. To do
this, follow these two steps:
Place the S-function block into a subsystem block
Mask the Subsystem block
See “Creating Subsystems” and “Using Masks to Customize Blocks” in Using
Simulink for more information about subsystems and masking.