]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfAnalogInput.c
Include license header to prepare code for publication.
[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 "mf624_SIMULINK.h"
50
51
52 /* Error handling
53  * --------------
54  *
55  * You should use the following technique to report errors encountered within
56  * an S-function:
57  *
58  *       ssSetErrorStatus(S,"Error encountered due to ...");
59  *       return;
60  *
61  * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
62  * It cannot be a local variable. For example the following will cause
63  * unpredictable errors:
64  *
65  *      mdlOutputs()
66  *      {
67  *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
68  *         sprintf(msg,"Error due to %s", string);
69  *         ssSetErrorStatus(S,msg);
70  *         return;
71  *      }
72  *
73  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
74  */
75
76 /*====================*
77  * S-function methods *
78  *====================*/
79
80 /* Function: mdlInitializeSizes ===============================================
81  * Abstract:
82  *    The sizes information is used by Simulink to determine the S-function
83  *    block's characteristics (number of inputs, outputs, states, etc.).
84  */
85 static void mdlInitializeSizes(SimStruct *S)
86 {
87     /* See sfuntmpl_doc.c for more details on the macros below */
88
89     ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
90     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
91         /* Return if number of expected != number of actual parameters */
92         ssSetErrorStatus(S,"Parameter mismatch");
93         return;
94     }
95
96     ssSetNumContStates(S, 0);
97     ssSetNumDiscStates(S, 0);
98     
99     int ADCCMask = (int)MASK_PRM(S);
100     int i;
101     if(ADCCMask > 255 || ADCCMask < 0) {
102         ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
103     }
104     
105     int ADCChannels = __builtin_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
106         
107     
108     if (!ssSetNumInputPorts(S, 0)) return;
109     
110     
111
112     if (!ssSetNumOutputPorts(S, ADCChannels)) return;
113     for(i=0; i < ADCChannels;i++){
114         ssSetOutputPortWidth(S, i, 1);
115     }
116     ssSetNumSampleTimes(S, 1);
117     ssSetNumRWork(S, 0);
118     ssSetNumIWork(S, 1);
119     ssSetNumPWork(S, 0);
120     ssSetNumModes(S, 0);
121     ssSetNumNonsampledZCs(S, 0);
122
123     /* Specify the sim state compliance to be same as a built-in block */
124     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
125
126     ssSetOptions(S, 0);
127     
128
129 }
130
131
132
133 /* Function: mdlInitializeSampleTimes =========================================
134  * Abstract:
135  *    This function is used to specify the sample time(s) for your
136  *    S-function. You must register the same number of sample times as
137  *    specified in ssSetNumSampleTimes.
138  */
139 static void mdlInitializeSampleTimes(SimStruct *S)
140 {
141     ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
142     ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
143
144 }
145
146
147
148 #undef MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
149 #if defined(MDL_INITIALIZE_CONDITIONS)
150   /* Function: mdlInitializeConditions ========================================
151    * Abstract:
152    *    In this function, you should initialize the continuous and discrete
153    *    states for your S-function block.  The initial states are placed
154    *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
155    *    You can also perform any other initialization activities that your
156    *    S-function may require. Note, this routine will be called at the
157    *    start of simulation and if it is present in an enabled subsystem
158    *    configured to reset states, it will be call when the enabled subsystem
159    *    restarts execution to reset the states.
160    */
161   static void mdlInitializeConditions(SimStruct *S)
162   {
163   }
164 #endif /* MDL_INITIALIZE_CONDITIONS */
165
166
167
168 #define MDL_START  /* Change to #undef to remove function */
169 #if defined(MDL_START) 
170   /* Function: mdlStart =======================================================
171    * Abstract:
172    *    This function is called once at start of model execution. If you
173    *    have states that should be initialized once, this is the place
174    *    to do it.
175    */
176   static void mdlStart(SimStruct *S)
177   {
178     if (mf624_init(NULL) != 0)
179         return;
180             
181     int ADCCMask = (int)MASK_PRM(S);
182
183     if(ADCCMask > 255 || ADCCMask < 0) {
184         ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
185     }
186     
187     int ADCChannels = __builtin_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
188     
189     ssSetIWorkValue(S, 0, ADCChannels);
190     
191     mfst->ADC_enabled = ADCCMask;
192     mfst->ADC_enabled &= 0xFF;
193     
194     mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg));
195     
196     
197     
198   }
199 #endif /*  MDL_START */
200
201
202
203 /* Function: mdlOutputs =======================================================
204  * Abstract:
205  *    In this function, you compute the outputs of your S-function
206  *    block.
207  */
208 static void mdlOutputs(SimStruct *S, int_T tid)
209 {
210
211     int ADCChannels = ssGetIWorkValue(S, 0);
212     real_T*       y[ADCChannels];
213     int i;
214     int res,res1;
215     
216     if (mf624_check(S) != 0)
217             return;
218
219     // Activate trigger to start conversion
220         mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
221     
222     for(i=0; i < ADCChannels;i++){
223         y[i]=ssGetOutputPortSignal(S,i);
224     }
225     
226     // Check if conversion has finished
227         while((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg)) & GPIOC_EOLC_mask)) { 
228                 for (i = 0; i < 1000; i++) {} // small wait
229         }
230     
231     
232      for(i=1; i < ADCChannels;i+=2){
233         res = mf624_read32(MFST2REG(mfst, 2, ADDATA0_reg));
234         res1= res >> 16;
235         res = res & 0xFFFF;
236         *y[i-1] = (real_T) (10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
237         *y[i] = (real_T) (10.0 * ((int16_t) (res1 << 2)) / (double) 0x8000);
238     }
239     
240     if(i == ADCChannels){
241         res = mf624_read16(MFST2REG(mfst, 2, ADDATA0_reg));
242         *y[ADCChannels-1]=(real_T)(10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
243     }
244 }
245
246
247
248 #undef MDL_UPDATE  /* Change to #undef to remove function */
249 #if defined(MDL_UPDATE)
250   /* Function: mdlUpdate ======================================================
251    * Abstract:
252    *    This function is called once for every major integration time step.
253    *    Discrete states are typically updated here, but this function is useful
254    *    for performing any tasks that should only take place once per
255    *    integration step.
256    */
257   static void mdlUpdate(SimStruct *S, int_T tid)
258   {
259   }
260 #endif /* MDL_UPDATE */
261
262
263
264 #undef MDL_DERIVATIVES  /* Change to #undef to remove function */
265 #if defined(MDL_DERIVATIVES)
266   /* Function: mdlDerivatives =================================================
267    * Abstract:
268    *    In this function, you compute the S-function block's derivatives.
269    *    The derivatives are placed in the derivative vector, ssGetdX(S).
270    */
271   static void mdlDerivatives(SimStruct *S)
272   {
273   }
274 #endif /* MDL_DERIVATIVES */
275
276
277
278 /* Function: mdlTerminate =====================================================
279  * Abstract:
280  *    In this function, you should perform any actions that are necessary
281  *    at the termination of a simulation.  For example, if memory was
282  *    allocated in mdlStart, this is the place to free it.
283  */
284 static void mdlTerminate(SimStruct *S)
285 {
286     mf624_done(S);
287 }
288
289
290 /*======================================================*
291  * See sfuntmpl_doc.c for the optional S-function methods *
292  *======================================================*/
293
294 /*=============================*
295  * Required S-function trailer *
296  *=============================*/
297
298 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
299 #include "simulink.c"      /* MEX-file interface mechanism */
300 #else
301 #include "cg_sfun.h"       /* Code generation registration function */
302 #endif