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.

This DAC is the 12-bit resolution digital-to-analog converters with programmable reference generator output. Its output data items are loaded into a FIFO, so that various FIFO mode can be used to output the value for user-defined sequence.

The DAC driver provides an user-friendly interface to operate the DAC peripheral. User can initialize/deinitialize the DAC driver, set data into FIFO, enable the interrupt or DMA for special events so that the hardware can process the DAC output data automatically. Also, the configuration for software and hardware trigger are also included in the driver.

Typical use case

A simple use case to output the user-defined DAC value.

// ...
dac_config_t dacConfigStruct;
/* Configure the DAC. */
DAC_GetDefaultConfig(&dacConfigStruct);
DAC_Init(DEMO_DAC_BASEADDR, &dacConfigStruct);
DAC_Enable(DEMO_DAC_BASEADDR, true); /* Enable the logic and output. */
DAC_SetData(DEMO_DAC_BASEADDR, dacValue);
// The value in the first item would be converted. User can measure the output voltage from
// DACx_OUT pin.
}

Working with the trigger

Once more than one data is filled into the FIFO, the output pointer would move in configured mode when a trigger come. This trigger can be from software or hardware, and move one item for each trigger. Also, the interrupt/DMA event could be activated when the output pointer hits to the configured position.

volatile uint32_t g_DacIndex = 0U;
volatile uint32_t g_DacInterruptDone = false;
// User-defined wave for DAC output.
const uint32_t g_DacValues[DEMO_DAC_VALUE_ARRAY_SIZE] = {
0U, 100, 200, 300, 400, 500, 600, 700,
800, 900, 1000, 1100, 1200, 1300, 1400, 1500,
1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300,
2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100};
int main(void)
{
dac_config_t dacConfigStruct;
// ...
// Configure the DAC.
DAC_GetDefaultConfig(&dacConfigStruct);
dacConfigStruct.fifoTriggerMode = kDAC_FIFOTriggerBySoftwareMode; // Software trigger.
dacConfigStruct.fifoWorkMode = kDAC_FIFOWorkAsNormalMode; // Normal FIFO mode.
dacConfigStruct.fifoWatermarkLevel = 4U; // Watermark.
DAC_Init(DEMO_DAC_BASEADDR, &dacConfigStruct);
DAC_Enable(DEMO_DAC_BASEADDR, true); // Enable output.
PRINTF("Press any key to trigger the DAC...\r\n");
// Enable DAC interrupts.
EnableIRQ(DEMO_DAC_IRQ_ID); // Enable interrupt in NVIC.
while (1)
{
// Trigger the buffer and move the pointer.
GETCHAR();
DAC_DoSoftwareTriggerFIFO(DEMO_DAC_BASEADDR);
// Wait.
while (!g_DacInterruptDone)
{
}
g_DacInterruptDone = false;
PRINTF("DAC next output: %d\r\n", g_DacValues[g_DacIndex]);
}
}
// IRQ function for DAC buffer interrupt
void DEMO_DAC_IRQ_HANDLER_FUNC(void)
{
uint32_t flags = DAC_GetStatusFlags(DEMO_DAC_BASEADDR);
if (0U != (kDAC_FIFOEmptyFlag & flags))
{
if (g_DacIndex >= (DEMO_DAC_VALUE_ARRAY_SIZE - 1U))
{
g_DacIndex = 0U;
}
else
{
g_DacIndex++;
}
DAC_SetData(DEMO_DAC_BASEADDR, g_DacValues[g_DacIndex]);
}
g_DacInterruptDone = true;
}

Files

file  fsl_dac.h
 

Data Structures

struct  dac_config_t
 DAC configuration structure. More...
 

Enumerations

enum  _dac_reset_control {
  kDAC_ResetFIFO = LPDAC_RCR_FIFORST_MASK,
  kDAC_ResetLogic = LPDAC_RCR_SWRST_MASK
}
 DAC reset control. More...
 
