1 /* Copyright (C) 2013 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
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.
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.
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/>.
21 * Analog Output RPP API implementation file.
25 * RPP API documentation.
31 #if rppCONFIG_INCLUDE_AOUT == 1
33 #if rppCONFIG_DRV == 1
37 static boolean_t initialized = FALSE;
39 int8_t rpp_aout_init()
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);
57 static boolean_t changed_st[4] = {
58 FALSE, FALSE, FALSE, FALSE
60 static boolean_t enabled_cache[4] = {
61 FALSE, FALSE, FALSE, FALSE
63 static uint16_t out_cache[4] = {
67 int8_t rpp_aout_setup(uint8_t pin, boolean_t enabled)
70 if((pin < 1) || (pin > 4)) {
74 uint8_t index = pin - 1;
77 enabled_cache[index] = enabled;
80 changed_st[index] = TRUE;
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));
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)
95 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
98 int8_t rpp_aout_set(uint8_t pin, uint16_t val)
101 if((pin < 1) || (pin > 4)) {
106 val = map(val, 0, 4095, 0, dac_top);
113 uint8_t index = pin - 1;
115 // Set value to output cache
116 out_cache[index] = val;
119 changed_st[index] = TRUE;
125 int8_t rpp_aout_set_voltage(uint8_t pin, uint16_t mv)
128 if((pin < 1) || (pin > 4)) {
132 // Check millivolts range
137 // Calculate millivolts -> value
138 int val = (int)(4095.0 * ((float)mv / 1000.0) / (RPP_DAC_OA*RPP_DAC_VREF));
143 uint8_t index = pin - 1;
145 // Set value to output cache
146 out_cache[index] = val;
149 changed_st[index] = TRUE;
155 int8_t rpp_aout_update()
158 for(i = 0; i < 4; i++) {
160 // If changed commit changes to hardware
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]);
167 UNUSED(enabled_cache);
171 changed_st[i] = FALSE;
179 #endif /* rppCONFIG_INCLUDE_AOUT */