]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/rpp/lout.c
Make the RPP layer thread safe
[pes-rpp/rpp-lib.git] / rpp / src / rpp / lout.c
index 78cd9b8b9e5d7ed4990c89e9a38f578aaa90f6e1..ca2366574fc936d57b328b19d6c2cf20666769f2 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2013 Czech Technical University in Prague
+/* Copyright (C) 2013, 2015 Czech Technical University in Prague
  *
  * Authors:
  *     - Carlos Jenkins <carlos@jenkins.co.cr>
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
 
-#if rppCONFIG_INCLUDE_LOUT == 1
+RPP_MUTEX_DEFINE(mutex_lout);
 
-#if rppCONFIG_DRV == 1
+#ifndef FREERTOS_POSIX
 #include "drv/lout.h"
+#include "drv/spi_tms570.h"
 #endif
 
 static boolean_t initialized = FALSE;
 
 int8_t rpp_lout_init()
 {
-    if(initialized) {
-        return FAILURE;
-    }
-    initialized = TRUE;
+       if (initialized)
+               return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_lout))
+               return FAILURE;
+       initialized = TRUE;
+#ifndef FREERTOS_POSIX
+       spi_tms570_init();
+#endif
 
-    // FIXME: Implement.
-   return SUCCESS;
+       // FIXME: Implement.
+       return SUCCESS;
 }
 
 
@@ -44,17 +50,17 @@ static uint8_t out_cache = 0x0;
 
 int8_t rpp_lout_set(uint8_t pin, uint8_t val)
 {
-    if((pin < 1) || (pin > 8)) {
-        return -1;
-    }
-
-    uint8_t index = pin - 1;
-    if(val) {
-        bit_set(out_cache, index);
-    } else {
-        bit_clear(out_cache, index);
-    }
-    return SUCCESS;
+       if ((pin < 1) || (pin > 8))
+               return -1;
+
+       uint8_t index = pin - 1;
+       RPP_MUTEX_LOCK(mutex_lout);
+       if (val)
+               bit_set(out_cache, index);
+       else
+               bit_clear(out_cache, index);
+       RPP_MUTEX_UNLOCK(mutex_lout);
+       return SUCCESS;
 }
 
 
@@ -62,37 +68,38 @@ static uint8_t diag_cache = 0x0;
 
 int8_t rpp_lout_diag(uint8_t pin)
 {
-    if((pin < 1) || (pin > 8)) {
-        return -1;
-    }
-
-    if(is_bit_set(diag_cache, pin - 1)) {
-        return HIGH;
-    }
-    return LOW;
-}
+       if ((pin < 1) || (pin > 8))
+               return -1;
 
+       int8_t ret_val = LOW;
 
-int8_t rpp_lout_update()
-{
-    #if rppCONFIG_DRV == 1
-    // Update output values
-    lout_set_word(out_cache);
-    // FIXME: Check which SPI transfer statuses could be considered errors
-    lout_spi_transfer();
-
-    // Read back diagnostic values
-    // FIXME: Implement. Dummy assign for now.
-    diag_cache = out_cache;
-
-    if(diag_cache != out_cache) {
-        return FAILURE;
-    }
-    #endif
-
-    return SUCCESS;
+       RPP_MUTEX_LOCK(mutex_lout);
+       if (is_bit_set(diag_cache, pin - 1))
+               ret_val = HIGH;
+       RPP_MUTEX_UNLOCK(mutex_lout);
+       return ret_val;
 }
 
 
-#endif /* rppCONFIG_INCLUDE_LOUT */
+int8_t rpp_lout_update()
+{
+#ifndef FREERTOS_POSIX
+       RPP_MUTEX_LOCK(mutex_lout);
+       // Update output values
+       lout_set_word(out_cache);
+       // FIXME: Check which SPI transfer statuses could be considered errors
+       lout_spi_transfer();
+
+       // Read back diagnostic values
+       // FIXME: Implement. Dummy assign for now.
+       diag_cache = out_cache;
+
+       if (diag_cache != out_cache) {
+               RPP_MUTEX_UNLOCK(mutex_lout);
+               return FAILURE;
+       }
+       RPP_MUTEX_UNLOCK(mutex_lout);
+#endif
 
+       return SUCCESS;
+}