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_DAC32_H_
10 #define _FSL_DAC32_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup dac32
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /*! @brief DAC32 driver version. */
23 #define FSL_DAC32_DRIVER_VERSION (MAKE_VERSION(2, 0, 2)) /*!< Version 2.0.2. */
24 
25 /*!
26  * @brief DAC32 buffer flags.
27  */
28 enum _dac32_buffer_status_flags
29 {
30     kDAC32_BufferWatermarkFlag = DAC_STATCTRL_DACBFWMF_MASK,
31     /*!< DAC32 Buffer Watermark Flag. */ /* FSL_FEATURE_DAC32_HAS_WATERMARK_DETECTION */
32     kDAC32_BufferReadPointerTopPositionFlag =
33         DAC_STATCTRL_DACBFRPTF_MASK, /*!< DAC32 Buffer Read Pointer Top Position Flag. */
34     kDAC32_BufferReadPointerBottomPositionFlag = DAC_STATCTRL_DACBFRPBF_MASK, /*!< DAC32 Buffer Read Pointer Bottom
35                                                                            Position Flag. */
36 };
37 
38 /*!
39  * @brief DAC32 buffer interrupts.
40  */
41 enum _dac32_buffer_interrupt_enable
42 {
43     kDAC32_BufferWatermarkInterruptEnable = DAC_STATCTRL_DACBWIEN_MASK, /*!< DAC32 Buffer Watermark Interrupt Enable. */
44     kDAC32_BufferReadPointerTopInterruptEnable = DAC_STATCTRL_DACBTIEN_MASK,    /*!< DAC32 Buffer Read Pointer Top Flag
45                                                                              Interrupt    Enable. */
46     kDAC32_BufferReadPointerBottomInterruptEnable = DAC_STATCTRL_DACBBIEN_MASK, /*!< DAC32 Buffer Read Pointer Bottom
47                                                                              Flag Interrupt Enable */
48 };
49 
50 /*!
51  * @brief DAC32 reference voltage source.
52  */
53 typedef enum _dac32_reference_voltage_source
54 {
55     kDAC32_ReferenceVoltageSourceVref1 = 0U, /*!< The DAC32 selects DACREF_1 as the reference voltage. */
56     kDAC32_ReferenceVoltageSourceVref2 = 1U, /*!< The DAC32 selects DACREF_2 as the reference voltage. */
57 } dac32_reference_voltage_source_t;
58 
59 /*!
60  * @brief DAC32 buffer trigger mode.
61  */
62 typedef enum _dac32_buffer_trigger_mode
63 {
64     kDAC32_BufferTriggerByHardwareMode = 0U, /*!< The DAC32 hardware trigger is selected. */
65     kDAC32_BufferTriggerBySoftwareMode = 1U, /*!< The DAC32 software trigger is selected. */
66 } dac32_buffer_trigger_mode_t;
67 
68 /*!
69  * @brief DAC32 buffer watermark.
70  */
71 typedef enum _dac32_buffer_watermark
72 {
73     kDAC32_BufferWatermark1Word = 0U, /*!< 1 word  away from the upper limit. */
74     kDAC32_BufferWatermark2Word = 1U, /*!< 2 words away from the upper limit. */
75     kDAC32_BufferWatermark3Word = 2U, /*!< 3 words away from the upper limit. */
76     kDAC32_BufferWatermark4Word = 3U, /*!< 4 words away from the upper limit. */
77 } dac32_buffer_watermark_t;
78 
79 /*!
80  * @brief DAC32 buffer work mode.
81  */
82 typedef enum _dac32_buffer_work_mode
83 {
84     kDAC32_BufferWorkAsNormalMode      = 0U, /*!< Normal mode. */
85     kDAC32_BufferWorkAsSwingMode       = 1U, /*!< Swing mode. */
86     kDAC32_BufferWorkAsOneTimeScanMode = 2U, /*!< One-Time Scan mode. */
87     kDAC32_BufferWorkAsFIFOMode        = 3U, /*!< FIFO mode. */
88 } dac32_buffer_work_mode_t;
89 
90 /*!
91  * @brief DAC32 module configuration.
92  */
93 typedef struct _dac32_config
94 {
95     dac32_reference_voltage_source_t referenceVoltageSource; /*!< Select the DAC32 reference voltage source. */
96     bool enableLowPowerMode;                                 /*!< Enable the low power mode. */
97 } dac32_config_t;
98 
99 /*!
100  * @brief DAC32 buffer configuration.
101  */
102 typedef struct _dac32_buffer_config
103 {
104     dac32_buffer_trigger_mode_t triggerMode; /*!< Select the buffer's trigger mode. */
105     dac32_buffer_watermark_t watermark;      /*!< Select the buffer's watermark. */
106     dac32_buffer_work_mode_t workMode;       /*!< Select the buffer's work mode. */
107     uint32_t upperLimit;                     /*!< Set the upper limit for buffer index.
108                                                  Normally, 0-15 is available for buffer with 16 item. */
109 } dac32_buffer_config_t;
110 
111 /*******************************************************************************
112  * API
113  ******************************************************************************/
114 #if defined(__cplusplus)
115 extern "C" {
116 #endif
117 
118 /*!
119  * @name Initialization and deinitiailzation
120  * @{
121  */
122 
123 /*!
124  * @brief Initializes the DAC32 module.
125  *
126  * This function initializes the DAC32 module, including:
127  *  - Enabling the clock for DAC32 module.
128  *  - Configuring the DAC32 converter with a user configuration.
129  *  - Enabling the DAC32 module.
130  *
131  * @param base DAC32 peripheral base address.
132  * @param config Pointer to the configuration structure. See "dac32_config_t".
133  */
134 void DAC32_Init(DAC_Type *base, const dac32_config_t *config);
135 
136 /*!
137  * @brief De-initializes the DAC32 module.
138  *
139  * This function de-initializes the DAC32 module, including:
140  *  - Disabling the DAC32 module.
141  *  - Disabling the clock for the DAC32 module.
142  *
143  * @param base DAC32 peripheral base address.
144  */
145 void DAC32_Deinit(DAC_Type *base);
146 
147 /*!
148  * @brief Initializes the DAC32 user configuration structure.
149  *
150  * This function initializes the user configuration structure to a default value. The default values are:
151  * @code
152  *   config->referenceVoltageSource = kDAC32_ReferenceVoltageSourceVref2;
153  *   config->enableLowPowerMode = false;
154  * @endcode
155  * @param config Pointer to the configuration structure. See "dac32_config_t".
156  */
157 void DAC32_GetDefaultConfig(dac32_config_t *config);
158 
159 /*!
160  * @brief Enables the DAC32 module.
161  *
162  * @param base DAC32 peripheral base address.
163  * @param enable Enables the feature or not.
164  */
DAC32_Enable(DAC_Type * base,bool enable)165 static inline void DAC32_Enable(DAC_Type *base, bool enable)
166 {
167     if (enable)
168     {
169         base->STATCTRL |= DAC_STATCTRL_DACEN_MASK;
170     }
171     else
172     {
173         base->STATCTRL &= ~DAC_STATCTRL_DACEN_MASK;
174     }
175 }
176 
177 /* @} */
178 
179 /*!
180  * @name Buffer
181  * @{
182  */
183 
184 /*!
185  * @brief Enables the DAC32 buffer.
186  *
187  * @param base DAC32 peripheral base address.
188  * @param enable Enables the feature or not.
189  */
DAC32_EnableBuffer(DAC_Type * base,bool enable)190 static inline void DAC32_EnableBuffer(DAC_Type *base, bool enable)
191 {
192     if (enable)
193     {
194         base->STATCTRL |= DAC_STATCTRL_DACBFEN_MASK;
195     }
196     else
197     {
198         base->STATCTRL &= ~DAC_STATCTRL_DACBFEN_MASK;
199     }
200 }
201 
202 /*!
203  * @brief Configures the DAC32 buffer.
204  *
205  * @param base   DAC32 peripheral base address.
206  * @param config Pointer to the configuration structure. See "dac32_buffer_config_t".
207  */
208 void DAC32_SetBufferConfig(DAC_Type *base, const dac32_buffer_config_t *config);
209 
210 /*!
211  * @brief Initializes the DAC32 buffer configuration structure.
212  *
213  * This function initializes the DAC32 buffer configuration structure to a default value. The default values are:
214  * @code
215  *   config->triggerMode = kDAC32_BufferTriggerBySoftwareMode;
216  *   config->watermark   = kDAC32_BufferWatermark1Word;
217  *   config->workMode    = kDAC32_BufferWorkAsNormalMode;
218  *   config->upperLimit  = DAC_DAT_COUNT * 2U - 1U;
219  * @endcode
220  * @param config Pointer to the configuration structure. See "dac32_buffer_config_t".
221  */
222 void DAC32_GetDefaultBufferConfig(dac32_buffer_config_t *config);
223 
224 /*!
225  * @brief Enables the DMA for DAC32 buffer.
226  *
227  * @param base DAC32 peripheral base address.
228  * @param enable Enables the feature or not.
229  */
DAC32_EnableBufferDMA(DAC_Type * base,bool enable)230 static inline void DAC32_EnableBufferDMA(DAC_Type *base, bool enable)
231 {
232     if (enable)
233     {
234         base->STATCTRL |= DAC_STATCTRL_DMAEN_MASK;
235     }
236     else
237     {
238         base->STATCTRL &= ~DAC_STATCTRL_DMAEN_MASK;
239     }
240 }
241 
242 /*!
243  * @brief Sets the value for  items in the buffer.
244  *
245  * @param base  DAC32 peripheral base address.
246  * @param index Setting index for items in the buffer. The available index should not exceed the size of the DAC32
247  * buffer.
248  * @param value Setting value for items in the buffer. 12-bits are available.
249  */
250 void DAC32_SetBufferValue(DAC_Type *base, uint32_t index, uint32_t value);
251 
252 /*!
253  * @brief Triggers the buffer by software and updates the read pointer of the DAC32 buffer.
254  *
255  * This function triggers the function by software. The read pointer of the DAC32 buffer is updated with one step
256  * after this function is called. Changing the read pointer depends on the buffer's work mode.
257  *
258  * @param base DAC32 peripheral base address.
259  */
DAC32_DoSoftwareTriggerBuffer(DAC_Type * base)260 static inline void DAC32_DoSoftwareTriggerBuffer(DAC_Type *base)
261 {
262     base->STATCTRL |= DAC_STATCTRL_DACSWTRG_MASK;
263 }
264 
265 /*!
266  * @brief Gets the current read pointer of the DAC32 buffer.
267  *
268  * This function gets the current read pointer of the DAC32 buffer.
269  * The current output value depends on the item indexed by the read pointer. It is updated
270  * by software trigger or hardware trigger.
271  *
272  * @param  base DAC32 peripheral base address.
273  *
274  * @return      Current read pointer of DAC32 buffer.
275  */
DAC32_GetBufferReadPointer(DAC_Type * base)276 static inline uint32_t DAC32_GetBufferReadPointer(DAC_Type *base)
277 {
278     return ((base->STATCTRL & DAC_STATCTRL_DACBFRP_MASK) >> DAC_STATCTRL_DACBFRP_SHIFT);
279 }
280 
281 /*!
282  * @brief Sets the current read pointer of the DAC32 buffer.
283  *
284  * This function sets the current read pointer of the DAC32 buffer.
285  * The current output value depends on the item indexed by the read pointer. It is updated by
286  * software trigger or hardware trigger. After the read pointer changes, the DAC32 output value also changes.
287  *
288  * @param base  DAC32 peripheral base address.
289  * @param index Setting index value for the pointer.
290  */
DAC32_SetBufferReadPointer(DAC_Type * base,uint32_t index)291 static inline void DAC32_SetBufferReadPointer(DAC_Type *base, uint32_t index)
292 {
293     assert(index < (DAC_DAT_COUNT * 2U));
294 
295     base->STATCTRL = (base->STATCTRL & ~DAC_STATCTRL_DACBFRP_MASK) | DAC_STATCTRL_DACBFRP(index);
296 }
297 
298 /*!
299  * @brief Enables interrupts for the DAC32 buffer.
300  *
301  * @param base DAC32 peripheral base address.
302  * @param mask Mask value for interrupts. See "_dac32_buffer_interrupt_enable".
303  */
DAC32_EnableBufferInterrupts(DAC_Type * base,uint32_t mask)304 static inline void DAC32_EnableBufferInterrupts(DAC_Type *base, uint32_t mask)
305 {
306     assert(0U == (mask & ~(DAC_STATCTRL_DACBWIEN_MASK | DAC_STATCTRL_DACBTIEN_MASK | DAC_STATCTRL_DACBBIEN_MASK)));
307 
308     base->STATCTRL |= mask;
309 }
310 
311 /*!
312  * @brief Disables interrupts for the DAC32 buffer.
313  *
314  * @param base DAC32 peripheral base address.
315  * @param mask Mask value for interrupts. See  "_dac32_buffer_interrupt_enable".
316  */
DAC32_DisableBufferInterrupts(DAC_Type * base,uint32_t mask)317 static inline void DAC32_DisableBufferInterrupts(DAC_Type *base, uint32_t mask)
318 {
319     assert(0U == (mask & ~(DAC_STATCTRL_DACBWIEN_MASK | DAC_STATCTRL_DACBTIEN_MASK | DAC_STATCTRL_DACBBIEN_MASK)));
320 
321     base->STATCTRL &= ~mask;
322 }
323 
324 /*!
325  * @brief Gets the flags of events for the DAC32 buffer.
326  *
327  * @param  base DAC32 peripheral base address.
328  *
329  * @return      Mask value for the asserted flags. See  "_dac32_buffer_status_flags".
330  */
DAC32_GetBufferStatusFlags(DAC_Type * base)331 static inline uint32_t DAC32_GetBufferStatusFlags(DAC_Type *base)
332 {
333     return (base->STATCTRL & (DAC_STATCTRL_DACBFWMF_MASK | DAC_STATCTRL_DACBFRPTF_MASK | DAC_STATCTRL_DACBFRPBF_MASK));
334 }
335 
336 /*!
337  * @brief Clears the flags of events for the DAC32 buffer.
338  *
339  * @param base DAC32 peripheral base address.
340  * @param mask Mask value for flags. See "_dac32_buffer_status_flags_t".
341  */
DAC32_ClearBufferStatusFlags(DAC_Type * base,uint32_t mask)342 static inline void DAC32_ClearBufferStatusFlags(DAC_Type *base, uint32_t mask)
343 {
344     base->STATCTRL &= ~mask;
345 }
346 
347 /*!
348  * @brief Enable the buffer output.
349  *
350  * @param base DAC32 peripheral base address.
351  * @param enable Enable the buffer output or not.
352  */
DAC32_EnableBufferOutput(DAC_Type * base,bool enable)353 static inline void DAC32_EnableBufferOutput(DAC_Type *base, bool enable)
354 {
355     if (enable)
356     {
357         base->STATCTRL |= DAC_STATCTRL_BFOUTEN_MASK;
358     }
359     else
360     {
361         base->STATCTRL &= ~DAC_STATCTRL_BFOUTEN_MASK;
362     }
363 }
364 
365 /*!
366  * @brief Enable the test output.
367  *
368  * @param base DAC32 peripheral base address.
369  * @param enable Enable the test output or not.
370  */
DAC32_EnableTestOutput(DAC_Type * base,bool enable)371 static inline void DAC32_EnableTestOutput(DAC_Type *base, bool enable)
372 {
373     if (enable)
374     {
375         base->STATCTRL |= DAC_STATCTRL_TESTOUTEN_MASK;
376     }
377     else
378     {
379         base->STATCTRL &= ~DAC_STATCTRL_TESTOUTEN_MASK;
380     }
381 }
382 
383 /* @} */
384 
385 #if defined(__cplusplus)
386 }
387 #endif
388 /*!
389  * @}
390  */
391 #endif /* _FSL_DAC32_H_ */
392