1 /*
2 * Copyright 2022 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8 #ifndef FSL_DAC14_H_
9 #define FSL_DAC14_H_
10
11 #include "fsl_common.h"
12
13 /*!
14 * @addtogroup dac14
15 * @{
16 */
17
18 /*! @file */
19
20 /*******************************************************************************
21 * Definitions
22 ******************************************************************************/
23
24 /*! @name Driver version */
25 /*! @{ */
26 /*! @brief DAC14 driver version 2.0.0. */
27 #define FSL_DAC14_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
28 /*! @} */
29
30 /*!
31 * @brief DAC14 interrupts enumeration.
32 */
33 enum _dac14_interrupt_enable
34 {
35 kDAC14_PeriodTriggerCompleteInterruptEnable = HPDAC_IER_PTGCOCO_IE_MASK, /*!< Period trigger mode conversion complete interrupt enable */
36 kDAC14_FIFOUnderflowInterruptEnable = HPDAC_IER_UF_IE_MASK, /*!< FIFO underflow interrupt enable. */
37 kDAC14_FIFOOverflowInterruptEnable = HPDAC_IER_OF_IE_MASK, /*!< FIFO overflow interrupt enable. */
38 kDAC14_SwingBackInterruptEnable = HPDAC_IER_SWBK_IE_MASK, /*!< Swing back one cycle complete interrupt enable. */
39 kDAC14_FIFOWatermarkInterruptEnable = HPDAC_IER_WM_IE_MASK, /*!< FIFO watermark interrupt enable. */
40 kDAC14_FIFOEmptyInterruptEnable = HPDAC_IER_EMPTY_IE_MASK, /*!< FIFO empty interrupt enable. */
41 kDAC14_FIFOFullInterruptEnable = HPDAC_IER_FULL_IE_MASK, /*!< FIFO full interrupt enable. */
42 };
43
44 /*!
45 * @brief DAC14 DMA switchers.
46 */
47 enum _dac14_dma_enable
48 {
49 kDAC14_FIFOWatermarkDMAEnable = HPDAC_DER_WM_DMAEN_MASK, /*!< FIFO watermark DMA enable. */
50 kDAC14_FIFOEmptyDMAEnable = HPDAC_DER_EMPTY_DMAEN_MASK, /*!< FIFO empty DMA enable. */
51 };
52
53 /*!
54 * @brief DAC14 status flags.
55 */
56 enum _dac14_status_flags
57 {
58 kDAC14_PeriodTriggerCompleteFlag = HPDAC_FSR_PTGCOCO_MASK, /*!< Period trigger mode conversion complete flag. */
59 kDAC14_FIFOUnderflowFlag = HPDAC_FSR_UF_MASK, /*!< This flag means that there is a new trigger after the buffer is empty. The FIFO read pointer will not increase in this case and the data sent to DAC analog conversion will not changed. This flag is cleared by writing a 1
60 to it. */
61 kDAC14_FIFOOverflowFlag = HPDAC_FSR_OF_MASK, /*!< This flag indicates that data is intended to write into FIFO after the buffer is full. The writer pointer will
62 not increase in this case. The extra data will not be written into the FIFO. This flag is cleared by writing a 1 to it.
63 */
64 kDAC14_SwingBackCompleteFlag = HPDAC_FSR_SWBK_MASK, /*!< This flag indicates that the DAC has completed one period of conversion in swing back mode. It means that the read pointer has increased to the top (write pointer) once and then decreased to zero once. For
65 example, after three data is written to FIFO, the writer pointer is now 3. Then, if continually triggered, the
66 read pointer will swing like: 0-1-2-1-0-1-2-, and so on. After the fourth trigger, the flag is set. This flag is
67 cleared by writing a 1 to it. */
68 kDAC14_FIFOWaterMarkFlag = HPDAC_FSR_WM_MASK, /*!< This field is set if the remaining data in FIFO is less than or
69 equal to the setting value of wartermark. By writing data into FIFO by DMA or CPU, this flag is
70 cleared automatically when the data in FIFO is more than the setting value of watermark. */
71 kDAC14_FIFOEmptyFlag = HPDAC_FSR_EMPTY_MASK, /*!< FIFO empty flag, when CPU or DMA writes data to FIFO, this bit will automatically clear.*/
72 kDAC14_FIFOFullFlag = HPDAC_FSR_FULL_MASK, /*!< FIFO full flag, when software trigger and hardware trigger read FIFO automatically clears this flag.*/
73 };
74
75 /*!
76 * @brief DAC14 trigger source, include software and hardware.
77 */
78 typedef enum _dac14_trigger_source
79 {
80 kDAC14_HardwareTriggerSource = 0U, /*!< Trigger source selection hardware . */
81 kDAC14_SoftwareTriggerSource = 1U, /*!< Trigger source selection software . */
82 } dac14_trigger_source_t;
83
84 /*!
85 * @brief DAC14 work mode.
86 */
87 typedef enum _dac14_work_mode
88 {
89 kDAC14_BufferWorkMode = 0U, /*!< FIFO mode is disabled and buffer mode is enabled. Any data written to DATA[DATA] goes
90 to buffer then goes to conversion. */
91 kDAC14_FIFOWorkMode = 1U, /*!< FIFO mode is enabled. Data will be first read from FIFO to buffer then goes to
92 conversion. */
93 kDAC14_SwingBackWorkMode = 2U, /*!< In swing mode, the read pointer swings between the writer pointer and zero. That
94 is, the trigger increases the read pointer till reach the writer pointer and
95 decreases the read pointer till zero, and so on. The FIFO empty/full/watermark
96 flag will not update during swing back mode. */
97 kDAC14_PeriodTriggerWorkMode = 3U, /*!< In periodic trigger mode, user only needs to send the first trigger. Then after every [PTG_PERIOD+1]
98 RCLK cycles, DAC will be automatically triggered by internal trigger. There will be [PTG_NUM] internal
99 triggers, thus in total [PTG_NUM+1] conversions including the first trigger sent by user. User can
100 terminate the current conversion queue by clearing the GCR[PTGEN] bit. Then, after the current conversion
101 is completed, the conversion is terminated and the PTGCOCO flag is set. If PCR[PTG_NUM] is set to zero,
102 there will be infinite triggers following the first hardware/software trigger, until the GCR[PTGEN] is
103 cleared by software. In any case, the conversion can be terminated by FIFORST/SWRST. */
104 kDAC14_PeriodTriggerAndSwingBackWorkMode = 4U, /*!< Periodically trigger DAC and swing back. */
105 } dac14_work_mode_t;
106
107 /*!
108 * @brief DAC14 configuration structure.
109 */
110 typedef struct _dac14_config
111 {
112 uint16_t periodicTriggerNumber; /*!< There will be 'periodicTriggerNumber' internal triggers following the first
113 hardware/software trigger. So there will be 'periodicTriggerNumber + 1'
114 conversions in total. If set to zero, there will be infinite triggers following
115 the first hw/sw trigger, until the GCR[PTGEN] is cleared. */
116 uint16_t periodicTriggerWidth; /*!< Control the periodic trigger frequency. There will be 'periodicTriggerWidth + 1'
117 RCLK cycles between each periodic trigger. The periodic trigger frequency should
118 be configured to not larger than the analog conversion speed. */
119 uint8_t fifoWatermarkLevel :5; /*!< FIFO's watermark, the max value can be the hardware FIFO size. */
120 bool enableOpampBuffer :1; /*!< Opamp is used as buffer.*/
121 bool enableDAC :1; /*!< Enable the DAC system.*/
122 dac14_work_mode_t WorkMode; /*!< Select DAC work mode.*/
123 dac14_trigger_source_t TriggerSource; /*!< Select DAC trigger source. */
124 } dac14_config_t;
125
126 /*******************************************************************************
127 * API
128 ******************************************************************************/
129 #if defined(__cplusplus)
130 extern "C" {
131 #endif
132
133 /*!
134 * @name Initialization And De-initialization
135 * @{
136 */
137
138 /*!
139 * @brief Initialize the DAC14 module with common configuartion.
140 *
141 * The clock will be enabled in this function.
142 *
143 * @param base DAC14 peripheral base address.
144 * @param config Pointer to configuration structure.
145 */
146 void DAC14_Init(HPDAC_Type *base, const dac14_config_t *config);
147
148 /*!
149 * @brief Get the default settings for initialization's configuration.
150 *
151 * This function initializes the user configuration structure to a default value. The default values are:
152 * @code
153 config->fifoWatermarkLevel = 0U;
154 config->TriggerSource = kDAC14_HardwareTriggerSource;
155 config->WorkMode = kDAC14_BufferWorkMode;
156 config->enableOpampBuffer = false;
157 config->enableADC = false;
158 config->periodicTriggerNumber = 0U;
159 config->periodicTriggerWidth = 0U;
160 * @endcode
161 *
162 * @param config Pointer to configuration structure.
163 */
164 void DAC14_GetDefaultConfig(dac14_config_t *config);
165
166 /*!
167 * @brief De-initialize the DAC14 module.
168 *
169 * The clock will be disabled in this function.
170 *
171 * @param base DAC14 peripheral base address.
172 */
173 void DAC14_Deinit(HPDAC_Type *base);
174
175 /*!
176 * @brief Do software reset .
177 *
178 * This function is resets all DAC registers and internal logic.
179 *
180 * @param base DAC14 peripheral base address.
181 */
DAC14_DoSoftwareReset(HPDAC_Type * base)182 static inline void DAC14_DoSoftwareReset(HPDAC_Type *base)
183 {
184 base->RCR |= HPDAC_RCR_SWRST_MASK;
185 base->RCR &= ~HPDAC_RCR_SWRST_MASK;
186 }
187
188 /*!
189 * @brief Do FIFO reset.
190 *
191 * This function is resets the FIFO pointers and flags in FIFO Status.
192 *
193 * @param base DAC14 peripheral base address.
194 */
DAC14_DoFIFOReset(HPDAC_Type * base)195 static inline void DAC14_DoFIFOReset(HPDAC_Type *base)
196 {
197 base->RCR |= HPDAC_RCR_FIFORST_MASK;
198 base->RCR &= ~HPDAC_RCR_FIFORST_MASK;
199 }
200
201 /*!
202 * @brief Abort DAC14 period trigger conversion sequence.
203 *
204 * This function is write 0 to PTGEN to terminate the current conversion sequence.
205 *
206 * @param base DAC14 peripheral base address.
207 */
DAC14_AbortPeriodTriggerConvSequence(HPDAC_Type * base)208 static inline void DAC14_AbortPeriodTriggerConvSequence(HPDAC_Type *base)
209 {
210 base->GCR &= ~HPDAC_GCR_PTGEN_MASK;
211 }
212 /*! @} */
213
214 /*!
215 * @name DAC Control Interface
216 * @{
217 */
218
219 /*!
220 * @brief Enable the DAC14 system.
221 *
222 * @param base DAC14 peripheral base address.
223 * @param enable true to enable and false to disable.
224 */
DAC14_Enable(HPDAC_Type * base,bool enable)225 static inline void DAC14_Enable(HPDAC_Type *base, bool enable)
226 {
227 if (enable)
228 {
229 base->GCR |= HPDAC_GCR_DACEN_MASK;
230 }
231 else
232 {
233 base->GCR &= ~HPDAC_GCR_DACEN_MASK;
234 }
235 }
236 /*! @} */
237
238 /*!
239 * @name Swing Back Mode Control Interface
240 * @{
241 */
242 /*!
243 * @brief Enable swing back mode.
244 *
245 * @param base DAC14 peripheral base address.
246 * @param enable true to enable and false to disable.
247 */
DAC14_EnableSwingBackMode(HPDAC_Type * base,bool enable)248 static inline void DAC14_EnableSwingBackMode(HPDAC_Type *base, bool enable)
249 {
250 if (enable)
251 {
252 base->GCR |= HPDAC_GCR_SWMD_MASK;
253 }
254 else
255 {
256 base->GCR &= ~HPDAC_GCR_SWMD_MASK;
257 }
258 }
259 /*! @} */
260
261 /*!
262 * @name FIFO Mode Control Interface
263 * @{
264 */
265 /*!
266 * @brief Enable FIFO mode.
267 *
268 * @param base DAC14 peripheral base address.
269 * @param enable true to enable and false to disable.
270 */
DAC14_EnableFIFOMode(HPDAC_Type * base,bool enable)271 static inline void DAC14_EnableFIFOMode(HPDAC_Type *base, bool enable)
272 {
273 if (enable)
274 {
275 base->GCR |= HPDAC_GCR_FIFOEN_MASK;
276 }
277 else
278 {
279 base->GCR &= ~HPDAC_GCR_FIFOEN_MASK;
280 }
281 }
282 /*! @} */
283
284 /*!
285 * @name Interrupts
286 * @{
287 */
288 /*!
289 * @brief Enable the interrupts.
290 *
291 * @param base DAC14 peripheral base address.
292 * @param mask Mask value of indicated interrupt events, please see @ref _dac14_interrupt_enable for details.
293 */
DAC14_EnableInterrupts(HPDAC_Type * base,uint32_t mask)294 static inline void DAC14_EnableInterrupts(HPDAC_Type *base, uint32_t mask)
295 {
296 base->IER |= mask;
297 }
298
299 /*!
300 * @brief Disable the interrupts.
301 *
302 * @param base DAC14 peripheral base address.
303 * @param mask Mask value of indicated interrupt events, please see @ref _dac14_interrupt_enable for details.
304 */
DAC14_DisableInterrupts(HPDAC_Type * base,uint32_t mask)305 static inline void DAC14_DisableInterrupts(HPDAC_Type *base, uint32_t mask)
306 {
307 base->IER &= ~mask;
308 }
309 /*! @} */
310
311 /*!
312 * @name DMA Control Interface
313 * @{
314 */
315 /*!
316 * @brief Enable the DMA switchers or not.
317 *
318 * @param base DAC14 peripheral base address.
319 * @param mask Mask value of indicated DMA requeset, please see @ref _dac14_dma_enable for details.
320 * @param enable true to enable and false to disable.
321 */
DAC14_EnableDMA(HPDAC_Type * base,uint32_t mask,bool enable)322 static inline void DAC14_EnableDMA(HPDAC_Type *base, uint32_t mask, bool enable)
323 {
324 if (enable)
325 {
326 base->DER |= mask;
327 }
328 else
329 {
330 base->DER &= ~mask;
331 }
332 }
333 /*! @} */
334
335 /*!
336 * @name Status
337 * @{
338 */
339 /*!
340 * @brief Get status flags of DAC14 module.
341 *
342 * @param base DAC14 peripheral base address.
343 * @return Current DAC status flags.
344 */
DAC14_GetStatusFlags(HPDAC_Type * base)345 static inline uint32_t DAC14_GetStatusFlags(HPDAC_Type *base)
346 {
347 return base->FSR;
348 }
349
350 /*!
351 * @brief Clear status flags of DAC14 module.
352 *
353 * @param base DAC14 peripheral base address.
354 * @param flags Mask value of status flags to be cleared, please see @ref _dac14_status_flags for details.
355 */
DAC14_ClearStatusFlags(HPDAC_Type * base,uint32_t flags)356 static inline void DAC14_ClearStatusFlags(HPDAC_Type *base, uint32_t flags)
357 {
358 base->FSR = flags;
359 }
360 /*! @} */
361
362 /*!
363 * @name Functional Feature
364 * @{
365 */
366 /*!
367 * @brief Set data into the entry of FIFO buffer.
368 *
369 * @param base DAC14 peripheral base address.
370 * @param value Setting value into FIFO buffer.
371 */
DAC14_SetData(HPDAC_Type * base,uint32_t value)372 static inline void DAC14_SetData(HPDAC_Type *base, uint32_t value)
373 {
374 base->DATA = HPDAC_DATA_DATA(value);
375 }
376
377 /*!
378 * @brief Get the value of the FIFO write pointer.
379 *
380 * @param base DAC14 peripheral base address.
381 * @return Current value of the FIFO write pointer.
382 */
383
DAC14_GetFIFOWritePointer(HPDAC_Type * base)384 static inline uint32_t DAC14_GetFIFOWritePointer(HPDAC_Type *base)
385 {
386 return (HPDAC_FPR_FIFO_WPT_MASK & base->FPR) >> HPDAC_FPR_FIFO_WPT_SHIFT;
387 }
388
389 /*!
390 * @brief Get the value of the FIFO read pointer.
391 *
392 * @param base DAC14 peripheral base address.
393 * @return Current value of the FIFO read pointer.
394 */
395
DAC14_GetFIFOReadPointer(HPDAC_Type * base)396 static inline uint32_t DAC14_GetFIFOReadPointer(HPDAC_Type *base)
397 {
398 return (HPDAC_FPR_FIFO_RPT_MASK & base->FPR) >> HPDAC_FPR_FIFO_RPT_SHIFT;
399 }
400
401 /*!
402 * @brief Do software trigger.
403 *
404 * @param base DAC14 peripheral base address.
405 */
DAC14_DoSoftwareTrigger(HPDAC_Type * base)406 static inline void DAC14_DoSoftwareTrigger(HPDAC_Type *base)
407 {
408 base->TCR = HPDAC_TCR_SWTRG_MASK;
409 }
410 /*! @} */
411
412 #if defined(__cplusplus)
413 }
414 #endif
415
416 /*!
417 * @}
418 */
419 #endif /* FSL_DAC14_H_ */
420