enum  _dac_interrupt_enable {
  kDAC_FIFOFullInterruptEnable = LPDAC_IER_FULL_IE_MASK,
  kDAC_FIFOEmptyInterruptEnable = LPDAC_IER_EMPTY_IE_MASK,
  kDAC_FIFOWatermarkInterruptEnable = LPDAC_IER_WM_IE_MASK,
  kDAC_SwingBackInterruptEnable = LPDAC_IER_SWBK_IE_MASK,
  kDAC_FIFOOverflowInterruptEnable = LPDAC_IER_OF_IE_MASK,
  kDAC_FIFOUnderflowInterruptEnable = LPDAC_IER_UF_IE_MASK
}
 DAC interrupts. More...
 
enum  _dac_dma_enable {
  kDAC_FIFOEmptyDMAEnable = LPDAC_DER_EMPTY_DMAEN_MASK,
  kDAC_FIFOWatermarkDMAEnable = LPDAC_DER_WM_DMAEN_MASK
}
 DAC DMA switchers. More...
 
enum  _dac_status_flags {
  kDAC_FIFOUnderflowFlag = LPDAC_FSR_UF_MASK,
  kDAC_FIFOOverflowFlag = LPDAC_FSR_OF_MASK,
  kDAC_FIFOSwingBackFlag = LPDAC_FSR_SWBK_MASK,
  kDAC_FIFOWatermarkFlag = LPDAC_FSR_WM_MASK,
  kDAC_FIFOEmptyFlag = LPDAC_FSR_EMPTY_MASK,
  kDAC_FIFOFullFlag = LPDAC_FSR_FULL_MASK
}
 DAC status flags. More...
 
enum  dac_fifo_trigger_mode_t {
  kDAC_FIFOTriggerByHardwareMode = 0U,
  kDAC_FIFOTriggerBySoftwareMode = 1U
}
 DAC FIFO trigger mode. More...
 
enum  dac_fifo_work_mode_t {
  kDAC_FIFODisabled = 0U,
  kDAC_FIFOWorkAsNormalMode = 1U,
  kDAC_FIFOWorkAsSwingMode = 2U
}
 DAC FIFO work mode. More...
 
enum  dac_reference_voltage_source_t {
  kDAC_ReferenceVoltageSourceAlt1 = 0U,
  kDAC_ReferenceVoltageSourceAlt2 = 1U
}
 DAC reference voltage source. More...
 

Driver version

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

Initialization and de-initialization

void DAC_Init (LPDAC_Type *base, const dac_config_t *config)
 Initialize the DAC module with common configuartion. More...
 
void DAC_GetDefaultConfig (dac_config_t *config)
 Get the default settings for initialization's configuration. More...
 
void DAC_Deinit (LPDAC_Type *base)
 De-initialize the DAC module. More...
 
static void DAC_SetReset (LPDAC_Type *base, uint32_t mask)
 Assert the reset control to part hardware. More...
 
static void DAC_ClearReset (LPDAC_Type *base, uint32_t mask)
 Clear the reset control to part hardware. More...
 
static void DAC_Enable (LPDAC_Type *base, bool enable)
 Enable the DAC hardware system or not. More...
 

Interrupts

static void DAC_EnableInterrupts (LPDAC_Type *base, uint32_t mask)
 Enable the interrupts. More...
 
static void DAC_DisableInterrupts (LPDAC_Type *base, uint32_t mask)
 Disable the interrupts. More...
 

DMA control

static void DAC_EnableDMA (LPDAC_Type *base, uint32_t mask, bool enable)
 Enable the DMA switchers or not. More...
 

Status flags

static uint32_t DAC_GetStatusFlags (LPDAC_Type *base)
 Get status flags of DAC module. More...
 
static void DAC_ClearStatusFlags (LPDAC_Type *base, uint32_t flags)
 Clear status flags of DAC module. More...
 

Functional feature

static void DAC_SetData (LPDAC_Type *base, uint32_t value)
 Set data into the entry of FIFO buffer. More...
 
