]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfReadPWM.c
Created s-functions for DigitalOutputs and for reading PWM via counters.
[mf624-simulink.git] / sfReadPWM.c
1
2 #define S_FUNCTION_NAME  sfReadPWM
3 #define S_FUNCTION_LEVEL 2
4
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
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, 1);  /* 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, 1)) return;
66     ssSetInputPortWidth(S, 0, 1);
67     ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
68     /*
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.
73      */
74     ssSetInputPortDirectFeedThrough(S, 0, 1);
75
76     if (!ssSetNumOutputPorts(S, 3)) return;
77     ssSetOutputPortWidth(S, 0, 1);
78     ssSetOutputPortWidth(S, 1, 1);
79     ssSetOutputPortWidth(S, 2, 1);
80
81     ssSetNumSampleTimes(S, 1);
82     ssSetNumRWork(S, 0);
83     ssSetNumIWork(S, 4);
84     ssSetNumPWork(S, 0);
85     ssSetNumModes(S, 0);
86     ssSetNumNonsampledZCs(S, 0);
87
88     /* Specify the sim state compliance to be same as a built-in block */
89     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
90
91     ssSetOptions(S, 0);
92 }
93
94
95
96 /* Function: mdlInitializeSampleTimes =========================================
97  * Abstract:
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.
101  */
102 static void mdlInitializeSampleTimes(SimStruct *S)
103 {
104     ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
105     ssSetOffsetTime(S, 0, 0.0);
106
107 }
108
109
110
111 #define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
112 #if defined(MDL_INITIALIZE_CONDITIONS)
113   /* Function: mdlInitializeConditions ========================================
114    * Abstract:
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.
123    */
124   static void mdlInitializeConditions(SimStruct *S)
125   {
126   }
127 #endif /* MDL_INITIALIZE_CONDITIONS */
128
129
130
131 #define MDL_START  /* Change to #undef to remove function */
132 #if defined(MDL_START) 
133   /* Function: mdlStart =======================================================
134    * Abstract:
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
137    *    to do it.
138    */
139   static void mdlStart(SimStruct *S)
140   {
141     if(mfst==NULL){
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";
147
148         strncat(buff, "/dev/", 5);
149         strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
150
151         mfst->device_fd = open_device(buff);
152     
153         if (mfst->device_fd < 0) {
154             ssSetErrorStatus(S,"open failed");
155             return;
156         }
157     
158         if (mmap_regions(mfst) < 0) {
159             ssSetErrorStatus(S,"mmap_regions failed");
160                 return;
161         }
162     
163     }
164             
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));
170     
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));
176     
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)));
182     
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));
185             
186     
187   }
188 #endif /*  MDL_START */
189
190
191
192 /* Function: mdlOutputs =======================================================
193  * Abstract:
194  *    In this function, you compute the outputs of your S-function
195  *    block.
196  */
197 static void mdlOutputs(SimStruct *S, int_T tid)
198 {
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);
203     uint period;
204     uint c0,c1,c2,c4;
205     
206     
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));
211     
212     period = (uint)(c4-(uint)ssGetIWorkValue(S,3));
213     
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;
217     
218     ssSetIWorkValue(S,0,c0);
219     ssSetIWorkValue(S,1,c1);
220     ssSetIWorkValue(S,2,c2);
221     ssSetIWorkValue(S,3,c4);
222 }
223
224
225
226 #define MDL_UPDATE  /* Change to #undef to remove function */
227 #if defined(MDL_UPDATE)
228   /* Function: mdlUpdate ======================================================
229    * Abstract:
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
233    *    integration step.
234    */
235   static void mdlUpdate(SimStruct *S, int_T tid)
236   {
237   }
238 #endif /* MDL_UPDATE */
239
240
241
242 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
243 #if defined(MDL_DERIVATIVES)
244   /* Function: mdlDerivatives =================================================
245    * Abstract:
246    *    In this function, you compute the S-function block's derivatives.
247    *    The derivatives are placed in the derivative vector, ssGetdX(S).
248    */
249   static void mdlDerivatives(SimStruct *S)
250   {
251   }
252 #endif /* MDL_DERIVATIVES */
253
254
255
256 /* Function: mdlTerminate =====================================================
257  * Abstract:
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.
261  */
262 static void mdlTerminate(SimStruct *S)
263 {
264     if(mfst!=NULL){
265         free(mfst);
266     }
267 }
268
269
270 /*======================================================*
271  * See sfuntmpl_doc.c for the optional S-function methods *
272  *======================================================*/
273
274 /*=============================*
275  * Required S-function trailer *
276  *=============================*/
277
278 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
279 #include "simulink.c"      /* MEX-file interface mechanism */
280 #else
281 #include "cg_sfun.h"       /* Code generation registration function */
282 #endif