2 * S-function to measure external PWM signal duty cycle on Humusoft MF624 card
4 * Copyright (C) 2013 Michal Kreč <krecmich@fel.cvut.cz>
5 * Copyright (C) 2013 Michal Sojka <sojkam1@fel.cvut.cz>
7 * Department of Control Engineering
8 * Faculty of Electrical Engineering
9 * Czech Technical University in Prague (CTU)
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.
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
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/
30 * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
31 * required S-function structure.
34 #define S_FUNCTION_NAME sfReadPWM
35 #define S_FUNCTION_LEVEL 2
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
44 * Need to include simstruc.h for the definition of the SimStruct and
45 * its associated macro definitions.
50 #include "mf624_SIMULINK.h"
56 * You should use the following technique to report errors encountered within
59 * ssSetErrorStatus(S,"Error encountered due to ...");
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:
68 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
69 * sprintf(msg,"Error due to %s", string);
70 * ssSetErrorStatus(S,msg);
74 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
77 /*====================*
78 * S-function methods *
79 *====================*/
81 /* Function: mdlInitializeSizes ===============================================
83 * The sizes information is used by Simulink to determine the S-function
84 * block's characteristics (number of inputs, outputs, states, etc.).
86 static void mdlInitializeSizes(SimStruct *S)
88 /* See sfuntmpl_doc.c for more details on the macros below */
90 ssSetNumSFcnParams(S, 0); /* Number of expected parameters */
91 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
92 /* Return if number of expected != number of actual parameters */
96 ssSetNumContStates(S, 0);
97 ssSetNumDiscStates(S, 0);
99 if (!ssSetNumInputPorts(S, 0)) return;
102 if (!ssSetNumOutputPorts(S, 3)) return;
103 ssSetOutputPortWidth(S, 0, 1);
104 ssSetOutputPortWidth(S, 1, 1);
105 ssSetOutputPortWidth(S, 2, 1);
107 ssSetNumSampleTimes(S, 1);
112 ssSetNumNonsampledZCs(S, 0);
114 /* Specify the sim state compliance to be same as a built-in block */
115 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
122 /* Function: mdlInitializeSampleTimes =========================================
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.
128 static void mdlInitializeSampleTimes(SimStruct *S)
130 ssSetSampleTime(S, 0, 0.00005);
131 ssSetOffsetTime(S, 0, 0.0);
137 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
138 #if defined(MDL_INITIALIZE_CONDITIONS)
139 /* Function: mdlInitializeConditions ========================================
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.
150 static void mdlInitializeConditions(SimStruct *S)
153 #endif /* MDL_INITIALIZE_CONDITIONS */
157 #define MDL_START /* Change to #undef to remove function */
158 #if defined(MDL_START)
159 /* Function: mdlStart =======================================================
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
165 static void mdlStart(SimStruct *S)
168 if (mf624_init(NULL) != 0)
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));
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));
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)));
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*/
193 #endif /* MDL_START */
197 /* Function: mdlOutputs =======================================================
199 * In this function, you compute the outputs of your S-function
202 static void mdlOutputs(SimStruct *S, int_T tid)
204 real_T *y0 = ssGetOutputPortSignal(S,0);
205 real_T *y1 = ssGetOutputPortSignal(S,1);
206 real_T *y2 = ssGetOutputPortSignal(S,2);
208 unsigned int c0,c1,c2,c4;
210 if (mf624_check(S) != 0)
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));
218 period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3));
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;
224 ssSetIWorkValue(S,0,c0);
225 ssSetIWorkValue(S,1,c1);
226 ssSetIWorkValue(S,2,c2);
227 ssSetIWorkValue(S,3,c4);
229 y0[0] = y1[0] = y2[0] = 0;
230 #endif /*WITHOUT_HW*/
235 #define MDL_UPDATE /* Change to #undef to remove function */
236 #if defined(MDL_UPDATE)
237 /* Function: mdlUpdate ======================================================
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
244 static void mdlUpdate(SimStruct *S, int_T tid)
247 #endif /* MDL_UPDATE */
251 #define MDL_DERIVATIVES /* Change to #undef to remove function */
252 #if defined(MDL_DERIVATIVES)
253 /* Function: mdlDerivatives =================================================
255 * In this function, you compute the S-function block's derivatives.
256 * The derivatives are placed in the derivative vector, ssGetdX(S).
258 static void mdlDerivatives(SimStruct *S)
261 #endif /* MDL_DERIVATIVES */
265 /* Function: mdlTerminate =====================================================
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.
271 static void mdlTerminate(SimStruct *S)
274 if (mf624_check(NULL) != 0)
277 mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL_reg));
279 #endif /*WITHOUT_HW*/
283 /*======================================================*
284 * See sfuntmpl_doc.c for the optional S-function methods *
285 *======================================================*/
287 /*=============================*
288 * Required S-function trailer *
289 *=============================*/
291 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
292 #include "simulink.c" /* MEX-file interface mechanism */
294 #include "cg_sfun.h" /* Code generation registration function */