1 /******************************************************************************
2 *  Filename:       aux_dac.h
3 *
4 *  Description:    Defines and prototypes for the AUX Digital-to-Analog
5 *                  Converter
6 *
7 *  Copyright (c) 2015 - 2022, Texas Instruments Incorporated
8 *  All rights reserved.
9 *
10 *  Redistribution and use in source and binary forms, with or without
11 *  modification, are permitted provided that the following conditions are met:
12 *
13 *  1) Redistributions of source code must retain the above copyright notice,
14 *     this list of conditions and the following disclaimer.
15 *
16 *  2) Redistributions in binary form must reproduce the above copyright notice,
17 *     this list of conditions and the following disclaimer in the documentation
18 *     and/or other materials provided with the distribution.
19 *
20 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
21 *     be used to endorse or promote products derived from this software without
22 *     specific prior written permission.
23 *
24 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 *  POSSIBILITY OF SUCH DAMAGE.
35 *
36 ******************************************************************************/
37 
38 //*****************************************************************************
39 //
40 //! \addtogroup aux_group
41 //! @{
42 //! \addtogroup auxdac_api
43 //! @{
44 //
45 //*****************************************************************************
46 
47 #ifndef __AUX_DAC_H__
48 #define __AUX_DAC_H__
49 
50 //*****************************************************************************
51 //
52 // If building with a C++ compiler, make all of the definitions in this header
53 // have a C binding.
54 //
55 //*****************************************************************************
56 #ifdef __cplusplus
57 extern "C"
58 {
59 #endif
60 
61 #include <stdbool.h>
62 #include <stdint.h>
63 #include "../inc/hw_types.h"
64 #include "../inc/hw_memmap.h"
65 #include "../inc/hw_adi_4_aux.h"
66 #include "../inc/hw_aux_anaif.h"
67 #include "../inc/hw_aux_sysif.h"
68 #include "../inc/hw_fcfg1.h"
69 
70 
71 //*****************************************************************************
72 //
73 // Defines for COMPA_IN connection to IO pins.
74 //
75 //*****************************************************************************
76 #define AUXDAC_COMPA_IN_NC             0x00
77 #define AUXDAC_COMPA_IN_AUXIO26        0x01
78 #define AUXDAC_COMPA_IN_AUXIO25        0x02
79 #define AUXDAC_COMPA_IN_AUXIO24        0x04
80 #define AUXDAC_COMPA_IN_AUXIO23        0x08
81 #define AUXDAC_COMPA_IN_AUXIO22        0x10
82 #define AUXDAC_COMPA_IN_AUXIO21        0x20
83 #define AUXDAC_COMPA_IN_AUXIO20        0x40
84 #define AUXDAC_COMPA_IN_AUXIO19        0x80
85 #define AUXDAC_COMPA_IN_AUXIO7         AUXDAC_COMPA_IN_AUXIO26
86 #define AUXDAC_COMPA_IN_AUXIO6         AUXDAC_COMPA_IN_AUXIO25
87 #define AUXDAC_COMPA_IN_AUXIO5         AUXDAC_COMPA_IN_AUXIO24
88 #define AUXDAC_COMPA_IN_AUXIO4         AUXDAC_COMPA_IN_AUXIO23
89 #define AUXDAC_COMPA_IN_AUXIO3         AUXDAC_COMPA_IN_AUXIO22
90 #define AUXDAC_COMPA_IN_AUXIO2         AUXDAC_COMPA_IN_AUXIO21
91 #define AUXDAC_COMPA_IN_AUXIO1         AUXDAC_COMPA_IN_AUXIO20
92 #define AUXDAC_COMPA_IN_AUXIO0         AUXDAC_COMPA_IN_AUXIO19
93 
94 //*****************************************************************************
95 //
96 // Defines for DAC reference sources.
97 //
98 //*****************************************************************************
99 #define AUXDAC_VREF_SEL_VDDS           ADI_4_AUX_MUX2_DAC_VREF_SEL_VDDS
100 #define AUXDAC_VREF_SEL_ADCREF         ADI_4_AUX_MUX2_DAC_VREF_SEL_ADCREF
101 #define AUXDAC_VREF_SEL_DCOUPL         ADI_4_AUX_MUX2_DAC_VREF_SEL_DCOUPL
102 #define AUXDAC_VREF_SEL_NC             ADI_4_AUX_MUX2_DAC_VREF_SEL_NC
103 
104 //*****************************************************************************
105 //
106 // API Functions and prototypes
107 //
108 //*****************************************************************************
109 
110 
111 //*****************************************************************************
112 //
113 //! \brief Sets the voltage reference source.
114 //!
115 //! The DAC's output range depends on the selection of a voltage reference
116 //! source. This function must be called before enabling the DAC using
117 //! \ref AUXDACEnable() or before setting a DAC code using
118 //! \ref AUXDACCalcCode().
119 //!
120 //! \param refSource
121 //!     DAC voltage reference source:
122 //!     - \ref AUXDAC_VREF_SEL_VDDS
123 //!     - \ref AUXDAC_VREF_SEL_ADCREF
124 //!     - \ref AUXDAC_VREF_SEL_DCOUPL
125 //!     - \ref AUXDAC_VREF_SEL_NC
126 //
127 //*****************************************************************************
128 extern void AUXDACSetVref(uint8_t refSource);
129 
130 //*****************************************************************************
131 //
132 //! \brief Enables DAC precharge.
133 //!
134 //! If \ref AUXDAC_VREF_SEL_DCOUPL has been selected as a voltage reference
135 //! source, the DAC's output voltage range can be further controlled by
136 //! enabling or disabling precharge.
137 //!
138 //! When precharge is enabled the DAC's output voltage will range from 1.28 V
139 //! to 2.56 V.
140 //!
141 //! \note This function must only be called if \ref AUXDAC_VREF_SEL_DCOUPL
142 //!       has been selected as a voltage reference source and while the DAC is
143 //!       disabled, before calling \ref AUXDACEnable().
144 //
145 //*****************************************************************************
146 extern void AUXDACEnablePreCharge(void);
147 
148 //*****************************************************************************
149 //
150 //! \brief Disables DAC precharge.
151 //!
152 //! If \ref AUXDAC_VREF_SEL_DCOUPL has been selected as a voltage reference
153 //! source, the DAC's output voltage range can be further controlled by
154 //! enabling or disabling precharge.
155 //!
156 //! When precharge is disabled the DAC's output voltage will range from 0 V
157 //! to 1.28 V.
158 //!
159 //! \note This function must only be called if \ref AUXDAC_VREF_SEL_DCOUPL
160 //!       has been selected as a voltage reference source and while the DAC is
161 //!       disabled, before calling \ref AUXDACEnable().
162 //
163 //*****************************************************************************
164 extern void AUXDACDisablePreCharge(void);
165 
166 //*****************************************************************************
167 //
168 //! \brief Calculates the upper limit voltage the DAC can output.
169 //!
170 //! This function uses calibration coefficients stored in the factory
171 //! configuration area (FCFG1) to determine the DAC's output voltage that
172 //! corresponds to DAC code 255. This voltage depends on the selected voltage
173 //! reference source.
174 //!
175 //! \note This function must only be called after \ref AUXDACSetVref has been
176 //! called.
177 //!
178 //! \return
179 //!     The maximum voltage in mV that the DAC can output given a specific
180 //!     reference voltage.
181 //
182 //*****************************************************************************
183 extern uint32_t AUXDACCalcMax(void);
184 
185 //*****************************************************************************
186 //
187 //! \brief Calculates the lower limit voltage the DAC can output.
188 //!
189 //! This function uses calibration coefficients stored in the factory
190 //! configuration area (FCFG1) to determine the DAC's output voltage that
191 //! corresponds to DAC code 1. This voltage depends on the selected voltage
192 //! reference source.
193 //!
194 //! DAC Code 0 corresponds to 0 V. Therefore, the DAC can only output 0 V and
195 //! values between the lower limit defined by this function and the upper limit
196 //! defined by \ref AUXDACCalcMax.
197 //!
198 //! \note This function must only be called after \ref AUXDACSetVref has been
199 //! called.
200 //!
201 //! \return
202 //!     The minimum voltage (other than 0 V) in mV that the DAC can output
203 //!     given a specific reference voltage.
204 //
205 //*****************************************************************************
206 extern uint32_t AUXDACCalcMin(void);
207 
208 //*****************************************************************************
209 //
210 //! \brief Calculates the appropriate DAC code for a specific voltage value.
211 //!
212 //! Once a voltage reference source has been selected with \ref AUXDACSetVref
213 //! and the DAC's output voltage range has been defined with \ref AUXDACCalcMin
214 //! and \ref AUXDACCalcMax, a DAC code can be derived.
215 //!
216 //! \param uVoltOut
217 //!     The desired output voltage in millivolts [mV].
218 //! \param uVoltOutMin
219 //!     The minimum output voltage in millivolts [mV] which can be output by
220 //!     the DAC in the current configuration. It should be the value returned
221 //!     by \ref AUXDACCalcMin.
222 //! \param uVoltOutMax
223 //!     The maximum output voltage in millivolts [mV] which can be output by
224 //!     the DAC in the current configuration. It should be the value returned
225 //!     by \ref AUXDACCalcMax.
226 //! \return
227 //!     The dac code corresponding to the desired output value.
228 //
229 //*****************************************************************************
230 extern uint32_t AUXDACCalcCode(uint32_t uVoltOut, uint32_t uVoltOutMin, uint32_t uVoltOutMax);
231 
232 //*****************************************************************************
233 //
234 //! \brief Configures and sets the DAC sample clock.
235 //!
236 //! This function determines the sample clock base frequency considering that
237 //! the operational rate for the DAC sample clock state machine is the AUX Bus
238 //! Rate.
239 //!
240 //! AUX Bus Rate divided by (dacClkDiv + 1) determines the sample clock base
241 //! frequency.
242 //!
243 //! \note This function must be called before \ref AUXDACEnable.
244 //!
245 //! \param dacClkDiv
246 //!     The clock division factor
247 //
248 //*****************************************************************************
249 extern void AUXDACSetSampleClock(uint8_t dacClkDiv);
250 
251 //*****************************************************************************
252 //
253 //! \brief Updates the current DAC code.
254 //!
255 //! This function is meant to be used after \ref AUXDACEnable has been
256 //! called. It allows to update the DAC code by rapidly disabling the DAC,
257 //! setting the new DAC code, and then rapidly re-enabling the DAC.
258 //!
259 //! \param dacCode
260 //!     The new DAC code
261 //
262 //*****************************************************************************
263 extern void AUXDACSetCode(uint8_t dacCode);
264 
265 //*****************************************************************************
266 //
267 //! \brief Enables the DAC.
268 //!
269 //! This function enables the DAC sample clock and the DAC buffer. It selects
270 //! COMPA_IN as output, and it routes the DAC's output to the given I/O pin.
271 //!
272 //! \param dacPinId
273 //!     Analog capable I/O pin:
274 //!     - \ref AUXDAC_COMPA_IN_AUXIO26
275 //!     - \ref AUXDAC_COMPA_IN_AUXIO25
276 //!     - \ref AUXDAC_COMPA_IN_AUXIO24
277 //!     - \ref AUXDAC_COMPA_IN_AUXIO23
278 //!     - \ref AUXDAC_COMPA_IN_AUXIO22
279 //!     - \ref AUXDAC_COMPA_IN_AUXIO21
280 //!     - \ref AUXDAC_COMPA_IN_AUXIO20
281 //!     - \ref AUXDAC_COMPA_IN_AUXIO19
282 //
283 //*****************************************************************************
284 extern void AUXDACEnable(uint8_t dacPinId);
285 
286 //*****************************************************************************
287 //
288 //! \brief Disables the DAC.
289 //!
290 //! This function disables the DAC sample clock, the DAC buffer, the DAC.
291 //! It also disconnects COMPA_IN internally.
292 //!
293 //! \note This function must be called only after \ref AUXDACEnable has been
294 //! called.
295 //
296 //*****************************************************************************
297 extern void AUXDACDisable(void);
298 
299 
300 //*****************************************************************************
301 //
302 // Mark the end of the C bindings section for C++ compilers.
303 //
304 //*****************************************************************************
305 #ifdef __cplusplus
306 }
307 #endif
308 
309 #endif // __AUX_DAC_H__
310 
311 //*****************************************************************************
312 //
313 //! Close the Doxygen group.
314 //! @}
315 //! @}
316 //
317 //*****************************************************************************
318