MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
DAC: Digital-to-Analog Converter Driver

Overview

The MCUXpresso SDK provides a peripheral driver for the Digital-to-Analog Converter (DAC) module of MCUXpresso SDK devices.

The DAC driver includes a basic DAC module (converter) and a DAC buffer.

The basic DAC module supports operations unique to the DAC converter in each DAC instance. The APIs in this section are used in the initialization phase, which enables the DAC module in the application. The APIs enable/disable the clock, enable/disable the module, and configure the converter. Call the initial APIs to prepare the DAC module for the application.

The DAC buffer operates the DAC hardware buffer. The DAC module supports a hardware buffer to keep a group of DAC values to be converted. This feature supports updating the DAC output value automatically by triggering the buffer read pointer to move in the buffer. Use the APIs to configure the hardware buffer's trigger mode, watermark, work mode, and use size. Additionally, the APIs operate the DMA, interrupts, flags, the pointer (the index of the buffer), item values, and so on.

Note that the most functional features are designed for the DAC hardware buffer.

Typical use case

Working as a basic DAC without the hardware buffer feature

// ...
// Configures the DAC.
DAC_GetDefaultConfig(&dacConfigStruct);
DAC_Init(DEMO_DAC_INSTANCE, &dacConfigStruct);
DAC_Enable(DEMO_DAC_INSTANCE, true);
DAC_SetBufferReadPointer(DEMO_DAC_INSTANCE, 0U);
// ...
DAC_SetBufferValue(DEMO_DAC_INSTANCE, 0U, dacValue);

Working with the hardware buffer

// ...
EnableIRQ(DEMO_DAC_IRQ_ID);
// ...
// Configures the DAC.
DAC_GetDefaultConfig(&dacConfigStruct);
DAC_Init(DEMO_DAC_INSTANCE, &dacConfigStruct);
DAC_Enable(DEMO_DAC_INSTANCE, true);
// Configures the DAC buffer.
DAC_GetDefaultBufferConfig(&dacBufferConfigStruct);
DAC_SetBufferConfig(DEMO_DAC_INSTANCE, &dacBufferConfigStruct);
DAC_SetBufferReadPointer(DEMO_DAC_INSTANCE, 0U); // Make sure the read pointer to the start.
for (index = 0U, dacValue = 0; index < DEMO_DAC_USED_BUFFER_SIZE; index++, dacValue += (0xFFFU / DEMO_DAC_USED_BUFFER_SIZE))
{
DAC_SetBufferValue(DEMO_DAC_INSTANCE, index, dacValue);
}
// Clears flags.
#if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
g_DacBufferWatermarkInterruptFlag = false;
#endif // FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
g_DacBufferReadPointerTopPositionInterruptFlag = false;
g_DacBufferReadPointerBottomPositionInterruptFlag = false;
// Enables interrupts.
mask = 0U;
#if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
#endif // FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
DAC_EnableBuffer(DEMO_DAC_INSTANCE, true);
DAC_EnableBufferInterrupts(DEMO_DAC_INSTANCE, mask);
// ISR for the DAC interrupt.
void DEMO_DAC_IRQ_HANDLER_FUNC(void)
{
uint32_t flags = DAC_GetBufferStatusFlags(DEMO_DAC_INSTANCE);
#if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
{
g_DacBufferWatermarkInterruptFlag = true;
}
#endif // FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
{
g_DacBufferReadPointerTopPositionInterruptFlag = true;
}
{
g_DacBufferReadPointerBottomPositionInterruptFlag = true;
}
DAC_ClearBufferStatusFlags(DEMO_DAC_INSTANCE, flags); /* Clear flags. */
}

Data Structures

struct  dac_config_t
 DAC module configuration. More...
 
struct  dac_buffer_config_t
 DAC buffer configuration. More...
 

Enumerations

enum  _dac_buffer_status_flags {
  kDAC_BufferWatermarkFlag = DAC_SR_DACBFWMF_MASK,
  kDAC_BufferReadPointerTopPositionFlag = DAC_SR_DACBFRPTF_MASK,
  kDAC_BufferReadPointerBottomPositionFlag = DAC_SR_DACBFRPBF_MASK
}
 DAC buffer flags. More...
 
enum  _dac_buffer_interrupt_enable {
  kDAC_BufferWatermarkInterruptEnable = DAC_C0_DACBWIEN_MASK,
  kDAC_BufferReadPointerTopInterruptEnable = DAC_C0_DACBTIEN_MASK,
  kDAC_BufferReadPointerBottomInterruptEnable = DAC_C0_DACBBIEN_MASK
}
 DAC buffer interrupts. More...
 
enum  dac_reference_voltage_source_t {
  kDAC_ReferenceVoltageSourceVref1 = 0U,
  kDAC_ReferenceVoltageSourceVref2 = 1U
}
 DAC reference voltage source. More...
 
enum  dac_buffer_trigger_mode_t {
  kDAC_BufferTriggerByHardwareMode = 0U,
  kDAC_BufferTriggerBySoftwareMode = 1U
}
 DAC buffer trigger mode. More...
 
