1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2017 NXP
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef _FSL_DAC_H_
32 #define _FSL_DAC_H_
33
34 #include "fsl_common.h"
35
36 /*!
37 * @addtogroup dac
38 * @{
39 */
40
41
42 /*******************************************************************************
43 * Definitions
44 ******************************************************************************/
45
46 /*! @name Driver version */
47 /*@{*/
48 /*! @brief DAC driver version 2.0.1. */
49 #define FSL_DAC_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
50 /*@}*/
51
52 /*!
53 * @brief DAC buffer flags.
54 */
55 enum _dac_buffer_status_flags
56 {
57 #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
58 kDAC_BufferWatermarkFlag = DAC_SR_DACBFWMF_MASK, /*!< DAC Buffer Watermark Flag. */
59 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
60 kDAC_BufferReadPointerTopPositionFlag = DAC_SR_DACBFRPTF_MASK, /*!< DAC Buffer Read Pointer Top Position Flag. */
61 kDAC_BufferReadPointerBottomPositionFlag = DAC_SR_DACBFRPBF_MASK, /*!< DAC Buffer Read Pointer Bottom Position
62 Flag. */
63 };
64
65 /*!
66 * @brief DAC buffer interrupts.
67 */
68 enum _dac_buffer_interrupt_enable
69 {
70 #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
71 kDAC_BufferWatermarkInterruptEnable = DAC_C0_DACBWIEN_MASK, /*!< DAC Buffer Watermark Interrupt Enable. */
72 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
73 kDAC_BufferReadPointerTopInterruptEnable = DAC_C0_DACBTIEN_MASK, /*!< DAC Buffer Read Pointer Top Flag Interrupt
74 Enable. */
75 kDAC_BufferReadPointerBottomInterruptEnable = DAC_C0_DACBBIEN_MASK, /*!< DAC Buffer Read Pointer Bottom Flag
76 Interrupt Enable */
77 };
78
79 /*!
80 * @brief DAC reference voltage source.
81 */
82 typedef enum _dac_reference_voltage_source
83 {
84 kDAC_ReferenceVoltageSourceVref1 = 0U, /*!< The DAC selects DACREF_1 as the reference voltage. */
85 kDAC_ReferenceVoltageSourceVref2 = 1U, /*!< The DAC selects DACREF_2 as the reference voltage. */
86 } dac_reference_voltage_source_t;
87
88 /*!
89 * @brief DAC buffer trigger mode.
90 */
91 typedef enum _dac_buffer_trigger_mode
92 {
93 kDAC_BufferTriggerByHardwareMode = 0U, /*!< The DAC hardware trigger is selected. */
94 kDAC_BufferTriggerBySoftwareMode = 1U, /*!< The DAC software trigger is selected. */
95 } dac_buffer_trigger_mode_t;
96
97 #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
98 /*!
99 * @brief DAC buffer watermark.
100 */
101 typedef enum _dac_buffer_watermark
102 {
103 #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_1_WORD) && FSL_FEATURE_DAC_HAS_WATERMARK_1_WORD
104 kDAC_BufferWatermark1Word = 0U, /*!< 1 word away from the upper limit. */
105 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_1_WORD */
106 #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_2_WORDS) && FSL_FEATURE_DAC_HAS_WATERMARK_2_WORDS
107 kDAC_BufferWatermark2Word = 1U, /*!< 2 words away from the upper limit. */
108 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_2_WORDS */
109 #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_3_WORDS) && FSL_FEATURE_DAC_HAS_WATERMARK_3_WORDS
110 kDAC_BufferWatermark3Word = 2U, /*!< 3 words away from the upper limit. */
111 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_3_WORDS */
112 #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_4_WORDS) && FSL_FEATURE_DAC_HAS_WATERMARK_4_WORDS
113 kDAC_BufferWatermark4Word = 3U, /*!< 4 words away from the upper limit. */
114 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_4_WORDS */
115 } dac_buffer_watermark_t;
116 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
117
118 /*!
119 * @brief DAC buffer work mode.
120 */
121 typedef enum _dac_buffer_work_mode
122 {
123 kDAC_BufferWorkAsNormalMode = 0U, /*!< Normal mode. */
124 #if defined(FSL_FEATURE_DAC_HAS_BUFFER_SWING_MODE) && FSL_FEATURE_DAC_HAS_BUFFER_SWING_MODE
125 kDAC_BufferWorkAsSwingMode, /*!< Swing mode. */
126 #endif /* FSL_FEATURE_DAC_HAS_BUFFER_SWING_MODE */
127 kDAC_BufferWorkAsOneTimeScanMode, /*!< One-Time Scan mode. */
128 #if defined(FSL_FEATURE_DAC_HAS_BUFFER_FIFO_MODE) && FSL_FEATURE_DAC_HAS_BUFFER_FIFO_MODE
129 kDAC_BufferWorkAsFIFOMode, /*!< FIFO mode. */
130 #endif /* FSL_FEATURE_DAC_HAS_BUFFER_FIFO_MODE */
131 } dac_buffer_work_mode_t;
132
133 /*!
134 * @brief DAC module configuration.
135 */
136 typedef struct _dac_config
137 {
138 dac_reference_voltage_source_t referenceVoltageSource; /*!< Select the DAC reference voltage source. */
139 bool enableLowPowerMode; /*!< Enable the low-power mode. */
140 } dac_config_t;
141
142 /*!
143 * @brief DAC buffer configuration.
144 */
145 typedef struct _dac_buffer_config
146 {
147 dac_buffer_trigger_mode_t triggerMode; /*!< Select the buffer's trigger mode. */
148 #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
149 dac_buffer_watermark_t watermark; /*!< Select the buffer's watermark. */
150 #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
151 dac_buffer_work_mode_t workMode; /*!< Select the buffer's work mode. */
152 uint8_t upperLimit; /*!< Set the upper limit for the buffer index.
153 Normally, 0-15 is available for a buffer with 16 items. */
154 } dac_buffer_config_t;
155
156 /*******************************************************************************
157 * API
158 ******************************************************************************/
159 #if defined(__cplusplus)
160 extern "C" {
161 #endif
162
163 /*!
164 * @name Initialization
165 * @{
166 */
167
168 /*!
169 * @brief Initializes the DAC module.
170 *
171 * This function initializes the DAC module including the following operations.
172 * - Enabling the clock for DAC module.
173 * - Configuring the DAC converter with a user configuration.
174 * - Enabling the DAC module.
175 *
176 * @param base DAC peripheral base address.
177 * @param config Pointer to the configuration structure. See "dac_config_t".
178 */
179 void DAC_Init(DAC_Type *base, const dac_config_t *config);
180
181 /*!
182 * @brief De-initializes the DAC module.
183 *
184 * This function de-initializes the DAC module including the following operations.
185 * - Disabling the DAC module.
186 * - Disabling the clock for the DAC module.
187 *
188 * @param base DAC peripheral base address.
189 */
190 void DAC_Deinit(DAC_Type *base);
191
192 /*!
193 * @brief Initializes the DAC user configuration structure.
194 *
195 * This function initializes the user configuration structure to a default value. The default values are as follows.
196 * @code
197 * config->referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2;
198 * config->enableLowPowerMode = false;
199 * @endcode
200 * @param config Pointer to the configuration structure. See "dac_config_t".
201 */
202 void DAC_GetDefaultConfig(dac_config_t *config);
203
204 /*!
205 * @brief Enables the DAC module.
206 *
207 * @param base DAC peripheral base address.
208 * @param enable Enables or disables the feature.
209 */
DAC_Enable(DAC_Type * base,bool enable)210 static inline void DAC_Enable(DAC_Type *base, bool enable)
211 {
212 if (enable)
213 {
214 base->C0 |= DAC_C0_DACEN_MASK;
215 }
216 else
217 {
218 base->C0 &= ~DAC_C0_DACEN_MASK;
219 }
220 }
221
222 /* @} */
223
224 /*!
225 * @name Buffer
226 * @{
227 */
228
229 /*!
230 * @brief Enables the DAC buffer.
231 *
232 * @param base DAC peripheral base address.
233 * @param enable Enables or disables the feature.
234 */
DAC_EnableBuffer(DAC_Type * base,bool enable)235 static inline void DAC_EnableBuffer(DAC_Type *base, bool enable)
236 {
237 if (enable)
238 {
239 base->C1 |= DAC_C1_DACBFEN_MASK;
240 }
241 else
242 {
243 base->C1 &= ~DAC_C1_DACBFEN_MASK;
244 }
245 }
246
247 /*!
248 * @brief Configures the CMP buffer.
249 *
250 * @param base DAC peripheral base address.
251 * @param config Pointer to the configuration structure. See "dac_buffer_config_t".
252 */
253 void DAC_SetBufferConfig(DAC_Type *base, const dac_buffer_config_t *config);
254
255 /*!
256 * @brief Initializes the DAC buffer configuration structure.
257 *
258 * This function initializes the DAC buffer configuration structure to default values. The default values are as follows.
259 * @code
260 * config->triggerMode = kDAC_BufferTriggerBySoftwareMode;
261 * config->watermark = kDAC_BufferWatermark1Word;
262 * config->workMode = kDAC_BufferWorkAsNormalMode;
263 * config->upperLimit = DAC_DATL_COUNT - 1U;
264 * @endcode
265 * @param config Pointer to the configuration structure. See "dac_buffer_config_t".
266 */
267 void DAC_GetDefaultBufferConfig(dac_buffer_config_t *config);
268
269 /*!
270 * @brief Enables the DMA for DAC buffer.
271 *
272 * @param base DAC peripheral base address.
273 * @param enable Enables or disables the feature.
274 */
DAC_EnableBufferDMA(DAC_Type * base,bool enable)275 static inline void DAC_EnableBufferDMA(DAC_Type *base, bool enable)
276 {
277 if (enable)
278 {
279 base->C1 |= DAC_C1_DMAEN_MASK;
280 }
281 else
282 {
283 base->C1 &= ~DAC_C1_DMAEN_MASK;
284 }
285 }
286
287 /*!
288 * @brief Sets the value for items in the buffer.
289 *
290 * @param base DAC peripheral base address.
291 * @param index Setting the index for items in the buffer. The available index should not exceed the size of the DAC buffer.
292 * @param value Setting the value for items in the buffer. 12-bits are available.
293 */
294 void DAC_SetBufferValue(DAC_Type *base, uint8_t index, uint16_t value);
295
296 /*!
297 * @brief Triggers the buffer using software and updates the read pointer of the DAC buffer.
298 *
299 * This function triggers the function using software. The read pointer of the DAC buffer is updated with one step
300 * after this function is called. Changing the read pointer depends on the buffer's work mode.
301 *
302 * @param base DAC peripheral base address.
303 */
DAC_DoSoftwareTriggerBuffer(DAC_Type * base)304 static inline void DAC_DoSoftwareTriggerBuffer(DAC_Type *base)
305 {
306 base->C0 |= DAC_C0_DACSWTRG_MASK;
307 }
308
309 /*!
310 * @brief Gets the current read pointer of the DAC buffer.
311 *
312 * This function gets the current read pointer of the DAC buffer.
313 * The current output value depends on the item indexed by the read pointer. It is updated either
314 * by a software trigger or a hardware trigger.
315 *
316 * @param base DAC peripheral base address.
317 *
318 * @return The current read pointer of the DAC buffer.
319 */
DAC_GetBufferReadPointer(DAC_Type * base)320 static inline uint8_t DAC_GetBufferReadPointer(DAC_Type *base)
321 {
322 return ((base->C2 & DAC_C2_DACBFRP_MASK) >> DAC_C2_DACBFRP_SHIFT);
323 }
324
325 /*!
326 * @brief Sets the current read pointer of the DAC buffer.
327 *
328 * This function sets the current read pointer of the DAC buffer.
329 * The current output value depends on the item indexed by the read pointer. It is updated either by a
330 * software trigger or a hardware trigger. After the read pointer changes, the DAC output value also changes.
331 *
332 * @param base DAC peripheral base address.
333 * @param index Setting an index value for the pointer.
334 */
335 void DAC_SetBufferReadPointer(DAC_Type *base, uint8_t index);
336
337 /*!
338 * @brief Enables interrupts for the DAC buffer.
339 *
340 * @param base DAC peripheral base address.
341 * @param mask Mask value for interrupts. See "_dac_buffer_interrupt_enable".
342 */
343 void DAC_EnableBufferInterrupts(DAC_Type *base, uint32_t mask);
344
345 /*!
346 * @brief Disables interrupts for the DAC buffer.
347 *
348 * @param base DAC peripheral base address.
349 * @param mask Mask value for interrupts. See "_dac_buffer_interrupt_enable".
350 */
351 void DAC_DisableBufferInterrupts(DAC_Type *base, uint32_t mask);
352
353 /*!
354 * @brief Gets the flags of events for the DAC buffer.
355 *
356 * @param base DAC peripheral base address.
357 *
358 * @return Mask value for the asserted flags. See "_dac_buffer_status_flags".
359 */
360 uint32_t DAC_GetBufferStatusFlags(DAC_Type *base);
361
362 /*!
363 * @brief Clears the flags of events for the DAC buffer.
364 *
365 * @param base DAC peripheral base address.
366 * @param mask Mask value for flags. See "_dac_buffer_status_flags_t".
367 */
368 void DAC_ClearBufferStatusFlags(DAC_Type *base, uint32_t mask);
369
370 /* @} */
371
372 #if defined(__cplusplus)
373 }
374 #endif
375 /*!
376 * @}
377 */
378 #endif /* _FSL_DAC_H_ */
379