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)
134 #define BUFF_SMALL 32
135 mfst = malloc(sizeof(mf624_state_t));
136 char buff[BUFF_SMALL];
137 memset(buff, '\0', BUFF_SMALL);
138 mfst->uio_dev = "uio0";
140 strncat(buff, "/dev/", 5);
141 strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
143 mfst->device_fd = open_device(buff);
145 if (mfst->device_fd < 0) {
146 ssSetErrorStatus(S,"open failed");
150 if (mmap_regions(mfst) < 0) {
151 ssSetErrorStatus(S,"mmap_regions failed");
157 /*Configuration of desired counter modes*/
158 mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR0MODE));
159 mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR1MODE));
160 mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR2MODE));
161 mf624_write32(CTR4_MODE,MFST2REG(mfst,4,CTR4MODE));
163 /*Set reload values of ctrs 0,1,2,4 to 0 just to be sure*/
164 mf624_write32(0,MFST2REG(mfst,4,CTR0));
165 mf624_write32(0,MFST2REG(mfst,4,CTR1));
166 mf624_write32(0,MFST2REG(mfst,4,CTR2));
167 mf624_write32(0,MFST2REG(mfst,4,CTR4));
169 /*Read values from counters and initialize IWork values with them*/
170 ssSetIWorkValue(S,0,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR0)));
171 ssSetIWorkValue(S,1,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR1)));
172 ssSetIWorkValue(S,2,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR2)));
173 ssSetIWorkValue(S,3,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR4)));
175 /*Start counters 0,1,2, tehy are gated with their inputs so no worries about premature start*/
176 mf624_write32(CTR_START,MFST2REG(mfst,4,CTRXCTRL));
180 #endif /* MDL_START */
184 /* Function: mdlOutputs =======================================================
186 * In this function, you compute the outputs of your S-function
189 static void mdlOutputs(SimStruct *S, int_T tid)
191 real_T *y0 = ssGetOutputPortSignal(S,0);
192 real_T *y1 = ssGetOutputPortSignal(S,1);
193 real_T *y2 = ssGetOutputPortSignal(S,2);
195 unsigned int c0,c1,c2,c4;
198 c0 = mf624_read32(MFST2REG(mfst,4,CTR0));
199 c1 = mf624_read32(MFST2REG(mfst,4,CTR1));
200 c2 = mf624_read32(MFST2REG(mfst,4,CTR2));
201 c4 = mf624_read32(MFST2REG(mfst,4,CTR4));
203 period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3));
205 y0[0] = (real_T)(c0-(unsigned int)ssGetIWorkValue(S,0))/(real_T)period;
206 y1[0] = (real_T)(c1-(unsigned int)ssGetIWorkValue(S,1))/(real_T)period;
207 y2[0] = (real_T)(c2-(unsigned int)ssGetIWorkValue(S,2))/(real_T)period;
209 ssSetIWorkValue(S,0,c0);
210 ssSetIWorkValue(S,1,c1);
211 ssSetIWorkValue(S,2,c2);
212 ssSetIWorkValue(S,3,c4);
217 #define MDL_UPDATE /* Change to #undef to remove function */
218 #if defined(MDL_UPDATE)
219 /* Function: mdlUpdate ======================================================
221 * This function is called once for every major integration time step.
222 * Discrete states are typically updated here, but this function is useful
223 * for performing any tasks that should only take place once per
226 static void mdlUpdate(SimStruct *S, int_T tid)
229 #endif /* MDL_UPDATE */
233 #define MDL_DERIVATIVES /* Change to #undef to remove function */
234 #if defined(MDL_DERIVATIVES)
235 /* Function: mdlDerivatives =================================================
237 * In this function, you compute the S-function block's derivatives.
238 * The derivatives are placed in the derivative vector, ssGetdX(S).
240 static void mdlDerivatives(SimStruct *S)
243 #endif /* MDL_DERIVATIVES */
247 /* Function: mdlTerminate =====================================================
249 * In this function, you should perform any actions that are necessary
250 * at the termination of a simulation. For example, if memory was
251 * allocated in mdlStart, this is the place to free it.
253 static void mdlTerminate(SimStruct *S)
256 mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL));
260 mfst = malloc(sizeof(mf624_state_t));
261 char buff[BUFF_SMALL];
262 memset(buff, '\0', BUFF_SMALL);
263 mfst->uio_dev = "uio0";
265 strncat(buff, "/dev/", 5);
266 strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
268 mfst->device_fd = open_device(buff);
270 if (mfst->device_fd < 0) {
271 ssSetErrorStatus(S,"open failed");
275 if (mmap_regions(mfst) < 0) {
276 ssSetErrorStatus(S,"mmap_regions failed");
279 mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL));
286 /*======================================================*
287 * See sfuntmpl_doc.c for the optional S-function methods *
288 *======================================================*/
290 /*=============================*
291 * Required S-function trailer *
292 *=============================*/
294 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
295 #include "simulink.c" /* MEX-file interface mechanism */
297 #include "cg_sfun.h" /* Code generation registration function */