]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfIRCInput.c
Allow build of m624 AnalogInput by C89 compiler - i.e. MS Visual C.
[mf624-simulink.git] / sfIRCInput.c
1 /*
2  * S-function to support IRC inputs on Humusoft MF624 card
3  *
4  * Copyright (C) 2014 Pavel Pisa <pisa@cmp.felk.cvut.cz>
5  *
6  * Department of Control Engineering
7  * Faculty of Electrical Engineering
8  * Czech Technical University in Prague (CTU)
9  *
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.
13  *
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
22  * although.
23  *
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/
28  *
29  * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
30  * required S-function structure.
31  */
32
33
34 #define S_FUNCTION_NAME  sfIRCInput
35 #define S_FUNCTION_LEVEL 2
36
37 /*
38  * The S-function has next parameters
39  *
40  * Sample time     - sample time value or -1 for inherited
41  * Counter Mode    -
42  * Counter Gating
43  * Reset Control
44  * Digital Filter
45  */
46
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)))
54
55 #define PRM_COUNT                   7
56
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
63
64 #define IWORK_COUNT                 6
65
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])
72
73 /*
74  * Need to include simstruc.h for the definition of the SimStruct and
75  * its associated macro definitions.
76  */
77 #include "simstruc.h"
78
79 #ifndef WITHOUT_HW
80 #include "mf624_SIMULINK.h"
81
82 static const int32_T irc_channel2reg[] = {
83     IRC0_reg,
84     IRC1_reg,
85     IRC2_reg,
86     IRC3_reg
87 };
88 #endif /*WITHOUT_HW*/
89
90 /* Error handling
91  * --------------
92  *
93  * You should use the following technique to report errors encountered within
94  * an S-function:
95  *
96  *       ssSetErrorStatus(S,"Error encountered due to ...");
97  *       return;
98  *
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:
102  *
103  *      mdlOutputs()
104  *      {
105  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
106  *         sprintf(msg,"Error due to %s", string);
107  *         ssSetErrorStatus(S,msg);
108  *         return;
109  *      }
110  *
111  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
112  */
113
114 /*====================*
115  * S-function methods *
116  *====================*/
117
118 #define MDL_CHECK_PARAMETERS   /* Change to #undef to remove function */
119 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
120   /* Function: mdlCheckParameters =============================================
121    * Abstract:
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.
126    */
127 static void mdlCheckParameters(SimStruct *S)
128 {
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");
133   #ifndef WITHOUT_HW
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");
151 }
152 #endif /* MDL_CHECK_PARAMETERS */
153
154
155 /* Function: mdlInitializeSizes ===============================================
156  * Abstract:
157  *    The sizes information is used by Simulink to determine the S-function
158  *    block's characteristics (number of inputs, outputs, states, etc.).
159  */
160 static void mdlInitializeSizes(SimStruct *S)
161 {
162     int_T nInputPorts  = 0;
163     int_T i;
164
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");
169         return;
170     }
171
172   #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
173     mdlCheckParameters(S);
174     if (ssGetErrorStatus(S) != NULL) return;
175   #endif
176
177     if (PRM_COUNTER_GATING(S) == -1)
178         nInputPorts++;
179     if (PRM_RESET_CONTROL(S) == -1)
180         nInputPorts++;
181
182     ssSetNumContStates(S, 0);
183     ssSetNumDiscStates(S, 0);
184
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);
191     }
192     /*
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.
197      */
198
199     if (!ssSetNumOutputPorts(S, 1)) return;
200     ssSetOutputPortWidth(S, 0, 1);
201     ssSetOutputPortDataType(S, 0, SS_INT32);
202
203     ssSetNumSampleTimes(S, 1);
204     ssSetNumRWork(S, 0);
205     ssSetNumIWork(S, IWORK_COUNT);
206     ssSetNumPWork(S, 0);
207     ssSetNumModes(S, 0);
208     ssSetNumNonsampledZCs(S, 0);
209
210     /* Specify the sim state compliance to be same as a built-in block */
211     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
212
213     ssSetOptions(S, 0);
214 }
215
216
217
218 /* Function: mdlInitializeSampleTimes =========================================
219  * Abstract:
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.
223  */
224 static void mdlInitializeSampleTimes(SimStruct *S)
225 {
226     if (PRM_TS(S) == -1) {
227         ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
228         ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
229     } else {
230         ssSetSampleTime(S, 0, PRM_TS(S));
231         ssSetOffsetTime(S, 0, 0.0);
232     }
233 }
234
235
236
237 #define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
238 #if defined(MDL_INITIALIZE_CONDITIONS)
239   /* Function: mdlInitializeConditions ========================================
240    * Abstract:
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.
249    */
250 static void mdlInitializeConditions(SimStruct *S)
251 {
252     int_T shift;
253     int_T mask;
254     int_T reset_mode;
255     int_T reset_mode_save;
256
257   #ifndef WITHOUT_HW
258     if (mf624_check(S) != 0)
259         return;
260
261     if (PRM_RESET_AT_STARTUP(S)) {
262         shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
263
264         mask = IRCCTRL_IRC0RESET_mask << shift;
265         reset_mode = __val2mfld(IRCCTRL_IRC0RESET_mask, IRCCTRL_RESET_ALWAYS) << shift;
266
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);
270     }
271   #endif /*WITHOUT_HW*/
272 }
273 #endif /* MDL_INITIALIZE_CONDITIONS */
274
275
276
277 #define MDL_START  /* Change to #undef to remove function */
278 #if defined(MDL_START)
279   /* Function: mdlStart =======================================================
280    * Abstract:
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
283    *    to do it.
284    */
285 static void mdlStart(SimStruct *S)
286 {
287     int_T shift;
288     int_T mode;
289     int_T filter;
290     int_T mask;
291
292   #ifndef WITHOUT_HW
293     if (mf624_init(NULL) != 0)
294         return;
295
296     mask = IRCCTRL_IRC0MODE_mask | IRCCTRL_IRC0COUNT_mask |
297            IRCCTRL_IRC0RESET_mask | IRCCTRL_IRC0FILTER_mask;
298
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;
304
305     mode = PRM_COUNTER_MODE(S);
306     filter = PRM_DIGITAL_FILTER(S);
307
308     IWORK_IRCCTRL(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);
315
316     shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
317
318     IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
319
320   #endif /*WITHOUT_HW*/
321
322     mdlInitializeConditions(S);
323 }
324 #endif /*  MDL_START */
325
326
327
328 /* Function: mdlOutputs =======================================================
329  * Abstract:
330  *    In this function, you compute the outputs of your S-function
331  *    block.
332  */
333 static void mdlOutputs(SimStruct *S, int_T tid)
334 {
335     int32_T *y = ssGetOutputPortSignal(S,0);
336
337   #ifndef WITHOUT_HW
338     if (mf624_check(S) != 0)
339         return;
340
341     y[0] = mf624_read32(MFST2REG(mfst, 4, irc_channel2reg[IWORK_CHANNEL(S)]));
342   #else /*WITHOUT_HW*/
343     y[0] = 0;
344   #endif /*WITHOUT_HW*/
345 }
346
347
348
349 #define MDL_UPDATE  /* Change to #undef to remove function */
350 #if defined(MDL_UPDATE)
351   /* Function: mdlUpdate ======================================================
352    * Abstract:
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
356    *    integration step.
357    */
358 static void mdlUpdate(SimStruct *S, int_T tid)
359 {
360     int_T mask = 0;
361     int_T shift;
362     int_T inp_num = 0;
363
364   #ifndef WITHOUT_HW
365     if (mf624_check(S) != 0)
366         return;
367
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;
375         }
376         inp_num++;
377     }
378
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;
386         }
387         inp_num++;
388     }
389
390     if (mask) {
391         shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
392         IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
393     }
394   #endif /*WITHOUT_HW*/
395 }
396 #endif /* MDL_UPDATE */
397
398
399
400 #undef MDL_DERIVATIVES  /* Change to #undef to remove function */
401 #if defined(MDL_DERIVATIVES)
402   /* Function: mdlDerivatives =================================================
403    * Abstract:
404    *    In this function, you compute the S-function block's derivatives.
405    *    The derivatives are placed in the derivative vector, ssGetdX(S).
406    */
407   static void mdlDerivatives(SimStruct *S)
408   {
409   }
410 #endif /* MDL_DERIVATIVES */
411
412
413
414 /* Function: mdlTerminate =====================================================
415  * Abstract:
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.
419  */
420 static void mdlTerminate(SimStruct *S)
421 {
422   #ifndef WITHOUT_HW
423     mf624_done();
424   #endif /*WITHOUT_HW*/
425 }
426
427
428 /*======================================================*
429  * See sfuntmpl_doc.c for the optional S-function methods *
430  *======================================================*/
431
432 /*=============================*
433  * Required S-function trailer *
434  *=============================*/
435
436 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
437 #include "simulink.c"      /* MEX-file interface mechanism */
438 #else
439 #include "cg_sfun.h"       /* Code generation registration function */
440 #endif