enum  dac_buffer_watermark_t {
  kDAC_BufferWatermark1Word = 0U,
  kDAC_BufferWatermark2Word = 1U,
  kDAC_BufferWatermark3Word = 2U,
  kDAC_BufferWatermark4Word = 3U
}
 DAC buffer watermark. More...
 
enum  dac_buffer_work_mode_t {
  kDAC_BufferWorkAsNormalMode = 0U,
  kDAC_BufferWorkAsSwingMode,
  kDAC_BufferWorkAsOneTimeScanMode,
  kDAC_BufferWorkAsFIFOMode
}
 DAC buffer work mode. More...
 

Driver version

#define FSL_DAC_DRIVER_VERSION   (MAKE_VERSION(2, 0, 1))
 DAC driver version 2.0.1. More...
 

Initialization

void DAC_Init (DAC_Type *base, const dac_config_t *config)
 Initializes the DAC module. More...
 
void DAC_Deinit (DAC_Type *base)
 De-initializes the DAC module. More...
 
void DAC_GetDefaultConfig (dac_config_t *config)
 Initializes the DAC user configuration structure. More...
 
static void DAC_Enable (DAC_Type *base, bool enable)
 Enables the DAC module. More...
 

Buffer

static void DAC_EnableBuffer (DAC_Type *base, bool enable)
 Enables the DAC buffer. More...
 
void DAC_SetBufferConfig (DAC_Type *base, const dac_buffer_config_t *config)
 Configures the CMP buffer. More...
 
void DAC_GetDefaultBufferConfig (dac_buffer_config_t *config)
 Initializes the DAC buffer configuration structure. More...
 
static void DAC_EnableBufferDMA (DAC_Type *base, bool enable)
 Enables the DMA for DAC buffer. More...
 
void DAC_SetBufferValue (DAC_Type *base, uint8_t index, uint16_t value)
 Sets the value for items in the buffer. More...
 
static void DAC_DoSoftwareTriggerBuffer (DAC_Type *base)
 Triggers the buffer using software and updates the read pointer of the DAC buffer. More...
 
static uint8_t DAC_GetBufferReadPointer (DAC_Type *base)
 Gets the current read pointer of the DAC buffer. More...
 
void DAC_SetBufferReadPointer (DAC_Type *base, uint8_t index)
 Sets the current read pointer of the DAC buffer. More...
 
void DAC_EnableBufferInterrupts (DAC_Type *base, uint32_t mask)
 Enables interrupts for the DAC buffer. More...
 
void DAC_DisableBufferInterrupts (DAC_Type *base, uint32_t mask)
 Disables interrupts for the DAC buffer. More...
 
uint32_t DAC_GetBufferStatusFlags (DAC_Type *base)
 Gets the flags of events for the DAC buffer. More...
 
void DAC_ClearBufferStatusFlags (DAC_Type *base, uint32_t mask)
 Clears the flags of events for the DAC buffer. More...
 

Data Structure Documentation

struct dac_config_t

Data Fields

dac_reference_voltage_source_t referenceVoltageSource
 Select the DAC reference voltage source. More...
 
bool enableLowPowerMode
 Enable the low-power mode. More...
 

Field Documentation

dac_reference_voltage_source_t dac_config_t::referenceVoltageSource
bool dac_config_t::enableLowPowerMode
struct dac_buffer_config_t

Data Fields

dac_buffer_trigger_mode_t triggerMode
 Select the buffer's trigger mode. More...
 
dac_buffer_watermark_t watermark
 Select the buffer's watermark. More...
 
dac_buffer_work_mode_t workMode
 Select the buffer's work mode. More...
 
uint8_t upperLimit
 Set the upper limit for the buffer index. More...
 

Field Documentation

dac_buffer_trigger_mode_t dac_buffer_config_t::triggerMode
dac_buffer_watermark_t dac_buffer_config_t::watermark
dac_buffer_work_mode_t dac_buffer_config_t::workMode
uint8_t dac_buffer_config_t::upperLimit

Normally, 0-15 is available for a buffer with 16 items.

Macro Definition Documentation

#define FSL_DAC_DRIVER_VERSION   (MAKE_VERSION(2, 0, 1))

Enumeration Type Documentation

Enumerator
kDAC_BufferWatermarkFlag 

DAC Buffer Watermark Flag.

kDAC_BufferReadPointerTopPositionFlag 

DAC Buffer Read Pointer Top Position Flag.

kDAC_BufferReadPointerBottomPositionFlag 

DAC Buffer Read Pointer Bottom Position Flag.

Enumerator
kDAC_BufferWatermarkInterruptEnable 

DAC Buffer Watermark Interrupt Enable.

kDAC_BufferReadPointerTopInterruptEnable 

DAC Buffer Read Pointer Top Flag Interrupt Enable.

kDAC_BufferReadPointerBottomInterruptEnable 

DAC Buffer Read Pointer Bottom Flag Interrupt Enable.

Enumerator
kDAC_ReferenceVoltageSourceVref1 

The DAC selects DACREF_1 as the reference voltage.

