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)))
30 #define IWORK_IDX_CHANNEL 0
31 #define IWORK_IDX_IRCCTRL 1
32 #define IWORK_IDX_USE_GATING_INPUT 2
33 #define IWORK_IDX_GATING_VALUE 3
34 #define IWORK_IDX_USE_RESET_INPUT 4
35 #define IWORK_IDX_RESET_VALUE 5
39 #define IWORK_CHANNEL(S) (ssGetIWork(S)[IWORK_IDX_CHANNEL])
40 #define IWORK_IRCCTRL(S) (ssGetIWork(S)[IWORK_IDX_IRCCTRL])
41 #define IWORK_USE_GATING_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_GATING_INPUT])
42 #define IWORK_GATING_VALUE(S) (ssGetIWork(S)[IWORK_IDX_GATING_VALUE])
43 #define IWORK_USE_RESET_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_RESET_INPUT])
44 #define IWORK_RESET_VALUE(S) (ssGetIWork(S)[IWORK_IDX_RESET_VALUE])
47 * Need to include simstruc.h for the definition of the SimStruct and
48 * its associated macro definitions.
51 #include "mf624_SIMULINK.h"
53 static const int32_T irc_channel2reg[] = {
63 * You should use the following technique to report errors encountered within
66 * ssSetErrorStatus(S,"Error encountered due to ...");
69 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
70 * It cannot be a local variable. For example the following will cause
71 * unpredictable errors:
75 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
76 * sprintf(msg,"Error due to %s", string);
77 * ssSetErrorStatus(S,msg);
81 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
84 /*====================*
85 * S-function methods *
86 *====================*/
88 #define MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
89 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
90 /* Function: mdlCheckParameters =============================================
92 * mdlCheckParameters verifies new parameter settings whenever parameter
93 * change or are re-evaluated during a simulation. When a simulation is
94 * running, changes to S-function parameters can occur at any time during
95 * the simulation loop.
97 static void mdlCheckParameters(SimStruct *S)
99 if ((PRM_TS(S) < 0) && (PRM_TS(S) != -1))
100 ssSetErrorStatus(S, "Ts has to be positive or -1 for automatic step");
101 if ((PRM_CHANNEL(S) < 0) || (PRM_CHANNEL(S) > 3))
102 ssSetErrorStatus(S, "valid IRC channel is 0, 1, 2, or 3");
103 if ((PRM_COUNTER_MODE(S) < 0) || PRM_COUNTER_MODE(S) >
104 __mfld2val(IRCCTRL_IRC0MODE_mask, IRCCTRL_IRC0MODE_mask))
105 ssSetErrorStatus(S, "Counter Mode out of valid range");
106 if ((PRM_COUNTER_GATING(S) < 0) || PRM_COUNTER_GATING(S) >
107 __mfld2val(IRCCTRL_IRC0COUNT_mask, IRCCTRL_IRC0COUNT_mask))
108 if ((PRM_COUNTER_GATING(S) != -1))
109 ssSetErrorStatus(S, "Counter Gating out of valid range and -1 for external input not set");
110 if ((PRM_RESET_CONTROL(S) < 0) || PRM_RESET_CONTROL(S) >
111 __mfld2val(IRCCTRL_IRC0RESET_mask, IRCCTRL_IRC0RESET_mask))
112 if (PRM_RESET_CONTROL(S) != -1)
113 ssSetErrorStatus(S, "Reset Control out of valid range and -1 for external input not set");
114 if ((PRM_DIGITAL_FILTER(S) < 0) || PRM_DIGITAL_FILTER(S) >
115 __mfld2val(IRCCTRL_IRC0FILTER_mask, IRCCTRL_IRC0FILTER_mask))
116 ssSetErrorStatus(S, "Digital Filter out of valid range");
118 #endif /* MDL_CHECK_PARAMETERS */
121 /* Function: mdlInitializeSizes ===============================================
123 * The sizes information is used by Simulink to determine the S-function
124 * block's characteristics (number of inputs, outputs, states, etc.).
126 static void mdlInitializeSizes(SimStruct *S)
128 int_T nInputPorts = 0;
131 ssSetNumSFcnParams(S, PRM_COUNT); /* Number of expected parameters */
132 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
133 /* Return if number of expected != number of actual parameters */
134 ssSetErrorStatus(S, "6-parameters requited: Ts, Channel, Counter Mode, Counter Gating, Reset Control and Digital Filter");
138 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
139 mdlCheckParameters(S);
140 if (ssGetErrorStatus(S) != NULL) return;
143 if (PRM_COUNTER_GATING(S) == -1)
145 if (PRM_RESET_CONTROL(S) == -1)
148 ssSetNumContStates(S, 0);
149 ssSetNumDiscStates(S, 0);
151 if (!ssSetNumInputPorts(S, nInputPorts)) return;
152 for (i = 0; i < nInputPorts; i++) {
153 ssSetInputPortWidth(S, i, 1);
154 ssSetInputPortDataType(S, i, SS_UINT8);
155 ssSetInputPortRequiredContiguous(S, i, true); /*direct input signal access*/
156 ssSetInputPortDirectFeedThrough(S, i, 0);
159 * Set direct feedthrough flag (1=yes, 0=no).
160 * A port has direct feedthrough if the input is used in either
161 * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
162 * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
165 if (!ssSetNumOutputPorts(S, 1)) return;
166 ssSetOutputPortWidth(S, 0, 1);
167 ssSetOutputPortDataType(S, 0, SS_INT32);
169 ssSetNumSampleTimes(S, 1);
171 ssSetNumIWork(S, IWORK_COUNT);
174 ssSetNumNonsampledZCs(S, 0);
176 /* Specify the sim state compliance to be same as a built-in block */
177 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
184 /* Function: mdlInitializeSampleTimes =========================================
186 * This function is used to specify the sample time(s) for your
187 * S-function. You must register the same number of sample times as
188 * specified in ssSetNumSampleTimes.
190 static void mdlInitializeSampleTimes(SimStruct *S)
192 if (PRM_TS(S) == -1) {
193 ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
194 ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
196 ssSetSampleTime(S, 0, PRM_TS(S));
197 ssSetOffsetTime(S, 0, 0.0);
203 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
204 #if defined(MDL_INITIALIZE_CONDITIONS)
205 /* Function: mdlInitializeConditions ========================================
207 * In this function, you should initialize the continuous and discrete
208 * states for your S-function block. The initial states are placed
209 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
210 * You can also perform any other initialization activities that your
211 * S-function may require. Note, this routine will be called at the
212 * start of simulation and if it is present in an enabled subsystem
213 * configured to reset states, it will be call when the enabled subsystem
214 * restarts execution to reset the states.
216 static void mdlInitializeConditions(SimStruct *S)
219 #endif /* MDL_INITIALIZE_CONDITIONS */
223 #define MDL_START /* Change to #undef to remove function */
224 #if defined(MDL_START)
225 /* Function: mdlStart =======================================================
227 * This function is called once at start of model execution. If you
228 * have states that should be initialized once, this is the place
231 static void mdlStart(SimStruct *S)
238 if (mf624_init(NULL) != 0)
241 mask = IRCCTRL_IRC0MODE_mask | IRCCTRL_IRC0COUNT_mask |
242 IRCCTRL_IRC0RESET_mask | IRCCTRL_IRC0FILTER_mask;
244 IWORK_CHANNEL(S) = PRM_CHANNEL(S);
245 IWORK_GATING_VALUE(S) = PRM_COUNTER_GATING(S);
246 IWORK_USE_GATING_INPUT(S) = (IWORK_GATING_VALUE(S) == -1)? 1: 0;
247 IWORK_RESET_VALUE(S) = PRM_RESET_CONTROL(S);
248 IWORK_USE_RESET_INPUT(S) = (IWORK_RESET_VALUE(S) == -1)? 1: 0;
250 mode = PRM_COUNTER_MODE(S);
251 filter = PRM_DIGITAL_FILTER(S);
254 __val2mfld(IRCCTRL_IRC0MODE_mask, mode) |
255 (IWORK_USE_GATING_INPUT(S)? 0 :
256 __val2mfld(IRCCTRL_IRC0COUNT_mask, IWORK_GATING_VALUE(S))) |
257 (IWORK_USE_RESET_INPUT(S)? 0 :
258 __val2mfld(IRCCTRL_IRC0RESET_mask, IWORK_RESET_VALUE(S))) |
259 __val2mfld(IRCCTRL_IRC0FILTER_mask, filter);
261 shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
263 IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
265 #endif /* MDL_START */
269 /* Function: mdlOutputs =======================================================
271 * In this function, you compute the outputs of your S-function
274 static void mdlOutputs(SimStruct *S, int_T tid)
276 int32_T *y = ssGetOutputPortSignal(S,0);
278 if (mf624_check(S) != 0)
281 y[0] = mf624_read32(MFST2REG(mfst, 4, irc_channel2reg[IWORK_CHANNEL(S)]));
286 #define MDL_UPDATE /* Change to #undef to remove function */
287 #if defined(MDL_UPDATE)
288 /* Function: mdlUpdate ======================================================
290 * This function is called once for every major integration time step.
291 * Discrete states are typically updated here, but this function is useful
292 * for performing any tasks that should only take place once per
295 static void mdlUpdate(SimStruct *S, int_T tid)
301 if (mf624_check(S) != 0)
304 if (IWORK_USE_GATING_INPUT(S)) {
305 const uint8_T *u = (const uint8_T*) ssGetInputPortSignal(S, inp_num);
306 if (*u != IWORK_GATING_VALUE(S)) {
307 IWORK_GATING_VALUE(S) = *u;
308 IWORK_IRCCTRL(S) = (IWORK_IRCCTRL(S) & ~IRCCTRL_IRC0COUNT_mask) |
309 __val2mfld(IRCCTRL_IRC0COUNT_mask, IWORK_GATING_VALUE(S));
310 mask |= IRCCTRL_IRC0COUNT_mask;
315 if (IWORK_USE_RESET_INPUT(S)) {
316 const uint8_T *u = (const uint8_T*) ssGetInputPortSignal(S, inp_num);
317 if (*u != IWORK_RESET_VALUE(S)) {
318 IWORK_RESET_VALUE(S) = *u;
319 IWORK_IRCCTRL(S) = (IWORK_IRCCTRL(S) & ~IRCCTRL_IRC0RESET_mask) |
320 __val2mfld(IRCCTRL_IRC0RESET_mask, IWORK_RESET_VALUE(S));
321 mask |= IRCCTRL_IRC0RESET_mask;
327 shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
328 IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
331 #endif /* MDL_UPDATE */
335 #undef MDL_DERIVATIVES /* Change to #undef to remove function */
336 #if defined(MDL_DERIVATIVES)
337 /* Function: mdlDerivatives =================================================
339 * In this function, you compute the S-function block's derivatives.
340 * The derivatives are placed in the derivative vector, ssGetdX(S).
342 static void mdlDerivatives(SimStruct *S)
345 #endif /* MDL_DERIVATIVES */
349 /* Function: mdlTerminate =====================================================
351 * In this function, you should perform any actions that are necessary
352 * at the termination of a simulation. For example, if memory was
353 * allocated in mdlStart, this is the place to free it.
355 static void mdlTerminate(SimStruct *S)
361 /*======================================================*
362 * See sfuntmpl_doc.c for the optional S-function methods *
363 *======================================================*/
365 /*=============================*
366 * Required S-function trailer *
367 *=============================*/
369 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
370 #include "simulink.c" /* MEX-file interface mechanism */
372 #include "cg_sfun.h" /* Code generation registration function */