]> rtime.felk.cvut.cz Git - rpp-test-sw.git/blob - rpp/lib/rpp/src/rpp/aout.c
Yet another place to fix
[rpp-test-sw.git] / rpp / lib / rpp / src / rpp / aout.c
1 /* Copyright (C) 2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * File : aout.c
20  * Abstract:
21  *     Analog Output RPP API implementation file.
22  *
23  * References:
24  *     aout.h
25  *     RPP API documentation.
26  */
27
28
29 #include "rpp/rpp.h"
30
31 #if rppCONFIG_INCLUDE_AOUT == 1
32
33 #if rppCONFIG_DRV == 1
34 #include "drv/dac.h"
35 #endif
36
37 static boolean_t initialized = FALSE;
38
39 int8_t rpp_aout_init()
40 {
41     if(initialized) {
42         return FAILURE;
43     }
44     initialized = TRUE;
45
46     // Configure board
47     // FIXME find out why board has default output of ~3.8V
48     //rpp_aout_setup(1, FALSE);
49     //rpp_aout_setup(2, FALSE);
50     //rpp_aout_setup(3, FALSE);
51     //rpp_aout_setup(4, FALSE);
52
53     return SUCCESS;
54 }
55
56
57 static boolean_t changed_st[4] = {
58         FALSE, FALSE, FALSE, FALSE
59     };
60 static boolean_t enabled_cache[4] = {
61         FALSE, FALSE, FALSE, FALSE
62     };
63 static uint16_t out_cache[4] = {
64         0, 0, 0, 0
65     };
66
67 int8_t rpp_aout_setup(uint8_t pin, boolean_t enabled)
68 {
69     // Check range
70     if((pin < 1) || (pin > 4)) {
71         return -1;
72     }
73
74     uint8_t index = pin - 1;
75
76     // Mark state
77     enabled_cache[index] = enabled;
78
79     // Mark as changed
80     changed_st[index] = TRUE;
81
82     return SUCCESS;
83 }
84
85
86 // Value that tops the DAC output
87 const static uint16_t dac_top =
88         (uint16_t)(4095.0 * (12000.0 / 1000.0) / (RPP_DAC_OA * RPP_DAC_VREF));
89
90 // Simple mapping function
91 static int32_t map(int32_t x,
92         int32_t in_min,  int32_t in_max,
93         int32_t out_min, int32_t out_max)
94 {
95     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
96 }
97
98 int8_t rpp_aout_set(uint8_t pin, uint16_t val)
99 {
100     // Check pin range
101     if((pin < 1) || (pin > 4)) {
102         return -1;
103     }
104
105     // Map value
106     val = map(val, 0, 4095, 0, dac_top);
107
108     // Check value range
109     if(val > 4095) {
110         return -2;
111     }
112
113     uint8_t index = pin - 1;
114
115     // Set value to output cache
116     out_cache[index] = val;
117
118     // Mark as changed
119     changed_st[index] = TRUE;
120
121     return SUCCESS;
122 }
123
124
125 int8_t rpp_aout_set_voltage(uint8_t pin, uint16_t mv)
126 {
127     // Check pin range
128     if((pin < 1) || (pin > 4)) {
129         return -1;
130     }
131
132     // Check millivolts range
133     if(mv > 12000) {
134         return -2;
135     }
136
137     // Calculate millivolts -> value
138     int val = (int)(4095.0 * ((float)mv / 1000.0) / (RPP_DAC_OA*RPP_DAC_VREF));
139     if(val > 4095) {
140         val = 4095;
141     }
142
143     uint8_t index = pin - 1;
144
145     // Set value to output cache
146     out_cache[index] = val;
147
148     // Mark as changed
149     changed_st[index] = TRUE;
150
151     return SUCCESS;
152 }
153
154
155 int8_t rpp_aout_update()
156 {
157     int i = 0;
158     for(i = 0; i < 4; i++) {
159
160         // If changed commit changes to hardware
161         if(changed_st[i]) {
162
163             #if rppCONFIG_DRV == 1
164             // TODO: Confirm via returned SPI code that transfer was successfull
165             drv_dac_spi_transfer(i, enabled_cache[i], out_cache[i]);
166             #else
167             UNUSED(enabled_cache);
168             UNUSED(out_cache);
169             #endif
170
171             changed_st[i] = FALSE;
172         }
173     }
174
175     return SUCCESS;
176 }
177
178
179 #endif /* rppCONFIG_INCLUDE_AOUT */
180