int8_t rpp_hbr_init()
{
- if(initialized) {
- return FAILURE;
- }
- initialized = TRUE;
+ if (initialized)
+ return FAILURE;
+ initialized = TRUE;
#ifndef FREERTOS_POSIX
- dmmInit();
- hetInit();
- spi_tms570_init();
+ dmmInit();
+ hetInit();
+ spi_tms570_init();
#endif
- return SUCCESS;
+ return SUCCESS;
}
static void rpp_hdr_reset()
{
#ifndef FREERTOS_POSIX
- drv_hbr_set_en(LOW);
- drv_hbr_pwm_set_duty(0);
- drv_hbr_pwm_stop();
- drv_hbr_set_dir(LOW);
+ 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 (enabled)
+ return FAILURE;
- if(period < 1) {
- period = 55; // ~18kHz (18181.818181818 Hz to be precise)
- }
+ if (period < 1)
+ period = 55; // ~18kHz (18181.818181818 Hz to be precise)
- rpp_hdr_reset();
+ rpp_hdr_reset();
#ifndef FREERTOS_POSIX
- // Configure N2HET
- if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS) {
- return FAILURE;
- }
- drv_hbr_pwm_start();
+ // Configure N2HET
+ if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS)
+ return FAILURE;
+ drv_hbr_pwm_start();
- drv_hbr_set_en(HIGH);
+ 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
+ return FAILURE;
- // Start watchdog
- int ret = drv_hbr_wdg_start();
- if (ret != SUCCESS && ret != -RPP_EBUSY) { // Don't fail if already started
- return FAILURE;
- }
#endif
- enabled = TRUE;
+ enabled = TRUE;
- return SUCCESS;
+ return SUCCESS;
}
int8_t rpp_hbr_control(double cmd)
{
- if(!enabled) {
- return -1;
- }
+ if (!enabled)
+ return -1;
- // Check range of the command
- if((cmd < -1.0) || (cmd > 1.0)) {
- return -2;
- }
+ // Check range of the command
+ if ((cmd < -1.0) || (cmd > 1.0))
+ return -2;
#ifndef FREERTOS_POSIX
- // Great, now scale and return to sanity world of ints :D
- int32_t scaled = (int32_t)(cmd * 100.0);
+ // Great, now scale and return to sanity world of ints :D
+ int32_t scaled = (int32_t)(cmd * 100.0);
- // Set direction
- drv_hbr_set_dir(scaled > 0);
+ // Set direction
+ drv_hbr_set_dir(scaled > 0);
- // Set PWM duty cycle
- drv_hbr_pwm_set_duty(abs(scaled));
+ // Set PWM duty cycle
+ drv_hbr_pwm_set_duty(abs(scaled));
#endif
- return SUCCESS;
+ return SUCCESS;
}
int8_t rpp_hbr_disable()
{
- if(!enabled) {
- return FAILURE;
- }
+ if (!enabled)
+ return FAILURE;
#ifndef FREERTOS_POSIX
- rpp_hdr_reset();
+ 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();
+ // 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();
#endif
- enabled = FALSE;
+ enabled = FALSE;
- return SUCCESS;
+ return SUCCESS;
}