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