]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/rpp/can.c
Make the RPP layer thread safe
[pes-rpp/rpp-lib.git] / rpp / src / rpp / can.c
index fbea3bef3a3beb103d185eff0439f709617fd517..ef86da43fed5819e4d536e677f06cf709df7821a 100644 (file)
@@ -22,6 +22,9 @@
 #include "rpp/rpp.h"
 #include <math.h>
 #include "sys/ti_drv_dmm.h"
+#include "rpp/mutex.h"
+
+RPP_MUTEX_DEFINE(mutex_can);
 
 static const struct rpp_can_config *can_config = NULL;
 
@@ -562,6 +565,9 @@ int8_t rpp_can_init(const struct rpp_can_config *config)
 {
        uint16_t i;
 
+       if (!RPP_MUTEX_INIT(mutex_can))
+               return FAILURE;
+
 #ifdef TARGET_TMS570_RPP
        dmmInit();
 
@@ -621,7 +627,6 @@ int8_t rpp_can_init(const struct rpp_can_config *config)
        return SUCCESS;
 }
 
-
 int8_t rpp_can_write(rpp_can_hw_obj hw_obj, const struct rpp_can_pdu *pdu)
 {
        uint8_t i;
@@ -631,6 +636,8 @@ int8_t rpp_can_write(rpp_can_hw_obj hw_obj, const struct rpp_can_pdu *pdu)
        if (!(controller = map_controller(tx_cfg->controller)))
                return -RPP_EINVAL;
 
+       RPP_MUTEX_LOCK(mutex_can);
+
        // Wait until IF1 is ready to use
        while (controller->IF1STAT & (1U << 7)) ;
 
@@ -660,6 +667,8 @@ int8_t rpp_can_write(rpp_can_hw_obj hw_obj, const struct rpp_can_pdu *pdu)
        // Copy TX data into message box
        controller->IF1NO = tx_cfg->msg_obj;
 
+       RPP_MUTEX_UNLOCK(mutex_can);
+
        return SUCCESS;
 }
 
@@ -692,9 +701,13 @@ int8_t rpp_can_read(rpp_can_hw_obj hw_obj, struct rpp_can_pdu *pdu)
        reg_index = (can_config->rx_config[hw_obj].msg_obj - 1) >> 5;
        bit_mask = 1 << ((can_config->rx_config[hw_obj].msg_obj - 1) & 0x1FU);
 
+       RPP_MUTEX_LOCK(mutex_can);
+
        // FIXME: Check whether to abort if there are no new data
-       if (!(controller->NWDATx[reg_index] & bit_mask))
+       if (!(controller->NWDATx[reg_index] & bit_mask)) {
+               RPP_MUTEX_UNLOCK(mutex_can);
                return -RPP_ENODATA;
+       }
 
        // Wait until IF2 is ready to use
        while (controller->IF2STAT & (1U << 7)) ;
@@ -723,6 +736,8 @@ int8_t rpp_can_read(rpp_can_hw_obj hw_obj, struct rpp_can_pdu *pdu)
 #endif
        }
 
+       RPP_MUTEX_UNLOCK(mutex_can);
+
        return SUCCESS;
 }