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