]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfReadPWM.c
Changed gate mode from low to high.
[mf624-simulink.git] / sfReadPWM.c
1
2 #define S_FUNCTION_NAME  sfReadPWM
3 #define S_FUNCTION_LEVEL 2
4
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
10
11 /*
12  * Need to include simstruc.h for the definition of the SimStruct and
13  * its associated macro definitions.
14  */
15 #include "simstruc.h"
16
17 #include "mf624_SIMULINK.h"
18
19 /* Error handling
20  * --------------
21  *
22  * You should use the following technique to report errors encountered within
23  * an S-function:
24  *
25  *       ssSetErrorStatus(S,"Error encountered due to ...");
26  *       return;
27  *
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:
31  *
32  *      mdlOutputs()
33  *      {
34  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
35  *         sprintf(msg,"Error due to %s", string);
36  *         ssSetErrorStatus(S,msg);
37  *         return;
38  *      }
39  *
40  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
41  */
42
43 /*====================*
44  * S-function methods *
45  *====================*/
46
47 /* Function: mdlInitializeSizes ===============================================
48  * Abstract:
49  *    The sizes information is used by Simulink to determine the S-function
50  *    block's characteristics (number of inputs, outputs, states, etc.).
51  */
52 static void mdlInitializeSizes(SimStruct *S)
53 {
54     /* See sfuntmpl_doc.c for more details on the macros below */
55
56     ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */
57     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
58         /* Return if number of expected != number of actual parameters */
59         return;
60     }
61
62     ssSetNumContStates(S, 0);
63     ssSetNumDiscStates(S, 0);
64
65     if (!ssSetNumInputPorts(S, 0)) return;
66     
67
68     if (!ssSetNumOutputPorts(S, 3)) return;
69     ssSetOutputPortWidth(S, 0, 1);
70     ssSetOutputPortWidth(S, 1, 1);
71     ssSetOutputPortWidth(S, 2, 1);
72
73     ssSetNumSampleTimes(S, 1);
74     ssSetNumRWork(S, 0);
75     ssSetNumIWork(S, 4);
76     ssSetNumPWork(S, 0);
77     ssSetNumModes(S, 0);
78     ssSetNumNonsampledZCs(S, 0);
79
80     /* Specify the sim state compliance to be same as a built-in block */
81     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
82
83     ssSetOptions(S, 0);
84 }
85
86
87
88 /* Function: mdlInitializeSampleTimes =========================================
89  * Abstract:
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.
93  */
94 static void mdlInitializeSampleTimes(SimStruct *S)
95 {
96     ssSetSampleTime(S, 0, 0.00005);
97     ssSetOffsetTime(S, 0, 0.0);
98
99 }
100
101
102
103 #define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
104 #if defined(MDL_INITIALIZE_CONDITIONS)
105   /* Function: mdlInitializeConditions ========================================
106    * Abstract:
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.
115    */
116   static void mdlInitializeConditions(SimStruct *S)
117   {
118   }
119 #endif /* MDL_INITIALIZE_CONDITIONS */
120
121
122
123 #define MDL_START  /* Change to #undef to remove function */
124 #if defined(MDL_START) 
125   /* Function: mdlStart =======================================================
126    * Abstract:
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
129    *    to do it.
130    */
131   static void mdlStart(SimStruct *S)
132   {
133     if(mfst==NULL){
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";
139
140         strncat(buff, "/dev/", 5);
141         strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
142
143         mfst->device_fd = open_device(buff);
144     
145         if (mfst->device_fd < 0) {
146             ssSetErrorStatus(S,"open failed");
147             return;
148         }
149     
150         if (mmap_regions(mfst) < 0) {
151             ssSetErrorStatus(S,"mmap_regions failed");
152                 return;
153         }
154     
155     }
156             
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));
162     
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));
168     
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)));
174     
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));
177             
178     
179   }
180 #endif /*  MDL_START */
181
182
183
184 /* Function: mdlOutputs =======================================================
185  * Abstract:
186  *    In this function, you compute the outputs of your S-function
187  *    block.
188  */
189 static void mdlOutputs(SimStruct *S, int_T tid)
190 {
191     real_T       *y0 = ssGetOutputPortSignal(S,0);
192     real_T       *y1 = ssGetOutputPortSignal(S,1);
193     real_T       *y2 = ssGetOutputPortSignal(S,2);
194     unsigned int period;
195     unsigned int c0,c1,c2,c4;
196     
197     
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));
202     
203     period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3));
204     
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;
208     
209     ssSetIWorkValue(S,0,c0);
210     ssSetIWorkValue(S,1,c1);
211     ssSetIWorkValue(S,2,c2);
212     ssSetIWorkValue(S,3,c4);
213 }
214
215
216
217 #define MDL_UPDATE  /* Change to #undef to remove function */
218 #if defined(MDL_UPDATE)
219   /* Function: mdlUpdate ======================================================
220    * Abstract:
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
224    *    integration step.
225    */
226   static void mdlUpdate(SimStruct *S, int_T tid)
227   {
228   }
229 #endif /* MDL_UPDATE */
230
231
232
233 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
234 #if defined(MDL_DERIVATIVES)
235   /* Function: mdlDerivatives =================================================
236    * Abstract:
237    *    In this function, you compute the S-function block's derivatives.
238    *    The derivatives are placed in the derivative vector, ssGetdX(S).
239    */
240   static void mdlDerivatives(SimStruct *S)
241   {
242   }
243 #endif /* MDL_DERIVATIVES */
244
245
246
247 /* Function: mdlTerminate =====================================================
248  * Abstract:
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.
252  */
253 static void mdlTerminate(SimStruct *S)
254 {
255     if(mfst!=NULL){
256         mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL));
257         free(mfst);
258         mfst=NULL;
259     } else {
260         mfst = malloc(sizeof(mf624_state_t));
261         char buff[BUFF_SMALL];
262         memset(buff, '\0', BUFF_SMALL);
263         mfst->uio_dev = "uio0";
264
265         strncat(buff, "/dev/", 5);
266         strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
267
268         mfst->device_fd = open_device(buff);
269     
270         if (mfst->device_fd < 0) {
271             ssSetErrorStatus(S,"open failed");
272             return;
273         }
274     
275         if (mmap_regions(mfst) < 0) {
276             ssSetErrorStatus(S,"mmap_regions failed");
277                 return;
278         }
279         mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL));
280         free(mfst);
281         mfst=NULL;
282     }
283 }
284
285
286 /*======================================================*
287  * See sfuntmpl_doc.c for the optional S-function methods *
288  *======================================================*/
289
290 /*=============================*
291  * Required S-function trailer *
292  *=============================*/
293
294 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
295 #include "simulink.c"      /* MEX-file interface mechanism */
296 #else
297 #include "cg_sfun.h"       /* Code generation registration function */
298 #endif