2 #define S_FUNCTION_NAME sfReadPWM
3 #define S_FUNCTION_LEVEL 2
5 #define PERIOD_PRM(S) (mxGetScalar(ssGetSFcnParam(S, 0)))
6 #define CTRX_MODE 2083 //=100000100011, count up, repeat, outpu low,gate by ctrIn
7 #define CTR4_MODE 35 //100011, count upm, repeat,output low
8 #define CTR_START ((1 << 0) | (1 << 6) | (1 << 12) | (1 << 24)) //start 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, 1); /* 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, 1)) return;
66 ssSetInputPortWidth(S, 0, 1);
67 ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
69 * Set direct feedthrough flag (1=yes, 0=no).
70 * A port has direct feedthrough if the input is used in either
71 * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
72 * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
74 ssSetInputPortDirectFeedThrough(S, 0, 1);
76 if (!ssSetNumOutputPorts(S, 3)) return;
77 ssSetOutputPortWidth(S, 0, 1);
78 ssSetOutputPortWidth(S, 1, 1);
79 ssSetOutputPortWidth(S, 2, 1);
81 ssSetNumSampleTimes(S, 1);
86 ssSetNumNonsampledZCs(S, 0);
88 /* Specify the sim state compliance to be same as a built-in block */
89 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
96 /* Function: mdlInitializeSampleTimes =========================================
98 * This function is used to specify the sample time(s) for your
99 * S-function. You must register the same number of sample times as
100 * specified in ssSetNumSampleTimes.
102 static void mdlInitializeSampleTimes(SimStruct *S)
104 ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
105 ssSetOffsetTime(S, 0, 0.0);
111 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
112 #if defined(MDL_INITIALIZE_CONDITIONS)
113 /* Function: mdlInitializeConditions ========================================
115 * In this function, you should initialize the continuous and discrete
116 * states for your S-function block. The initial states are placed
117 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
118 * You can also perform any other initialization activities that your
119 * S-function may require. Note, this routine will be called at the
120 * start of simulation and if it is present in an enabled subsystem
121 * configured to reset states, it will be call when the enabled subsystem
122 * restarts execution to reset the states.
124 static void mdlInitializeConditions(SimStruct *S)
127 #endif /* MDL_INITIALIZE_CONDITIONS */
131 #define MDL_START /* Change to #undef to remove function */
132 #if defined(MDL_START)
133 /* Function: mdlStart =======================================================
135 * This function is called once at start of model execution. If you
136 * have states that should be initialized once, this is the place
139 static void mdlStart(SimStruct *S)
142 #define BUFF_SMALL 32
143 mfst = malloc(sizeof(mf624_state_t));
144 char buff[BUFF_SMALL];
145 memset(buff, '\0', BUFF_SMALL);
146 mfst->uio_dev = "uio0";
148 strncat(buff, "/dev/", 5);
149 strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
151 mfst->device_fd = open_device(buff);
153 if (mfst->device_fd < 0) {
154 ssSetErrorStatus(S,"open failed");
158 if (mmap_regions(mfst) < 0) {
159 ssSetErrorStatus(S,"mmap_regions failed");
165 /*Configuration of desired counter modes*/
166 mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR0MODE));
167 mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR1MODE));
168 mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR2MODE));
169 mf624_write32(CTR4_MODE,MFST2REG(mfst,4,CTR4MODE));
171 /*Set reload values of ctrs 0,1,2,4 to 0 just to be sure*/
172 mf624_write32(0,MFST2REG(mfst,4,CTR0));
173 mf624_write32(0,MFST2REG(mfst,4,CTR1));
174 mf624_write32(0,MFST2REG(mfst,4,CTR2));
175 mf624_write32(0,MFST2REG(mfst,4,CTR4));
177 /*Read values from counters and initialize IWork values with them*/
178 ssSetIWorkValue(S,0,(uint)mf624_read32(MFST2REG(mfst,4,CTR0)));
179 ssSetIWorkValue(S,1,(uint)mf624_read32(MFST2REG(mfst,4,CTR1)));
180 ssSetIWorkValue(S,2,(uint)mf624_read32(MFST2REG(mfst,4,CTR2)));
181 ssSetIWorkValue(S,3,(uint)mf624_read32(MFST2REG(mfst,4,CTR4)));
183 /*Start counters 0,1,2, tehy are gated with their inputs so no worries about premature start*/
184 mf624_write32(CTR_START,MFST2REG(mfst,4,CTRXCTRL));
188 #endif /* MDL_START */
192 /* Function: mdlOutputs =======================================================
194 * In this function, you compute the outputs of your S-function
197 static void mdlOutputs(SimStruct *S, int_T tid)
199 const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
200 real_T *y0 = ssGetOutputPortSignal(S,0);
201 real_T *y1 = ssGetOutputPortSignal(S,1);
202 real_T *y2 = ssGetOutputPortSignal(S,2);
207 c0 = mf624_read32(MFST2REG(mfst,4,CTR0));
208 c1 = mf624_read32(MFST2REG(mfst,4,CTR1));
209 c2 = mf624_read32(MFST2REG(mfst,4,CTR2));
210 c4 = mf624_read32(MFST2REG(mfst,4,CTR4));
212 period = (uint)(c4-(uint)ssGetIWorkValue(S,3));
214 y0[0] = (c0-(uint)ssGetIWorkValue(S,0))/period;
215 y1[0] = (c1-(uint)ssGetIWorkValue(S,1))/period;
216 y2[0] = (c2-(uint)ssGetIWorkValue(S,2))/period;
218 ssSetIWorkValue(S,0,c0);
219 ssSetIWorkValue(S,1,c1);
220 ssSetIWorkValue(S,2,c2);
221 ssSetIWorkValue(S,3,c4);
226 #define MDL_UPDATE /* Change to #undef to remove function */
227 #if defined(MDL_UPDATE)
228 /* Function: mdlUpdate ======================================================
230 * This function is called once for every major integration time step.
231 * Discrete states are typically updated here, but this function is useful
232 * for performing any tasks that should only take place once per
235 static void mdlUpdate(SimStruct *S, int_T tid)
238 #endif /* MDL_UPDATE */
242 #define MDL_DERIVATIVES /* Change to #undef to remove function */
243 #if defined(MDL_DERIVATIVES)
244 /* Function: mdlDerivatives =================================================
246 * In this function, you compute the S-function block's derivatives.
247 * The derivatives are placed in the derivative vector, ssGetdX(S).
249 static void mdlDerivatives(SimStruct *S)
252 #endif /* MDL_DERIVATIVES */
256 /* Function: mdlTerminate =====================================================
258 * In this function, you should perform any actions that are necessary
259 * at the termination of a simulation. For example, if memory was
260 * allocated in mdlStart, this is the place to free it.
262 static void mdlTerminate(SimStruct *S)
270 /*======================================================*
271 * See sfuntmpl_doc.c for the optional S-function methods *
272 *======================================================*/
274 /*=============================*
275 * Required S-function trailer *
276 *=============================*/
278 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
279 #include "simulink.c" /* MEX-file interface mechanism */
281 #include "cg_sfun.h" /* Code generation registration function */