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.
49 #include "mf624_SIMULINK.h"
54 * You should use the following technique to report errors encountered within
57 * ssSetErrorStatus(S,"Error encountered due to ...");
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:
66 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
67 * sprintf(msg,"Error due to %s", string);
68 * ssSetErrorStatus(S,msg);
72 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
75 /*====================*
76 * S-function methods *
77 *====================*/
79 /* Function: mdlInitializeSizes ===============================================
81 * The sizes information is used by Simulink to determine the S-function
82 * block's characteristics (number of inputs, outputs, states, etc.).
84 static void mdlInitializeSizes(SimStruct *S)
86 /* See sfuntmpl_doc.c for more details on the macros below */
88 ssSetNumSFcnParams(S, 0); /* Number of expected parameters */
89 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
90 /* Return if number of expected != number of actual parameters */
94 ssSetNumContStates(S, 0);
95 ssSetNumDiscStates(S, 0);
97 if (!ssSetNumInputPorts(S, 0)) return;
100 if (!ssSetNumOutputPorts(S, 3)) return;
101 ssSetOutputPortWidth(S, 0, 1);
102 ssSetOutputPortWidth(S, 1, 1);
103 ssSetOutputPortWidth(S, 2, 1);
105 ssSetNumSampleTimes(S, 1);
110 ssSetNumNonsampledZCs(S, 0);
112 /* Specify the sim state compliance to be same as a built-in block */
113 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
120 /* Function: mdlInitializeSampleTimes =========================================
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.
126 static void mdlInitializeSampleTimes(SimStruct *S)
128 ssSetSampleTime(S, 0, 0.00005);
129 ssSetOffsetTime(S, 0, 0.0);
135 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
136 #if defined(MDL_INITIALIZE_CONDITIONS)
137 /* Function: mdlInitializeConditions ========================================
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.
148 static void mdlInitializeConditions(SimStruct *S)
151 #endif /* MDL_INITIALIZE_CONDITIONS */
155 #define MDL_START /* Change to #undef to remove function */
156 #if defined(MDL_START)
157 /* Function: mdlStart =======================================================
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
163 static void mdlStart(SimStruct *S)
165 if (mf624_init(NULL) != 0)
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));
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));
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)));
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));
191 #endif /* MDL_START */
195 /* Function: mdlOutputs =======================================================
197 * In this function, you compute the outputs of your S-function
200 static void mdlOutputs(SimStruct *S, int_T tid)
202 real_T *y0 = ssGetOutputPortSignal(S,0);
203 real_T *y1 = ssGetOutputPortSignal(S,1);
204 real_T *y2 = ssGetOutputPortSignal(S,2);
206 unsigned int c0,c1,c2,c4;
208 if (mf624_check(S) != 0)
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));
216 period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3));
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;
222 ssSetIWorkValue(S,0,c0);
223 ssSetIWorkValue(S,1,c1);
224 ssSetIWorkValue(S,2,c2);
225 ssSetIWorkValue(S,3,c4);
230 #define MDL_UPDATE /* Change to #undef to remove function */
231 #if defined(MDL_UPDATE)
232 /* Function: mdlUpdate ======================================================
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
239 static void mdlUpdate(SimStruct *S, int_T tid)
242 #endif /* MDL_UPDATE */
246 #define MDL_DERIVATIVES /* Change to #undef to remove function */
247 #if defined(MDL_DERIVATIVES)
248 /* Function: mdlDerivatives =================================================
250 * In this function, you compute the S-function block's derivatives.
251 * The derivatives are placed in the derivative vector, ssGetdX(S).
253 static void mdlDerivatives(SimStruct *S)
256 #endif /* MDL_DERIVATIVES */
260 /* Function: mdlTerminate =====================================================
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.
266 static void mdlTerminate(SimStruct *S)
268 if (mf624_check(NULL) != 0)
271 mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL_reg));
276 /*======================================================*
277 * See sfuntmpl_doc.c for the optional S-function methods *
278 *======================================================*/
280 /*=============================*
281 * Required S-function trailer *
282 *=============================*/
284 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
285 #include "simulink.c" /* MEX-file interface mechanism */
287 #include "cg_sfun.h" /* Code generation registration function */