]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfIRCInput.c
1cd43673d1406df48135e7ed1814ff8155f3d723
[mf624-simulink.git] / sfIRCInput.c
1 /*
2  * S-function to support IRC inputs on Humusoft MF624 card
3  *
4  * Based on sfuntmpl_basic.c by The MathWorks, Inc.
5  */
6
7
8 #define S_FUNCTION_NAME  sfIRCInput
9 #define S_FUNCTION_LEVEL 2
10
11 /*
12  * The S-function has next parameters
13  *
14  * Sample time     - sample time value or -1 for inherited
15  * Counter Mode    -
16  * Counter Gating
17  * Reset Control
18  * Digital Filter
19  */
20
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
28 #define PRM_COUNT                   6
29
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
36
37 #define IWORK_COUNT                 6
38
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])
45
46 /*
47  * Need to include simstruc.h for the definition of the SimStruct and
48  * its associated macro definitions.
49  */
50 #include "simstruc.h"
51 #include "mf624_SIMULINK.h"
52
53 static const int32_T irc_channel2reg[] = {
54     IRC0_reg,
55     IRC1_reg,
56     IRC2_reg,
57     IRC3_reg
58 };
59
60 /* Error handling
61  * --------------
62  *
63  * You should use the following technique to report errors encountered within
64  * an S-function:
65  *
66  *       ssSetErrorStatus(S,"Error encountered due to ...");
67  *       return;
68  *
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:
72  *
73  *      mdlOutputs()
74  *      {
75  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
76  *         sprintf(msg,"Error due to %s", string);
77  *         ssSetErrorStatus(S,msg);
78  *         return;
79  *      }
80  *
81  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
82  */
83
84 /*====================*
85  * S-function methods *
86  *====================*/
87
88 #define MDL_CHECK_PARAMETERS   /* Change to #undef to remove function */
89 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
90   /* Function: mdlCheckParameters =============================================
91    * Abstract:
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.
96    */
97 static void mdlCheckParameters(SimStruct *S)
98 {
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");
117 }
118 #endif /* MDL_CHECK_PARAMETERS */
119
120
121 /* Function: mdlInitializeSizes ===============================================
122  * Abstract:
123  *    The sizes information is used by Simulink to determine the S-function
124  *    block's characteristics (number of inputs, outputs, states, etc.).
125  */
126 static void mdlInitializeSizes(SimStruct *S)
127 {
128     int_T nInputPorts  = 0;
129     int_T i;
130
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");
135         return;
136     }
137
138   #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
139     mdlCheckParameters(S);
140     if (ssGetErrorStatus(S) != NULL) return;
141   #endif
142
143     if (PRM_COUNTER_GATING(S) == -1)
144         nInputPorts++;
145     if (PRM_RESET_CONTROL(S) == -1)
146         nInputPorts++;
147
148     ssSetNumContStates(S, 0);
149     ssSetNumDiscStates(S, 0);
150
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);
157     }
158     /*
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.
163      */
164
165     if (!ssSetNumOutputPorts(S, 1)) return;
166     ssSetOutputPortWidth(S, 0, 1);
167     ssSetOutputPortDataType(S, 0, SS_INT32);
168
169     ssSetNumSampleTimes(S, 1);
170     ssSetNumRWork(S, 0);
171     ssSetNumIWork(S, IWORK_COUNT);
172     ssSetNumPWork(S, 0);
173     ssSetNumModes(S, 0);
174     ssSetNumNonsampledZCs(S, 0);
175
176     /* Specify the sim state compliance to be same as a built-in block */
177     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
178
179     ssSetOptions(S, 0);
180 }
181
182
183
184 /* Function: mdlInitializeSampleTimes =========================================
185  * Abstract:
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.
189  */
190 static void mdlInitializeSampleTimes(SimStruct *S)
191 {
192     if (PRM_TS(S) == -1) {
193         ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
194         ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
195     } else {
196         ssSetSampleTime(S, 0, PRM_TS(S));
197         ssSetOffsetTime(S, 0, 0.0);
198     }
199 }
200
201
202
203 #define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
204 #if defined(MDL_INITIALIZE_CONDITIONS)
205   /* Function: mdlInitializeConditions ========================================
206    * Abstract:
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.
215    */
216   static void mdlInitializeConditions(SimStruct *S)
217   {
218   }
219 #endif /* MDL_INITIALIZE_CONDITIONS */
220
221
222
223 #define MDL_START  /* Change to #undef to remove function */
224 #if defined(MDL_START)
225   /* Function: mdlStart =======================================================
226    * Abstract:
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
229    *    to do it.
230    */
231 static void mdlStart(SimStruct *S)
232 {
233     int_T shift;
234     int_T mode;
235     int_T filter;
236     int_T mask;
237
238     if (mf624_init(NULL) != 0)
239         return;
240
241     mask = IRCCTRL_IRC0MODE_mask | IRCCTRL_IRC0COUNT_mask |
242            IRCCTRL_IRC0RESET_mask | IRCCTRL_IRC0FILTER_mask;
243
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;
249
250     mode = PRM_COUNTER_MODE(S);
251     filter = PRM_DIGITAL_FILTER(S);
252
253     IWORK_IRCCTRL(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);
260
261     shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
262
263     IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
264 }
265 #endif /*  MDL_START */
266
267
268
269 /* Function: mdlOutputs =======================================================
270  * Abstract:
271  *    In this function, you compute the outputs of your S-function
272  *    block.
273  */
274 static void mdlOutputs(SimStruct *S, int_T tid)
275 {
276     int32_T *y = ssGetOutputPortSignal(S,0);
277
278     if (mf624_check(S) != 0)
279         return;
280
281     y[0] = mf624_read32(MFST2REG(mfst, 4, irc_channel2reg[IWORK_CHANNEL(S)]));
282 }
283
284
285
286 #define MDL_UPDATE  /* Change to #undef to remove function */
287 #if defined(MDL_UPDATE)
288   /* Function: mdlUpdate ======================================================
289    * Abstract:
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
293    *    integration step.
294    */
295 static void mdlUpdate(SimStruct *S, int_T tid)
296 {
297     int_T mask = 0;
298     int_T shift;
299     int_T inp_num = 0;
300
301     if (mf624_check(S) != 0)
302         return;
303
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;
311         }
312         inp_num++;
313     }
314
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;
322         }
323         inp_num++;
324     }
325
326     if (mask) {
327         shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
328         IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
329     }
330 }
331 #endif /* MDL_UPDATE */
332
333
334
335 #undef MDL_DERIVATIVES  /* Change to #undef to remove function */
336 #if defined(MDL_DERIVATIVES)
337   /* Function: mdlDerivatives =================================================
338    * Abstract:
339    *    In this function, you compute the S-function block's derivatives.
340    *    The derivatives are placed in the derivative vector, ssGetdX(S).
341    */
342   static void mdlDerivatives(SimStruct *S)
343   {
344   }
345 #endif /* MDL_DERIVATIVES */
346
347
348
349 /* Function: mdlTerminate =====================================================
350  * Abstract:
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.
354  */
355 static void mdlTerminate(SimStruct *S)
356 {
357     mf624_done();
358 }
359
360
361 /*======================================================*
362  * See sfuntmpl_doc.c for the optional S-function methods *
363  *======================================================*/
364
365 /*=============================*
366  * Required S-function trailer *
367  *=============================*/
368
369 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
370 #include "simulink.c"      /* MEX-file interface mechanism */
371 #else
372 #include "cg_sfun.h"       /* Code generation registration function */
373 #endif