]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfReadPWM.c
Allow build of m624 AnalogInput by C89 compiler - i.e. MS Visual C.
[mf624-simulink.git] / sfReadPWM.c
1 /*
2  * S-function to measure external PWM signal duty cycle on Humusoft MF624 card
3  *
4  * Copyright (C) 2013 Michal Kreč <krecmich@fel.cvut.cz>
5  * Copyright (C) 2013 Michal Sojka <sojkam1@fel.cvut.cz>
6  *
7  * Department of Control Engineering
8  * Faculty of Electrical Engineering
9  * Czech Technical University in Prague (CTU)
10  *
11  * The S-Function for ERT Linux can be distributed in compliance
12  * with GNU General Public License (GPL) version 2 or later.
13  * Other licence can negotiated with CTU.
14  *
15  * Next exception is granted in addition to GPL.
16  * Instantiating or linking compiled version of this code
17  * to produce an application image/executable, does not
18  * by itself cause the resulting application image/executable
19  * to be covered by the GNU General Public License.
20  * This exception does not however invalidate any other reasons
21  * why the executable file might be covered by the GNU Public License.
22  * Publication of enhanced or derived S-function files is required
23  * although.
24  *
25  * Linux ERT code is available from
26  *    http://rtime.felk.cvut.cz/gitweb/ert_linux.git
27  * More CTU Linux target for Simulink components are available at
28  *    http://lintarget.sourceforge.net/
29  *
30  * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
31  * required S-function structure.
32  */
33
34 #define S_FUNCTION_NAME  sfReadPWM
35 #define S_FUNCTION_LEVEL 2
36
37 #define CTRX_MODE 10275 //=10100000100011, count up, repeat, outpu low,gate by ctrIn, gate high
38 #define CTR4_MODE 35 //100011, count upm, repeat,output low
39 #define CTR_START ((1 << 0) | (1 << 6) | (1 << 12) | (1 << 24)) //start counters 0,1,2,4
40 #define CTR_STOP  ((1 << 1) | (1 << 7) | (1 << 13) | (1 << 25)) //stop counters 0,1,2,4
41 #define CTRCLOCK 50000000
42
43 /*
44  * Need to include simstruc.h for the definition of the SimStruct and
45  * its associated macro definitions.
46  */
47 #include "simstruc.h"
48
49 #ifndef WITHOUT_HW
50 #include "mf624_SIMULINK.h"
51 #endif /*WITHOUT_HW*/
52
53 /* Error handling
54  * --------------
55  *
56  * You should use the following technique to report errors encountered within
57  * an S-function:
58  *
59  *       ssSetErrorStatus(S,"Error encountered due to ...");
60  *       return;
61  *
62  * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
63  * It cannot be a local variable. For example the following will cause
64  * unpredictable errors:
65  *
66  *      mdlOutputs()
67  *      {
68  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
69  *         sprintf(msg,"Error due to %s", string);
70  *         ssSetErrorStatus(S,msg);
71  *         return;
72  *      }
73  *
74  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
75  */
76
77 /*====================*
78  * S-function methods *
79  *====================*/
80
81 /* Function: mdlInitializeSizes ===============================================
82  * Abstract:
83  *    The sizes information is used by Simulink to determine the S-function
84  *    block's characteristics (number of inputs, outputs, states, etc.).
85  */
86 static void mdlInitializeSizes(SimStruct *S)
87 {
88     /* See sfuntmpl_doc.c for more details on the macros below */
89
90     ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */
91     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
92         /* Return if number of expected != number of actual parameters */
93         return;
94     }
95
96     ssSetNumContStates(S, 0);
97     ssSetNumDiscStates(S, 0);
98
99     if (!ssSetNumInputPorts(S, 0)) return;
100     
101
102     if (!ssSetNumOutputPorts(S, 3)) return;
103     ssSetOutputPortWidth(S, 0, 1);
104     ssSetOutputPortWidth(S, 1, 1);
105     ssSetOutputPortWidth(S, 2, 1);
106
107     ssSetNumSampleTimes(S, 1);
108     ssSetNumRWork(S, 0);
109     ssSetNumIWork(S, 4);
110     ssSetNumPWork(S, 0);
111     ssSetNumModes(S, 0);
112     ssSetNumNonsampledZCs(S, 0);
113
114     /* Specify the sim state compliance to be same as a built-in block */
115     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
116
117     ssSetOptions(S, 0);
118 }
119
120
121
122 /* Function: mdlInitializeSampleTimes =========================================
123  * Abstract:
124  *    This function is used to specify the sample time(s) for your
125  *    S-function. You must register the same number of sample times as
126  *    specified in ssSetNumSampleTimes.
127  */
128 static void mdlInitializeSampleTimes(SimStruct *S)
129 {
130     ssSetSampleTime(S, 0, 0.00005);
131     ssSetOffsetTime(S, 0, 0.0);
132
133 }
134
135
136
137 #define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
138 #if defined(MDL_INITIALIZE_CONDITIONS)
139   /* Function: mdlInitializeConditions ========================================
140    * Abstract:
141    *    In this function, you should initialize the continuous and discrete
142    *    states for your S-function block.  The initial states are placed
143    *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
144    *    You can also perform any other initialization activities that your
145    *    S-function may require. Note, this routine will be called at the
146    *    start of simulation and if it is present in an enabled subsystem
147    *    configured to reset states, it will be call when the enabled subsystem
148    *    restarts execution to reset the states.
149    */
150   static void mdlInitializeConditions(SimStruct *S)
151   {
152   }
153 #endif /* MDL_INITIALIZE_CONDITIONS */
154
155
156
157 #define MDL_START  /* Change to #undef to remove function */
158 #if defined(MDL_START) 
159   /* Function: mdlStart =======================================================
160    * Abstract:
161    *    This function is called once at start of model execution. If you
162    *    have states that should be initialized once, this is the place
163    *    to do it.
164    */
165   static void mdlStart(SimStruct *S)
166   {
167   #ifndef WITHOUT_HW
168     if (mf624_init(NULL) != 0)
169         return;
170             
171     /*Configuration of desired counter modes*/
172     mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR0MODE_reg));
173     mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR1MODE_reg));
174     mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR2MODE_reg));
175     mf624_write32(CTR4_MODE,MFST2REG(mfst,4,CTR4MODE_reg));
176     
177     /*Set reload values of ctrs 0,1,2,4 to 0 just to be sure*/
178     mf624_write32(0,MFST2REG(mfst,4,CTR0_reg));
179     mf624_write32(0,MFST2REG(mfst,4,CTR1_reg));
180     mf624_write32(0,MFST2REG(mfst,4,CTR2_reg));
181     mf624_write32(0,MFST2REG(mfst,4,CTR4_reg));
182     
183     /*Read values from counters and initialize IWork values with them*/
184     ssSetIWorkValue(S,0,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR0_reg)));
185     ssSetIWorkValue(S,1,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR1_reg)));
186     ssSetIWorkValue(S,2,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR2_reg)));
187     ssSetIWorkValue(S,3,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR4_reg)));
188     
189     /*Start counters 0,1,2, tehy are gated with their inputs so no worries about premature start*/
190     mf624_write32(CTR_START,MFST2REG(mfst,4,CTRXCTRL_reg));
191   #endif /*WITHOUT_HW*/
192   }
193 #endif /*  MDL_START */
194
195
196
197 /* Function: mdlOutputs =======================================================
198  * Abstract:
199  *    In this function, you compute the outputs of your S-function
200  *    block.
201  */
202 static void mdlOutputs(SimStruct *S, int_T tid)
203 {
204     real_T       *y0 = ssGetOutputPortSignal(S,0);
205     real_T       *y1 = ssGetOutputPortSignal(S,1);
206     real_T       *y2 = ssGetOutputPortSignal(S,2);
207     unsigned int period;
208     unsigned int c0,c1,c2,c4;
209   #ifndef WITHOUT_HW
210     if (mf624_check(S) != 0)
211         return;
212
213     c0 = mf624_read32(MFST2REG(mfst,4,CTR0_reg));
214     c1 = mf624_read32(MFST2REG(mfst,4,CTR1_reg));
215     c2 = mf624_read32(MFST2REG(mfst,4,CTR2_reg));
216     c4 = mf624_read32(MFST2REG(mfst,4,CTR4_reg));
217
218     period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3));
219
220     y0[0] = (real_T)(c0-(unsigned int)ssGetIWorkValue(S,0))/(real_T)period;
221     y1[0] = (real_T)(c1-(unsigned int)ssGetIWorkValue(S,1))/(real_T)period;
222     y2[0] = (real_T)(c2-(unsigned int)ssGetIWorkValue(S,2))/(real_T)period;
223
224     ssSetIWorkValue(S,0,c0);
225     ssSetIWorkValue(S,1,c1);
226     ssSetIWorkValue(S,2,c2);
227     ssSetIWorkValue(S,3,c4);
228   #else /*WITHOUT_HW*/
229     y0[0] = y1[0] = y2[0] = 0;
230   #endif /*WITHOUT_HW*/
231 }
232
233
234
235 #define MDL_UPDATE  /* Change to #undef to remove function */
236 #if defined(MDL_UPDATE)
237   /* Function: mdlUpdate ======================================================
238    * Abstract:
239    *    This function is called once for every major integration time step.
240    *    Discrete states are typically updated here, but this function is useful
241    *    for performing any tasks that should only take place once per
242    *    integration step.
243    */
244   static void mdlUpdate(SimStruct *S, int_T tid)
245   {
246   }
247 #endif /* MDL_UPDATE */
248
249
250
251 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
252 #if defined(MDL_DERIVATIVES)
253   /* Function: mdlDerivatives =================================================
254    * Abstract:
255    *    In this function, you compute the S-function block's derivatives.
256    *    The derivatives are placed in the derivative vector, ssGetdX(S).
257    */
258   static void mdlDerivatives(SimStruct *S)
259   {
260   }
261 #endif /* MDL_DERIVATIVES */
262
263
264
265 /* Function: mdlTerminate =====================================================
266  * Abstract:
267  *    In this function, you should perform any actions that are necessary
268  *    at the termination of a simulation.  For example, if memory was
269  *    allocated in mdlStart, this is the place to free it.
270  */
271 static void mdlTerminate(SimStruct *S)
272 {
273   #ifndef WITHOUT_HW
274     if (mf624_check(NULL) != 0)
275         return;
276
277     mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL_reg));
278     mf624_done();
279   #endif /*WITHOUT_HW*/
280 }
281
282
283 /*======================================================*
284  * See sfuntmpl_doc.c for the optional S-function methods *
285  *======================================================*/
286
287 /*=============================*
288  * Required S-function trailer *
289  *=============================*/
290
291 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
292 #include "simulink.c"      /* MEX-file interface mechanism */
293 #else
294 #include "cg_sfun.h"       /* Code generation registration function */
295 #endif