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_Init(DEMO_DAC_BASEADDR, &dacConfigStruct);
}
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;
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_Init(DEMO_DAC_BASEADDR, &dacConfigStruct);
PRINTF("Press any key to trigger the DAC...\r\n");
EnableIRQ(DEMO_DAC_IRQ_ID);
while (1)
{
GETCHAR();
while (!g_DacInterruptDone)
{
}
g_DacInterruptDone = false;
PRINTF("DAC next output: %d\r\n", g_DacValues[g_DacIndex]);
}
}
void DEMO_DAC_IRQ_HANDLER_FUNC(void)
{
{
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;
}
|
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...
|
|
|
static void | DAC_EnableDMA (LPDAC_Type *base, uint32_t mask, bool enable) |
| Enable the DMA switchers or not. More...
|
|
uint32_t dac_config_t::fifoWatermarkLevel |
bool dac_config_t::enableLowPowerMode |
#define FSL_DAC_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) |
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.
|
void DAC_Init |
( |
LPDAC_Type * |
base, |
|
|
const dac_config_t * |
config |
|
) |
| |
The clock will be enabled in this function.
- Parameters
-
base | DAC peripheral base address. |
config | Pointer to configuration structure. |
This function initializes the user configuration structure to a default value. The default values are:
* config->fifoWatermarkLevel = 0U;
* config->enableLowPowerMode = false;
*
- Parameters
-
config | Pointer to configuration structure. |
| |
void DAC_Deinit |
( |
LPDAC_Type * |
base | ) |
|
The clock will be disabled in this function.
- Parameters
-
base | DAC 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
-
base | DAC peripheral base address. |
mask | The 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
-
base | DAC peripheral base address. |
mask | The 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
-
base | DAC peripheral base address. |
enable | Assertion of indicated event. |
static void DAC_EnableInterrupts |
( |
LPDAC_Type * |
base, |
|
|
uint32_t |
mask |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | DAC peripheral base address. |
mask | Mask value of indicated interrupt events. See to _dac_interrupt_enable. |
static void DAC_DisableInterrupts |
( |
LPDAC_Type * |
base, |
|
|
uint32_t |
mask |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | DAC peripheral base address. |
mask | Mask value of indicated interrupt events. See to _dac_interrupt_enable. |
static void DAC_EnableDMA |
( |
LPDAC_Type * |
base, |
|
|
uint32_t |
mask, |
|
|
bool |
enable |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | DAC peripheral base address. |
mask | Mask value of indicated DMA requeset. See to _dac_dma_enable. |
enable | Enable the DMA or not. |
static uint32_t DAC_GetStatusFlags |
( |
LPDAC_Type * |
base | ) |
|
|
inlinestatic |
- Parameters
-
base | DAC 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
-
base | DAC peripheral base address. |
flags | Mask value of status flags to be cleared. See to _dac_status_flags. |
static void DAC_SetData |
( |
LPDAC_Type * |
base, |
|
|
uint32_t |
value |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | DAC peripheral base address. |
value | Setting value into FIFO buffer. |
static uint32_t DAC_GetFIFOWritePointer |
( |
LPDAC_Type * |
base | ) |
|
|
inlinestatic |
- Parameters
-
base | DAC peripheral base address. |
- Returns
- Current value of the FIFO write pointer.
static uint32_t DAC_GetFIFOReadPointer |
( |
LPDAC_Type * |
base | ) |
|
|
inlinestatic |
- Parameters
-
base | DAC peripheral base address. |
- Returns
- Current value of the FIFO read pointer.
static void DAC_DoSoftwareTriggerFIFO |
( |
LPDAC_Type * |
base | ) |
|
|
inlinestatic |
- Parameters
-
base | DAC peripheral base address. |