2 #define S_FUNCTION_NAME sfReadPWM
3 #define S_FUNCTION_LEVEL 2
5 #define CTRX_MODE 10275 //=10100000100011, count up, repeat, outpu low,gate by ctrIn, gate high
6 #define CTR4_MODE 35 //100011, count upm, repeat,output low
7 #define CTR_START ((1 << 0) | (1 << 6) | (1 << 12) | (1 << 24)) //start counters 0,1,2,4
8 #define CTR_STOP ((1 << 1) | (1 << 7) | (1 << 13) | (1 << 25)) //stop counters 0,1,2,4
9 #define CTRCLOCK 50000000
12 * Need to include simstruc.h for the definition of the SimStruct and
13 * its associated macro definitions.
17 #include "mf624_SIMULINK.h"
22 * You should use the following technique to report errors encountered within
25 * ssSetErrorStatus(S,"Error encountered due to ...");
28 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
29 * It cannot be a local variable. For example the following will cause
30 * unpredictable errors:
34 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
35 * sprintf(msg,"Error due to %s", string);
36 * ssSetErrorStatus(S,msg);
40 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
43 /*====================*
44 * S-function methods *
45 *====================*/
47 /* Function: mdlInitializeSizes ===============================================
49 * The sizes information is used by Simulink to determine the S-function
50 * block's characteristics (number of inputs, outputs, states, etc.).
52 static void mdlInitializeSizes(SimStruct *S)
54 /* See sfuntmpl_doc.c for more details on the macros below */
56 ssSetNumSFcnParams(S, 0); /* Number of expected parameters */
57 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
58 /* Return if number of expected != number of actual parameters */
62 ssSetNumContStates(S, 0);
63 ssSetNumDiscStates(S, 0);
65 if (!ssSetNumInputPorts(S, 0)) return;
68 if (!ssSetNumOutputPorts(S, 3)) return;
69 ssSetOutputPortWidth(S, 0, 1);
70 ssSetOutputPortWidth(S, 1, 1);
71 ssSetOutputPortWidth(S, 2, 1);
73 ssSetNumSampleTimes(S, 1);
78 ssSetNumNonsampledZCs(S, 0);
80 /* Specify the sim state compliance to be same as a built-in block */
81 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
88 /* Function: mdlInitializeSampleTimes =========================================
90 * This function is used to specify the sample time(s) for your
91 * S-function. You must register the same number of sample times as
92 * specified in ssSetNumSampleTimes.
94 static void mdlInitializeSampleTimes(SimStruct *S)
96 ssSetSampleTime(S, 0, 0.00005);
97 ssSetOffsetTime(S, 0, 0.0);
103 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
104 #if defined(MDL_INITIALIZE_CONDITIONS)
105 /* Function: mdlInitializeConditions ========================================
107 * In this function, you should initialize the continuous and discrete
108 * states for your S-function block. The initial states are placed
109 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
110 * You can also perform any other initialization activities that your
111 * S-function may require. Note, this routine will be called at the
112 * start of simulation and if it is present in an enabled subsystem
113 * configured to reset states, it will be call when the enabled subsystem
114 * restarts execution to reset the states.
116 static void mdlInitializeConditions(SimStruct *S)
119 #endif /* MDL_INITIALIZE_CONDITIONS */
123 #define MDL_START /* Change to #undef to remove function */
124 #if defined(MDL_START)
125 /* Function: mdlStart =======================================================
127 * This function is called once at start of model execution. If you
128 * have states that should be initialized once, this is the place
131 static void mdlStart(SimStruct *S)
133 if (mf624_init(NULL) != 0)
136 /*Configuration of desired counter modes*/
137 mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR0MODE_reg));
138 mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR1MODE_reg));
139 mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR2MODE_reg));
140 mf624_write32(CTR4_MODE,MFST2REG(mfst,4,CTR4MODE_reg));
142 /*Set reload values of ctrs 0,1,2,4 to 0 just to be sure*/
143 mf624_write32(0,MFST2REG(mfst,4,CTR0_reg));
144 mf624_write32(0,MFST2REG(mfst,4,CTR1_reg));
145 mf624_write32(0,MFST2REG(mfst,4,CTR2_reg));
146 mf624_write32(0,MFST2REG(mfst,4,CTR4_reg));
148 /*Read values from counters and initialize IWork values with them*/
149 ssSetIWorkValue(S,0,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR0_reg)));
150 ssSetIWorkValue(S,1,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR1_reg)));
151 ssSetIWorkValue(S,2,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR2_reg)));
152 ssSetIWorkValue(S,3,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR4_reg)));
154 /*Start counters 0,1,2, tehy are gated with their inputs so no worries about premature start*/
155 mf624_write32(CTR_START,MFST2REG(mfst,4,CTRXCTRL_reg));
159 #endif /* MDL_START */
163 /* Function: mdlOutputs =======================================================
165 * In this function, you compute the outputs of your S-function
168 static void mdlOutputs(SimStruct *S, int_T tid)
170 real_T *y0 = ssGetOutputPortSignal(S,0);
171 real_T *y1 = ssGetOutputPortSignal(S,1);
172 real_T *y2 = ssGetOutputPortSignal(S,2);
174 unsigned int c0,c1,c2,c4;
176 if (mf624_check(S) != 0)
179 c0 = mf624_read32(MFST2REG(mfst,4,CTR0_reg));
180 c1 = mf624_read32(MFST2REG(mfst,4,CTR1_reg));
181 c2 = mf624_read32(MFST2REG(mfst,4,CTR2_reg));
182 c4 = mf624_read32(MFST2REG(mfst,4,CTR4_reg));
184 period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3));
186 y0[0] = (real_T)(c0-(unsigned int)ssGetIWorkValue(S,0))/(real_T)period;
187 y1[0] = (real_T)(c1-(unsigned int)ssGetIWorkValue(S,1))/(real_T)period;
188 y2[0] = (real_T)(c2-(unsigned int)ssGetIWorkValue(S,2))/(real_T)period;
190 ssSetIWorkValue(S,0,c0);
191 ssSetIWorkValue(S,1,c1);
192 ssSetIWorkValue(S,2,c2);
193 ssSetIWorkValue(S,3,c4);
198 #define MDL_UPDATE /* Change to #undef to remove function */
199 #if defined(MDL_UPDATE)
200 /* Function: mdlUpdate ======================================================
202 * This function is called once for every major integration time step.
203 * Discrete states are typically updated here, but this function is useful
204 * for performing any tasks that should only take place once per
207 static void mdlUpdate(SimStruct *S, int_T tid)
210 #endif /* MDL_UPDATE */
214 #define MDL_DERIVATIVES /* Change to #undef to remove function */
215 #if defined(MDL_DERIVATIVES)
216 /* Function: mdlDerivatives =================================================
218 * In this function, you compute the S-function block's derivatives.
219 * The derivatives are placed in the derivative vector, ssGetdX(S).
221 static void mdlDerivatives(SimStruct *S)
224 #endif /* MDL_DERIVATIVES */
228 /* Function: mdlTerminate =====================================================
230 * In this function, you should perform any actions that are necessary
231 * at the termination of a simulation. For example, if memory was
232 * allocated in mdlStart, this is the place to free it.
234 static void mdlTerminate(SimStruct *S)
236 if (mf624_check(NULL) != 0)
239 mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL_reg));
244 /*======================================================*
245 * See sfuntmpl_doc.c for the optional S-function methods *
246 *======================================================*/
248 /*=============================*
249 * Required S-function trailer *
250 *=============================*/
252 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
253 #include "simulink.c" /* MEX-file interface mechanism */
255 #include "cg_sfun.h" /* Code generation registration function */