All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
DAC: Digital-to-Analog Converter Driver

## Overview

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

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

The basic DAC module supports operations unique to the DAC converter in each DAC instance. The APIs in this part are used in the initialization phase, which is necessary for enabling 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 (index of buffer), item values, and so on.

# 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
mask |= kDAC_BufferWatermarkInterruptEnable;
#endif // FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
mask |= kDAC_BufferReadPointerTopInterruptEnable | kDAC_BufferReadPointerBottomInterruptEnable;
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
if (kDAC_BufferWatermarkFlag == (kDAC_BufferWatermarkFlag & flags))
{
g_DacBufferWatermarkInterruptFlag = true;
}
#endif // FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
if (kDAC_BufferReadPointerTopPositionFlag == (kDAC_BufferReadPointerTopPositionFlag & flags))
{
g_DacBufferReadPointerTopPositionInterruptFlag = true;
}
if (kDAC_BufferReadPointerBottomPositionFlag == (kDAC_BufferReadPointerBottomPositionFlag & flags))
{
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_BufferReadPointerTopPositionFlag = DAC_SR_DACBFRPTF_MASK, kDAC_BufferReadPointerBottomPositionFlag = DAC_SR_DACBFRPBF_MASK }
| DAC buffer flags. More...
enum | _dac_buffer_interrupt_enable { 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_work_mode_t { kDAC_BufferWorkAsNormalMode = 0U, kDAC_BufferWorkAsOneTimeScanMode }
| 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 by 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_work_mode_t | workMode
| Select the buffer's work mode. More...
uint8_t | upperLimit
| Set the upper limit for buffer index. More...

#### Field Documentation

dac_buffer_trigger_mode_t dac_buffer_config_t::triggerMode

dac_buffer_work_mode_t dac_buffer_config_t::workMode

uint8_t dac_buffer_config_t::upperLimit

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

## Macro Definition Documentation

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

## Enumeration Type Documentation

enum _dac_buffer_status_flags

Enumerator
kDAC_BufferReadPointerTopPositionFlag | DAC Buffer Read Pointer Top Position Flag.
kDAC_BufferReadPointerBottomPositionFlag | DAC Buffer Read Pointer Bottom Position Flag.

enum _dac_buffer_interrupt_enable

Enumerator
kDAC_BufferReadPointerTopInterruptEnable | DAC Buffer Read Pointer Top Flag Interrupt Enable.
kDAC_BufferReadPointerBottomInterruptEnable | DAC Buffer Read Pointer Bottom Flag Interrupt Enable.

enum dac_reference_voltage_source_t

Enumerator
kDAC_ReferenceVoltageSourceVref1 | The DAC selects DACREF_1 as the reference voltage.
kDAC_ReferenceVoltageSourceVref2 | The DAC selects DACREF_2 as the reference voltage.

enum dac_buffer_trigger_mode_t

Enumerator
kDAC_BufferTriggerByHardwareMode | The DAC hardware trigger is selected.
kDAC_BufferTriggerBySoftwareMode | The DAC software trigger is selected.

enum dac_buffer_work_mode_t

Enumerator
kDAC_BufferWorkAsNormalMode | Normal mode.
kDAC_BufferWorkAsOneTimeScanMode | One-Time Scan mode.

## Function Documentation

void DAC_Init | ( | DAC_Type * | base,
|  | const dac_config_t * | config
| ) |  |

This function initializes the DAC module, including:

- Enabling the clock for DAC module.
- Configuring the DAC converter with a user configuration.
- Enabling the DAC module.

Parameters

  base | DAC peripheral base address.
  config | Pointer to the configuration structure. See "dac_config_t".

void DAC_Deinit | ( | DAC_Type * | base | ) |

This function de-initializes the DAC module, including:

- Disabling the DAC module.
- Disabling the clock for the DAC module.

Parameters

  base | DAC 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:

* config->referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2;
* config->enableLowPowerMode = false;
*

Parameters

  config | Pointer to the configuration structure. See "dac_config_t".

static void DAC_Enable | ( | DAC_Type * | base,
|  | bool | enable
| ) |  |

inlinestatic

Parameters

  base | DAC peripheral base address.
  enable | Enables/disables the feature.

static void DAC_EnableBuffer | ( | DAC_Type * | base,
|  | bool | enable
| ) |  |

inlinestatic

Parameters

  base | DAC peripheral base address.
  enable | Enables/disables the feature.

void DAC_SetBufferConfig | ( | DAC_Type * | base,
|  | const dac_buffer_config_t * | config
| ) |  |

Parameters

  base | DAC peripheral base address.
  config | Pointer 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 a default value. The default values are:

* config->triggerMode = kDAC_BufferTriggerBySoftwareMode;
* config->watermark = kDAC_BufferWatermark1Word;
* config->workMode = kDAC_BufferWorkAsNormalMode;
* config->upperLimit = DAC_DATL_COUNT - 1U;
*

Parameters

  config | Pointer to the configuration structure. See "dac_buffer_config_t".

static void DAC_EnableBufferDMA | ( | DAC_Type * | base,
|  | bool | enable
| ) |  |

inlinestatic

Parameters

  base | DAC peripheral base address.
  enable | Enables/disables the feature.

void DAC_SetBufferValue | ( | DAC_Type * | base,
|  | uint8_t | index,
|  | uint16_t | value
| ) |  |

Parameters

  base | DAC peripheral base address.
  index | Setting index for items in the buffer. The available index should not exceed the size of the DAC buffer.
  value | Setting value for items in the buffer. 12-bits are available.

static void DAC_DoSoftwareTriggerBuffer | ( | DAC_Type * | base | ) |

inlinestatic

This function triggers the function by 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

  base | DAC 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 by software trigger or hardware trigger.

Parameters

  base | DAC peripheral base address.

Returns
  Current read pointer of 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 by software trigger or hardware trigger. After the read pointer changes, the DAC output value also changes.

Parameters

  base | DAC peripheral base address.
  index | Setting index value for the pointer.

void DAC_EnableBufferInterrupts | ( | DAC_Type * | base,
|  | uint32_t | mask
| ) |  |

Parameters

  base | DAC peripheral base address.
  mask | Mask value for interrupts. See "_dac_buffer_interrupt_enable".

void DAC_DisableBufferInterrupts | ( | DAC_Type * | base,
|  | uint32_t | mask
| ) |  |

Parameters

  base | DAC peripheral base address.
  mask | Mask value for interrupts. See "_dac_buffer_interrupt_enable".

uint32_t DAC_GetBufferStatusFlags | ( | DAC_Type * | base | ) |

Parameters

  base | DAC 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

  base | DAC peripheral base address.
  mask | Mask value for flags. See "_dac_buffer_status_flags_t".