]> rtime.felk.cvut.cz Git - mf624-simulink.git/commitdiff
Modified MF624 related s-functions to use single mf624_state structure
authorkrecmich <krecmich@emotor.felk.cvut.cz>
Mon, 1 Jul 2013 12:48:01 +0000 (14:48 +0200)
committerkrecmich <krecmich@emotor.felk.cvut.cz>
Mon, 1 Jul 2013 12:48:01 +0000 (14:48 +0200)
declared in mf624_SIMULINK.c.

Signed-off-by: krecmich <krecmich@emotor.felk.cvut.cz>
sfAnalogInput.c
sfAnalogOutput.c

index e37b3f9e5a599ccf216cbff000dae5893dba109e..940bf439794789f838584415c014b2695efea60f 100644 (file)
@@ -77,16 +77,9 @@ static void mdlInitializeSizes(SimStruct *S)
     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*/
-    /*
-     * 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 (!ssSetNumInputPorts(S, 0)) return;
+    
+    
 
     if (!ssSetNumOutputPorts(S, ADCChannels)) return;
     for(i=0; i < ADCChannels;i++){
@@ -95,7 +88,7 @@ static void mdlInitializeSizes(SimStruct *S)
     ssSetNumSampleTimes(S, 1);
     ssSetNumRWork(S, 0);
     ssSetNumIWork(S, 1);
-    ssSetNumPWork(S, 1);
+    ssSetNumPWork(S, 0);
     ssSetNumModes(S, 0);
     ssSetNumNonsampledZCs(S, 0);
 
@@ -154,31 +147,29 @@ 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)
-    #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 == NULL){
+        #define BUFF_SMALL             32
+        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 (mfst->device_fd < 0) {
+            ssSetErrorStatus(S,"open failed");
+            return;
+       }
     
-       if (mmap_regions(mfst) < 0) {
-        ssSetErrorStatus(S,"mmap_regions failed");
-               return;
-       }
+        if (mmap_regions(mfst) < 0) {
+            ssSetErrorStatus(S,"mmap_regions failed");
+            return;
+        }
     
-    #endif
+    }
             
     int ADCCMask = (int)MASK_PRM(S);
     int i;
@@ -195,7 +186,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
     
     mf624_write16(mfst->ADC_enabled, MFST2REG(mfst, 2, ADCTRL_reg));
     
-    ssSetPWorkValue(S, 0, mfst);
+    
     
   }
 #endif /*  MDL_START */
@@ -210,7 +201,6 @@ static void mdlInitializeSampleTimes(SimStruct *S)
 static void mdlOutputs(SimStruct *S, int_T tid)
 {
 
-    mf624_state_t* mfst = ssGetPWorkValue(S, 0);
     int ADCChannels = ssGetIWorkValue(S, 0);
     real_T*       y[ADCChannels];
     int i;
@@ -283,9 +273,10 @@ static void mdlOutputs(SimStruct *S, int_T tid)
  */
 static void mdlTerminate(SimStruct *S)
 {
-    #if defined(TEST)
-    free(ssGetPWorkValue(S,0));
-    #endif
+    if(mfst != NULL){
+        free(mfst);
+        mfst=NULL;
+    }
 }
 
 
index aec11cf0705c0745c08bc193d3e3ebd5d25aacac..888bff8303adc03f45d48decc17348d073dc4433 100644 (file)
@@ -88,8 +88,8 @@ static void mdlInitializeSizes(SimStruct *S)
 
     ssSetNumSampleTimes(S, 1);
     ssSetNumRWork(S, 0);
-    ssSetNumIWork(S, 1);
-    ssSetNumPWork(S, 1);
+    ssSetNumIWork(S, 0);
+    ssSetNumPWork(S, 0);
     ssSetNumModes(S, 0);
     ssSetNumNonsampledZCs(S, 0);
 
@@ -146,31 +146,29 @@ 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)
-    #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;
-       }
+    if(mfst==NULL){
+        mfst = malloc(sizeof(mf624_state_t));
+        #define BUFF_SMALL             32
+        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;
+        }
 
+    }
        DAC_enable(mfst);
-    ssSetPWorkValue(S, 0, mfst);
-    #endif
+    //ssSetPWorkValue(S, 0, mfst);
   }
 #endif /*  MDL_START */
 
@@ -184,7 +182,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
 static void mdlOutputs(SimStruct *S, int_T tid)
 {
     const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
-    mf624_state_t* mfst = ssGetPWorkValue(S,0);
+    //mf624_state_t* mfst = ssGetPWorkValue(S,0);
     int out;
     if(u[0] > 9.9988){
         out = 0x3FFF;
@@ -195,8 +193,7 @@ static void mdlOutputs(SimStruct *S, int_T tid)
     else {
         out = (int) ((u[0] + 10) * 8192 / 10 + 0.5);
     }
-    printf("tik\n");
-    DAC_write(mfst, DA0, out);
+    mf624_write16(out, MFST2REG(mfst, 2, dac_channel2reg[(int)CHNL_PRM(S)-1]));
 }
 
 
@@ -239,9 +236,39 @@ static void mdlOutputs(SimStruct *S, int_T tid)
  */
 static void mdlTerminate(SimStruct *S)
 {
-    #if defined(TEST)
-    free(ssGetPWorkValue(S,0));
-    #endif
+    //mf624_state_t* mfst = ssGetPWorkValue(S,0);
+    /*At the end of simulation disable D/A outputs*/
+    if(mfst != NULL){
+        mf624_write32((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg))
+            & ~GPIOC_DACEN_mask), // disable output,
+            MFST2REG(mfst, 0, GPIOC_reg));
+        free(mfst);
+        mfst=NULL;
+    } else {
+        mfst = malloc(sizeof(mf624_state_t));
+        #define BUFF_SMALL             32
+        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;
+        }   
+        mf624_write32((mf624_read32(MFST2REG(mfst, 0, GPIOC_reg))
+            & ~GPIOC_DACEN_mask), // disable output,
+            MFST2REG(mfst, 0, GPIOC_reg));
+        free(mfst);
+        mfst=NULL;
+    }
     
 }