2 * sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
4 * -------------------------------------------------------------------------
5 * | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
6 * -------------------------------------------------------------------------
8 * Copyright 1990-2002 The MathWorks, Inc.
9 * $Revision: 1.27.4.2 $
14 * You must specify the S_FUNCTION_NAME as the name of your S-function
15 * (i.e. replace sfuntmpl_basic with the name of your S-function).
18 #define S_FUNCTION_NAME sfDigitalInput
19 #define S_FUNCTION_LEVEL 2
21 #define CHNL_PRM(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
24 * Need to include simstruc.h for the definition of the SimStruct and
25 * its associated macro definitions.
28 #include "mf624_SIMULINK.h"
33 * You should use the following technique to report errors encountered within
36 * ssSetErrorStatus(S,"Error encountered due to ...");
39 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
40 * It cannot be a local variable. For example the following will cause
41 * unpredictable errors:
45 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
46 * sprintf(msg,"Error due to %s", string);
47 * ssSetErrorStatus(S,msg);
51 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
54 /*====================*
55 * S-function methods *
56 *====================*/
58 /* Function: mdlInitializeSizes ===============================================
60 * The sizes information is used by Simulink to determine the S-function
61 * block's characteristics (number of inputs, outputs, states, etc.).
63 static void mdlInitializeSizes(SimStruct *S)
65 /* See sfuntmpl_doc.c for more details on the macros below */
67 ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
68 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
69 /* Return if number of expected != number of actual parameters */
70 ssSetErrorStatus(S,"Parameter count mismatch");
74 ssSetNumContStates(S, 0);
75 ssSetNumDiscStates(S, 0);
77 if (!ssSetNumInputPorts(S, 0)) return;
79 if (!ssSetNumOutputPorts(S, 1)) return;
80 ssSetOutputPortWidth(S, 0, 1);
82 ssSetNumSampleTimes(S, 1);
87 ssSetNumNonsampledZCs(S, 0);
89 /* Specify the sim state compliance to be same as a built-in block */
90 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
97 /* Function: mdlInitializeSampleTimes =========================================
99 * This function is used to specify the sample time(s) for your
100 * S-function. You must register the same number of sample times as
101 * specified in ssSetNumSampleTimes.
103 static void mdlInitializeSampleTimes(SimStruct *S)
105 ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
106 ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
111 #undef MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
112 #if defined(MDL_INITIALIZE_CONDITIONS)
113 /* Function: mdlInitializeConditions ========================================
115 * In this function, you should initialize the continuous and discrete
116 * states for your S-function block. The initial states are placed
117 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
118 * You can also perform any other initialization activities that your
119 * S-function may require. Note, this routine will be called at the
120 * start of simulation and if it is present in an enabled subsystem
121 * configured to reset states, it will be call when the enabled subsystem
122 * restarts execution to reset the states.
124 static void mdlInitializeConditions(SimStruct *S)
127 #endif /* MDL_INITIALIZE_CONDITIONS */
131 #define MDL_START /* Change to #undef to remove function */
132 #if defined(MDL_START)
133 /* Function: mdlStart =======================================================
135 * This function is called once at start of model execution. If you
136 * have states that should be initialized once, this is the place
139 static void mdlStart(SimStruct *S)
141 if (mf624_init(NULL) != 0)
144 #endif /* MDL_START */
148 /* Function: mdlOutputs =======================================================
150 * In this function, you compute the outputs of your S-function
153 static void mdlOutputs(SimStruct *S, int_T tid)
158 if (mf624_check(S) != 0)
161 y = (real_T *)ssGetOutputPortSignal(S, 0);
163 din = mf624_read16(MFST2REG(mfst, 2, DIN_reg));
165 *y = din & (1 << (int)(CHNL_PRM(S)-1)) ? 0.0 : 1.0;
170 #undef MDL_UPDATE /* Change to #undef to remove function */
171 #if defined(MDL_UPDATE)
172 /* Function: mdlUpdate ======================================================
174 * This function is called once for every major integration time step.
175 * Discrete states are typically updated here, but this function is useful
176 * for performing any tasks that should only take place once per
179 static void mdlUpdate(SimStruct *S, int_T tid)
182 #endif /* MDL_UPDATE */
186 #undef MDL_DERIVATIVES /* Change to #undef to remove function */
187 #if defined(MDL_DERIVATIVES)
188 /* Function: mdlDerivatives =================================================
190 * In this function, you compute the S-function block's derivatives.
191 * The derivatives are placed in the derivative vector, ssGetdX(S).
193 static void mdlDerivatives(SimStruct *S)
196 #endif /* MDL_DERIVATIVES */
200 /* Function: mdlTerminate =====================================================
202 * In this function, you should perform any actions that are necessary
203 * at the termination of a simulation. For example, if memory was
204 * allocated in mdlStart, this is the place to free it.
206 static void mdlTerminate(SimStruct *S)
212 /*======================================================*
213 * See sfuntmpl_doc.c for the optional S-function methods *
214 *======================================================*/
216 /*=============================*
217 * Required S-function trailer *
218 *=============================*/
220 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
221 #include "simulink.c" /* MEX-file interface mechanism */
223 #include "cg_sfun.h" /* Code generation registration function */