static uint32_t DAC_GetFIFOWritePointer (LPDAC_Type *base)
 Get the value of the FIFO write pointer. More...
 
static uint32_t DAC_GetFIFOReadPointer (LPDAC_Type *base)
 Get the value of the FIFO read pointer. More...
 
static void DAC_DoSoftwareTriggerFIFO (LPDAC_Type *base)
 Do software trigger to FIFO when in software mode. More...
 

Data Structure Documentation

struct dac_config_t

Data Fields

uint32_t fifoWatermarkLevel
 FIFO's watermark, the max value can be the hardware FIFO size. More...
 
dac_fifo_trigger_mode_t fifoTriggerMode
 Select the trigger mode for FIFO. More...
 
dac_fifo_work_mode_t fifoWorkMode
 Select the work mode for FIFO. More...
 
bool enableLowPowerMode
 Enable the low power mode. More...
 
dac_reference_voltage_source_t referenceVoltageSource
 Select the reference voltage source. More...
 

Field Documentation

uint32_t dac_config_t::fifoWatermarkLevel
dac_fifo_trigger_mode_t dac_config_t::fifoTriggerMode
dac_fifo_work_mode_t dac_config_t::fifoWorkMode
bool dac_config_t::enableLowPowerMode
dac_reference_voltage_source_t dac_config_t::referenceVoltageSource

Macro Definition Documentation

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

Enumeration Type Documentation

Enumerator
kDAC_ResetFIFO 

Resets the FIFO pointers and flags.

kDAC_ResetLogic 

Resets all DAC registers and internal logic.

Enumerator
kDAC_FIFOFullInterruptEnable 

FIFO full interrupt enable.

kDAC_FIFOEmptyInterruptEnable 

FIFO empty interrupt enable.

kDAC_FIFOWatermarkInterruptEnable 

FIFO watermark interrupt enable.

kDAC_SwingBackInterruptEnable 

Swing back one cycle complete interrupt enable.

kDAC_FIFOOverflowInterruptEnable 

FIFO overflow interrupt enable.

kDAC_FIFOUnderflowInterruptEnable 

FIFO underflow interrupt enable.

Enumerator
kDAC_FIFOEmptyDMAEnable 

FIFO empty DMA enable.

kDAC_FIFOWatermarkDMAEnable 

FIFO watermark DMA enable.

Enumerator
kDAC_FIFOUnderflowFlag 
   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 to it.

kDAC_FIFOOverflowFlag 
   This flag indicates that data is intended to write into FIFO after the

buffer is full.

The writer pointer will 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.

kDAC_FIFOSwingBackFlag 
   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 example, after three data is written to FIFO, the writer pointer is now 3. Then, if continually triggered, the 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 cleared by writing a 1 to it.

kDAC_FIFOWatermarkFlag 
   This field is set if the remaining data in FIFO is less than or equal

to the setting value of wartermark.

By writing data into FIFO by DMA or CPU, this flag is cleared automatically when the data in FIFO is more than the setting value of watermark.

kDAC_FIFOEmptyFlag 

FIFO empty flag.

kDAC_FIFOFullFlag 

FIFO full flag.

Enumerator
kDAC_FIFOTriggerByHardwareMode 

Buffer would be triggered by hardware.

kDAC_FIFOTriggerBySoftwareMode 

Buffer would be triggered by software.

Enumerator
kDAC_FIFODisabled 

FIFO mode is disabled and buffer mode is enabled.

Any data written to DATA[DATA] goes to buffer then goes to conversion.

kDAC_FIFOWorkAsNormalMode 

FIFO mode is enabled.

Data will be first read from FIFO to buffer then goes to conversion.

kDAC_FIFOWorkAsSwingMode 

In swing mode, the read pointer swings between the writer pointer and zero.

That is, the trigger increases the read pointer till reach the writer pointer and decreases the read pointer till zero, and so on. The FIFO empty/full/watermark flag will not update during swing back mode.

