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