]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/rpp/dac.c
Make the RPP layer thread safe
[pes-rpp/rpp-lib.git] / rpp / src / rpp / dac.c
index b2bf2ba3249ccd41f0c6eb3e4d337d9196d6e043..f2bdbe0527050b7581f10da24bf5f44fba1d62b3 100644 (file)
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
 
 #ifndef FREERTOS_POSIX
 #include "drv/dac.h"
 #include "drv/spi_tms570.h"
 #endif
 
+RPP_MUTEX_DEFINE(mutex_dac);
+
 static boolean_t initialized = FALSE;
 
 int8_t rpp_dac_init()
 {
        if (initialized)
                return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_dac))
+               return FAILURE;
        initialized = TRUE;
 #ifndef FREERTOS_POSIX
        spi_tms570_init();
@@ -64,12 +69,13 @@ int8_t rpp_dac_setup(uint8_t pin, boolean_t enabled)
 
        uint8_t index = pin - 1;
 
+       RPP_MUTEX_LOCK(mutex_dac);
        // Mark state
        enabled_cache[index] = enabled;
 
        // Mark as changed
        changed_st[index] = TRUE;
-
+       RPP_MUTEX_UNLOCK(mutex_dac);
        return SUCCESS;
 }
 
@@ -101,11 +107,13 @@ int8_t rpp_dac_set(uint8_t pin, uint16_t val)
 
        uint8_t index = pin - 1;
 
+       RPP_MUTEX_LOCK(mutex_dac);
        // Set value to output cache
        out_cache[index] = val;
 
        // Mark as changed
        changed_st[index] = TRUE;
+       RPP_MUTEX_UNLOCK(mutex_dac);
 
        return SUCCESS;
 }
@@ -128,11 +136,13 @@ int8_t rpp_dac_set_voltage(uint8_t pin, uint16_t mv)
 
        uint8_t index = pin - 1;
 
+       RPP_MUTEX_LOCK(mutex_dac);
        // Set value to output cache
        out_cache[index] = val;
 
        // Mark as changed
        changed_st[index] = TRUE;
+       RPP_MUTEX_UNLOCK(mutex_dac);
 
        return SUCCESS;
 }
@@ -143,7 +153,7 @@ int8_t rpp_dac_update()
        int i = 0;
 
        for (i = 0; i < 4; i++) {
-
+               RPP_MUTEX_LOCK(mutex_dac);
                // If changed commit changes to hardware
                if (changed_st[i]) {
 
@@ -157,6 +167,7 @@ int8_t rpp_dac_update()
 
                        changed_st[i] = FALSE;
                }
+               RPP_MUTEX_UNLOCK(mutex_dac);
        }
 
        return SUCCESS;