User`s guide

6 Custom xPC Target™ Driver Notes
S-Function Guidelines
You implement xPC Target device driver blocks using Simulink S-functions.
An S-function is a set of subroutines that implements a function. On the host,
youcanwriteanS-functioninMATLABcode,C,orFortran. ForxPCTarget
device drivers, you must write an S-function in C.
Simulink S-functions have a number of callback methods. For xPC Target
drivers, you typically need to write C code for the following callbac k metho ds:
Method
Description
mdlInitializeSizes
Initializes the S-function with the number of inputs, outputs,
states, parameters, and o the r characteristics.
mdlInitializeSampleTimes
Initializes the sample rates of the S-function.
mdlStart
Initializes the state vectors of this S-function. It also initia li zes
hardware as necessary.
mdlOutputs
Computes the signals that this block emits.
mdlTerminate
Performs any actions required at termination of the simulation.
After you create the S-function, createamaskforit.SeeChapter4,“Masking
Drivers”. Also, refer to the Simulink documentation (in particular, Developing
S-Functions).
Of particular note when writing S-functions:
Keep track of the input parameters the driv er will require. When yo u
create a mask for the driver, you will need to know this.
Work vectors are not shared between runs. All S-function work variables
are cle ared after ca llin g
mdlTerminate. This implies that each time the
S-function calls
mdlStart, you must reinitialize all work variables.
Declare all mem ory-mapped registers as
volatile.
An S-function is compiled into a MEX-file to run as part of the simulated
model on the host PC. D uring code generation, the S-function calls the
mdlInitializeSizes and mdlInitializeSampleTimes functions to
determine the data structures that are used on the target. The same C-file
6-2