]> rtime.felk.cvut.cz Git - mf624-simulink.git/blobdiff - sfAnalogInput.c
Add build mode WITHOUT_HW to edit model without MF624 hardware access.
[mf624-simulink.git] / sfAnalogInput.c
index bfee43a3e81ee66b4a4ce03a482a3dcc5833623f..70e4ca956d0307bfe47b5c5d30759567d9addfa6 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).
@@ -10,6 +38,7 @@
 
 #define S_FUNCTION_NAME  sfAnalogInput
 #define S_FUNCTION_LEVEL 2
+#define MASK_PRM(S)     (mxGetScalar(ssGetSFcnParam(S, 0)))
 
 /*
  * Need to include simstruc.h for the definition of the SimStruct and
  */
 #include "simstruc.h"
 
-#include "mf624.h"
+#include <stdint.h>
+
+#ifndef WITHOUT_HW
+#include "mf624_SIMULINK.h"
+#endif /*WITHOUT_HW*/
 
 
 /* Error handling
@@ -57,33 +90,37 @@ static void mdlInitializeSizes(SimStruct *S)
 {
     /* See sfuntmpl_doc.c for more details on the macros below */
 
-    ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */
+    ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
         /* Return if number of expected != number of actual parameters */
+        ssSetErrorStatus(S,"Parameter mismatch");
         return;
     }
 
     ssSetNumContStates(S, 0);
     ssSetNumDiscStates(S, 0);
+    
+    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((uint32_t)ADCCMask);           //Counts number of set bits in ADCCMask
+        
+    
+    if (!ssSetNumInputPorts(S, 0)) return;
+    
+    
 
-    if (!ssSetNumInputPorts(S, 1)) return;
-    ssSetInputPortWidth(S, 0, 1);
-    ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
-    /*
-     * Set direct feedthrough flag (1=yes, 0=no).
-     * A port has direct feedthrough if the input is used in either
-     * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
-     * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
-     */
-    ssSetInputPortDirectFeedThrough(S, 0, 1);
-
-    if (!ssSetNumOutputPorts(S, 1)) return;
-    ssSetOutputPortWidth(S, 0, 1);
-
+    if (!ssSetNumOutputPorts(S, ADCChannels)) return;
+    for(i=0; i < ADCChannels;i++){
+        ssSetOutputPortWidth(S, i, 1);
+    }
     ssSetNumSampleTimes(S, 1);
     ssSetNumRWork(S, 0);
-    ssSetNumIWork(S, 0);
-    ssSetNumPWork(S, 1);
+    ssSetNumIWork(S, 1);
+    ssSetNumPWork(S, 0);
     ssSetNumModes(S, 0);
     ssSetNumNonsampledZCs(S, 0);
 
@@ -91,6 +128,8 @@ static void mdlInitializeSizes(SimStruct *S)
     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
 
     ssSetOptions(S, 0);
+    
+
 }
 
 
@@ -104,13 +143,13 @@ 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);
 
 }
 
 
 
-#define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
+#undef MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
 #if defined(MDL_INITIALIZE_CONDITIONS)
   /* Function: mdlInitializeConditions ========================================
    * Abstract:
@@ -140,15 +179,27 @@ static void mdlInitializeSampleTimes(SimStruct *S)
    */
   static void mdlStart(SimStruct *S)
   {
-    #if defined(TEST)
-    mf624_state_t* mfst = &mf624_state;
-       char buff[BUFF_SMALL];
-       memset(buff, '\0', BUFF_SMALL);
-    
-    
-    
-    #endif
-    //TODO: inicializovat tady pointer na stavovou strukturu a ulozit ho do PWork
+
+  #ifndef WITHOUT_HW
+    if (mf624_init(NULL) != 0)
+       return;
+  #endif /*WITHOUT_HW*/
+
+    int 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
+
+    ssSetIWorkValue(S, 0, ADCChannels);
+  #ifndef WITHOUT_HW
+    mfst->ADC_enabled = ADCCMask;
+    mfst->ADC_enabled &= 0xFF;
+
+    mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg));
+  #endif /*WITHOUT_HW*/
   }
 #endif /*  MDL_START */
 
@@ -161,14 +212,51 @@ static void mdlInitializeSampleTimes(SimStruct *S)
  */
 static void mdlOutputs(SimStruct *S, int_T tid)
 {
-    mf624_state_t* mfstPtr = ssGetPWork(S)[0];
-    real_T       *y = ssGetOutputPortSignal(S,0);
-    y[0] = ADC_read(mfstPtr, AD0);
+
+    int ADCChannels = ssGetIWorkValue(S, 0);
+    real_T*       y[ADCChannels];
+    int i;
+    int res,res1;
+  #ifndef WITHOUT_HW
+    if (mf624_check(S) != 0)
+        return;
+
+    // Activate trigger to start conversion
+    mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
+  #endif /*WITHOUT_HW*/
+
+    for(i=0; i < ADCChannels;i++){
+        y[i]=ssGetOutputPortSignal(S,i);
+    }
+
+  #ifndef WITHOUT_HW
+    // Check if conversion has finished
+    while((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg)) & GPIOC_EOLC_mask)) { 
+        for (i = 0; i < 1000; i++) {} // small wait
+    }
+
+    for(i=1; i < ADCChannels;i+=2){
+        res = mf624_read32(MFST2REG(mfst, 2, ADDATA0_reg));
+        res1= res >> 16;
+        res = res & 0xFFFF;
+        *y[i-1] = (real_T) (10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
+        *y[i] = (real_T) (10.0 * ((int16_t) (res1 << 2)) / (double) 0x8000);
+    }
+
+    if(i == ADCChannels){
+        res = mf624_read16(MFST2REG(mfst, 2, ADDATA0_reg));
+        *y[ADCChannels-1]=(real_T)(10.0 * ((int16_t) (res << 2)) / (double) 0x8000);
+    }
+  #else /*WITHOUT_HW*/
+    for(i=0; i < ADCChannels; i++){
+        *y[i] = 0;
+    }
+  #endif /*WITHOUT_HW*/
 }
 
 
 
-#define MDL_UPDATE  /* Change to #undef to remove function */
+#undef MDL_UPDATE  /* Change to #undef to remove function */
 #if defined(MDL_UPDATE)
   /* Function: mdlUpdate ======================================================
    * Abstract:
@@ -184,7 +272,7 @@ static void mdlOutputs(SimStruct *S, int_T tid)
 
 
 
-#define MDL_DERIVATIVES  /* Change to #undef to remove function */
+#undef MDL_DERIVATIVES  /* Change to #undef to remove function */
 #if defined(MDL_DERIVATIVES)
   /* Function: mdlDerivatives =================================================
    * Abstract:
@@ -206,6 +294,9 @@ static void mdlOutputs(SimStruct *S, int_T tid)
  */
 static void mdlTerminate(SimStruct *S)
 {
+  #ifndef WITHOUT_HW
+    mf624_done(S);
+  #endif /*WITHOUT_HW*/
 }