]> rtime.felk.cvut.cz Git - mf624-simulink.git/blob - sfAnalogInput.c
Created s-functions for DigitalOutputs and for reading PWM via counters.
[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((uint)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(mfst == NULL){
151         #define BUFF_SMALL              32
152         mfst = malloc(sizeof(mf624_state_t));
153         char buff[BUFF_SMALL];
154         memset(buff, '\0', BUFF_SMALL);
155         mfst->uio_dev = "uio0";
156
157         strncat(buff, "/dev/", 5);
158         strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
159
160         mfst->device_fd = open_device(buff);
161     
162         if (mfst->device_fd < 0) {
163             ssSetErrorStatus(S,"open failed");
164             return;
165         }
166     
167         if (mmap_regions(mfst) < 0) {
168             ssSetErrorStatus(S,"mmap_regions failed");
169             return;
170         }
171     
172     }
173             
174     int ADCCMask = (int)MASK_PRM(S);
175     int i;
176     if(ADCCMask > 255 || ADCCMask < 0) {
177         ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
178     }
179     
180     int ADCChannels = __builtin_popcount((uint)ADCCMask);           //Counts number of set bits in ADCCMask
181     
182     ssSetIWorkValue(S, 0, ADCChannels);
183     
184     mfst->ADC_enabled = ADCCMask;
185     mfst->ADC_enabled &= 0xFF;
186     
187     mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg));
188     
189     
190     
191   }
192 #endif /*  MDL_START */
193
194
195
196 /* Function: mdlOutputs =======================================================
197  * Abstract:
198  *    In this function, you compute the outputs of your S-function
199  *    block.
200  */
201 static void mdlOutputs(SimStruct *S, int_T tid)
202 {
203
204     int ADCChannels = ssGetIWorkValue(S, 0);
205     real_T*       y[ADCChannels];
206     int i;
207     int res,res1;
208     
209     // Activate trigger to start conversion
210         mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
211     
212     for(i=0; i < ADCChannels;i++){
213         y[i]=ssGetOutputPortSignal(S,i);
214     }
215     
216     // Check if conversion has finished
217         while((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg)) & GPIOC_EOLC_mask)) { 
218                 for (i = 0; i < 1000; i++) {} // small wait
219         }
220     
221     
222      for(i=1; i < ADCChannels;i+=2){
223         res = mf624_read32(MFST2REG(mfst, 2, ADDATA0_reg));
224         res1= res >> 16;
225         res = res & 0xFFFF;
226         *y[i-1] = (real_T) (10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
227         *y[i] = (real_T) (10.0 * ((int16_t) (res1 << 2)) / (double) 0x8000);
228     }
229     
230     if(i == ADCChannels){
231         res = mf624_read16(MFST2REG(mfst, 2, ADDATA0_reg));
232         *y[ADCChannels-1]=(real_T)(10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
233     }
234 }
235
236
237
238 #undef MDL_UPDATE  /* Change to #undef to remove function */
239 #if defined(MDL_UPDATE)
240   /* Function: mdlUpdate ======================================================
241    * Abstract:
242    *    This function is called once for every major integration time step.
243    *    Discrete states are typically updated here, but this function is useful
244    *    for performing any tasks that should only take place once per
245    *    integration step.
246    */
247   static void mdlUpdate(SimStruct *S, int_T tid)
248   {
249   }
250 #endif /* MDL_UPDATE */
251
252
253
254 #undef MDL_DERIVATIVES  /* Change to #undef to remove function */
255 #if defined(MDL_DERIVATIVES)
256   /* Function: mdlDerivatives =================================================
257    * Abstract:
258    *    In this function, you compute the S-function block's derivatives.
259    *    The derivatives are placed in the derivative vector, ssGetdX(S).
260    */
261   static void mdlDerivatives(SimStruct *S)
262   {
263   }
264 #endif /* MDL_DERIVATIVES */
265
266
267
268 /* Function: mdlTerminate =====================================================
269  * Abstract:
270  *    In this function, you should perform any actions that are necessary
271  *    at the termination of a simulation.  For example, if memory was
272  *    allocated in mdlStart, this is the place to free it.
273  */
274 static void mdlTerminate(SimStruct *S)
275 {
276     if(mfst != NULL){
277         free(mfst);
278         mfst=NULL;
279     }
280 }
281
282
283 /*======================================================*
284  * See sfuntmpl_doc.c for the optional S-function methods *
285  *======================================================*/
286
287 /*=============================*
288  * Required S-function trailer *
289  *=============================*/
290
291 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
292 #include "simulink.c"      /* MEX-file interface mechanism */
293 #else
294 #include "cg_sfun.h"       /* Code generation registration function */
295 #endif