]> 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 507506844b3b63839866c11f2ef6ded4a1496201..ca2366574fc936d57b328b19d6c2cf20666769f2 100644 (file)
@@ -19,6 +19,9 @@
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_lout);
 
 #ifndef FREERTOS_POSIX
 #include "drv/lout.h"
@@ -31,6 +34,8 @@ int8_t rpp_lout_init()
 {
        if (initialized)
                return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_lout))
+               return FAILURE;
        initialized = TRUE;
 #ifndef FREERTOS_POSIX
        spi_tms570_init();
@@ -49,10 +54,12 @@ int8_t rpp_lout_set(uint8_t pin, uint8_t val)
                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;
 }
 
@@ -64,15 +71,20 @@ int8_t rpp_lout_diag(uint8_t pin)
        if ((pin < 1) || (pin > 8))
                return -1;
 
+       int8_t ret_val = LOW;
+
+       RPP_MUTEX_LOCK(mutex_lout);
        if (is_bit_set(diag_cache, pin - 1))
-               return HIGH;
-       return LOW;
+               ret_val = HIGH;
+       RPP_MUTEX_UNLOCK(mutex_lout);
+       return ret_val;
 }
 
 
 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
@@ -82,9 +94,11 @@ int8_t rpp_lout_update()
        // FIXME: Implement. Dummy assign for now.
        diag_cache = out_cache;
 
-       if (diag_cache != out_cache)
+       if (diag_cache != out_cache) {
+               RPP_MUTEX_UNLOCK(mutex_lout);
                return FAILURE;
-
+       }
+       RPP_MUTEX_UNLOCK(mutex_lout);
 #endif
 
        return SUCCESS;