]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfReadPWM.c
Include license header to prepare code for publication.
[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 #include "mf624_SIMULINK.h"
50
51 /* Error handling
52  * --------------
53  *
54  * You should use the following technique to report errors encountered within
55  * an S-function:
56  *
57  *       ssSetErrorStatus(S,"Error encountered due to ...");
58  *       return;
59  *
60  * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
61  * It cannot be a local variable. For example the following will cause
62  * unpredictable errors:
63  *
64  *      mdlOutputs()
65  *      {
66  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
67  *         sprintf(msg,"Error due to %s", string);
68  *         ssSetErrorStatus(S,msg);
69  *         return;
70  *      }
71  *
72  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
73  */
74
75 /*====================*
76  * S-function methods *
77  *====================*/
78
79 /* Function: mdlInitializeSizes ===============================================
80  * Abstract:
81  *    The sizes information is used by Simulink to determine the S-function
82  *    block's characteristics (number of inputs, outputs, states, etc.).
83  */
84 static void mdlInitializeSizes(SimStruct *S)
85 {
86     /* See sfuntmpl_doc.c for more details on the macros below */
87
88     ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */
89     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
90         /* Return if number of expected != number of actual parameters */
91         return;
92     }
93
94     ssSetNumContStates(S, 0);
95     ssSetNumDiscStates(S, 0);
96
97     if (!ssSetNumInputPorts(S, 0)) return;
98     
99
100     if (!ssSetNumOutputPorts(S, 3)) return;
101     ssSetOutputPortWidth(S, 0, 1);
102     ssSetOutputPortWidth(S, 1, 1);
103     ssSetOutputPortWidth(S, 2, 1);
104
105     ssSetNumSampleTimes(S, 1);
106     ssSetNumRWork(S, 0);
107     ssSetNumIWork(S, 4);
108     ssSetNumPWork(S, 0);
109     ssSetNumModes(S, 0);
110     ssSetNumNonsampledZCs(S, 0);
111
112     /* Specify the sim state compliance to be same as a built-in block */
113     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
114
115     ssSetOptions(S, 0);
116 }
117
118
119
120 /* Function: mdlInitializeSampleTimes =========================================
121  * Abstract:
122  *    This function is used to specify the sample time(s) for your
123  *    S-function. You must register the same number of sample times as
124  *    specified in ssSetNumSampleTimes.
125  */
126 static void mdlInitializeSampleTimes(SimStruct *S)
127 {
128     ssSetSampleTime(S, 0, 0.00005);
129     ssSetOffsetTime(S, 0, 0.0);
130
131 }
132
133
134
135 #define 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     if (mf624_init(NULL) != 0)
166         return;
167             
168     /*Configuration of desired counter modes*/
169     mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR0MODE_reg));
170     mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR1MODE_reg));
171     mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR2MODE_reg));
172     mf624_write32(CTR4_MODE,MFST2REG(mfst,4,CTR4MODE_reg));
173     
174     /*Set reload values of ctrs 0,1,2,4 to 0 just to be sure*/
175     mf624_write32(0,MFST2REG(mfst,4,CTR0_reg));
176     mf624_write32(0,MFST2REG(mfst,4,CTR1_reg));
177     mf624_write32(0,MFST2REG(mfst,4,CTR2_reg));
178     mf624_write32(0,MFST2REG(mfst,4,CTR4_reg));
179     
180     /*Read values from counters and initialize IWork values with them*/
181     ssSetIWorkValue(S,0,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR0_reg)));
182     ssSetIWorkValue(S,1,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR1_reg)));
183     ssSetIWorkValue(S,2,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR2_reg)));
184     ssSetIWorkValue(S,3,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR4_reg)));
185     
186     /*Start counters 0,1,2, tehy are gated with their inputs so no worries about premature start*/
187     mf624_write32(CTR_START,MFST2REG(mfst,4,CTRXCTRL_reg));
188             
189     
190   }
191 #endif /*  MDL_START */
192
193
194
195 /* Function: mdlOutputs =======================================================
196  * Abstract:
197  *    In this function, you compute the outputs of your S-function
198  *    block.
199  */
200 static void mdlOutputs(SimStruct *S, int_T tid)
201 {
202     real_T       *y0 = ssGetOutputPortSignal(S,0);
203     real_T       *y1 = ssGetOutputPortSignal(S,1);
204     real_T       *y2 = ssGetOutputPortSignal(S,2);
205     unsigned int period;
206     unsigned int c0,c1,c2,c4;
207     
208     if (mf624_check(S) != 0)
209             return;
210         
211     c0 = mf624_read32(MFST2REG(mfst,4,CTR0_reg));
212     c1 = mf624_read32(MFST2REG(mfst,4,CTR1_reg));
213     c2 = mf624_read32(MFST2REG(mfst,4,CTR2_reg));
214     c4 = mf624_read32(MFST2REG(mfst,4,CTR4_reg));
215     
216     period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3));
217     
218     y0[0] = (real_T)(c0-(unsigned int)ssGetIWorkValue(S,0))/(real_T)period;
219     y1[0] = (real_T)(c1-(unsigned int)ssGetIWorkValue(S,1))/(real_T)period;
220     y2[0] = (real_T)(c2-(unsigned int)ssGetIWorkValue(S,2))/(real_T)period;
221     
222     ssSetIWorkValue(S,0,c0);
223     ssSetIWorkValue(S,1,c1);
224     ssSetIWorkValue(S,2,c2);
225     ssSetIWorkValue(S,3,c4);
226 }
227
228
229
230 #define MDL_UPDATE  /* Change to #undef to remove function */
231 #if defined(MDL_UPDATE)
232   /* Function: mdlUpdate ======================================================
233    * Abstract:
234    *    This function is called once for every major integration time step.
235    *    Discrete states are typically updated here, but this function is useful
236    *    for performing any tasks that should only take place once per
237    *    integration step.
238    */
239   static void mdlUpdate(SimStruct *S, int_T tid)
240   {
241   }
242 #endif /* MDL_UPDATE */
243
244
245
246 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
247 #if defined(MDL_DERIVATIVES)
248   /* Function: mdlDerivatives =================================================
249    * Abstract:
250    *    In this function, you compute the S-function block's derivatives.
251    *    The derivatives are placed in the derivative vector, ssGetdX(S).
252    */
253   static void mdlDerivatives(SimStruct *S)
254   {
255   }
256 #endif /* MDL_DERIVATIVES */
257
258
259
260 /* Function: mdlTerminate =====================================================
261  * Abstract:
262  *    In this function, you should perform any actions that are necessary
263  *    at the termination of a simulation.  For example, if memory was
264  *    allocated in mdlStart, this is the place to free it.
265  */
266 static void mdlTerminate(SimStruct *S)
267 {
268     if (mf624_check(NULL) != 0)
269             return;
270
271     mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL_reg));
272     mf624_done();
273 }
274
275
276 /*======================================================*
277  * See sfuntmpl_doc.c for the optional S-function methods *
278  *======================================================*/
279
280 /*=============================*
281  * Required S-function trailer *
282  *=============================*/
283
284 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
285 #include "simulink.c"      /* MEX-file interface mechanism */
286 #else
287 #include "cg_sfun.h"       /* Code generation registration function */
288 #endif