Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages

Overview

This section describes the programming interface of the DAC Peripheral driver. The DAC peripheral driver configures the DAC (Digital-to-Analog Converter). It also handles the module initialization and configuration by converting attributes.

DAC Initialization

To initialize the DAC module, call the DAC_DRV_Init() function and pass the configuration data structure, which can be filled by the DAC_DRV_StructInitUserConfigNormal() function with the default settings for the converter. After it is initialized, the DAC module can function as a DAC converter.

The DAC module provides advanced features internally with the hardware buffer. To use the advanced features, the API of the DAC_DRV_ConfigBuffer() function should be called to initialize the buffer.

DAC Model building

The DAC module provides advanced features with the hardware DAC buffer.

When the DAC is enabled and the buffer is not enabled, the DAC module always converts the data in DAT0, the first item in the buffer, to analog output voltage. If the buffer is enabled, the DAC converts the items in the data buffer to analog output voltage according to the buffer configuration. The data buffer read pointer advances to the next word whenever any hardware or software trigger event occurs. The data buffer can be configured to operate in Normal mode, Swing mode, One-Time Scan mode or FIFO mode:

DAC Call diagram

There are four kinds of typical use cases:

These use cases function either on the software trigger or the hardware trigger. To use the hardware trigger, enable the hardware trigger setting in the DAC module. Then, configure the other module that can generate the trigger, such as the PDB.

These are examples to initialize and configure the DAC driver for typical use cases:

Normal converter mode:

// dac_test_normal.c //
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "fsl_dac_driver.h"
#include "fsl_os_abstraction.h"
#define DAC_TEST_BUFF_SIZE (16U)
volatile uint32_t g_dacInstance = 0U;
static uint16_t g_dacBuffDat[DAC_TEST_BUFF_SIZE];
extern void DAC_TEST_FillBuffDat(uint16_t *buffPtr, uint32_t buffLen);
void DAC_TEST_NormalMode(uint32_t instance)
{
dac_user_config_t MyDacUserConfigStruct;
uint8_t i;
g_dacInstance = instance;
// Fill values into data buffer. //
DAC_TEST_FillBuffDat(g_dacBuffDat, DAC_TEST_BUFF_SIZE);
// Fill the structure with configuration of software trigger. //
DAC_DRV_StructInitUserConfigNormal(&MyDacUserConfigStruct);
// Initialize the DAC Converter. //
DAC_DRV_Init(instance, &MyDacUserConfigStruct);
// Output the DAC value. //
for (i = 0U; i < DAC_TEST_BUFF_SIZE; i++)
{
printf("DAC_DRV_Output: %d\r\n", g_dacInstance[i]);
DAC_DRV_Output(instance, g_dacInstance[i]);
}
// De-initialize the DAC converter. //
DAC_DRV_Deinit(instance);
}

Buffer Normal mode:

// dac_test_buffer_normal.c //
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "fsl_dac_driver.h"
#include "fsl_os_abstraction.h"
#define DAC_TEST_BUFF_SIZE (16U)
static dac_state_t MyDacStateStructForBufferNormal;
static uint32_t MyDacIsrCounterBuffStart = 0U;
static uint32_t MyDacIsrCounterBuffUpper = 0U;
static uint32_t MyDacIsrCounterBuffWatermark = 0U;
volatile uint32_t g_dacInstance = 0U;
static uint16_t g_dacBuffDat[DAC_TEST_BUFF_SIZE];
static void DAC_ISR_Buffer(void);
extern void DAC_TEST_FillBuffDat(uint16_t *buffPtr, uint32_t buffLen);
void DAC_TEST_BufferNormalMode(uint32_t instance, uint16_t *buffPtr, uint8_t buffLen)
{
dac_converter_config_t dacUserConfigStruct;
dac_buffer_config_t dacBuffConfigStruct;
uint8_t i;
volatile uint16_t dacValue;
// Fill the structure with configuration of software trigger. //
DAC_DRV_StructInitUserConfigNormal(&dacUserConfigStruct);
// Initialize the DAC Converter. //
DAC_DRV_Init(instance, &dacUserConfigStruct);
// Register the callback function for DAC buffer event. //
DAC_TEST_InstallCallback(instance, DAC_ISR_Buffer);
// Enable the feature of DAC internal buffer. //
dacBuffConfigStruct.bufferEnable = true;
dacBuffConfigStruct.triggerMode = kDacTriggerBySoftware;
#if FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
dacBuffConfigStruct.idxWatermarkIntEnable = true;
dacBuffConfigStruct.watermarkMode = kDacBuffWatermarkFromUpperAs2Word;
#endif // FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION //
dacBuffConfigStruct.idxStartIntEnable = true;
dacBuffConfigStruct.idxUpperIntEnable = true;
dacBuffConfigStruct.dmaEnable = false;
dacBuffConfigStruct.buffWorkMode = kDacBuffWorkAsNormalMode;
dacBuffConfigStruct.upperIdx = buffLen - 1U;
DAC_DRV_ConfigBuffer(instance, &dacBuffConfigStruct);
// Fill the buffer with setting data. //
DAC_DRV_SetBuffValue(instance, 0, buffLen, buffPtr);
// Trigger the buffer to output setting value. //
PRINTF("DAC_DRV_SoftTriggerBuff:\r\n");
for (i = 0; i < buffLen*2U; i++)
{
PRINTF("%d: %d\r\n", i, buffPtr[DAC_DRV_GetBuffCurIdx(instance)]);
}
// De-initialize the DAC converter. //
DAC_DRV_Deinit(instance);
}
static void DAC_ISR_Buffer(void)
{
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexStartFlag) )
{
MyDacIsrCounterBuffStart++;
}
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexWatermarkFlag) )
{
MyDacIsrCounterBuffWatermark++;
}
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexUpperFlag) )
{
MyDacIsrCounterBuffUpper++;
}
}