kDAC_ReferenceVoltageSourceVref2 

The DAC selects DACREF_2 as the reference voltage.

Enumerator
kDAC_BufferTriggerByHardwareMode 

The DAC hardware trigger is selected.

kDAC_BufferTriggerBySoftwareMode 

The DAC software trigger is selected.

Enumerator
kDAC_BufferWatermark1Word 

1 word away from the upper limit.

kDAC_BufferWatermark2Word 

2 words away from the upper limit.

kDAC_BufferWatermark3Word 

3 words away from the upper limit.

kDAC_BufferWatermark4Word 

4 words away from the upper limit.

Enumerator
kDAC_BufferWorkAsNormalMode 

Normal mode.

kDAC_BufferWorkAsSwingMode 

Swing mode.

kDAC_BufferWorkAsOneTimeScanMode 

One-Time Scan mode.

kDAC_BufferWorkAsFIFOMode 

FIFO mode.

Function Documentation

void DAC_Init ( DAC_Type *  base,
const dac_config_t config 
)

This function initializes the DAC module including the following operations.

  • Enabling the clock for DAC module.
  • Configuring the DAC converter with a user configuration.
  • Enabling the DAC module.
Parameters
baseDAC peripheral base address.
configPointer to the configuration structure. See "dac_config_t".
void DAC_Deinit ( DAC_Type *  base)

This function de-initializes the DAC module including the following operations.

  • Disabling the DAC module.
  • Disabling the clock for the DAC module.
Parameters
baseDAC peripheral base address.
void DAC_GetDefaultConfig ( dac_config_t config)

This function initializes the user configuration structure to a default value. The default values are as follows.

* config->referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2;
* config->enableLowPowerMode = false;
*
Parameters
configPointer to the configuration structure. See "dac_config_t".
static void DAC_Enable ( DAC_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseDAC peripheral base address.
enableEnables or disables the feature.
static void DAC_EnableBuffer ( DAC_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseDAC peripheral base address.
enableEnables or disables the feature.
void DAC_SetBufferConfig ( DAC_Type *  base,
const dac_buffer_config_t config 
)
Parameters
baseDAC peripheral base address.
configPointer to the configuration structure. See "dac_buffer_config_t".
void DAC_GetDefaultBufferConfig ( dac_buffer_config_t config)

This function initializes the DAC buffer configuration structure to default values. The default values are as follows.

* config->triggerMode = kDAC_BufferTriggerBySoftwareMode;
* config->watermark = kDAC_BufferWatermark1Word;
* config->workMode = kDAC_BufferWorkAsNormalMode;
* config->upperLimit = DAC_DATL_COUNT - 1U;
*
Parameters
configPointer to the configuration structure. See "dac_buffer_config_t".
static void DAC_EnableBufferDMA ( DAC_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseDAC peripheral base address.
enableEnables or disables the feature.
void DAC_SetBufferValue ( DAC_Type *  base,
uint8_t  index,
uint16_t  value 
)
Parameters
baseDAC peripheral base address.
indexSetting the index for items in the buffer. The available index should not exceed the size of the DAC buffer.
valueSetting the value for items in the buffer. 12-bits are available.
static void DAC_DoSoftwareTriggerBuffer ( DAC_Type *  base)
inlinestatic

This function triggers the function using software. The read pointer of the DAC buffer is updated with one step after this function is called. Changing the read pointer depends on the buffer's work mode.

Parameters
baseDAC peripheral base address.
static uint8_t DAC_GetBufferReadPointer ( DAC_Type *  base)
inlinestatic

This function gets the current read pointer of the DAC buffer. The current output value depends on the item indexed by the read pointer. It is updated either by a software trigger or a hardware trigger.

Parameters
baseDAC peripheral base address.
Returns
The current read pointer of the DAC buffer.
void DAC_SetBufferReadPointer ( DAC_Type *  base,
uint8_t  index 
)

This function sets the current read pointer of the DAC buffer. The current output value depends on the item indexed by the read pointer. It is updated either by a software trigger or a hardware trigger. After the read pointer changes, the DAC output value also changes.

Parameters
baseDAC peripheral base address.
indexSetting an index value for the pointer.
void DAC_EnableBufferInterrupts ( DAC_Type *  base,
uint32_t  mask 
)
Parameters
baseDAC peripheral base address.
maskMask value for interrupts. See "_dac_buffer_interrupt_enable".
void DAC_DisableBufferInterrupts ( DAC_Type *  base,
uint32_t  mask 
)
Parameters
baseDAC peripheral base address.
maskMask value for interrupts. See "_dac_buffer_interrupt_enable".
uint32_t DAC_GetBufferStatusFlags ( DAC_Type *  base)
Parameters
baseDAC peripheral base address.
Returns
Mask value for the asserted flags. See "_dac_buffer_status_flags".
void DAC_ClearBufferStatusFlags ( DAC_Type *  base,
uint32_t  mask 
)
Parameters
baseDAC peripheral base address.
maskMask value for flags. See "_dac_buffer_status_flags_t".