summaryrefslogtreecommitdiffstats
path: root/bsps/arm/imxrt/mcux-sdk/drivers/dac12/fsl_dac12.h
blob: f467ec038c58a91f705182355f774ffec7c79cbf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Copyright (c) 2016, Freescale Semiconductor, Inc.
 * Copyright 2016-2021 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef _FSL_DAC12_H_
#define _FSL_DAC12_H_

#include "fsl_common.h"

/*!
 * @addtogroup dac12
 * @{
 */

/*! @file */

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*! @name Driver version */
/*@{*/
/*! @brief DAC12 driver version 2.1.0. */
#define FSL_DAC12_DRIVER_VERSION (MAKE_VERSION(2, 1, 0))
/*@}*/

/*! @brief Define "write 1 to clear" flags. */
#define DAC12_CR_W1C_FLAGS_MASK (DAC_CR_OVFF_MASK | DAC_CR_UDFF_MASK)
/*! @brief Define all the flag bits in DACx_CR register. */
#define DAC12_CR_ALL_FLAGS_MASK (DAC12_CR_W1C_FLAGS_MASK | DAC_CR_WMF_MASK | DAC_CR_NEMPTF_MASK | DAC_CR_FULLF_MASK)

/*!
 * @brief DAC12 flags.
 */
enum _dac12_status_flags
{
    kDAC12_OverflowFlag = DAC_CR_OVFF_MASK,  /*!< FIFO overflow status flag, which indicates that more data has been
                                                  written into FIFO than it can hold. */
    kDAC12_UnderflowFlag = DAC_CR_UDFF_MASK, /*!< FIFO underflow status flag, which means that there is a new trigger
                                                  after the FIFO is nearly empty. */
    kDAC12_WatermarkFlag = DAC_CR_WMF_MASK, /*!< FIFO wartermark status flag, which indicates the remaining FIFO data is
                                                 less than the watermark setting. */
    kDAC12_NearlyEmptyFlag = DAC_CR_NEMPTF_MASK, /*!< FIFO nearly empty flag, which means there is only one data
                                                      remaining in FIFO. */
    kDAC12_FullFlag = DAC_CR_FULLF_MASK /*!< FIFO full status flag, which means that the FIFO read pointer equals the
                                             write pointer, as the write pointer increase. */
};

/*!
 * @brief DAC12 interrupts.
 */
enum _dac12_interrupt_enable
{
    kDAC12_UnderOrOverflowInterruptEnable = DAC_CR_UVIE_MASK,   /*!< Underflow and overflow interrupt enable. */
    kDAC12_WatermarkInterruptEnable       = DAC_CR_WTMIE_MASK,  /*!< Watermark interrupt enable. */
    kDAC12_NearlyEmptyInterruptEnable     = DAC_CR_EMPTIE_MASK, /*!< Nearly empty interrupt enable. */
    kDAC12_FullInterruptEnable            = DAC_CR_FULLIE_MASK  /*!< Full interrupt enable. */
};

/*!
 * @brief DAC12 FIFO size information provided by hardware.
 */
typedef enum _dac12_fifo_size_info
{
    kDAC12_FIFOSize2   = 0U, /*!< FIFO depth is 2. */
    kDAC12_FIFOSize4   = 1U, /*!< FIFO depth is 4. */
    kDAC12_FIFOSize8   = 2U, /*!< FIFO depth is 8. */
    kDAC12_FIFOSize16  = 3U, /*!< FIFO depth is 16. */
    kDAC12_FIFOSize32  = 4U, /*!< FIFO depth is 32. */
    kDAC12_FIFOSize64  = 5U, /*!< FIFO depth is 64. */
    kDAC12_FIFOSize128 = 6U, /*!< FIFO depth is 128. */
    kDAC12_FIFOSize256 = 7U, /*!< FIFO depth is 256. */
} dac12_fifo_size_info_t;

/*!
 * @brief DAC12 FIFO work mode.
 */