Buffer Swing mode:

// dac_test_buffer_swing.c //
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "fsl_dac_driver.h"
#include "fsl_os_abstraction.h"
#define DAC_TEST_BUFF_SIZE (16U)
static dac_state_t MyDacStateStructForBufferSwing;
static uint32_t MyDacIsrCounterBuffStart = 0U;
static uint32_t MyDacIsrCounterBuffUpper = 0U;
static uint32_t MyDacIsrCounterBuffWatermark = 0U;
volatile uint32_t g_dacInstance = 0U;
static uint16_t g_dacBuffDat[DAC_TEST_BUFF_SIZE];
static void DAC_ISR_Buffer(void);
extern void DAC_TEST_FillBuffDat(uint16_t *buffPtr, uint32_t buffLen);
void DAC_TEST_BufferSwingMode(uint32_t instance, uint16_t *buffPtr, uint8_t buffLen)
{
dac_converter_config_t dacUserConfigStruct;
dac_buffer_config_t MyDacBuffConfigStruct;
uint8_t i;
volatile uint16_t dacValue;
// Fill the structure with configuration of software trigger. //
DAC_DRV_StructInitUserConfigNormal(&dacUserConfigStruct);
// Initialize the DAC Converter. //
DAC_DRV_Init(instance, &dacUserConfigStruct);
// Register the callback function for DAC buffer event. //
DAC_TEST_InstallCallback(instance, DAC_ISR_Buffer);
// Enable the feature of DAC internal buffer. //
MyDacBuffConfigStruct.bufferEnable = true;
MyDacBuffConfigStruct.triggerMode = kDacTriggerBySoftware;
#if FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
MyDacBuffConfigStruct.idxWatermarkIntEnable = true;
MyDacBuffConfigStruct.watermarkMode = kDacBuffWatermarkFromUpperAs2Word;
#endif // FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION //
MyDacBuffConfigStruct.idxStartIntEnable = true;
MyDacBuffConfigStruct.idxUpperIntEnable = true;
MyDacBuffConfigStruct.dmaEnable = false;
MyDacBuffConfigStruct.buffWorkMode = kDacBuffWorkAsSwingMode;
MyDacBuffConfigStruct.upperIdx = buffLen - 1U;
DAC_DRV_ConfigBuffer(instance, &MyDacBuffConfigStruct);
// Fill the buffer with setting data. //
for (i = 0; i < buffLen; i++)
{
DAC_DRV_SetBuffValue(instance, 0, buffLen, buffPtr);
}
// Trigger the buffer to output setting value. //
PRINTF("DAC_DRV_SoftTriggerBuff:\r\n");
for (i = 0; i < buffLen*2U; i++)
{
PRINTF("%d: %d\r\n", i, buffPtr[DAC_DRV_GetBuffCurIdx(instance)]);
}
// De-initialize the DAC converter. //
DAC_DRV_Deinit(instance);
}
static void DAC_ISR_Buffer(void)
{
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexStartFlag) )
{
MyDacIsrCounterBuffStart++;
}
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexWatermarkFlag) )
{
MyDacIsrCounterBuffWatermark++;
}
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexUpperFlag) )
{
MyDacIsrCounterBuffUpper++;
}
}

