]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/dac.c
Compile errors fixed
[pes-rpp/rpp-lib.git] / rpp / src / rpp / dac.c
1 /* Copyright (C) 2013 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 : dac.c
12  * Abstract:
13  *     Analog Output RPP API implementation file.
14  *
15  * References:
16  *     dac.h
17  *     RPP API documentation.
18  */
19
20
21 #include "rpp/rpp.h"
22
23 #if rppCONFIG_DRV == 1
24 #include "drv/dac.h"
25 #endif
26
27 static boolean_t initialized = FALSE;
28
29 int8_t rpp_dac_init()
30 {
31     if(initialized) {
32         return FAILURE;
33     }
34     initialized = TRUE;
35 #if rppCONFIG_DRV == 1
36     spi_tms570_init();
37 #endif
38     // Configure board
39     // FIXME find out why board has default output of ~3.8V
40     //rpp_dac_setup(1, FALSE);
41     //rpp_dac_setup(2, FALSE);
42     //rpp_dac_setup(3, FALSE);
43     //rpp_dac_setup(4, FALSE);
44
45     return SUCCESS;
46 }
47
48
49 static boolean_t changed_st[4] = {
50         FALSE, FALSE, FALSE, FALSE
51     };
52 static boolean_t enabled_cache[4] = {
53         FALSE, FALSE, FALSE, FALSE
54     };
55 static uint16_t out_cache[4] = {
56         0, 0, 0, 0
57     };
58
59 int8_t rpp_dac_setup(uint8_t pin, boolean_t enabled)
60 {
61     // Check range
62     if((pin < 1) || (pin > 4)) {
63         return -1;
64     }
65
66     uint8_t index = pin - 1;
67
68     // Mark state
69     enabled_cache[index] = enabled;
70
71     // Mark as changed
72     changed_st[index] = TRUE;
73
74     return SUCCESS;
75 }
76
77
78 // Value that tops the DAC output
79 const static uint16_t dac_top =
80         (uint16_t)(4095.0 * (12000.0 / 1000.0) / (RPP_DAC_OA * RPP_DAC_VREF));
81
82 // Simple mapping function
83 static int32_t map(int32_t x,
84         int32_t in_min,  int32_t in_max,
85         int32_t out_min, int32_t out_max)
86 {
87     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
88 }
89
90 int8_t rpp_dac_set(uint8_t pin, uint16_t val)
91 {
92     // Check pin range
93     if((pin < 1) || (pin > 4)) {
94         return -1;
95     }
96
97     // Map value
98     val = map(val, 0, 4095, 0, dac_top);
99
100     // Check value range
101     if(val > 4095) {
102         return -2;
103     }
104
105     uint8_t index = pin - 1;
106
107     // Set value to output cache
108     out_cache[index] = val;
109
110     // Mark as changed
111     changed_st[index] = TRUE;
112
113     return SUCCESS;
114 }
115
116
117 int8_t rpp_dac_set_voltage(uint8_t pin, uint16_t mv)
118 {
119     // Check pin range
120     if((pin < 1) || (pin > 4)) {
121         return -1;
122     }
123
124     // Check millivolts range
125     if(mv > 12000) {
126         return -2;
127     }
128
129     // Calculate millivolts -> value
130     int val = (int)(4095.0 * ((float)mv / 1000.0) / (RPP_DAC_OA*RPP_DAC_VREF));
131     if(val > 4095) {
132         val = 4095;
133     }
134
135     uint8_t index = pin - 1;
136
137     // Set value to output cache
138     out_cache[index] = val;
139
140     // Mark as changed
141     changed_st[index] = TRUE;
142
143     return SUCCESS;
144 }
145
146
147 int8_t rpp_dac_update()
148 {
149     int i = 0;
150     for(i = 0; i < 4; i++) {
151
152         // If changed commit changes to hardware
153         if(changed_st[i]) {
154
155             #if rppCONFIG_DRV == 1
156             // TODO: Confirm via returned SPI code that transfer was successfull
157             drv_dac_spi_transfer(i, enabled_cache[i], out_cache[i]);
158             #else
159             UNUSED(enabled_cache);
160             UNUSED(out_cache);
161             #endif
162
163             changed_st[i] = FALSE;
164         }
165     }
166
167     return SUCCESS;
168 }