]> rtime.felk.cvut.cz Git - mf624-simulink.git/blobdiff - sfAnalogInput.c
Use correct header file for alloca for Linux build.
[mf624-simulink.git] / sfAnalogInput.c
index 70e4ca956d0307bfe47b5c5d30759567d9addfa6..69572f5f7ae70a31d52442e3c657c064c1286d7f 100644 (file)
 #include "mf624_SIMULINK.h"
 #endif /*WITHOUT_HW*/
 
+#ifdef __GNUC__
+  #include <alloca.h>
+  #define my_popcount __builtin_popcount
+#else
+  #include <malloc.h>
+  int my_popcount(uint32_t patt)
+  {
+    int pops = 0;
+    while (patt) {
+      if (patt & 1)
+        pops++;
+      patt >>= 1;
+    }
+    return pops;
+  }
+#endif
+
 
 /* Error handling
  * --------------
  */
 static void mdlInitializeSizes(SimStruct *S)
 {
+    int ADCCMask;
+    int i;
+    int ADCChannels;
     /* See sfuntmpl_doc.c for more details on the macros below */
 
     ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
@@ -100,15 +120,14 @@ static void mdlInitializeSizes(SimStruct *S)
     ssSetNumContStates(S, 0);
     ssSetNumDiscStates(S, 0);
     
-    int ADCCMask = (int)MASK_PRM(S);
-    int i;
+    ADCCMask = (int)MASK_PRM(S);
+
     if(ADCCMask > 255 || ADCCMask < 0) {
         ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
     }
     
-    int ADCChannels = __builtin_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
-        
-    
+    ADCChannels = my_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
+
     if (!ssSetNumInputPorts(S, 0)) return;
     
     
@@ -179,6 +198,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
    */
   static void mdlStart(SimStruct *S)
   {
+    int ADCChannels;
 
   #ifndef WITHOUT_HW
     if (mf624_init(NULL) != 0)
@@ -191,7 +211,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
         ssSetErrorStatus(S,"Invalid parameter mask, set to 0-255");
     }
 
-    int ADCChannels = __builtin_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
+    ADCChannels = my_popcount((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
 
     ssSetIWorkValue(S, 0, ADCChannels);
   #ifndef WITHOUT_HW
@@ -214,7 +234,7 @@ static void mdlOutputs(SimStruct *S, int_T tid)
 {
 
     int ADCChannels = ssGetIWorkValue(S, 0);
-    real_T*       y[ADCChannels];
+    real_T**       y;
     int i;
     int res,res1;
   #ifndef WITHOUT_HW
@@ -225,6 +245,8 @@ static void mdlOutputs(SimStruct *S, int_T tid)
     mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
   #endif /*WITHOUT_HW*/
 
+    y = alloca(ADCChannels * sizeof(*y));
+
     for(i=0; i < ADCChannels;i++){
         y[i]=ssGetOutputPortSignal(S,i);
     }