Buffer One-time Scan mode:

// dac_test_buffer_one_time_scan.c //
#include <stdio.h>
#inlude <stdint.h>
#include <stdbool.h>
#include "fsl_dac_driver.h"
#include "fsl_os_abstraction.h"
#define DAC_TEST_BUFF_SIZE (16U)
static dac_state_t MyDacStateStructForBufferOneTimeScan;
static uint32_t MyDacIsrCounterBuffStart = 0U;
static uint32_t MyDacIsrCounterBuffUpper = 0U;
static uint32_t MyDacIsrCounterBuffWatermark = 0U;
volatile uint32_t g_dacInstance = 0U;
static uint16_t g_dacBuffDat[DAC_TEST_BUFF_SIZE];
static void DAC_ISR_Buffer(void);
extern void DAC_TEST_FillBuffDat(uint16_t *buffPtr, uint32_t buffLen);
void DAC_TEST_BufferOneTimeScanMode(uint32_t instance, uint16_t *buffPtr, uint8_t buffLen)
{
dac_converter_config_t MyDacUserConfigStruct;
dac_buffer_config_t MyDacBuffConfigStruct;
uint8_t i;
volatile uint16_t dacValue;
// Fill the structure with configuration of software trigger. //
DAC_DRV_StructInitUserConfigNormal(&MyDacUserConfigStruct);
// Initialize the DAC Converter. //
DAC_DRV_Init(instance, &MyDacUserConfigStruct);
// Register the callback function for DAC buffer event. //
DAC_TEST_InstallCallback(instance, DAC_ISR_Buffer);
// Enable the feature of DAC internal buffer. //
MyDacBuffConfigStruct.bufferEnable = true;
MyDacBuffConfigStruct.triggerMode = kDacTriggerBySoftware;
#if FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
MyDacBuffConfigStruct.idxWatermarkIntEnable = true;
MyDacBuffConfigStruct.watermarkMode = kDacBuffWatermarkFromUpperAs2Word;
#endif // FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION //
MyDacBuffConfigStruct.idxStartIntEnable = true;
MyDacBuffConfigStruct.idxUpperIntEnable = true;
MyDacBuffConfigStruct.dmaEnable = false;
MyDacBuffConfigStruct.buffWorkMode = kDacBuffWorkAsOneTimeScanMode;
MyDacBuffConfigStruct.upperIdx = buffLen - 1U;
DAC_DRV_ConfigBuffer(instance, &MyDacBuffConfigStruct);
// Fill the buffer with setting data. //
DAC_DRV_SetBuffValue(instance, 0, buffLen, buffPtr);
// Trigger the buffer to output setting value. //
PRINTF("DAC_DRV_SoftTriggerBuff:\r\n");
for (i = 0; i < buffLen*2U; i++)
{
PRINTF("%d: %d\r\n", i, buffPtr[DAC_DRV_GetBuffCurIdx(instance)]);
}
// De-initialize the DAC converter. //
DAC_DRV_Deinit(instance);
}
static void DAC_ISR_Buffer(void)
{
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexStartFlag) )
{
MyDacIsrCounterBuffStart++;
}
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexWatermarkFlag) )
{
MyDacIsrCounterBuffWatermark++;
}
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexUpperFlag) )
{
MyDacIsrCounterBuffUpper++;
}
}

Buffer FIFO mode:

