]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/dac.c
Improve baudrate testing
[pes-rpp/rpp-lib.git] / rpp / src / rpp / dac.c
1 /* Copyright (C) 2013, 2015 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 #ifndef FREERTOS_POSIX
24 #include "drv/dac.h"
25 #include "drv/spi_tms570.h"
26 #endif
27
28 static boolean_t initialized = FALSE;
29
30 int8_t rpp_dac_init()
31 {
32         if (initialized)
33                 return FAILURE;
34         initialized = TRUE;
35 #ifndef FREERTOS_POSIX
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         uint8_t index = pin - 1;
66
67         // Mark state
68         enabled_cache[index] = enabled;
69
70         // Mark as changed
71         changed_st[index] = TRUE;
72
73         return SUCCESS;
74 }
75
76
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));
80
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)
85 {
86         return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
87 }
88
89 int8_t rpp_dac_set(uint8_t pin, uint16_t val)
90 {
91         // Check pin range
92         if ((pin < 1) || (pin > 4))
93                 return -1;
94
95         // Map value
96         val = map(val, 0, 4095, 0, dac_top);
97
98         // Check value range
99         if (val > 4095)
100                 return -2;
101
102         uint8_t index = pin - 1;
103
104         // Set value to output cache
105         out_cache[index] = val;
106
107         // Mark as changed
108         changed_st[index] = TRUE;
109
110         return SUCCESS;
111 }
112
113
114 int8_t rpp_dac_set_voltage(uint8_t pin, uint16_t mv)
115 {
116         // Check pin range
117         if ((pin < 1) || (pin > 4))
118                 return -1;
119
120         // Check millivolts range
121         if (mv > 12000)
122                 return -2;
123
124         // Calculate millivolts -> value
125         int val = (int)(4095.0 * ((float)mv / 1000.0) / (RPP_DAC_OA*RPP_DAC_VREF));
126         if (val > 4095)
127                 val = 4095;
128
129         uint8_t index = pin - 1;
130
131         // Set value to output cache
132         out_cache[index] = val;
133
134         // Mark as changed
135         changed_st[index] = TRUE;
136
137         return SUCCESS;
138 }
139
140
141 int8_t rpp_dac_update()
142 {
143         int i = 0;
144
145         for (i = 0; i < 4; i++) {
146
147                 // If changed commit changes to hardware
148                 if (changed_st[i]) {
149
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]);
153 #else
154                         UNUSED(enabled_cache);
155                         UNUSED(out_cache);
156 #endif
157
158                         changed_st[i] = FALSE;
159                 }
160         }
161
162         return SUCCESS;
163 }