]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/src/rpp/hbr.c
Make configuration of SPI devices target dependent
[pes-rpp/rpp-lib.git] / rpp / src / rpp / hbr.c
index 64dd6066d56e3c9fd9b1e50a72e8d28d83a59c89..d628092b7f2daa0252cec53985dfeac2b3598d11 100644 (file)
 
 
 #include "rpp/rpp.h"
+#include "rpp/mutex.h"
 
 #ifndef FREERTOS_POSIX
 #include "drv/hbridge.h"
-#include "hal/spi_tms570.h"
+#include "drv/spi_def.h"
 #endif
 
+RPP_MUTEX_DEFINE(mutex_hbr);
+
 static boolean_t initialized = FALSE;
 
 int8_t rpp_hbr_init()
 {
        if (initialized)
                return FAILURE;
+       if (!RPP_MUTEX_INIT(mutex_hbr))
+               return FAILURE;
        initialized = TRUE;
 #ifndef FREERTOS_POSIX
        dmmInit();
        hetInit();
-       spi_tms570_init();
+       spi_tms570_init(spi_ifcs, ARRAY_SIZE(spi_ifcs));
 #endif
        return SUCCESS;
 }
@@ -63,22 +68,26 @@ int8_t rpp_hbr_enable(int32_t period)
        if (period < 1)
                period = 55;  // ~18kHz (18181.818181818 Hz to be precise)
 
-       rpp_hdr_reset();
-
 #ifndef FREERTOS_POSIX
+       RPP_MUTEX_LOCK(mutex_hbr);
+       rpp_hdr_reset();
 
        // Configure N2HET
-       if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS)
+       if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS) {
+               RPP_MUTEX_UNLOCK(mutex_hbr);
                return FAILURE;
+       }
        drv_hbr_pwm_start();
 
        drv_hbr_set_en(HIGH);
 
        // Start watchdog
        int ret = drv_hbr_wdg_start();
-       if (ret != SUCCESS && ret != -RPP_EBUSY)   // Don't fail if already started
+       if (ret != SUCCESS && ret != -RPP_EBUSY) {   // Don't fail if already started
+               RPP_MUTEX_UNLOCK(mutex_hbr);
                return FAILURE;
-
+       }
+       RPP_MUTEX_UNLOCK(mutex_hbr);
 #endif
 
        enabled = TRUE;
@@ -102,10 +111,12 @@ int8_t rpp_hbr_control(double cmd)
 
 
        // Set direction
+       RPP_MUTEX_LOCK(mutex_hbr);
        drv_hbr_set_dir(scaled > 0);
 
        // Set PWM duty cycle
        drv_hbr_pwm_set_duty(abs(scaled));
+       RPP_MUTEX_UNLOCK(mutex_hbr);
 #endif
 
        return SUCCESS;
@@ -118,12 +129,14 @@ int8_t rpp_hbr_disable()
                return FAILURE;
 
 #ifndef FREERTOS_POSIX
+       RPP_MUTEX_LOCK(mutex_hbr);
        rpp_hdr_reset();
 
        // We ignore is watchdog could not be stopped, because is harmless.
        // It would be worse if we just could not stop the H-Bridge just because
        // the watchdog could not be stopped.
        drv_hbr_wdg_stop();
+       RPP_MUTEX_UNLOCK(mutex_hbr);
 #endif
 
        enabled = FALSE;