| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → xPC Target |
| Contents | Index |
| Learn more about xPC Target |
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, you can write an S-function in M-code, C, or Fortran. For xPC Target 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 callback methods:
| Method | Description |
|---|---|
| mdlInitializeSizes | Initializes the S-function with the number of inputs, outputs, states, parameters, and other characteristics. |
| mdlInitializeSampleTimes | Initializes the sample rates of the S-function. |
| mdlStart | Initializes the state vectors of this S-function. It also initializes 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, create a mask for it. See Masking Drivers. Also, refer to the Simulink documentation (in particular, Writing S-Functions).
Of particular note when writing S-functions:
Keep track of the input parameters the driver will require. When you 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 cleared after calling mdlTerminate. This implies that each time the S-function calls mdlStart, you must reinitialize all work variables.
Declare all memory-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. During 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 is also compiled with your application to run on the target PC. Because of the following reasons, you must conditionally compile code for the host PC and the target PC.
The host PC runs Windows and the target PC runs the xPC Target kernel.
The host PC does not have the same I/O hardware as the target PC.
The preprocessor symbol MATLAB_MEX_FILE is defined when you compile for simulation (via mex). Undefine this symbol when compiling for the xPC Target environment. Use this symbol to conditionally compile host PC or target PC specific code. For example:
#ifdef MATLAB_MEX_FILE /* host/simulation */ /* simulation code, typically nothing */ #else /* target */ /* code to access I/O board */ # endif
If you want the code to run on both the host and target PCs, do not conditionalize the code.
Include the xpctarget.h file in your S-function.
This provides definitions for the functions exported by the xPC Target kernel. The xPC Target kernel exports a number of functions for use in device drivers.
See mdlStart and mdlTerminate Considerations for notes on specific applications of the callback methods.
![]() | Custom xPC Target Driver Notes | mdlStart and mdlTerminate Considerations | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |