1 /* Copyright (C) 2013, 2015 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 #ifndef FREERTOS_POSIX
25 #include "drv/spi_tms570.h"
28 static boolean_t initialized = FALSE;
35 #ifndef FREERTOS_POSIX
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))
65 uint8_t index = pin - 1;
68 enabled_cache[index] = enabled;
71 changed_st[index] = TRUE;
77 // Value that tops the DAC output
78 const static uint16_t dac_top =
79 (uint16_t)(4095.0 * (12000.0 / 1000.0) / (RPP_DAC_OA * RPP_DAC_VREF));
81 // Simple mapping function
82 static int32_t map(int32_t x,
83 int32_t in_min, int32_t in_max,
84 int32_t out_min, int32_t out_max)
86 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
89 int8_t rpp_dac_set(uint8_t pin, uint16_t val)
92 if ((pin < 1) || (pin > 4))
96 val = map(val, 0, 4095, 0, dac_top);
102 uint8_t index = pin - 1;
104 // Set value to output cache
105 out_cache[index] = val;
108 changed_st[index] = TRUE;
114 int8_t rpp_dac_set_voltage(uint8_t pin, uint16_t mv)
117 if ((pin < 1) || (pin > 4))
120 // Check millivolts range
124 // Calculate millivolts -> value
125 int val = (int)(4095.0 * ((float)mv / 1000.0) / (RPP_DAC_OA*RPP_DAC_VREF));
129 uint8_t index = pin - 1;
131 // Set value to output cache
132 out_cache[index] = val;
135 changed_st[index] = TRUE;
141 int8_t rpp_dac_update()
145 for (i = 0; i < 4; i++) {
147 // If changed commit changes to hardware
150 #ifndef FREERTOS_POSIX
151 // TODO: Confirm via returned SPI code that transfer was successful
152 drv_dac_spi_transfer(i, enabled_cache[i], out_cache[i]);
154 UNUSED(enabled_cache);
158 changed_st[i] = FALSE;