-/* Copyright (C) 2013 Czech Technical University in Prague
+/* Copyright (C) 2013, 2014, 2015 Czech Technical University in Prague
*
* Authors:
* - Carlos Jenkins <carlos@jenkins.co.cr>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
*
* File : hbr.c
* Abstract:
#include "rpp/rpp.h"
+#include "rpp/mutex.h"
-#if rppCONFIG_INCLUDE_HBR == 1
-
-#if rppCONFIG_DRV == 1
+#ifndef FREERTOS_POSIX
#include "drv/hbridge.h"
+#include "drv/spi_tms570.h"
#endif
+RPP_MUTEX_DEFINE(mutex_hbr);
+
static boolean_t initialized = FALSE;
int8_t rpp_hbr_init()
{
- if(initialized) {
- return FAILURE;
- }
- initialized = TRUE;
-
- return SUCCESS;
+ if (initialized)
+ return FAILURE;
+ if (!RPP_MUTEX_INIT(mutex_hbr))
+ return FAILURE;
+ initialized = TRUE;
+#ifndef FREERTOS_POSIX
+ dmmInit();
+ hetInit();
+ spi_tms570_init();
+#endif
+ return SUCCESS;
}
// Private function to set the H-Bridge to default OFF settings
static void rpp_hdr_reset()
{
- #if rppCONFIG_DRV == 1
- hbr_set_en(LOW);
- hbr_pwm_set_duty(0);
- hbr_pwm_stop();
- hbr_set_dir(LOW);
- #endif
+#ifndef FREERTOS_POSIX
+ drv_hbr_set_en(LOW);
+ drv_hbr_pwm_set_duty(0);
+ drv_hbr_pwm_stop();
+ drv_hbr_set_dir(LOW);
+#endif
}
int8_t rpp_hbr_enable(int32_t period)
{
- if(enabled) {
- return FAILURE;
- }
-
- if(period < 1) {
- period = 55; // ~18kHz (18181.818181818 Hz to be precise)
- }
-
- #if rppCONFIG_DRV == 1
- hbr_pwm_set_signal(period, 0);
- rpp_hdr_reset();
- // FIXME enable watchdog task
- #endif
+ if (enabled)
+ return FAILURE;
+
+ if (period < 1)
+ period = 55; // ~18kHz (18181.818181818 Hz to be precise)
+
+#ifndef FREERTOS_POSIX
+ RPP_MUTEX_LOCK(mutex_hbr);
+ rpp_hdr_reset();
+
+ // Configure N2HET
+ 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
+ RPP_MUTEX_UNLOCK(mutex_hbr);
+ return FAILURE;
+ }
+ RPP_MUTEX_UNLOCK(mutex_hbr);
+#endif
- enabled = TRUE;
+ enabled = TRUE;
- return SUCCESS;
+ return SUCCESS;
}
int8_t rpp_hbr_control(double cmd)
{
- if(!enabled) {
- return -1;
- }
-
- // Check range of the command
- if((cmd < -1.0) || (cmd > 1.0)) {
- return -2;
- }
-
- #if rppCONFIG_DRV == 1
- // Great, now scale and return to sanity world of ints :D
- int32_t scaled = (int32_t)(cmd * 100.0);
-
- // Shutdown if required
- if(scaled == 0) {
- rpp_hdr_reset();
- return SUCCESS;
- }
-
- /// Enabled, configure
- // Set direction
- if(scaled < 0) {
- hbr_set_dir(LOW);
- } else {
- hbr_set_dir(HIGH);
- }
-
- // Set PWM duty cycle
- hbr_pwm_set_duty(abs(scaled));
- hbr_pwm_start();
-
- // Enable H-Bridge
- hbr_set_en(HIGH);
- #endif
-
- return SUCCESS;
-}
+ if (!enabled)
+ return -1;
+ // Check range of the command
+ if ((cmd < -1.0) || (cmd > 1.0))
+ return -2;
-int8_t rpp_hbr_disable()
-{
- if(!enabled) {
- return FAILURE;
- }
+#ifndef FREERTOS_POSIX
+ // Great, now scale and return to sanity world of ints :D
+ int32_t scaled = (int32_t)(cmd * 100.0);
+
+
+ // Set direction
+ RPP_MUTEX_LOCK(mutex_hbr);
+ drv_hbr_set_dir(scaled > 0);
- rpp_hdr_reset();
- enabled = FALSE;
+ // Set PWM duty cycle
+ drv_hbr_pwm_set_duty(abs(scaled));
+ RPP_MUTEX_UNLOCK(mutex_hbr);
+#endif
- return SUCCESS;
+ return SUCCESS;
}
+int8_t rpp_hbr_disable()
+{
+ if (!enabled)
+ 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
-#endif /* rppCONFIG_INCLUDE_HBR */
+ enabled = FALSE;
+ return SUCCESS;
+}