typedef enum _dac12_fifo_work_mode
{
    kDAC12_FIFODisabled = 0U, /*!< FIFO disabled and only one level buffer is enabled. Any data written from this buffer
                                   goes to conversion. */
    kDAC12_FIFOWorkAsNormalMode = 1U, /*!< Data will first read from FIFO to buffer then go to conversion. */
    kDAC12_FIFOWorkAsSwingMode  = 2U  /*!< In Swing mode, the FIFO must be set up to be full. In Swing back mode, a
                                           trigger changes the read pointer to make it swing between the FIFO Full and
                                           Nearly Empty state. That is, the trigger increases the read pointer till FIFO
                                           is nearly empty and decreases the read pointer till the FIFO is full. */
} dac12_fifo_work_mode_t;

/*!
 * @brief DAC12 reference voltage source.
 */
typedef enum _dac12_reference_voltage_source
{
    kDAC12_ReferenceVoltageSourceAlt1 = 0U, /*!< The DAC selects DACREF_1 as the reference voltage. */
    kDAC12_ReferenceVoltageSourceAlt2 = 1U, /*!< The DAC selects DACREF_2 as the reference voltage. */
} dac12_reference_voltage_source_t;

/*!
 * @brief DAC12 FIFO trigger mode.
 */
typedef enum _dac12_fifo_trigger_mode
{
    kDAC12_FIFOTriggerByHardwareMode = 0U, /*!< Buffer would be triggered by hardware. */
    kDAC12_FIFOTriggerBySoftwareMode = 1U, /*!< Buffer would be triggered by software. */
} dac12_fifo_trigger_mode_t;

/*!
 * @brief DAC internal reference current source.
 *
 * Analog module needs reference current to keep working . Such reference current can generated by IP itself, or by
 * on-chip PMC's "reference part". If no current reference be selected, analog module can’t working normally ,even when
 * other register can still be assigned, DAC would waste current but no function.
 * To make the DAC work, either kDAC12_ReferenceCurrentSourceAltx should be selected.
 */
typedef enum _dac12_reference_current_source
{
    kDAC12_ReferenceCurrentSourceDisabled = 0U, /*!< None of reference current source is enabled. */
    kDAC12_ReferenceCurrentSourceAlt0 = 1U, /*!< Use the internal reference current generated by the module itself. */
    kDAC12_ReferenceCurrentSourceAlt1 = 2U, /*!< Use the ZTC(Zero Temperature Coefficient) reference current generated
                                                 by on-chip power management module. */
    kDAC12_ReferenceCurrentSourceAlt2 = 3U, /*!< Use the PTAT(Proportional To Absolution Temperature) reference current
                                                 generated by power management module. */
} dac12_reference_current_source_t;

/*!
 * @brief DAC analog buffer speed mode for conversion.
 */
typedef enum _dac12_speed_mode
{
    kDAC12_SpeedLowMode    = 0U, /*!< Low speed mode. */
    kDAC12_SpeedMiddleMode = 1U, /*!< Middle speed mode. */
    kDAC12_SpeedHighMode   = 2U, /*!< High speed mode. */
} dac12_speed_mode_t;

/*!
 * @brief DAC12 hardware information.
 */
typedef struct _dac12_hardware_info
{
    dac12_fifo_size_info_t fifoSizeInfo; /*!< The number of words in this device's DAC buffer. */
} dac12_hardware_info_t;

/*!
 * @brief DAC12 module configuration.
 *
 * Actually, the most fields are for FIFO buffer.
 */
typedef struct
{
    uint32_t fifoWatermarkLevel;         /*!< FIFO's watermark, the max value can be the hardware FIFO size. */
    dac12_fifo_work_mode_t fifoWorkMode; /*!< FIFI's work mode about pointers. */
    dac12_reference_voltage_source_t referenceVoltageSource; /*!< Select the reference voltage source. */
    dac12_fifo_trigger_mode_t fifoTriggerMode;               /*! Select the trigger mode for FIFO. */

    /* Analog part configuration. */
    dac12_reference_current_source_t referenceCurrentSource; /*!< Select the reference current source. */
    dac12_speed_mode_t speedMode;                            /*!< Select the speed mode for conversion. */
    bool enableAnalogBuffer;                                 /*!< Enable analog buffer for high drive. */
#if !(defined(FSL_FEATURE_DAC12_HAS_NO_ITRM_REGISTER) && FSL_FEATURE_DAC12_HAS_NO_ITRM_REGISTER)
    uint32_t currentReferenceInternalTrimValue; /*!< Internal reference current trim value. 3-bit value is available.*/
#endif                                          /* FSL_FEATURE_DAC12_HAS_NO_ITRM_REGISTER */
} dac12_config_t;

