]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/hbr.c
5d3a457f025b97e4ae06453c2c02ee351ed89b42
[pes-rpp/rpp-lib.git] / rpp / src / rpp / hbr.c
1 /* Copyright (C) 2013, 2014 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
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.
10  *
11  * File : hbr.c
12  * Abstract:
13  *     H-Bridge Output RPP API implementation file.
14  *
15  * References:
16  *     hbr.h
17  *     RPP API documentation.
18  */
19
20
21 #include "rpp/rpp.h"
22
23 #ifndef FREERTOS_POSIX
24 #include "drv/hbridge.h"
25 #endif
26
27 static boolean_t initialized = FALSE;
28
29 int8_t rpp_hbr_init()
30 {
31         if (initialized)
32                 return FAILURE;
33         initialized = TRUE;
34 #ifndef FREERTOS_POSIX
35         dmmInit();
36         hetInit();
37         spi_tms570_init();
38 #endif
39         return SUCCESS;
40 }
41
42
43 // Private function to set the H-Bridge to default OFF settings
44 static void rpp_hdr_reset()
45 {
46 #ifndef FREERTOS_POSIX
47         drv_hbr_set_en(LOW);
48         drv_hbr_pwm_set_duty(0);
49         drv_hbr_pwm_stop();
50         drv_hbr_set_dir(LOW);
51 #endif
52 }
53
54
55 static boolean_t enabled = FALSE;
56
57 int8_t rpp_hbr_enable(int32_t period)
58 {
59         if (enabled)
60                 return FAILURE;
61
62         if (period < 1)
63                 period = 55;  // ~18kHz (18181.818181818 Hz to be precise)
64
65         rpp_hdr_reset();
66
67 #ifndef FREERTOS_POSIX
68
69         // Configure N2HET
70         if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS)
71                 return FAILURE;
72         drv_hbr_pwm_start();
73
74         drv_hbr_set_en(HIGH);
75
76         // Start watchdog
77         int ret = drv_hbr_wdg_start();
78         if (ret != SUCCESS && ret != -RPP_EBUSY)   // Don't fail if already started
79                 return FAILURE;
80
81 #endif
82
83         enabled = TRUE;
84
85         return SUCCESS;
86 }
87
88
89 int8_t rpp_hbr_control(double cmd)
90 {
91         if (!enabled)
92                 return -1;
93
94         // Check range of the command
95         if ((cmd < -1.0) || (cmd > 1.0))
96                 return -2;
97
98 #ifndef FREERTOS_POSIX
99         // Great, now scale and return to sanity world of ints :D
100         int32_t scaled = (int32_t)(cmd * 100.0);
101
102
103         // Set direction
104         drv_hbr_set_dir(scaled > 0);
105
106         // Set PWM duty cycle
107         drv_hbr_pwm_set_duty(abs(scaled));
108 #endif
109
110         return SUCCESS;
111 }
112
113
114 int8_t rpp_hbr_disable()
115 {
116         if (!enabled)
117                 return FAILURE;
118
119 #ifndef FREERTOS_POSIX
120         rpp_hdr_reset();
121
122         // We ignore is watchdog could not be stopped, because is harmless.
123         // It would be worse if we just could not stop the H-Bridge just because
124         // the watchdog could not be stopped.
125         drv_hbr_wdg_stop();
126 #endif
127
128         enabled = FALSE;
129
130         return SUCCESS;
131 }