]> rtime.felk.cvut.cz Git - mf624-simulink.git/blobdiff - sfAnalogInput.c
Blocks needed for control of mf624 I/O card via SIMULINK.
[mf624-simulink.git] / sfAnalogInput.c
index bfee43a3e81ee66b4a4ce03a482a3dcc5833623f..e37b3f9e5a599ccf216cbff000dae5893dba109e 100644 (file)
@@ -10,6 +10,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
@@ -17,7 +18,7 @@
  */
 #include "simstruc.h"
 
-#include "mf624.h"
+#include "mf624_SIMULINK.h"
 
 
 /* Error handling
@@ -57,15 +58,25 @@ 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((uint)ADCCMask);           //Counts number of set bits in ADCCMask
+        
+    
     if (!ssSetNumInputPorts(S, 1)) return;
     ssSetInputPortWidth(S, 0, 1);
     ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
@@ -77,12 +88,13 @@ static void mdlInitializeSizes(SimStruct *S)
      */
     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);
+    ssSetNumIWork(S, 1);
     ssSetNumPWork(S, 1);
     ssSetNumModes(S, 0);
     ssSetNumNonsampledZCs(S, 0);
@@ -91,6 +103,8 @@ static void mdlInitializeSizes(SimStruct *S)
     ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
 
     ssSetOptions(S, 0);
+    
+
 }
 
 
@@ -110,7 +124,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
 
 
 
-#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 +154,49 @@ static void mdlInitializeSampleTimes(SimStruct *S)
    */
   static void mdlStart(SimStruct *S)
   {
+    //TODO: get pointer to mfst state structure and store it in pwork
+    #define TEST
     #if defined(TEST)
-    mf624_state_t* mfst = &mf624_state;
-       char buff[BUFF_SMALL];
+    #define BUFF_SMALL         32
+    mf624_state_t* mfst = malloc(sizeof(mf624_state_t));
+    char buff[BUFF_SMALL];
        memset(buff, '\0', BUFF_SMALL);
+    mfst->uio_dev = "uio0";
+
+       strncat(buff, "/dev/", 5);
+       strncat(buff, mfst->uio_dev, sizeof(buff) - 6);
+
+       mfst->device_fd = open_device(buff);
     
+       if (mfst->device_fd < 0) {
+        ssSetErrorStatus(S,"open failed");
+        return;
+       }
     
+       if (mmap_regions(mfst) < 0) {
+        ssSetErrorStatus(S,"mmap_regions failed");
+               return;
+       }
     
     #endif
-    //TODO: inicializovat tady pointer na stavovou strukturu a ulozit ho do PWork
+            
+    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
+    
+    ssSetIWorkValue(S, 0, ADCChannels);
+    
+    mfst->ADC_enabled = ADCCMask;
+    mfst->ADC_enabled &= 0xFF;
+    
+    mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg));
+    
+    ssSetPWorkValue(S, 0, mfst);
+    
   }
 #endif /*  MDL_START */
 
@@ -161,14 +209,43 @@ 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);
+
+    mf624_state_t* mfst = ssGetPWorkValue(S, 0);
+    int ADCChannels = ssGetIWorkValue(S, 0);
+    real_T*       y[ADCChannels];
+    int i;
+    int res,res1;
+    
+    // Activate trigger to start conversion
+       mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
+    
+    for(i=0; i < ADCChannels;i++){
+        y[i]=ssGetOutputPortSignal(S,i);
+    }
+    
+    // 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);
+    }
 }
 
 
 
-#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 +261,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 +283,9 @@ static void mdlOutputs(SimStruct *S, int_T tid)
  */
 static void mdlTerminate(SimStruct *S)
 {
+    #if defined(TEST)
+    free(ssGetPWorkValue(S,0));
+    #endif
 }