User`s guide

10 Targeting Custom Hardware
10-18
The Termination Function
The final required function is typically used only in DACs to zero the output at
the end of the program. For example:
static void mdlTerminate(SimStruct *S)
{
uint_T num_channels = (uint_t)mxGetPr(ssGetSFcnParams(S,0)[0]);
uint_T i;
for (i = 0; i < num_channels; i++) {
ds1102_da(i + 1, 0.0);
}
}
This for loop simply sets the output of each channel to 0.0. ADCs typically
implement this function as an empty stub.
Additional Include Files
The code implementing a device driver block must serve as both MEX-file and
as stand-alone code module that can be compiled and linked with the generated
code. This code is used as a MEX-file to enable Simulink to obtain information
for performing consistency checks during the code generation process.
Each of these applications of the device driver S-function requires its own
include file. The two files are
simulink.c for MEX-files and cg_sfun.h for the
generated code.
MEX-files are built with the
mex command. mex defines the string
MATLAB_MEX_FILE to provide a mechanism that allows you to include certain
partsofyourcodeintheMEX-fileandotherpartsinthegeneratedcode.To
include the proper files, place the following statements at the end of your
S-function:
#ifdef MATLAB_MEX_FILE / Is this a MEX-file? /
#include "simulink.c" / MEX-file include file /
#else
#include "cg_sfun.h" / Generated code include file /
#endif
The S-function template file matlabroot/simulink/src/sfuntmpl.c contains
these statements.