2 * S-function to support IRC inputs on Humusoft MF624 card
4 * Based on sfuntmpl_basic.c by The MathWorks, Inc.
8 #define S_FUNCTION_NAME sfIRCInput
9 #define S_FUNCTION_LEVEL 2
12 * The S-function has next parameters
14 * Sample time - sample time value or -1 for inherited
21 #define PRM_TS(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
22 #define PRM_CHANNEL(S) (mxGetScalar(ssGetSFcnParam(S, 1)))
23 #define PRM_COUNTER_MODE(S) (mxGetScalar(ssGetSFcnParam(S, 2)))
24 #define PRM_COUNTER_GATING(S) (mxGetScalar(ssGetSFcnParam(S, 3)))
25 #define PRM_RESET_CONTROL(S) (mxGetScalar(ssGetSFcnParam(S, 4)))
26 #define PRM_DIGITAL_FILTER(S) (mxGetScalar(ssGetSFcnParam(S, 5)))
27 #define PRM_RESET_AT_STARTUP(S) (mxGetScalar(ssGetSFcnParam(S, 6)))
31 #define IWORK_IDX_CHANNEL 0
32 #define IWORK_IDX_IRCCTRL 1
33 #define IWORK_IDX_USE_GATING_INPUT 2
34 #define IWORK_IDX_GATING_VALUE 3
35 #define IWORK_IDX_USE_RESET_INPUT 4
36 #define IWORK_IDX_RESET_VALUE 5
40 #define IWORK_CHANNEL(S) (ssGetIWork(S)[IWORK_IDX_CHANNEL])
41 #define IWORK_IRCCTRL(S) (ssGetIWork(S)[IWORK_IDX_IRCCTRL])
42 #define IWORK_USE_GATING_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_GATING_INPUT])
43 #define IWORK_GATING_VALUE(S) (ssGetIWork(S)[IWORK_IDX_GATING_VALUE])
44 #define IWORK_USE_RESET_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_RESET_INPUT])
45 #define IWORK_RESET_VALUE(S) (ssGetIWork(S)[IWORK_IDX_RESET_VALUE])
48 * Need to include simstruc.h for the definition of the SimStruct and
49 * its associated macro definitions.
52 #include "mf624_SIMULINK.h"
54 static const int32_T irc_channel2reg[] = {
64 * You should use the following technique to report errors encountered within
67 * ssSetErrorStatus(S,"Error encountered due to ...");
70 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
71 * It cannot be a local variable. For example the following will cause
72 * unpredictable errors:
76 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
77 * sprintf(msg,"Error due to %s", string);
78 * ssSetErrorStatus(S,msg);
82 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
85 /*====================*
86 * S-function methods *
87 *====================*/
89 #define MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
90 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
91 /* Function: mdlCheckParameters =============================================
93 * mdlCheckParameters verifies new parameter settings whenever parameter
94 * change or are re-evaluated during a simulation. When a simulation is
95 * running, changes to S-function parameters can occur at any time during
96 * the simulation loop.
98 static void mdlCheckParameters(SimStruct *S)
100 if ((PRM_TS(S) < 0) && (PRM_TS(S) != -1))
101 ssSetErrorStatus(S, "Ts has to be positive or -1 for automatic step");
102 if ((PRM_CHANNEL(S) < 0) || (PRM_CHANNEL(S) > 3))
103 ssSetErrorStatus(S, "valid IRC channel is 0, 1, 2, or 3");
104 if ((PRM_COUNTER_MODE(S) < 0) || PRM_COUNTER_MODE(S) >
105 __mfld2val(IRCCTRL_IRC0MODE_mask, IRCCTRL_IRC0MODE_mask))
106 ssSetErrorStatus(S, "Counter Mode out of valid range");
107 if ((PRM_COUNTER_GATING(S) < 0) || PRM_COUNTER_GATING(S) >
108 __mfld2val(IRCCTRL_IRC0COUNT_mask, IRCCTRL_IRC0COUNT_mask))
109 if ((PRM_COUNTER_GATING(S) != -1))
110 ssSetErrorStatus(S, "Counter Gating out of valid range and -1 for external input not set");
111 if ((PRM_RESET_CONTROL(S) < 0) || PRM_RESET_CONTROL(S) >
112 __mfld2val(IRCCTRL_IRC0RESET_mask, IRCCTRL_IRC0RESET_mask))
113 if (PRM_RESET_CONTROL(S) != -1)
114 ssSetErrorStatus(S, "Reset Control out of valid range and -1 for external input not set");
115 if ((PRM_DIGITAL_FILTER(S) < 0) || PRM_DIGITAL_FILTER(S) >
116 __mfld2val(IRCCTRL_IRC0FILTER_mask, IRCCTRL_IRC0FILTER_mask))
117 ssSetErrorStatus(S, "Digital Filter out of valid range");
118 if ((PRM_RESET_AT_STARTUP(S) != 0) && (PRM_RESET_AT_STARTUP(S) != 1))
119 ssSetErrorStatus(S, "Reset at startup can be only 0 or 1");
121 #endif /* MDL_CHECK_PARAMETERS */
124 /* Function: mdlInitializeSizes ===============================================
126 * The sizes information is used by Simulink to determine the S-function
127 * block's characteristics (number of inputs, outputs, states, etc.).
129 static void mdlInitializeSizes(SimStruct *S)
131 int_T nInputPorts = 0;
134 ssSetNumSFcnParams(S, PRM_COUNT); /* Number of expected parameters */
135 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
136 /* Return if number of expected != number of actual parameters */
137 ssSetErrorStatus(S, "6-parameters requited: Ts, Channel, Counter Mode, Counter Gating, Reset Control and Digital Filter");
141 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
142 mdlCheckParameters(S);
143 if (ssGetErrorStatus(S) != NULL) return;
146 if (PRM_COUNTER_GATING(S) == -1)
148 if (PRM_RESET_CONTROL(S) == -1)
151 ssSetNumContStates(S, 0);
152 ssSetNumDiscStates(S, 0);
154 if (!ssSetNumInputPorts(S, nInputPorts)) return;
155 for (i = 0; i < nInputPorts; i++) {
156 ssSetInputPortWidth(S, i, 1);
157 ssSetInputPortDataType(S, i, SS_UINT8);
158 ssSetInputPortRequiredContiguous(S, i, true); /*direct input signal access*/
159 ssSetInputPortDirectFeedThrough(S, i, 0);
162 * Set direct feedthrough flag (1=yes, 0=no).
163 * A port has direct feedthrough if the input is used in either
164 * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
165 * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
168 if (!ssSetNumOutputPorts(S, 1)) return;
169 ssSetOutputPortWidth(S, 0, 1);
170 ssSetOutputPortDataType(S, 0, SS_INT32);
172 ssSetNumSampleTimes(S, 1);
174 ssSetNumIWork(S, IWORK_COUNT);
177 ssSetNumNonsampledZCs(S, 0);
179 /* Specify the sim state compliance to be same as a built-in block */
180 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
187 /* Function: mdlInitializeSampleTimes =========================================
189 * This function is used to specify the sample time(s) for your
190 * S-function. You must register the same number of sample times as
191 * specified in ssSetNumSampleTimes.
193 static void mdlInitializeSampleTimes(SimStruct *S)
195 if (PRM_TS(S) == -1) {
196 ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
197 ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
199 ssSetSampleTime(S, 0, PRM_TS(S));
200 ssSetOffsetTime(S, 0, 0.0);
206 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
207 #if defined(MDL_INITIALIZE_CONDITIONS)
208 /* Function: mdlInitializeConditions ========================================
210 * In this function, you should initialize the continuous and discrete
211 * states for your S-function block. The initial states are placed
212 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
213 * You can also perform any other initialization activities that your
214 * S-function may require. Note, this routine will be called at the
215 * start of simulation and if it is present in an enabled subsystem
216 * configured to reset states, it will be call when the enabled subsystem
217 * restarts execution to reset the states.
219 static void mdlInitializeConditions(SimStruct *S)
224 int_T reset_mode_save;
226 if (mf624_check(S) != 0)
229 if (PRM_RESET_AT_STARTUP(S)) {
230 shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
232 mask = IRCCTRL_IRC0RESET_mask << shift;
233 reset_mode = __val2mfld(IRCCTRL_IRC0RESET_mask, IRCCTRL_RESET_ALWAYS) << shift;
235 reset_mode_save = IRC_mode_change(mfst, 0, 0);
236 IRC_mode_change(mfst, mask, reset_mode);
237 IRC_mode_change(mfst, mask, reset_mode_save & mask);
240 #endif /* MDL_INITIALIZE_CONDITIONS */
244 #define MDL_START /* Change to #undef to remove function */
245 #if defined(MDL_START)
246 /* Function: mdlStart =======================================================
248 * This function is called once at start of model execution. If you
249 * have states that should be initialized once, this is the place
252 static void mdlStart(SimStruct *S)
259 if (mf624_init(NULL) != 0)
262 mask = IRCCTRL_IRC0MODE_mask | IRCCTRL_IRC0COUNT_mask |
263 IRCCTRL_IRC0RESET_mask | IRCCTRL_IRC0FILTER_mask;
265 IWORK_CHANNEL(S) = PRM_CHANNEL(S);
266 IWORK_GATING_VALUE(S) = PRM_COUNTER_GATING(S);
267 IWORK_USE_GATING_INPUT(S) = (IWORK_GATING_VALUE(S) == -1)? 1: 0;
268 IWORK_RESET_VALUE(S) = PRM_RESET_CONTROL(S);
269 IWORK_USE_RESET_INPUT(S) = (IWORK_RESET_VALUE(S) == -1)? 1: 0;
271 mode = PRM_COUNTER_MODE(S);
272 filter = PRM_DIGITAL_FILTER(S);
275 __val2mfld(IRCCTRL_IRC0MODE_mask, mode) |
276 (IWORK_USE_GATING_INPUT(S)? 0 :
277 __val2mfld(IRCCTRL_IRC0COUNT_mask, IWORK_GATING_VALUE(S))) |
278 (IWORK_USE_RESET_INPUT(S)? 0 :
279 __val2mfld(IRCCTRL_IRC0RESET_mask, IWORK_RESET_VALUE(S))) |
280 __val2mfld(IRCCTRL_IRC0FILTER_mask, filter);
282 shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
284 IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
286 mdlInitializeConditions(S);
288 #endif /* MDL_START */
292 /* Function: mdlOutputs =======================================================
294 * In this function, you compute the outputs of your S-function
297 static void mdlOutputs(SimStruct *S, int_T tid)
299 int32_T *y = ssGetOutputPortSignal(S,0);
301 if (mf624_check(S) != 0)
304 y[0] = mf624_read32(MFST2REG(mfst, 4, irc_channel2reg[IWORK_CHANNEL(S)]));
309 #define MDL_UPDATE /* Change to #undef to remove function */
310 #if defined(MDL_UPDATE)
311 /* Function: mdlUpdate ======================================================
313 * This function is called once for every major integration time step.
314 * Discrete states are typically updated here, but this function is useful
315 * for performing any tasks that should only take place once per
318 static void mdlUpdate(SimStruct *S, int_T tid)
324 if (mf624_check(S) != 0)
327 if (IWORK_USE_GATING_INPUT(S)) {
328 const uint8_T *u = (const uint8_T*) ssGetInputPortSignal(S, inp_num);
329 if (*u != IWORK_GATING_VALUE(S)) {
330 IWORK_GATING_VALUE(S) = *u;
331 IWORK_IRCCTRL(S) = (IWORK_IRCCTRL(S) & ~IRCCTRL_IRC0COUNT_mask) |
332 __val2mfld(IRCCTRL_IRC0COUNT_mask, IWORK_GATING_VALUE(S));
333 mask |= IRCCTRL_IRC0COUNT_mask;
338 if (IWORK_USE_RESET_INPUT(S)) {
339 const uint8_T *u = (const uint8_T*) ssGetInputPortSignal(S, inp_num);
340 if (*u != IWORK_RESET_VALUE(S)) {
341 IWORK_RESET_VALUE(S) = *u;
342 IWORK_IRCCTRL(S) = (IWORK_IRCCTRL(S) & ~IRCCTRL_IRC0RESET_mask) |
343 __val2mfld(IRCCTRL_IRC0RESET_mask, IWORK_RESET_VALUE(S));
344 mask |= IRCCTRL_IRC0RESET_mask;
350 shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
351 IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
354 #endif /* MDL_UPDATE */
358 #undef MDL_DERIVATIVES /* Change to #undef to remove function */
359 #if defined(MDL_DERIVATIVES)
360 /* Function: mdlDerivatives =================================================
362 * In this function, you compute the S-function block's derivatives.
363 * The derivatives are placed in the derivative vector, ssGetdX(S).
365 static void mdlDerivatives(SimStruct *S)
368 #endif /* MDL_DERIVATIVES */
372 /* Function: mdlTerminate =====================================================
374 * In this function, you should perform any actions that are necessary
375 * at the termination of a simulation. For example, if memory was
376 * allocated in mdlStart, this is the place to free it.
378 static void mdlTerminate(SimStruct *S)
384 /*======================================================*
385 * See sfuntmpl_doc.c for the optional S-function methods *
386 *======================================================*/
388 /*=============================*
389 * Required S-function trailer *
390 *=============================*/
392 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
393 #include "simulink.c" /* MEX-file interface mechanism */
395 #include "cg_sfun.h" /* Code generation registration function */