]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfDigitalInput.c
Use correct header file for alloca for Linux build.
[mf624-simulink.git] / sfDigitalInput.c
1 /*
2  * S-function to support digital input 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 /*
35  * You must specify the S_FUNCTION_NAME as the name of your S-function
36  * (i.e. replace sfuntmpl_basic with the name of your S-function).
37  */
38
39 #define S_FUNCTION_NAME  sfDigitalInput
40 #define S_FUNCTION_LEVEL 2
41
42 #define CHNL_PRM(S)     (mxGetScalar(ssGetSFcnParam(S, 0)))
43
44 /*
45  * Need to include simstruc.h for the definition of the SimStruct and
46  * its associated macro definitions.
47  */
48 #include "simstruc.h"
49
50 #ifndef WITHOUT_HW
51 #include "mf624_SIMULINK.h"
52 #endif /*WITHOUT_HW*/
53
54 /* Error handling
55  * --------------
56  *
57  * You should use the following technique to report errors encountered within
58  * an S-function:
59  *
60  *       ssSetErrorStatus(S,"Error encountered due to ...");
61  *       return;
62  *
63  * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
64  * It cannot be a local variable. For example the following will cause
65  * unpredictable errors:
66  *
67  *      mdlOutputs()
68  *      {
69  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
70  *         sprintf(msg,"Error due to %s", string);
71  *         ssSetErrorStatus(S,msg);
72  *         return;
73  *      }
74  *
75  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
76  */
77
78 /*====================*
79  * S-function methods *
80  *====================*/
81
82 /* Function: mdlInitializeSizes ===============================================
83  * Abstract:
84  *    The sizes information is used by Simulink to determine the S-function
85  *    block's characteristics (number of inputs, outputs, states, etc.).
86  */
87 static void mdlInitializeSizes(SimStruct *S)
88 {
89     /* See sfuntmpl_doc.c for more details on the macros below */
90
91     ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
92     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
93         /* Return if number of expected != number of actual parameters */
94          ssSetErrorStatus(S,"Parameter count mismatch");
95         return;
96     }
97
98     ssSetNumContStates(S, 0);
99     ssSetNumDiscStates(S, 0);
100
101     if (!ssSetNumInputPorts(S, 0)) return;
102
103     if (!ssSetNumOutputPorts(S, 1)) return;
104     ssSetOutputPortWidth(S, 0, 1);
105
106     ssSetNumSampleTimes(S, 1);
107     ssSetNumRWork(S, 0);
108     ssSetNumIWork(S, 0);
109     ssSetNumPWork(S, 0);
110     ssSetNumModes(S, 0);
111     ssSetNumNonsampledZCs(S, 0);
112
113     /* Specify the sim state compliance to be same as a built-in block */
114     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
115
116     ssSetOptions(S, 0);
117 }
118
119
120
121 /* Function: mdlInitializeSampleTimes =========================================
122  * Abstract:
123  *    This function is used to specify the sample time(s) for your
124  *    S-function. You must register the same number of sample times as
125  *    specified in ssSetNumSampleTimes.
126  */
127 static void mdlInitializeSampleTimes(SimStruct *S)
128 {
129     ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
130     ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
131 }
132
133
134
135 #undef MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
136 #if defined(MDL_INITIALIZE_CONDITIONS)
137   /* Function: mdlInitializeConditions ========================================
138    * Abstract:
139    *    In this function, you should initialize the continuous and discrete
140    *    states for your S-function block.  The initial states are placed
141    *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
142    *    You can also perform any other initialization activities that your
143    *    S-function may require. Note, this routine will be called at the
144    *    start of simulation and if it is present in an enabled subsystem
145    *    configured to reset states, it will be call when the enabled subsystem
146    *    restarts execution to reset the states.
147    */
148   static void mdlInitializeConditions(SimStruct *S)
149   {
150   }
151 #endif /* MDL_INITIALIZE_CONDITIONS */
152
153
154
155 #define MDL_START  /* Change to #undef to remove function */
156 #if defined(MDL_START)
157   /* Function: mdlStart =======================================================
158    * Abstract:
159    *    This function is called once at start of model execution. If you
160    *    have states that should be initialized once, this is the place
161    *    to do it.
162    */
163   static void mdlStart(SimStruct *S)
164   {
165   #ifndef WITHOUT_HW
166     if (mf624_init(NULL) != 0)
167         return;
168   #endif /*WITHOUT_HW*/
169   }
170 #endif /*  MDL_START */
171
172
173
174 /* Function: mdlOutputs =======================================================
175  * Abstract:
176  *    In this function, you compute the outputs of your S-function
177  *    block.
178  */
179 static void mdlOutputs(SimStruct *S, int_T tid)
180 {
181     real_T *y;
182     uint_T din;
183
184     y = (real_T *)ssGetOutputPortSignal(S, 0);
185
186   #ifndef WITHOUT_HW
187     if (mf624_check(S) != 0)
188             return;
189
190     din = mf624_read16(MFST2REG(mfst, 2, DIN_reg));
191
192     *y = din & (1 << (int)(CHNL_PRM(S)-1)) ? 0.0 : 1.0;
193   #else /*WITHOUT_HW*/
194     *y = 0;
195   #endif /*WITHOUT_HW*/
196 }
197
198
199
200 #undef MDL_UPDATE  /* Change to #undef to remove function */
201 #if defined(MDL_UPDATE)
202   /* Function: mdlUpdate ======================================================
203    * Abstract:
204    *    This function is called once for every major integration time step.
205    *    Discrete states are typically updated here, but this function is useful
206    *    for performing any tasks that should only take place once per
207    *    integration step.
208    */
209   static void mdlUpdate(SimStruct *S, int_T tid)
210   {
211   }
212 #endif /* MDL_UPDATE */
213
214
215
216 #undef MDL_DERIVATIVES  /* Change to #undef to remove function */
217 #if defined(MDL_DERIVATIVES)
218   /* Function: mdlDerivatives =================================================
219    * Abstract:
220    *    In this function, you compute the S-function block's derivatives.
221    *    The derivatives are placed in the derivative vector, ssGetdX(S).
222    */
223   static void mdlDerivatives(SimStruct *S)
224   {
225   }
226 #endif /* MDL_DERIVATIVES */
227
228
229
230 /* Function: mdlTerminate =====================================================
231  * Abstract:
232  *    In this function, you should perform any actions that are necessary
233  *    at the termination of a simulation.  For example, if memory was
234  *    allocated in mdlStart, this is the place to free it.
235  */
236 static void mdlTerminate(SimStruct *S)
237 {
238   #ifndef WITHOUT_HW
239     mf624_done();
240   #endif /*WITHOUT_HW*/
241 }
242
243
244 /*======================================================*
245  * See sfuntmpl_doc.c for the optional S-function methods *
246  *======================================================*/
247
248 /*=============================*
249  * Required S-function trailer *
250  *=============================*/
251
252 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
253 #include "simulink.c"      /* MEX-file interface mechanism */
254 #else
255 #include "cg_sfun.h"       /* Code generation registration function */
256 #endif