/*******************************************************************************
 * API
 ******************************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @name Initialization and de-initialization
 * @{
 */

/*!
 * @brief Get hardware information about this module.
 *
 * @param base DAC12 peripheral base address.
 * @param info Pointer to info structure, see to #dac12_hardware_info_t.
 */
void DAC12_GetHardwareInfo(DAC_Type *base, dac12_hardware_info_t *info);

/*!
 * @brief Initialize the DAC12 module.
 *
 * @param base DAC12 peripheral base address.
 * @param config Pointer to configuration structure, see to #dac12_config_t.
 */
void DAC12_Init(DAC_Type *base, const dac12_config_t *config);

/*!
 * @brief Initializes the DAC12 user configuration structure.
 *
 * This function initializes the user configuration structure to a default value. The default values are:
 * @code
 *   config->fifoWatermarkLevel = 0U;
 *   config->fifoWorkMode = kDAC12_FIFODisabled;
 *   config->referenceVoltageSource = kDAC12_ReferenceVoltageSourceAlt1;
 *   config->fifoTriggerMode = kDAC12_FIFOTriggerByHardwareMode;
 *   config->referenceCurrentSource = kDAC12_ReferenceCurrentSourceAlt0;
 *   config->speedMode = kDAC12_SpeedLowMode;
 *   config->speedMode = false;
 *   config->currentReferenceInternalTrimValue = 0x4;
 * @endcode
 * @param config Pointer to the configuration structure. See "dac12_config_t".
 */
void DAC12_GetDefaultConfig(dac12_config_t *config);

/*!
 * @brief De-initialize the DAC12 module.
 *
 * @param base DAC12 peripheral base address.
 */
void DAC12_Deinit(DAC_Type *base);

/*!
 * @brief Enable the DAC12's converter or not.
 *
 * @param base DAC12 peripheral base address.
 * @param enable Enable the DAC12's converter or not.
 */
static inline void DAC12_Enable(DAC_Type *base, bool enable)
{
    if (enable)
    {
        base->CR = (base->CR & ~DAC12_CR_W1C_FLAGS_MASK) | DAC_CR_DACEN_MASK;
    }
    else
    {
        base->CR &= ~DAC_CR_DACEN_MASK;
    }
}

/*!
 * @brief Reset all internal logic and registers.
 *
 * @param base DAC12 peripheral base address.
 */
static inline void DAC12_ResetConfig(DAC_Type *base)
{
    base->CR = DAC_CR_SWRST_MASK;
}

/*!
 * @brief Reset the FIFO pointers.
 *
 * FIFO pointers should only be reset when the DAC12 is disabled. This function can be used to configure both pointers
 * to the same address to reset the FIFO as empty.
 *
 * @param base DAC12 peripheral base address.
 */
static inline void DAC12_ResetFIFO(DAC_Type *base)
{
    /* FIFO pointers should only be reset when the module is disabled. */
    base->CR = (base->CR & ~DAC12_CR_W1C_FLAGS_MASK) | DAC_CR_FIFORST_MASK;
}

/* @} */

/*!
 * @name Status
 * @{
 */

/*!
 * @brief Get status flags.
 *
 * @param base DAC12 peripheral base address.
 * @return Mask of current status flags. See to #_dac12_status_flags.
 */
static inline uint32_t DAC12_GetStatusFlags(DAC_Type *base)
{
    return (DAC12_CR_ALL_FLAGS_MASK & base->CR);
}

/*!
 * @brief Clear status flags.
 *
 * Note: Not all the flags can be cleared by this API. Several flags need special condition to clear them according to
 * target chip's reference manual document.
 *
 * @param base DAC12 peripheral base address.
 * @param flags Mask of status flags to be cleared. See to #_dac12_status_flags.
 */