// dac_test_buffer_fifo.c //
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "fsl_dac_driver.h"
#include "fsl_os_abstraction.h"
#define DAC_TEST_BUFF_SIZE (16U)
static dac_state_t MyDacStateStructForBufferFIFO;
static uint32_t MyDacIsrCounterBuffStart = 0U;
static uint32_t MyDacIsrCounterBuffUpper = 0U;
static uint32_t MyDacIsrCounterBuffWatermark = 0U;
volatile uint32_t g_dacInstance = 0U;
volatile uint32_t g_dacBuffIndex = 0U;
static uint16_t g_dacBuffDat[DAC_TEST_BUFF_SIZE];
static void DAC_ISR_Buffer(void);
extern void DAC_TEST_FillBuffDat(uint16_t *buffPtr, uint32_t buffLen);
void DAC_TEST_BufferFIFOMode(uint32_t instance, uint16_t *buffPtr, uint8_t buffLen)
{
dac_converter_config_t dacUserConfigStruct;
dac_buffer_config_t dacBuffConfigStruct;
uint8_t i;
volatile uint16_t dacValue;
// Fill the structure with configuration of software trigger. //
DAC_DRV_StructInitUserConfigNormal(&dacUserConfigStruct);
// Initialize the DAC Converter. //
DAC_DRV_Init(instance, &dacUserConfigStruct);
// Enable the feature of DAC internal buffer. //
dacBuffConfigStruct.bufferEnable = true;
dacBuffConfigStruct.triggerMode = kDacTriggerBySoftware;
#if FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
dacBuffConfigStruct.idxWatermarkIntEnable = false;
dacBuffConfigStruct.watermarkMode = kDacBuffWatermarkFromUpperAs2Word;
#endif // FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION //
dacBuffConfigStruct.idxStartIntEnable = false;
dacBuffConfigStruct.idxUpperIntEnable = false;
dacBuffConfigStruct.dmaEnable = false;
dacBuffConfigStruct.buffWorkMode = kDacBuffWorkAsFIFOMode;
dacBuffConfigStruct.upperIdx = 0; // Write Pointer. //
DAC_DRV_ConfigBuffer(instance, &dacBuffConfigStruct);
// Fill the FIFO with setting data. //
for (i = 0; i < buffLen; i++)
{
DAC_DRV_SetBuffValue(instance, 0, 1U, &buffPtr[i]);
}
// Trigger the buffer to output setting value. //
PRINTF("DAC_DRV_SoftTriggerBuff:\r\n");
for (i = 0; i < buffLen*2U; i++)
{
PRINTF("%d: %d\r\n", i, buffPtr[DAC_DRV_GetBuffCurIdx(instance)]);
}
// De-initialize the DAC converter. //
DAC_DRV_Deinit(instance);
}
static void DAC_ISR_Buffer(void)
{
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexStartFlag) )
{
MyDacIsrCounterBuffStart++;
}
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexWatermarkFlag) )
{
// When the FIFO's data count is less then the watermark, it would be filled here automatically.
//
if (g_dacBuffIndex >= DAC_TEST_BUFF_SIZE)
{
g_dacBuffIndex = 0U;
}
DAC_DRV_SetBuffValue(g_dacInstance, 0U, 1U, &g_dacBuffDat[g_dacBuffIndex++]);
MyDacIsrCounterBuffWatermark++;
}
if ( DAC_DRV_GetFlag(g_dacInstance, kDacBuffIndexUpperFlag) )
{
MyDacIsrCounterBuffUpper++;
}
}

Enumerations

enum  dac_flag_t {
  kDacBuffIndexStartFlag = 1U,
  kDacBuffIndexUpperFlag = 2U
}
 Defines the type of event flags. More...
 

Functions

dac_status_t DAC_DRV_StructInitUserConfigNormal (dac_converter_config_t *userConfigPtr)
 Populates the initial user configuration for the DAC module without interrupt and buffer features. More...
 
dac_status_t DAC_DRV_Init (uint32_t instance, const dac_converter_config_t *userConfigPtr)
 Initializes the converter. More...
 
dac_status_t DAC_DRV_Deinit (uint32_t instance)
 De-initializes the DAC module converter. More...
 
void DAC_DRV_Output (uint32_t instance, uint16_t value)
 Drives the converter to output the DAC value. More...
 
dac_status_t DAC_DRV_ConfigBuffer (uint32_t instance, const dac_buffer_config_t *configPtr)
 Configures the internal buffer. More...
 
dac_status_t DAC_DRV_SetBuffValue (uint32_t instance, uint8_t start, uint8_t offset, uint16_t arr[])
 Sets values into the DAC internal buffer. More...
 
void DAC_DRV_SoftTriggerBuffCmd (uint32_t instance)
 Triggers the buffer by software and returns the current value. More...
 
void DAC_DRV_ClearBuffFlag (uint32_t instance, dac_flag_t flag)
 Clears the flag for an indicated event causing an interrupt. More...
 
bool DAC_DRV_GetBuffFlag (uint32_t instance, dac_flag_t flag)
 Gets the flag for an indicated event causing an interrupt. More...
 
void DAC_DRV_SetBuffCurIdx (uint32_t instance, uint8_t idx)
 Sets the current read pointer in DAC buffer. More...
 
