]> rtime.felk.cvut.cz Git - mf624-simulink.git/commitdiff
Do not fail if mdlStart() cannot open /dev/uio0
authorMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 23 Jul 2013 14:13:21 +0000 (16:13 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 23 Jul 2013 15:30:39 +0000 (17:30 +0200)
Such failing is inconvenient because mdlStart() is called even during
code generation [1]. If one tries to generate the code on a computer
without MF624 hardware, it fails.

This is solved that we ignore error in mdlStart(). When initialization
fails, we simply return from mdlStart() without reporing an error to the
user. The status of initialization is checked in every call to mdlOutputs()
and there the failres are already reported to the user.

[1] http://www.mathworks.com/matlabcentral/newsreader/view_thread/32986

mf624_SIMULINK.c
sfAnalogInput.c
sfAnalogOutput.c
sfDigitalOutput.c
sfReadPWM.c

index 05b04896f039af99cc5eb2293ef9cfd43b59634c..f9cb9736d1d70c1b3926b8490b6fe935e53572c7 100644 (file)
@@ -409,6 +409,17 @@ int mmap_regions(mf624_state_t* mfst)
 
 
 
+/** 
+ * Try to initialize the MF624 UIO driver.
+ *
+ * If and only if S is not NULL and initialization fails, then
+ * ssSetErrorStatus() gets called to report the error via Simulink
+ * interface.
+ * 
+ * @param S Pointer to SimStruct for error reporting or NULL.
+ * 
+ * @return Zero in case of success, -1 in case of error.
+ */
 int mf624_init(SimStruct *S)
 {
        if (mfst==NULL) {
@@ -419,15 +430,22 @@ int mf624_init(SimStruct *S)
 
                mfst->device_fd = open_device(fn);
                if (mfst->device_fd < 0) {
-                       ssSetErrorStatus(S,"/dev/" UIO ": open failed");
-                       return;
+                       if (S) ssSetErrorStatus(S,"/dev/" UIO ": open failed");
+                       goto free;
                }
                if (mmap_regions(mfst) < 0) {
-                       ssSetErrorStatus(S,"/dev/" UIO ": mmap_regions failed");
-                       return;
+                       if (S) ssSetErrorStatus(S,"/dev/" UIO ": mmap_regions failed");
+                       goto close;
                }
 
        }
+       return 0;
+close:
+       close(mfst->device_fd);
+free:
+       free(mfst);
+       mfst = NULL;
+       return -1;
 }
 
 /*int main(int argc, char* argv[])
index d949801db58d68e438dc6515040d14efc73bcfc3..efd2f3184e0285231d0647d7c60f34b5832cabf5 100644 (file)
@@ -147,7 +147,7 @@ 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);
@@ -185,6 +185,9 @@ static void mdlOutputs(SimStruct *S, int_T tid)
     int i;
     int res,res1;
     
+    if (mf624_init(S) != 0)
+           return;
+
     // Activate trigger to start conversion
        mf624_read16(MFST2REG(mfst, 2, ADSTART_reg));
     
index d51a28a3be9178081bfad0bddc8a7ff117035f52..249919fd4870d1c8c735ff379d62401c96c74031 100644 (file)
@@ -146,7 +146,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
    */
   static void mdlStart(SimStruct *S)
   {
-        if (mf624_init(S) != 0)
+        if (mf624_init(NULL) != 0)
            return;
        DAC_enable(mfst);
     //ssSetPWorkValue(S, 0, mfst);
@@ -165,6 +165,10 @@ 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);
     int out;
+
+    if (mf624_init(S) != 0)
+           return;
+
     if(u[0] > 9.9988){
         out = 0x3FFF;
     }
index 9d4d81b4d3c13104451f33f0df06c7f5a63a7417..c0a6c4837a68e09b352613fee1145fb5cd642a9f 100644 (file)
@@ -146,7 +146,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
    */
   static void mdlStart(SimStruct *S)
   {
-    if (mf624_init(S) != 0)
+    if (mf624_init(NULL) != 0)
        return;
     mfst->DOut=0;
     
@@ -163,6 +163,10 @@ static void mdlInitializeSampleTimes(SimStruct *S)
 static void mdlOutputs(SimStruct *S, int_T tid)
 {
     const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
+
+    if (mf624_init(S) != 0)
+           return;
+
     if(u[0] > 0.5){
         /*Update the "word" holding Output Values*/
         mfst->DOut |= (1 << (int)(CHNL_PRM(S)-1));
index 451af83631631dd2fb244d8bb19b94686cd6dd52..793838c43687446d1235a635824bf9b88b7c9a75 100644 (file)
@@ -130,7 +130,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
    */
   static void mdlStart(SimStruct *S)
   {
-    if (mf624_init(S) != 0)
+    if (mf624_init(NULL) != 0)
        return;
             
     /*Configuration of desired counter modes*/
@@ -173,7 +173,9 @@ static void mdlOutputs(SimStruct *S, int_T tid)
     unsigned int period;
     unsigned int c0,c1,c2,c4;
     
-    
+    if (mf624_init(S) != 0)
+           return;
+       
     c0 = mf624_read32(MFST2REG(mfst,4,CTR0));
     c1 = mf624_read32(MFST2REG(mfst,4,CTR1));
     c2 = mf624_read32(MFST2REG(mfst,4,CTR2));