]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/hbr.c
Merge branch 'init_rework'
[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 #if rppCONFIG_DRV == 1
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     }
34     initialized = TRUE;
35 #if rppCONFIG_DRV == 1
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     #if rppCONFIG_DRV == 1
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
64     if(period < 1) {
65         period = 55; // ~18kHz (18181.818181818 Hz to be precise)
66     }
67
68     rpp_hdr_reset();
69
70     #if rppCONFIG_DRV == 1
71
72     // Configure N2HET
73     if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS) {
74         return FAILURE;
75     }
76     drv_hbr_pwm_start();
77
78     drv_hbr_set_en(HIGH);
79
80     // Start watchdog
81     int ret = drv_hbr_wdg_start();
82     if (ret != SUCCESS && ret != -RPP_EBUSY) { // Don't fail if already started
83         return FAILURE;
84     }
85     #endif
86
87     enabled = TRUE;
88
89     return SUCCESS;
90 }
91
92
93 int8_t rpp_hbr_control(double cmd)
94 {
95     if(!enabled) {
96         return -1;
97     }
98
99     // Check range of the command
100     if((cmd < -1.0) || (cmd > 1.0)) {
101         return -2;
102     }
103
104     #if rppCONFIG_DRV == 1
105     // Great, now scale and return to sanity world of ints :D
106     int32_t scaled = (int32_t)(cmd * 100.0);
107
108
109     // Set direction
110     drv_hbr_set_dir(scaled > 0);
111
112     // Set PWM duty cycle
113     drv_hbr_pwm_set_duty(abs(scaled));
114     #endif
115
116     return SUCCESS;
117 }
118
119
120 int8_t rpp_hbr_disable()
121 {
122     if(!enabled) {
123         return FAILURE;
124     }
125
126     #if rppCONFIG_DRV == 1
127     rpp_hdr_reset();
128
129     // We ignore is watchdog could not be stopped, because is harmless.
130     // It would be worse if we just could not stop the H-Bridge just because
131     // the watchdog could not be stopped.
132     drv_hbr_wdg_stop();
133     #endif
134
135     enabled = FALSE;
136
137     return SUCCESS;
138 }