]> rtime.felk.cvut.cz Git - mf624-simulink.git/blobdiff - sfAnalogInput.c
Example of use of PWM combined with digital direction output.
[mf624-simulink.git] / sfAnalogInput.c
index d949801db58d68e438dc6515040d14efc73bcfc3..fe72ebf27d44d364190e70f9f1d56de63fbe7115 100644 (file)
@@ -1,8 +1,36 @@
 /*
- * (c) Michal Kreč, Czech Technical University in Prague, 2013
+ * S-function to support analog inputs on Humusoft MF624 card
+ *
+ * Copyright (C) 2013 Michal Kreč <krecmich@fel.cvut.cz>
+ * Copyright (C) 2013 Michal Sojka <sojkam1@fel.cvut.cz>
+ *
+ * Department of Control Engineering
+ * Faculty of Electrical Engineering
+ * Czech Technical University in Prague (CTU)
+ *
+ * The S-Function for ERT Linux can be distributed in compliance
+ * with GNU General Public License (GPL) version 2 or later.
+ * Other licence can negotiated with CTU.
+ *
+ * Next exception is granted in addition to GPL.
+ * Instantiating or linking compiled version of this code
+ * to produce an application image/executable, does not
+ * by itself cause the resulting application image/executable
+ * to be covered by the GNU General Public License.
+ * This exception does not however invalidate any other reasons
+ * why the executable file might be covered by the GNU Public License.
+ * Publication of enhanced or derived S-function files is required
+ * although.
+ *
+ * Linux ERT code is available from
+ *    http://rtime.felk.cvut.cz/gitweb/ert_linux.git
+ * More CTU Linux target for Simulink components are available at
+ *    http://lintarget.sourceforge.net/
+ *
+ * sfuntmpl_basic.c by The MathWorks, Inc. has been used to accomplish
+ * required S-function structure.
  */
 
-
 /*
  * You must specify the S_FUNCTION_NAME as the name of your S-function
  * (i.e. replace sfuntmpl_basic with the name of your S-function).
@@ -74,7 +102,7 @@ static void mdlInitializeSizes(SimStruct *S)
         ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
     }
     
-    int ADCChannels = __builtin_popcount((uint)ADCCMask);           //Counts number of set bits in ADCCMask
+    int ADCChannels = __builtin_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
         
     
     if (!ssSetNumInputPorts(S, 0)) return;
@@ -111,7 +139,7 @@ static void mdlInitializeSizes(SimStruct *S)
 static void mdlInitializeSampleTimes(SimStruct *S)
 {
     ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
-    ssSetOffsetTime(S, 0, 0.0);
+    ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
 
 }
 
@@ -147,16 +175,16 @@ static void mdlInitializeSampleTimes(SimStruct *S)
    */
   static void mdlStart(SimStruct *S)
   {
-    if (mf624_init(S) != 0)
+    if (mf624_init(NULL) != 0)
        return;
             
     int ADCCMask = (int)MASK_PRM(S);
-    int i;
+
     if(ADCCMask > 255 || ADCCMask < 0) {
         ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
     }
     
-    int ADCChannels = __builtin_popcount((uint)ADCCMask);           //Counts number of set bits in ADCCMask
+    int ADCChannels = __builtin_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
     
     ssSetIWorkValue(S, 0, ADCChannels);
     
@@ -185,6 +213,9 @@ static void mdlOutputs(SimStruct *S, int_T tid)
     int i;
     int res,res1;
     
+    if (mf624_check(S) != 0)
+           return;
+
     // Activate trigger to start conversion
        mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
     
@@ -252,10 +283,7 @@ static void mdlOutputs(SimStruct *S, int_T tid)
  */
 static void mdlTerminate(SimStruct *S)
 {
-    if(mfst != NULL){
-        free(mfst);
-        mfst=NULL;
-    }
+    mf624_done(S);
 }