]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/rpp/gio.c
Make the RPP layer thread safe
[pes-rpp/rpp-lib.git] / rpp / src / rpp / gio.c
index 4cbdefcb4eefc13535992c66e2cce00bf32a69ba..8c83c484e1462e28a28ab6328c6e3d2d93fea238 100644 (file)
@@ -13,6 +13,9 @@
 #include "base.h"
 #include "rpp/gio.h"
 #include "drv/gio_tab.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_gio);
 
 static uint32_t ports_initialized = 0;
 
@@ -25,6 +28,8 @@ STATIC_ASSERT(RPP_GIO_PORT_GIOA == (1 << GIO_PORT_GIOA) &&
 int8_t rpp_gio_init(uint32_t init_ports)
 {
        enum pin_name pin;
+       if (!RPP_MUTEX_INIT(mutex_gio))
+               return FAILURE;
 
        gioREG->GCR0 = 1;   // Bring GIO out of reset
 
@@ -45,7 +50,7 @@ int8_t rpp_gio_set(enum pin_name pin, boolean_t value)
        if (ports_initialized == 0 || pin >= _PIN_COUNT || pin < 0)
                return FAILURE;
 
-       gio_tab_set(pin, value);
+       gio_tab_set(pin, value);        /* Thread safe */
        return SUCCESS;
 }
 
@@ -54,7 +59,9 @@ int8_t rpp_gio_get(enum pin_name pin)
        if (ports_initialized == 0 || pin >= _PIN_COUNT || pin < 0)
                return FAILURE;
 
-       return gio_tab_get(pin) ? 1 : 0;
+       boolean_t ret_val = gio_tab_get(pin) ? 1 : 0; /* Thread safe */
+
+       return ret_val;
 }
 
 int8_t rpp_gio_setup(enum pin_name pin, enum rpp_gio_io io, enum rpp_gio_in_mode in_mode,
@@ -73,6 +80,9 @@ int8_t rpp_gio_setup(enum pin_name pin, enum rpp_gio_io io, enum rpp_gio_in_mode
        dsc |= mode_flags[in_mode];
        dsc |= open_drain ? GIO_PIN_CONF_OD_ON : GIO_PIN_CONF_OD_OFF;
 
+       RPP_MUTEX_LOCK(mutex_gio);
        gio_setup(dsc);
+       RPP_MUTEX_UNLOCK(mutex_gio);
+
        return SUCCESS;
 }