]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfReadPWM.c
Remove code duplication
[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 (mf624_init(S) != 0)
134         return;
135             
136     /*Configuration of desired counter modes*/
137     mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR0MODE));
138     mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR1MODE));
139     mf624_write32(CTRX_MODE,MFST2REG(mfst,4,CTR2MODE));
140     mf624_write32(CTR4_MODE,MFST2REG(mfst,4,CTR4MODE));
141     
142     /*Set reload values of ctrs 0,1,2,4 to 0 just to be sure*/
143     mf624_write32(0,MFST2REG(mfst,4,CTR0));
144     mf624_write32(0,MFST2REG(mfst,4,CTR1));
145     mf624_write32(0,MFST2REG(mfst,4,CTR2));
146     mf624_write32(0,MFST2REG(mfst,4,CTR4));
147     
148     /*Read values from counters and initialize IWork values with them*/
149     ssSetIWorkValue(S,0,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR0)));
150     ssSetIWorkValue(S,1,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR1)));
151     ssSetIWorkValue(S,2,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR2)));
152     ssSetIWorkValue(S,3,(unsigned int)mf624_read32(MFST2REG(mfst,4,CTR4)));
153     
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));
156             
157     
158   }
159 #endif /*  MDL_START */
160
161
162
163 /* Function: mdlOutputs =======================================================
164  * Abstract:
165  *    In this function, you compute the outputs of your S-function
166  *    block.
167  */
168 static void mdlOutputs(SimStruct *S, int_T tid)
169 {
170     real_T       *y0 = ssGetOutputPortSignal(S,0);
171     real_T       *y1 = ssGetOutputPortSignal(S,1);
172     real_T       *y2 = ssGetOutputPortSignal(S,2);
173     unsigned int period;
174     unsigned int c0,c1,c2,c4;
175     
176     
177     c0 = mf624_read32(MFST2REG(mfst,4,CTR0));
178     c1 = mf624_read32(MFST2REG(mfst,4,CTR1));
179     c2 = mf624_read32(MFST2REG(mfst,4,CTR2));
180     c4 = mf624_read32(MFST2REG(mfst,4,CTR4));
181     
182     period = (unsigned int)(c4-(unsigned int)ssGetIWorkValue(S,3));
183     
184     y0[0] = (real_T)(c0-(unsigned int)ssGetIWorkValue(S,0))/(real_T)period;
185     y1[0] = (real_T)(c1-(unsigned int)ssGetIWorkValue(S,1))/(real_T)period;
186     y2[0] = (real_T)(c2-(unsigned int)ssGetIWorkValue(S,2))/(real_T)period;
187     
188     ssSetIWorkValue(S,0,c0);
189     ssSetIWorkValue(S,1,c1);
190     ssSetIWorkValue(S,2,c2);
191     ssSetIWorkValue(S,3,c4);
192 }
193
194
195
196 #define MDL_UPDATE  /* Change to #undef to remove function */
197 #if defined(MDL_UPDATE)
198   /* Function: mdlUpdate ======================================================
199    * Abstract:
200    *    This function is called once for every major integration time step.
201    *    Discrete states are typically updated here, but this function is useful
202    *    for performing any tasks that should only take place once per
203    *    integration step.
204    */
205   static void mdlUpdate(SimStruct *S, int_T tid)
206   {
207   }
208 #endif /* MDL_UPDATE */
209
210
211
212 #define MDL_DERIVATIVES  /* Change to #undef to remove function */
213 #if defined(MDL_DERIVATIVES)
214   /* Function: mdlDerivatives =================================================
215    * Abstract:
216    *    In this function, you compute the S-function block's derivatives.
217    *    The derivatives are placed in the derivative vector, ssGetdX(S).
218    */
219   static void mdlDerivatives(SimStruct *S)
220   {
221   }
222 #endif /* MDL_DERIVATIVES */
223
224
225
226 /* Function: mdlTerminate =====================================================
227  * Abstract:
228  *    In this function, you should perform any actions that are necessary
229  *    at the termination of a simulation.  For example, if memory was
230  *    allocated in mdlStart, this is the place to free it.
231  */
232 static void mdlTerminate(SimStruct *S)
233 {
234     /* FIXME: This is ugly! */
235     if(mfst!=NULL){
236         mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL));
237         free(mfst);
238         mfst=NULL;
239     } else {
240         mfst = malloc(sizeof(mf624_state_t));
241         if (mf624_init(S) != 0)
242                 return;
243         mf624_write32(CTR_STOP,MFST2REG(mfst,4,CTRXCTRL));
244         free(mfst);
245         mfst=NULL;
246     }
247 }
248
249
250 /*======================================================*
251  * See sfuntmpl_doc.c for the optional S-function methods *
252  *======================================================*/
253
254 /*=============================*
255  * Required S-function trailer *
256  *=============================*/
257
258 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
259 #include "simulink.c"      /* MEX-file interface mechanism */
260 #else
261 #include "cg_sfun.h"       /* Code generation registration function */
262 #endif