]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfIRCInput.c
mf624_blocks_demo adapt for IRC extension and include PWM blocks.
[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 #define PRM_RESET_AT_STARTUP(S) (mxGetScalar(ssGetSFcnParam(S, 6)))
28
29 #define PRM_COUNT                   7
30
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
37
38 #define IWORK_COUNT                 6
39
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])
46
47 /*
48  * Need to include simstruc.h for the definition of the SimStruct and
49  * its associated macro definitions.
50  */
51 #include "simstruc.h"
52 #include "mf624_SIMULINK.h"
53
54 static const int32_T irc_channel2reg[] = {
55     IRC0_reg,
56     IRC1_reg,
57     IRC2_reg,
58     IRC3_reg
59 };
60
61 /* Error handling
62  * --------------
63  *
64  * You should use the following technique to report errors encountered within
65  * an S-function:
66  *
67  *       ssSetErrorStatus(S,"Error encountered due to ...");
68  *       return;
69  *
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:
73  *
74  *      mdlOutputs()
75  *      {
76  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
77  *         sprintf(msg,"Error due to %s", string);
78  *         ssSetErrorStatus(S,msg);
79  *         return;
80  *      }
81  *
82  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
83  */
84
85 /*====================*
86  * S-function methods *
87  *====================*/
88
89 #define MDL_CHECK_PARAMETERS   /* Change to #undef to remove function */
90 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
91   /* Function: mdlCheckParameters =============================================
92    * Abstract:
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.
97    */
98 static void mdlCheckParameters(SimStruct *S)
99 {
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");
120 }
121 #endif /* MDL_CHECK_PARAMETERS */
122
123
124 /* Function: mdlInitializeSizes ===============================================
125  * Abstract:
126  *    The sizes information is used by Simulink to determine the S-function
127  *    block's characteristics (number of inputs, outputs, states, etc.).
128  */
129 static void mdlInitializeSizes(SimStruct *S)
130 {
131     int_T nInputPorts  = 0;
132     int_T i;
133
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");
138         return;
139     }
140
141   #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
142     mdlCheckParameters(S);
143     if (ssGetErrorStatus(S) != NULL) return;
144   #endif
145
146     if (PRM_COUNTER_GATING(S) == -1)
147         nInputPorts++;
148     if (PRM_RESET_CONTROL(S) == -1)
149         nInputPorts++;
150
151     ssSetNumContStates(S, 0);
152     ssSetNumDiscStates(S, 0);
153
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);
160     }
161     /*
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.
166      */
167
168     if (!ssSetNumOutputPorts(S, 1)) return;
169     ssSetOutputPortWidth(S, 0, 1);
170     ssSetOutputPortDataType(S, 0, SS_INT32);
171
172     ssSetNumSampleTimes(S, 1);
173     ssSetNumRWork(S, 0);
174     ssSetNumIWork(S, IWORK_COUNT);
175     ssSetNumPWork(S, 0);
176     ssSetNumModes(S, 0);
177     ssSetNumNonsampledZCs(S, 0);
178
179     /* Specify the sim state compliance to be same as a built-in block */
180     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
181
182     ssSetOptions(S, 0);
183 }
184
185
186
187 /* Function: mdlInitializeSampleTimes =========================================
188  * Abstract:
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.
192  */
193 static void mdlInitializeSampleTimes(SimStruct *S)
194 {
195     if (PRM_TS(S) == -1) {
196         ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
197         ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
198     } else {
199         ssSetSampleTime(S, 0, PRM_TS(S));
200         ssSetOffsetTime(S, 0, 0.0);
201     }
202 }
203
204
205
206 #define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
207 #if defined(MDL_INITIALIZE_CONDITIONS)
208   /* Function: mdlInitializeConditions ========================================
209    * Abstract:
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.
218    */
219 static void mdlInitializeConditions(SimStruct *S)
220 {
221     int_T shift;
222     int_T mask;
223     int_T reset_mode;
224     int_T reset_mode_save;
225
226     if (mf624_check(S) != 0)
227         return;
228
229     if (PRM_RESET_AT_STARTUP(S)) {
230         shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
231
232         mask = IRCCTRL_IRC0RESET_mask << shift;
233         reset_mode = __val2mfld(IRCCTRL_IRC0RESET_mask, IRCCTRL_RESET_ALWAYS) << shift;
234
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);
238     }
239 }
240 #endif /* MDL_INITIALIZE_CONDITIONS */
241
242
243
244 #define MDL_START  /* Change to #undef to remove function */
245 #if defined(MDL_START)
246   /* Function: mdlStart =======================================================
247    * Abstract:
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
250    *    to do it.
251    */
252 static void mdlStart(SimStruct *S)
253 {
254     int_T shift;
255     int_T mode;
256     int_T filter;
257     int_T mask;
258
259     if (mf624_init(NULL) != 0)
260         return;
261
262     mask = IRCCTRL_IRC0MODE_mask | IRCCTRL_IRC0COUNT_mask |
263            IRCCTRL_IRC0RESET_mask | IRCCTRL_IRC0FILTER_mask;
264
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;
270
271     mode = PRM_COUNTER_MODE(S);
272     filter = PRM_DIGITAL_FILTER(S);
273
274     IWORK_IRCCTRL(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);
281
282     shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
283
284     IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
285
286     mdlInitializeConditions(S);
287 }
288 #endif /*  MDL_START */
289
290
291
292 /* Function: mdlOutputs =======================================================
293  * Abstract:
294  *    In this function, you compute the outputs of your S-function
295  *    block.
296  */
297 static void mdlOutputs(SimStruct *S, int_T tid)
298 {
299     int32_T *y = ssGetOutputPortSignal(S,0);
300
301     if (mf624_check(S) != 0)
302         return;
303
304     y[0] = mf624_read32(MFST2REG(mfst, 4, irc_channel2reg[IWORK_CHANNEL(S)]));
305 }
306
307
308
309 #define MDL_UPDATE  /* Change to #undef to remove function */
310 #if defined(MDL_UPDATE)
311   /* Function: mdlUpdate ======================================================
312    * Abstract:
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
316    *    integration step.
317    */
318 static void mdlUpdate(SimStruct *S, int_T tid)
319 {
320     int_T mask = 0;
321     int_T shift;
322     int_T inp_num = 0;
323
324     if (mf624_check(S) != 0)
325         return;
326
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;
334         }
335         inp_num++;
336     }
337
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;
345         }
346         inp_num++;
347     }
348
349     if (mask) {
350         shift = IWORK_CHANNEL(S) * IRCCTRL_CHANNEL_SHIFT;
351         IRC_mode_change(mfst, mask << shift, IWORK_IRCCTRL(S) << shift);
352     }
353 }
354 #endif /* MDL_UPDATE */
355
356
357
358 #undef MDL_DERIVATIVES  /* Change to #undef to remove function */
359 #if defined(MDL_DERIVATIVES)
360   /* Function: mdlDerivatives =================================================
361    * Abstract:
362    *    In this function, you compute the S-function block's derivatives.
363    *    The derivatives are placed in the derivative vector, ssGetdX(S).
364    */
365   static void mdlDerivatives(SimStruct *S)
366   {
367   }
368 #endif /* MDL_DERIVATIVES */
369
370
371
372 /* Function: mdlTerminate =====================================================
373  * Abstract:
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.
377  */
378 static void mdlTerminate(SimStruct *S)
379 {
380     mf624_done();
381 }
382
383
384 /*======================================================*
385  * See sfuntmpl_doc.c for the optional S-function methods *
386  *======================================================*/
387
388 /*=============================*
389  * Required S-function trailer *
390  *=============================*/
391
392 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
393 #include "simulink.c"      /* MEX-file interface mechanism */
394 #else
395 #include "cg_sfun.h"       /* Code generation registration function */
396 #endif