uint8_t DAC_DRV_GetBuffCurIdx (uint32_t instance)
 Gets the current read pointer in the DAC buffer. More...
 

Variables

DAC_Type *const g_dacBase []
 Table of base addresses for DAC instances. More...
 
const IRQn_Type g_dacIrqId [DAC_INSTANCE_COUNT]
 Table to save DAC IRQ enumeration numbers defined in the CMSIS header file. More...
 

Enumeration Type Documentation

enum dac_flag_t
Enumerator
kDacBuffIndexStartFlag 

Event for the buffer index reaching start (0).

kDacBuffIndexUpperFlag 

Event for the buffer index reaching the upper section.

Function Documentation

dac_status_t DAC_DRV_StructInitUserConfigNormal ( dac_converter_config_t userConfigPtr)

This function populates the initial user configuration without interrupt and buffer features. Calling the initialization function with the populated parameter configures the DAC module to operate as a simple converter. The settings are:

  • .dacRefVoltSrc = kDacRefVoltSrcOfVref2; // Vdda
  • .lowPowerEnable = false;
Parameters
userConfigPtrPointer to the user configuration structure. See the "dac_user_config_t".
Returns
Execution status.
dac_status_t DAC_DRV_Init ( uint32_t  instance,
const dac_converter_config_t userConfigPtr 
)

This function initializes the converter.

Parameters
instanceDAC instance ID.
userConfigPtrPointer to the initialization structure. See the "dac_user_config_t".
Returns
Execution status.
dac_status_t DAC_DRV_Deinit ( uint32_t  instance)

This function de-initializes the converter. It disables the DAC module and shuts down the clock to reduce the power consumption.

Parameters
instanceDAC instance ID.
Returns
Execution status.
void DAC_DRV_Output ( uint32_t  instance,
uint16_t  value 
)

This function drives the converter to output the DAC value. It forces the buffer index to be the first one and load the setting value to this item. Then, the converter outputs the voltage indicated by the indicated value immediately.

Parameters
instanceDAC instance ID.
valueSetting value for DAC.
dac_status_t DAC_DRV_ConfigBuffer ( uint32_t  instance,
const dac_buffer_config_t configPtr 
)

This function configures the feature of the internal buffer for the DAC module. By default, the buffer feature is disabled. Calling this API enables the buffer and configures it.

Parameters
instanceDAC instance ID.
configPtrPointer to the configuration structure. See the "dac_buff_config_t".
Returns
Execution status.
dac_status_t DAC_DRV_SetBuffValue ( uint32_t  instance,
uint8_t  start,
uint8_t  offset,
uint16_t  arr[] 
)

This function sets values into the DAC internal buffer. Note that the buffer size is defined by the "FSL_FEATURE_DAC_BUFFER_SIZE" macro and the available value is 12 bits.

Parameters
instanceDAC instance ID.
startStart index of setting values.
offsetLength of setting values' array.
arrSetting values' array.
Returns
Execution status.
void DAC_DRV_SoftTriggerBuffCmd ( uint32_t  instance)

This function triggers the buffer by software and returns the current value. After it is triggered, the buffer index updates according to work mode. Then, the value kept inside the pointed item is immediately output.

Parameters
instanceDAC instance ID.
void DAC_DRV_ClearBuffFlag ( uint32_t  instance,
dac_flag_t  flag 
)

This function clears the flag for an indicated event causing an interrupt.

Parameters
instanceDAC instance ID.
flagIndicated flag. See "dac_flag_t".
bool DAC_DRV_GetBuffFlag ( uint32_t  instance,
dac_flag_t  flag 
)

This function gets the flag for an indicated event causing an interrupt. If the event occurs, the return value is asserted.

Parameters
instanceDAC instance ID.
flagIndicated flag. See "dac_flag_t".
Returns
Assertion of indicated event.
void DAC_DRV_SetBuffCurIdx ( uint32_t  instance,
uint8_t  idx 
)

This function sets the current read pointer in DAC buffer.

Parameters
instanceDAC instance ID.
idxIndex for read pointer in buffer.
uint8_t DAC_DRV_GetBuffCurIdx ( uint32_t  instance)

This function gets the current read pointer in DAC buffer.

Parameters
instanceDAC instance ID.
Returns
Index for current read pointer in buffer.

Variable Documentation

DAC_Type* const g_dacBase[]
const IRQn_Type g_dacIrqId[DAC_INSTANCE_COUNT]