Enumerator
kDAC_ReferenceVoltageSourceAlt1 

The DAC selects VREFH_INT as the reference voltage.

kDAC_ReferenceVoltageSourceAlt2 

The DAC selects VREFH_EXT as the reference voltage.

Function Documentation

void DAC_Init ( LPDAC_Type *  base,
const dac_config_t config 
)

The clock will be enabled in this function.

Parameters
baseDAC peripheral base address.
configPointer to configuration structure.
void DAC_GetDefaultConfig ( dac_config_t config)

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

* config->fifoWatermarkLevel = 0U;
* config->fifoTriggerMode = kDAC_FIFOTriggerByHardwareMode;
* config->fifoWorkMode = kDAC_FIFODisabled;
* config->enableLowPowerMode = false;
* config->referenceVoltageSource = kDAC_ReferenceVoltageSourceAlt1;
*
Parameters
configPointer to configuration structure.
void DAC_Deinit ( LPDAC_Type *  base)

The clock will be disabled in this function.

Parameters
baseDAC peripheral base address.
static void DAC_SetReset ( LPDAC_Type *  base,
uint32_t  mask 
)
inlinestatic

This fucntion is to assert the reset control to part hardware. Responding part hardware would remain reset untill cleared by software.

Parameters
baseDAC peripheral base address.
maskThe reset control mask, see to #_dac_reset_control_t.
static void DAC_ClearReset ( LPDAC_Type *  base,
uint32_t  mask 
)
inlinestatic

This fucntion is to clear the reset control to part hardware. Responding part hardware would work after the reset control is cleared by software.

Parameters
baseDAC peripheral base address.
maskThe reset control mask, see to #_dac_reset_control_t.
static void DAC_Enable ( LPDAC_Type *  base,
bool  enable 
)
inlinestatic

This function is to start the Programmable Reference Generator operation or not.

Parameters
baseDAC peripheral base address.
enableAssertion of indicated event.
static void DAC_EnableInterrupts ( LPDAC_Type *  base,
uint32_t  mask 
)
inlinestatic
Parameters
baseDAC peripheral base address.
maskMask value of indicated interrupt events. See to _dac_interrupt_enable.
static void DAC_DisableInterrupts ( LPDAC_Type *  base,
uint32_t  mask 
)
inlinestatic
Parameters
baseDAC peripheral base address.
maskMask value of indicated interrupt events. See to _dac_interrupt_enable.
static void DAC_EnableDMA ( LPDAC_Type *  base,
uint32_t  mask,
bool  enable 
)
inlinestatic
Parameters
baseDAC peripheral base address.
maskMask value of indicated DMA requeset. See to _dac_dma_enable.
enableEnable the DMA or not.
static uint32_t DAC_GetStatusFlags ( LPDAC_Type *  base)
inlinestatic
Parameters
baseDAC peripheral base address.
Returns
Mask value of status flags. See to _dac_status_flags.
static void DAC_ClearStatusFlags ( LPDAC_Type *  base,
uint32_t  flags 
)
inlinestatic
Parameters
baseDAC peripheral base address.
flagsMask value of status flags to be cleared. See to _dac_status_flags.
static void DAC_SetData ( LPDAC_Type *  base,
uint32_t  value 
)
inlinestatic
Parameters
baseDAC peripheral base address.
valueSetting value into FIFO buffer.
static uint32_t DAC_GetFIFOWritePointer ( LPDAC_Type *  base)
inlinestatic
Parameters
baseDAC peripheral base address.
Returns
Current value of the FIFO write pointer.
static uint32_t DAC_GetFIFOReadPointer ( LPDAC_Type *  base)
inlinestatic
Parameters
baseDAC peripheral base address.
Returns
Current value of the FIFO read pointer.
static void DAC_DoSoftwareTriggerFIFO ( LPDAC_Type *  base)
inlinestatic
Parameters
baseDAC peripheral base address.