1 /* Copyright (C) 2013 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
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.
13 * Analog Output RPP API implementation file.
17 * RPP API documentation.
23 #if rppCONFIG_DRV == 1
27 static boolean_t initialized = FALSE;
35 #if rppCONFIG_DRV == 1
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);
49 static boolean_t changed_st[4] = {
50 FALSE, FALSE, FALSE, FALSE
52 static boolean_t enabled_cache[4] = {
53 FALSE, FALSE, FALSE, FALSE
55 static uint16_t out_cache[4] = {
59 int8_t rpp_dac_setup(uint8_t pin, boolean_t enabled)
62 if((pin < 1) || (pin > 4)) {
66 uint8_t index = pin - 1;
69 enabled_cache[index] = enabled;
72 changed_st[index] = TRUE;
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));
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)
87 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
90 int8_t rpp_dac_set(uint8_t pin, uint16_t val)
93 if((pin < 1) || (pin > 4)) {
98 val = map(val, 0, 4095, 0, dac_top);
105 uint8_t index = pin - 1;
107 // Set value to output cache
108 out_cache[index] = val;
111 changed_st[index] = TRUE;
117 int8_t rpp_dac_set_voltage(uint8_t pin, uint16_t mv)
120 if((pin < 1) || (pin > 4)) {
124 // Check millivolts range
129 // Calculate millivolts -> value
130 int val = (int)(4095.0 * ((float)mv / 1000.0) / (RPP_DAC_OA*RPP_DAC_VREF));
135 uint8_t index = pin - 1;
137 // Set value to output cache
138 out_cache[index] = val;
141 changed_st[index] = TRUE;
147 int8_t rpp_dac_update()
150 for(i = 0; i < 4; i++) {
152 // If changed commit changes to hardware
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]);
159 UNUSED(enabled_cache);
163 changed_st[i] = FALSE;