static inline void DAC12_ClearStatusFlags(DAC_Type *base, uint32_t flags)
{
    base->CR |= (flags & DAC12_CR_W1C_FLAGS_MASK);
}

/* @} */

/*!
 * @name Interrupts
 * @{
 */

/*!
 * @brief Enable interrupts.
 *
 * @param base DAC12 peripheral base address.
 * @param mask Mask value of interrupts to be enabled. See to #_dac12_interrupt_enable.
 */
static inline void DAC12_EnableInterrupts(DAC_Type *base, uint32_t mask)
{
    base->CR = (base->CR & ~DAC12_CR_W1C_FLAGS_MASK) | mask;
}

/*!
 * @brief Disable interrupts.
 *
 * @param base DAC12 peripheral base address.
 * @param mask Mask value of interrupts to be disabled. See to #_dac12_interrupt_enable.
 */
static inline void DAC12_DisableInterrupts(DAC_Type *base, uint32_t mask)
{
    base->CR &= ~mask;
}

/* @} */

/*!
 * @name DMA control
 * @{
 */

/*!
 * @brief Enable DMA or not.
 *
 * When DMA is enabled, the DMA request will be generated by original interrupts. The interrupts will not be presented
 * on this module at the same time.
 */
static inline void DAC12_EnableDMA(DAC_Type *base, bool enable)
{
    if (enable)
    {
        base->CR = (base->CR & ~DAC12_CR_W1C_FLAGS_MASK) | DAC_CR_DMAEN_MASK;
    }
    else
    {
        base->CR &= ~DAC_CR_DMAEN_MASK;
    }
}

/* @} */

/*!
 * @name Functional feature
 * @{
 */

/*!
 * @brief Set data into the entry of FIFO buffer.
 *
 * When the DAC FIFO is disabled, and the one entry buffer is enabled, the DAC converts the data in the buffer to analog
 * output voltage. Any write to the DATA register will replace the data in the buffer and push data to analog conversion
 * without trigger support.
 * When the DAC FIFO is enabled. Writing data would increase the write pointer of FIFO. Also, the data would be restored
 * into the FIFO buffer.
 *
 * @param base DAC12 peripheral base address.
 * @param value Setting value into FIFO buffer.
 */
static inline void DAC12_SetData(DAC_Type *base, uint32_t value)
{
    /* The module is connected internally to a 32-bit interface.
     * For the 8-bit or 16-bit, the write might be ignored. */
    base->DATA = DAC_DATA_DATA0(value);
}

/*!
 * @brief Do trigger the FIFO by software.
 *
 * When the DAC FIFO is enabled, and software trigger is used. Doing trigger would increase the read pointer, and the
 * data in the entry pointed by read pointer would be converted as new output.
 *
 * @param base DAC12 peripheral base address.
 */
static inline void DAC12_DoSoftwareTrigger(DAC_Type *base)
{
    base->CR = (base->CR & ~DAC12_CR_W1C_FLAGS_MASK) | DAC_CR_SWTRG_MASK;
}

/*!
 * @brief Get the current read pointer of FIFO.
 *
 * @param base DAC12 peripheral base address.
 * @return Read pointer index of FIFO buffer.
 */
static inline uint32_t DAC12_GetFIFOReadPointer(DAC_Type *base)
{
    return (DAC_PTR_DACRFP_MASK & base->PTR) >> DAC_PTR_DACRFP_SHIFT;
}

/*!
 * @brief Get the current write pointer of FIFO.
 *
 * @param base DAC12 peripheral base address.
 * @return Write pointer index of FIFO buffer
 */
static inline uint32_t DAC12_GetFIFOWritePointer(DAC_Type *base)
{
    return (DAC_PTR_DACWFP_MASK & base->PTR) >> DAC_PTR_DACWFP_SHIFT;
}

/* @} */

#if defined(__cplusplus)
}
#endif
/*!
 * @}
 */
#endif /* _FSL_DAC12_H_ */