1 /* Copyright (C) 2013, 2014, 2015 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
6 * This document contains proprietary information belonging to Czech
7 * Technical University in Prague. Passing on and copying of this
8 * document, and communication of its contents is not permitted
9 * without prior written authorization.
13 * H-Bridge Output RPP API implementation file.
17 * RPP API documentation.
22 #include "rpp/mutex.h"
24 #ifndef FREERTOS_POSIX
25 #include "drv/hbridge.h"
26 #include "drv/spi_def.h"
29 RPP_MUTEX_DEFINE(mutex_hbr);
31 static boolean_t initialized = FALSE;
37 if (!RPP_MUTEX_INIT(mutex_hbr))
40 #ifndef FREERTOS_POSIX
43 spi_tms570_init(spi_ifcs, ARRAY_SIZE(spi_ifcs));
49 // Private function to set the H-Bridge to default OFF settings
50 static void rpp_hdr_reset()
52 #ifndef FREERTOS_POSIX
54 drv_hbr_pwm_set_duty(0);
61 static boolean_t enabled = FALSE;
63 int8_t rpp_hbr_enable(int32_t period)
69 period = 55; // ~18kHz (18181.818181818 Hz to be precise)
71 #ifndef FREERTOS_POSIX
72 RPP_MUTEX_LOCK(mutex_hbr);
76 if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS) {
77 RPP_MUTEX_UNLOCK(mutex_hbr);
85 int ret = drv_hbr_wdg_start();
86 if (ret != SUCCESS && ret != -RPP_EBUSY) { // Don't fail if already started
87 RPP_MUTEX_UNLOCK(mutex_hbr);
90 RPP_MUTEX_UNLOCK(mutex_hbr);
99 int8_t rpp_hbr_control(double cmd)
104 // Check range of the command
105 if ((cmd < -1.0) || (cmd > 1.0))
108 #ifndef FREERTOS_POSIX
109 // Great, now scale and return to sanity world of ints :D
110 int32_t scaled = (int32_t)(cmd * 100.0);
114 RPP_MUTEX_LOCK(mutex_hbr);
115 drv_hbr_set_dir(scaled > 0);
117 // Set PWM duty cycle
118 drv_hbr_pwm_set_duty(abs(scaled));
119 RPP_MUTEX_UNLOCK(mutex_hbr);
126 int8_t rpp_hbr_disable()
131 #ifndef FREERTOS_POSIX
132 RPP_MUTEX_LOCK(mutex_hbr);
135 // We ignore is watchdog could not be stopped, because is harmless.
136 // It would be worse if we just could not stop the H-Bridge just because
137 // the watchdog could not be stopped.
139 RPP_MUTEX_UNLOCK(mutex_hbr);