]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfAnalogInput.c
70e4ca956d0307bfe47b5c5d30759567d9addfa6
[mf624-simulink.git] / sfAnalogInput.c
1 /*
2  * S-function to support analog inputs on Humusoft MF624 card
3  *
4  * Copyright (C) 2013 Michal Kreč <krecmich@fel.cvut.cz>
5  * Copyright (C) 2013 Michal Sojka <sojkam1@fel.cvut.cz>
6  *
7  * Department of Control Engineering
8  * Faculty of Electrical Engineering
9  * Czech Technical University in Prague (CTU)
10  *
11  * The S-Function for ERT Linux can be distributed in compliance
12  * with GNU General Public License (GPL) version 2 or later.
13  * Other licence can negotiated with CTU.
14  *
15  * Next exception is granted in addition to GPL.
16  * Instantiating or linking compiled version of this code
17  * to produce an application image/executable, does not
18  * by itself cause the resulting application image/executable
19  * to be covered by the GNU General Public License.
20  * This exception does not however invalidate any other reasons
21  * why the executable file might be covered by the GNU Public License.
22  * Publication of enhanced or derived S-function files is required
23  * although.
24  *
25  * Linux ERT code is available from
26  *    http://rtime.felk.cvut.cz/gitweb/ert_linux.git
27  * More CTU Linux target for Simulink components are available at
28  *    http://lintarget.sourceforge.net/
29  *
30  * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
31  * required S-function structure.
32  */
33
34 /*
35  * You must specify the S_FUNCTION_NAME as the name of your S-function
36  * (i.e. replace sfuntmpl_basic with the name of your S-function).
37  */
38
39 #define S_FUNCTION_NAME  sfAnalogInput
40 #define S_FUNCTION_LEVEL 2
41 #define MASK_PRM(S)     (mxGetScalar(ssGetSFcnParam(S, 0)))
42
43 /*
44  * Need to include simstruc.h for the definition of the SimStruct and
45  * its associated macro definitions.
46  */
47 #include "simstruc.h"
48
49 #include <stdint.h>
50
51 #ifndef WITHOUT_HW
52 #include "mf624_SIMULINK.h"
53 #endif /*WITHOUT_HW*/
54
55
56 /* Error handling
57  * --------------
58  *
59  * You should use the following technique to report errors encountered within
60  * an S-function:
61  *
62  *       ssSetErrorStatus(S,"Error encountered due to ...");
63  *       return;
64  *
65  * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
66  * It cannot be a local variable. For example the following will cause
67  * unpredictable errors:
68  *
69  *      mdlOutputs()
70  *      {
71  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
72  *         sprintf(msg,"Error due to %s", string);
73  *         ssSetErrorStatus(S,msg);
74  *         return;
75  *      }
76  *
77  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
78  */
79
80 /*====================*
81  * S-function methods *
82  *====================*/
83
84 /* Function: mdlInitializeSizes ===============================================
85  * Abstract:
86  *    The sizes information is used by Simulink to determine the S-function
87  *    block's characteristics (number of inputs, outputs, states, etc.).
88  */
89 static void mdlInitializeSizes(SimStruct *S)
90 {
91     /* See sfuntmpl_doc.c for more details on the macros below */
92
93     ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
94     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
95         /* Return if number of expected != number of actual parameters */
96         ssSetErrorStatus(S,"Parameter mismatch");
97         return;
98     }
99
100     ssSetNumContStates(S, 0);
101     ssSetNumDiscStates(S, 0);
102     
103     int ADCCMask = (int)MASK_PRM(S);
104     int i;
105     if(ADCCMask > 255 || ADCCMask < 0) {
106         ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
107     }
108     
109     int ADCChannels = __builtin_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
110         
111     
112     if (!ssSetNumInputPorts(S, 0)) return;
113     
114     
115
116     if (!ssSetNumOutputPorts(S, ADCChannels)) return;
117     for(i=0; i < ADCChannels;i++){
118         ssSetOutputPortWidth(S, i, 1);
119     }
120     ssSetNumSampleTimes(S, 1);
121     ssSetNumRWork(S, 0);
122     ssSetNumIWork(S, 1);
123     ssSetNumPWork(S, 0);
124     ssSetNumModes(S, 0);
125     ssSetNumNonsampledZCs(S, 0);
126
127     /* Specify the sim state compliance to be same as a built-in block */
128     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
129
130     ssSetOptions(S, 0);
131     
132
133 }
134
135
136
137 /* Function: mdlInitializeSampleTimes =========================================
138  * Abstract:
139  *    This function is used to specify the sample time(s) for your
140  *    S-function. You must register the same number of sample times as
141  *    specified in ssSetNumSampleTimes.
142  */
143 static void mdlInitializeSampleTimes(SimStruct *S)
144 {
145     ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
146     ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
147
148 }
149
150
151
152 #undef MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
153 #if defined(MDL_INITIALIZE_CONDITIONS)
154   /* Function: mdlInitializeConditions ========================================
155    * Abstract:
156    *    In this function, you should initialize the continuous and discrete
157    *    states for your S-function block.  The initial states are placed
158    *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
159    *    You can also perform any other initialization activities that your
160    *    S-function may require. Note, this routine will be called at the
161    *    start of simulation and if it is present in an enabled subsystem
162    *    configured to reset states, it will be call when the enabled subsystem
163    *    restarts execution to reset the states.
164    */
165   static void mdlInitializeConditions(SimStruct *S)
166   {
167   }
168 #endif /* MDL_INITIALIZE_CONDITIONS */
169
170
171
172 #define MDL_START  /* Change to #undef to remove function */
173 #if defined(MDL_START) 
174   /* Function: mdlStart =======================================================
175    * Abstract:
176    *    This function is called once at start of model execution. If you
177    *    have states that should be initialized once, this is the place
178    *    to do it.
179    */
180   static void mdlStart(SimStruct *S)
181   {
182
183   #ifndef WITHOUT_HW
184     if (mf624_init(NULL) != 0)
185         return;
186   #endif /*WITHOUT_HW*/
187
188     int ADCCMask = (int)MASK_PRM(S);
189
190     if(ADCCMask > 255 || ADCCMask < 0) {
191         ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
192     }
193
194     int ADCChannels = __builtin_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
195
196     ssSetIWorkValue(S, 0, ADCChannels);
197   #ifndef WITHOUT_HW
198     mfst->ADC_enabled = ADCCMask;
199     mfst->ADC_enabled &= 0xFF;
200
201     mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg));
202   #endif /*WITHOUT_HW*/
203   }
204 #endif /*  MDL_START */
205
206
207
208 /* Function: mdlOutputs =======================================================
209  * Abstract:
210  *    In this function, you compute the outputs of your S-function
211  *    block.
212  */
213 static void mdlOutputs(SimStruct *S, int_T tid)
214 {
215
216     int ADCChannels = ssGetIWorkValue(S, 0);
217     real_T*       y[ADCChannels];
218     int i;
219     int res,res1;
220   #ifndef WITHOUT_HW
221     if (mf624_check(S) != 0)
222         return;
223
224     // Activate trigger to start conversion
225     mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
226   #endif /*WITHOUT_HW*/
227
228     for(i=0; i < ADCChannels;i++){
229         y[i]=ssGetOutputPortSignal(S,i);
230     }
231
232   #ifndef WITHOUT_HW
233     // Check if conversion has finished
234     while((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg)) & GPIOC_EOLC_mask)) { 
235         for (i = 0; i < 1000; i++) {} // small wait
236     }
237
238     for(i=1; i < ADCChannels;i+=2){
239         res = mf624_read32(MFST2REG(mfst, 2, ADDATA0_reg));
240         res1= res >> 16;
241         res = res & 0xFFFF;
242         *y[i-1] = (real_T) (10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
243         *y[i] = (real_T) (10.0 * ((int16_t) (res1 << 2)) / (double) 0x8000);
244     }
245
246     if(i == ADCChannels){
247         res = mf624_read16(MFST2REG(mfst, 2, ADDATA0_reg));
248         *y[ADCChannels-1]=(real_T)(10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
249     }
250   #else /*WITHOUT_HW*/
251     for(i=0; i < ADCChannels; i++){
252         *y[i] = 0;
253     }
254   #endif /*WITHOUT_HW*/
255 }
256
257
258
259 #undef MDL_UPDATE  /* Change to #undef to remove function */
260 #if defined(MDL_UPDATE)
261   /* Function: mdlUpdate ======================================================
262    * Abstract:
263    *    This function is called once for every major integration time step.
264    *    Discrete states are typically updated here, but this function is useful
265    *    for performing any tasks that should only take place once per
266    *    integration step.
267    */
268   static void mdlUpdate(SimStruct *S, int_T tid)
269   {
270   }
271 #endif /* MDL_UPDATE */
272
273
274
275 #undef MDL_DERIVATIVES  /* Change to #undef to remove function */
276 #if defined(MDL_DERIVATIVES)
277   /* Function: mdlDerivatives =================================================
278    * Abstract:
279    *    In this function, you compute the S-function block's derivatives.
280    *    The derivatives are placed in the derivative vector, ssGetdX(S).
281    */
282   static void mdlDerivatives(SimStruct *S)
283   {
284   }
285 #endif /* MDL_DERIVATIVES */
286
287
288
289 /* Function: mdlTerminate =====================================================
290  * Abstract:
291  *    In this function, you should perform any actions that are necessary
292  *    at the termination of a simulation.  For example, if memory was
293  *    allocated in mdlStart, this is the place to free it.
294  */
295 static void mdlTerminate(SimStruct *S)
296 {
297   #ifndef WITHOUT_HW
298     mf624_done(S);
299   #endif /*WITHOUT_HW*/
300 }
301
302
303 /*======================================================*
304  * See sfuntmpl_doc.c for the optional S-function methods *
305  *======================================================*/
306
307 /*=============================*
308  * Required S-function trailer *
309  *=============================*/
310
311 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
312 #include "simulink.c"      /* MEX-file interface mechanism */
313 #else
314 #include "cg_sfun.h"       /* Code generation registration function */
315 #endif