2 * S-function to support IRC inputs on Humusoft MF624 card
4 * Copyright (C) 2014 Pavel Pisa <pisa@cmp.felk.cvut.cz>
6 * Department of Control Engineering
7 * Faculty of Electrical Engineering
8 * Czech Technical University in Prague (CTU)
10 * The S-Function for ERT Linux can be distributed in compliance
11 * with GNU General Public License (GPL) version 2 or later.
12 * Other licence can negotiated with CTU.
14 * Next exception is granted in addition to GPL.
15 * Instantiating or linking compiled version of this code
16 * to produce an application image/executable, does not
17 * by itself cause the resulting application image/executable
18 * to be covered by the GNU General Public License.
19 * This exception does not however invalidate any other reasons
20 * why the executable file might be covered by the GNU Public License.
21 * Publication of enhanced or derived S-function files is required
24 * Linux ERT code is available from
25 * http://rtime.felk.cvut.cz/gitweb/ert_linux.git
26 * More CTU Linux target for Simulink components are available at
27 * http://lintarget.sourceforge.net/
29 * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
30 * required S-function structure.
34 #define S_FUNCTION_NAME sfIRCInput
35 #define S_FUNCTION_LEVEL 2
38 * The S-function has next parameters
40 * Sample time - sample time value or -1 for inherited
47 #define PRM_TS(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
48 #define PRM_CHANNEL(S) (mxGetScalar(ssGetSFcnParam(S, 1)))
49 #define PRM_COUNTER_MODE(S) (mxGetScalar(ssGetSFcnParam(S, 2)))
50 #define PRM_COUNTER_GATING(S) (mxGetScalar(ssGetSFcnParam(S, 3)))
51 #define PRM_RESET_CONTROL(S) (mxGetScalar(ssGetSFcnParam(S, 4)))
52 #define PRM_DIGITAL_FILTER(S) (mxGetScalar(ssGetSFcnParam(S, 5)))
53 #define PRM_RESET_AT_STARTUP(S) (mxGetScalar(ssGetSFcnParam(S, 6)))
57 #define IWORK_IDX_CHANNEL 0
58 #define IWORK_IDX_IRCCTRL 1
59 #define IWORK_IDX_USE_GATING_INPUT 2
60 #define IWORK_IDX_GATING_VALUE 3
61 #define IWORK_IDX_USE_RESET_INPUT 4
62 #define IWORK_IDX_RESET_VALUE 5
66 #define IWORK_CHANNEL(S) (ssGetIWork(S)[IWORK_IDX_CHANNEL])
67 #define IWORK_IRCCTRL(S) (ssGetIWork(S)[IWORK_IDX_IRCCTRL])
68 #define IWORK_USE_GATING_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_GATING_INPUT])
69 #define IWORK_GATING_VALUE(S) (ssGetIWork(S)[IWORK_IDX_GATING_VALUE])
70 #define IWORK_USE_RESET_INPUT(S) (ssGetIWork(S)[IWORK_IDX_USE_RESET_INPUT])
71 #define IWORK_RESET_VALUE(S) (ssGetIWork(S)[IWORK_IDX_RESET_VALUE])
74 * Need to include simstruc.h for the definition of the SimStruct and
75 * its associated macro definitions.
80 #include "mf624_SIMULINK.h"
82 static const int32_T irc_channel2reg[] = {
93 * You should use the following technique to report errors encountered within
96 * ssSetErrorStatus(S,"Error encountered due to ...");
99 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
100 * It cannot be a local variable. For example the following will cause
101 * unpredictable errors:
105 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
106 * sprintf(msg,"Error due to %s", string);
107 * ssSetErrorStatus(S,msg);
111 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
114 /*====================*
115 * S-function methods *
116 *====================*/
118 #define MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
119 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
120 /* Function: mdlCheckParameters =============================================
122 * mdlCheckParameters verifies new parameter settings whenever parameter
123 * change or are re-evaluated during a simulation. When a simulation is
124 * running, changes to S-function parameters can occur at any time during
125 * the simulation loop.
127 static void mdlCheckParameters(SimStruct *S)
129 if ((PRM_TS(S) < 0) && (PRM_TS(S) != -1))
130 ssSetErrorStatus(S, "Ts has to be positive or -1 for automatic step");
131 if ((PRM_CHANNEL(S) < 0) || (PRM_CHANNEL(S) > 3))
132 ssSetErrorStatus(S, "valid IRC channel is 0, 1, 2, or 3");
134 if ((PRM_COUNTER_MODE(S) < 0) || PRM_COUNTER_MODE(S) >
135 __mfld2val(IRCCTRL_IRC0MODE_mask, IRCCTRL_IRC0MODE_mask))
136 ssSetErrorStatus(S, "Counter Mode out of valid range");
137 if ((PRM_COUNTER_GATING(S) < 0) || PRM_COUNTER_GATING(S) >
138 __mfld2val(IRCCTRL_IRC0COUNT_mask, IRCCTRL_IRC0COUNT_mask))
139 if ((PRM_COUNTER_GATING(S) != -1))
140 ssSetErrorStatus(S, "Counter Gating out of valid range and -1 for external input not set");
141 if ((PRM_RESET_CONTROL(S) < 0) || PRM_RESET_CONTROL(S) >
142 __mfld2val(IRCCTRL_IRC0RESET_mask, IRCCTRL_IRC0RESET_mask))
143 if (PRM_RESET_CONTROL(S) != -1)
144 ssSetErrorStatus(S, "Reset Control out of valid range and -1 for external input not set");
145 if ((PRM_DIGITAL_FILTER(S) < 0) || PRM_DIGITAL_FILTER(S) >
146 __mfld2val(IRCCTRL_IRC0FILTER_mask, IRCCTRL_IRC0FILTER_mask))
147 ssSetErrorStatus(S, "Digital Filter out of valid range");
148 #endif /*WITHOUT_HW*/
149 if ((PRM_RESET_AT_STARTUP(S) != 0) && (PRM_RESET_AT_STARTUP(S) != 1))
150 ssSetErrorStatus(S, "Reset at startup can be only 0 or 1");
152 #endif /* MDL_CHECK_PARAMETERS */
155 /* Function: mdlInitializeSizes ===============================================
157 * The sizes information is used by Simulink to determine the S-function
158 * block's characteristics (number of inputs, outputs, states, etc.).
160 static void mdlInitializeSizes(SimStruct *S)
162 int_T nInputPorts = 0;
165 ssSetNumSFcnParams(S, PRM_COUNT); /* Number of expected parameters */
166 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
167 /* Return if number of expected != number of actual parameters */
168 ssSetErrorStatus(S, "6-parameters requited: Ts, Channel, Counter Mode, Counter Gating, Reset Control and Digital Filter");
172 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
173 mdlCheckParameters(S);
174 if (ssGetErrorStatus(S) != NULL) return;
177 if (PRM_COUNTER_GATING(S) == -1)
179 if (PRM_RESET_CONTROL(S) == -1)
182 ssSetNumContStates(S, 0);
183 ssSetNumDiscStates(S, 0);
185 if (!ssSetNumInputPorts(S, nInputPorts)) return;
186 for (i = 0; i < nInputPorts; i++) {
187 ssSetInputPortWidth(S, i, 1);
188 ssSetInputPortDataType(S, i, SS_UINT8);
189 ssSetInputPortRequiredContiguous(S, i, true); /*direct input signal access*/
190 ssSetInputPortDirectFeedThrough(S, i, 0);
193 * Set direct feedthrough flag (1=yes, 0=no).
194 * A port has direct feedthrough if the input is used in either
195 * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
196 * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
199 if (!ssSetNumOutputPorts(S, 1)) return;
200 ssSetOutputPortWidth(S, 0, 1);
201 ssSetOutputPortDataType(S, 0, SS_INT32);
203 ssSetNumSampleTimes(S, 1);
205 ssSetNumIWork(S, IWORK_COUNT);
208 ssSetNumNonsampledZCs(S, 0);
210 /* Specify the sim state compliance to be same as a built-in block */
211 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
218 /* Function: mdlInitializeSampleTimes =========================================
220 * This function is used to specify the sample time(s) for your
221 * S-function. You must register the same number of sample times as
222 * specified in ssSetNumSampleTimes.
224 static void mdlInitializeSampleTimes(SimStruct *S)
226 if (PRM_TS(S) == -1) {
227 ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
228 ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
230 ssSetSampleTime(S, 0, PRM_TS(S));
231 ssSetOffsetTime(S, 0, 0.0);
237 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
238 #if defined(MDL_INITIALIZE_CONDITIONS)
239 /* Function: mdlInitializeConditions ========================================
241 * In this function, you should initialize the continuous and discrete
242 * states for your S-function block. The initial states are placed
243 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
244 * You can also perform any other initialization activities that your
245 * S-function may require. Note, this routine will be called at the
246 * start of simulation and if it is present in an enabled subsystem
247 * configured to reset states, it will be call when the enabled subsystem
248 * restarts execution to reset the states.
250 static void mdlInitializeConditions(SimStruct *S)
255 int_T reset_mode_save;
258 if (mf624_check(S) != 0)
261 if (PRM_RESET_AT_STARTUP(S)) {
262 shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
264 mask = IRCCTRL_IRC0RESET_mask << shift;
265 reset_mode = __val2mfld(IRCCTRL_IRC0RESET_mask, IRCCTRL_RESET_ALWAYS) << shift;
267 reset_mode_save = IRC_mode_change(mfst, 0, 0);
268 IRC_mode_change(mfst, mask, reset_mode);
269 IRC_mode_change(mfst, mask, reset_mode_save & mask);
271 #endif /*WITHOUT_HW*/
273 #endif /* MDL_INITIALIZE_CONDITIONS */
277 #define MDL_START /* Change to #undef to remove function */
278 #if defined(MDL_START)
279 /* Function: mdlStart =======================================================
281 * This function is called once at start of model execution. If you
282 * have states that should be initialized once, this is the place
285 static void mdlStart(SimStruct *S)
293 if (mf624_init(NULL) != 0)
296 mask = IRCCTRL_IRC0MODE_mask | IRCCTRL_IRC0COUNT_mask |
297 IRCCTRL_IRC0RESET_mask | IRCCTRL_IRC0FILTER_mask;
299 IWORK_CHANNEL(S) = PRM_CHANNEL(S);
300 IWORK_GATING_VALUE(S) = PRM_COUNTER_GATING(S);
301 IWORK_USE_GATING_INPUT(S) = (IWORK_GATING_VALUE(S) == -1)? 1: 0;
302 IWORK_RESET_VALUE(S) = PRM_RESET_CONTROL(S);
303 IWORK_USE_RESET_INPUT(S) = (IWORK_RESET_VALUE(S) == -1)? 1: 0;
305 mode = PRM_COUNTER_MODE(S);
306 filter = PRM_DIGITAL_FILTER(S);
309 __val2mfld(IRCCTRL_IRC0MODE_mask, mode) |
310 (IWORK_USE_GATING_INPUT(S)? 0 :
311 __val2mfld(IRCCTRL_IRC0COUNT_mask, IWORK_GATING_VALUE(S))) |
312 (IWORK_USE_RESET_INPUT(S)? 0 :
313 __val2mfld(IRCCTRL_IRC0RESET_mask, IWORK_RESET_VALUE(S))) |
314 __val2mfld(IRCCTRL_IRC0FILTER_mask, filter);
316 shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
318 IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
320 #endif /*WITHOUT_HW*/
322 mdlInitializeConditions(S);
324 #endif /* MDL_START */
328 /* Function: mdlOutputs =======================================================
330 * In this function, you compute the outputs of your S-function
333 static void mdlOutputs(SimStruct *S, int_T tid)
335 int32_T *y = ssGetOutputPortSignal(S,0);
338 if (mf624_check(S) != 0)
341 y[0] = mf624_read32(MFST2REG(mfst, 4, irc_channel2reg[IWORK_CHANNEL(S)]));
344 #endif /*WITHOUT_HW*/
349 #define MDL_UPDATE /* Change to #undef to remove function */
350 #if defined(MDL_UPDATE)
351 /* Function: mdlUpdate ======================================================
353 * This function is called once for every major integration time step.
354 * Discrete states are typically updated here, but this function is useful
355 * for performing any tasks that should only take place once per
358 static void mdlUpdate(SimStruct *S, int_T tid)
365 if (mf624_check(S) != 0)
368 if (IWORK_USE_GATING_INPUT(S)) {
369 const uint8_T *u = (const uint8_T*) ssGetInputPortSignal(S, inp_num);
370 if (*u != IWORK_GATING_VALUE(S)) {
371 IWORK_GATING_VALUE(S) = *u;
372 IWORK_IRCCTRL(S) = (IWORK_IRCCTRL(S) & ~IRCCTRL_IRC0COUNT_mask) |
373 __val2mfld(IRCCTRL_IRC0COUNT_mask, IWORK_GATING_VALUE(S));
374 mask |= IRCCTRL_IRC0COUNT_mask;
379 if (IWORK_USE_RESET_INPUT(S)) {
380 const uint8_T *u = (const uint8_T*) ssGetInputPortSignal(S, inp_num);
381 if (*u != IWORK_RESET_VALUE(S)) {
382 IWORK_RESET_VALUE(S) = *u;
383 IWORK_IRCCTRL(S) = (IWORK_IRCCTRL(S) & ~IRCCTRL_IRC0RESET_mask) |
384 __val2mfld(IRCCTRL_IRC0RESET_mask, IWORK_RESET_VALUE(S));
385 mask |= IRCCTRL_IRC0RESET_mask;
391 shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
392 IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
394 #endif /*WITHOUT_HW*/
396 #endif /* MDL_UPDATE */
400 #undef MDL_DERIVATIVES /* Change to #undef to remove function */
401 #if defined(MDL_DERIVATIVES)
402 /* Function: mdlDerivatives =================================================
404 * In this function, you compute the S-function block's derivatives.
405 * The derivatives are placed in the derivative vector, ssGetdX(S).
407 static void mdlDerivatives(SimStruct *S)
410 #endif /* MDL_DERIVATIVES */
414 /* Function: mdlTerminate =====================================================
416 * In this function, you should perform any actions that are necessary
417 * at the termination of a simulation. For example, if memory was
418 * allocated in mdlStart, this is the place to free it.
420 static void mdlTerminate(SimStruct *S)
424 #endif /*WITHOUT_HW*/
428 /*======================================================*
429 * See sfuntmpl_doc.c for the optional S-function methods *
430 *======================================================*/
432 /*=============================*
433 * Required S-function trailer *
434 *=============================*/
436 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
437 #include "simulink.c" /* MEX-file interface mechanism */
439 #include "cg_sfun.h" /* Code generation registration function */