]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/rpp/mout.c
mout: Remove stupid waiting in rpp_mout_set()
[pes-rpp/rpp-lib.git] / rpp / src / rpp / mout.c
index 071b97a8a4ac05bc1c2213deb8d6bf48fe7cd873..ae2d4a9501ac476272fd4babe3653b2950b70fa4 100644 (file)
@@ -1,20 +1,12 @@
-/* Copyright (C) 2013 Czech Technical University in Prague
+/* Copyright (C) 2013, 2016 Czech Technical University in Prague
  *
  * Authors:
  *     - Carlos Jenkins <carlos@jenkins.co.cr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
  *
  * File : mout.c
  * Abstract:
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
 
-#if rppCONFIG_INCLUDE_MOUT == 1
+RPP_MUTEX_DEFINE(mutex_mout);
 
-#if rppCONFIG_DRV == 1
+#ifndef FREERTOS_POSIX
 #include "drv/mout.h"
 #endif
 
@@ -38,84 +31,88 @@ static boolean_t initialized = FALSE;
 
 int8_t rpp_mout_init()
 {
-    if(initialized) {
-        return FAILURE;
-    }
-    initialized = TRUE;
+       if (initialized)
+               return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_mout))
+               return FAILURE;
+       initialized = TRUE;
+#ifndef FREERTOS_POSIX
+       dmmInit();
+       gioInit();
+       hetInit();
+#endif
 
-    return SUCCESS;
+       return SUCCESS;
 }
 
 
 static uint8_t cache[] = {
-    LOW, LOW, LOW, LOW, LOW, LOW
+       LOW, LOW, LOW, LOW, LOW, LOW
 };
 
 int8_t rpp_mout_set(uint8_t pin, uint8_t val)
 {
-    // Check range
-    if((pin < 1) || (pin > 6)) {
-        return -1;
-    }
-
-    // Check val
-    if((val != HIGH) && (val != LOW)) {
-        return -2;
-    }
-
-    #if rppCONFIG_DRV == 1
-    uint8_t idx = pin - 1;
-
-    // Set and store value
-    if(drv_mout_set(idx, val) != SUCCESS) {
-        return -3;
-    }
-    cache[idx] = val;
-
-    // FIXME Wait some time for synchronization
-    // Don't use vTaskDelay() here because the minimum time to wait is one tick,
-    // and depending of the configuration of the user model one tick can overrun
-    // the program.
-    int wait;
-    for(wait = 0; wait < 10; wait++) {
-        asm("nop");
-    }
-
-    // Get value back and compare
-    if(drv_mout_diag(idx) != val) {
-        return -4;
-    }
-    #endif
-
-    return SUCCESS;
+       int ret;
+       // Check range
+       if ((pin < 1) || (pin > 6))
+               return -1;
+
+       // Check val
+       if ((val != HIGH) && (val != LOW))
+               return -2;
+
+       uint8_t idx = pin - 1;
+
+#ifndef FREERTOS_POSIX
+       // Set and store value
+       RPP_MUTEX_LOCK(mutex_mout);
+       ret = drv_mout_set(idx, val);
+       if (ret == SUCCESS)
+               cache[idx] = val;
+       else {
+               RPP_MUTEX_UNLOCK(mutex_mout);
+               return -3;
+       }
+
+       ret = drv_mout_diag(idx);
+       RPP_MUTEX_UNLOCK(mutex_mout);
+       if (ret == 0)
+               return -4;
+#else
+       RPP_MUTEX_LOCK(mutex_mout);
+       cache[idx] = val;
+       RPP_MUTEX_UNLOCK(mutex_mout);
+#endif
+
+       return SUCCESS;
 }
 
 
 int8_t rpp_mout_get(uint8_t pin)
 {
-    // Check range
-    if((pin < 1) || (pin > 6)) {
-        return -1;
-    }
+       // Check range
+       if ((pin < 1) || (pin > 6))
+               return -1;
 
-    return cache[pin - 1];
+       RPP_MUTEX_LOCK(mutex_mout);
+       int8_t ret_val = cache[pin - 1];
+       RPP_MUTEX_UNLOCK(mutex_mout);
+
+       return ret_val;
 }
 
 
 int8_t rpp_mout_diag(uint8_t pin)
 {
-    // Check range
-    if((pin < 1) || (pin > 6)) {
-        return -1;
-    }
-
-    #if rppCONFIG_DRV == 1
-    return drv_mout_diag(pin - 1);
-    #else
-    return LOW;
-    #endif
+       int ret;
+       // Check range
+       if ((pin < 1) || (pin > 6))
+               return -RPP_EINVAL;
+
+#ifndef FREERTOS_POSIX
+       RPP_MUTEX_LOCK(mutex_mout);
+       ret = drv_mout_diag(pin - 1);
+       RPP_MUTEX_UNLOCK(mutex_mout);
+#endif
+       return ret;
 }
-
-
-#endif /* rppCONFIG_INCLUDE_MOUT */
-