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

Overview

The MCUXpresso SDK provides Peripheral driver for the 12-bit Analog to Digital Converter (ADC) module of MCUXpresso SDK devices.

Typical use case

Polling Configuration

volatile bool g_AdcConversionDoneFlag;
volatile uint32_t g_AdcConversionValue;
volatile uint32_t g_AdcInterruptCounter;
// ...
adc_config_t adcConfigStrcut;
adc_channel_config_t adcChannelConfigStruct;
ADC_GetDefaultConfig(&adcConfigStrcut);
ADC_Init(DEMO_ADC_BASE, &adcConfigStrcut);
ADC_EnableHardwareTrigger(DEMO_ADC_BASE, false);
if (kStatus_Success == ADC_DoAutoCalibration(DEMO_ADC_BASE))
{
PRINTF("ADC_DoAntoCalibration() Done.\r\n");
}
else
{
PRINTF("ADC_DoAutoCalibration() Failed.\r\n");
}
adcChannelConfigStruct.channelNumber = DEMO_ADC_USER_CHANNEL;
adcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
g_AdcInterruptCounter = 0U;
while (1)
{
PRINTF("Press any key to get user channel's ADC value.\r\n");
GETCHAR();
g_AdcConversionDoneFlag = false;
ADC_SetChannelConfig(DEMO_ADC_BASE, DEMO_ADC_CHANNEL_GROUP, &adcChannelConfigStruct);
while (g_AdcConversionDoneFlag == false)
{
}
PRINTF("ADC Value: %d\r\n", g_AdcConversionValue);
PRINTF("ADC Interrupt Counter: %d\r\n", g_AdcInterruptCounter);
}
// ...
void DEMO_ADC_IRQ_HANDLER_FUNC(void)
{
g_AdcConversionDoneFlag = true;
g_AdcConversionValue = ADC_GetChannelConversionValue(DEMO_ADC_BASE, DEMO_ADC_CHANNEL_GROUP);
g_AdcInterruptCounter++;
}

Polling Configuration

adc_config_t adcConfigStrcut;
adc_channel_config_t adcChannelConfigStruct;
ADC_GetDefaultConfig(&adcConfigStrcut);
ADC_Init(DEMO_ADC_BASE, &adcConfigStrcut);
ADC_EnableHardwareTrigger(DEMO_ADC_BASE, false);
if (KStatus_Success == ADC_DoAutoCalibration(DEMO_ADC_BASE))
{
PRINTF("ADC_DoAntoCalibration() Done.\r\n");
}
else
{
PRINTF("ADC_DoAutoCalibration() Failed.\r\n");
}
adcChannelConfigStruct.channelNumber = DEMO_ADC_USER_CHANNEL;
adcChannelConfigStruct.enableInterruptOnConversionCompleted = false;
while (1)
{
PRINTF("Press any key to get user channel's ADC value.\r\n");
GETCHAR();
ADC_SetChannelConfig(DEMO_ADC_BASE, DEMO_ADC_CHANNEL_GROUP, &adcChannelConfigStruct);
while (0U == ADC_GetChannelStatusFlags(DEMO_ADC_BASE, DEMO_ADC_CHANNEL_GROUP))
{
}
PRINTF("ADC Value: %d\r\n", ADC_GetChannelConversionValue(DEMO_ADC_BASE, DEMO_ADC_CHANNEL_GROUP));
}

Data Structures

struct  adc_config_t
 Converter configuration. More...
 
struct  adc_offest_config_t
 Converter Offset configuration. More...
 
struct  adc_hardware_compare_config_t
 ADC hardware compare configuration. More...
 
struct  adc_channel_config_t
 ADC channel conversion configuration. More...
 

Macros

#define FSL_ADC_DRIVER_VERSION   (MAKE_VERSION(2, 0, 0))
 ADC driver version. More...
 

Enumerations

enum  adc_status_flags_t {
  kADC_ConversionActiveFlag = ADC_GS_ADACT_MASK,
  kADC_CalibrationFailedFlag = ADC_GS_CALF_MASK,
  kADC_AsynchronousWakeupInterruptFlag
}
 Converter's status flags. More...
 
enum  adc_reference_voltage_source_t { kADC_ReferenceVoltageSourceAlt0 = 0U }
 Reference voltage source. More...
 
enum  adc_sample_period_mode_t {
  kADC_SamplePeriod2or12Clocks = 0U,
  kADC_SamplePeriod4or16Clocks = 1U,
  kADC_SamplePeriod6or20Clocks = 2U,
  kADC_SamplePeriod8or24Clocks = 3U,
  kADC_SamplePeriodLong12Clcoks = kADC_SamplePeriod2or12Clocks,
  kADC_SamplePeriodLong16Clcoks = kADC_SamplePeriod4or16Clocks,
  kADC_SamplePeriodLong20Clcoks = kADC_SamplePeriod6or20Clocks,
  kADC_SamplePeriodLong24Clcoks = kADC_SamplePeriod8or24Clocks,
  kADC_SamplePeriodShort2Clocks = kADC_SamplePeriod2or12Clocks,
  kADC_SamplePeriodShort4Clocks = kADC_SamplePeriod4or16Clocks,
  kADC_SamplePeriodShort6Clocks = kADC_SamplePeriod6or20Clocks,
  kADC_SamplePeriodShort8Clocks = kADC_SamplePeriod8or24Clocks
}
 Sample time duration. More...
 
enum  adc_clock_source_t {
  kADC_ClockSourceIPG = 0U,
  kADC_ClockSourceIPGDiv2 = 1U,
  kADC_ClockSourceALT = 2U,
  kADC_ClockSourceAD = 3U
}
 Clock source. More...
 
enum  adc_clock_driver_t {
  kADC_ClockDriver1 = 0U,
  kADC_ClockDriver2 = 1U,
  kADC_ClockDriver4 = 2U,
  kADC_ClockDriver8 = 3U
}
 Clock divider for the converter. More...
 
enum  adc_resolution_t {
  kADC_Resolution8Bit = 0U,
  kADC_Resolution10Bit = 1U,
  kADC_Resolution12Bit = 2U
}
 Converter's resolution. More...
 
enum  adc_hardware_compare_mode_t {
  kADC_HardwareCompareMode0 = 0U,
  kADC_HardwareCompareMode1 = 1U,
  kADC_HardwareCompareMode2 = 2U,
  kADC_HardwareCompareMode3 = 3U
}
 Converter hardware compare mode. More...
 
enum  adc_hardware_average_mode_t {
  kADC_HardwareAverageCount4 = 0U,
  kADC_HardwareAverageCount8 = 1U,
  kADC_HardwareAverageCount16 = 2U,
  kADC_HardwareAverageCount32 = 3U,
  kADC_HardwareAverageDiasable = 4U
}
 Converter hardware average mode. More...
 

Variables

bool adc_config_t::enableOverWrite
 Enable the overwriting. More...
 
bool adc_config_t::enableContinuousConversion
 Enable the continuous conversion mode. More...
 
bool adc_config_t::enableHighSpeed
 Enable the high-speed mode. More...
 
bool adc_config_t::enableLowPower
 Enable the low power mode. More...
 
bool adc_config_t::enableLongSample
 Enable the long sample mode. More...
 
bool adc_config_t::enableAsynchronousClockOutput
 Enable the asynchronous clock output. More...
 
adc_reference_voltage_source_t adc_config_t::referenceVoltageSource
 Select the reference voltage source. More...
 
adc_sample_period_mode_t adc_config_t::samplePeriodMode
 Select the sample period in long sample mode or short mode. More...
 
adc_clock_source_t adc_config_t::clockSource
 Select the input clock source to generate the internal clock ADCK. More...
 
adc_clock_driver_t adc_config_t::clockDriver
 Select the divide ratio used by the ADC to generate the internal clock ADCK. More...
 
adc_resolution_t adc_config_t::resolution
 Select the ADC resolution mode. More...
 
bool adc_offest_config_t::enableSigned
 if false,The offset value is added with the raw result. More...
 
uint32_t adc_offest_config_t::offsetValue
 User configurable offset value(0-4095). More...
 
adc_hardware_compare_mode_t adc_hardware_compare_config_t::hardwareCompareMode
 Select the hardware compare mode. More...
 
uint16_t adc_hardware_compare_config_t::value1
 Setting value1(0-4095) for hardware compare mode. More...
 
uint16_t adc_hardware_compare_config_t::value2
 Setting value2(0-4095) for hardware compare mode. More...
 
uint32_t adc_channel_config_t::channelNumber
 Setting the conversion channel number. More...
 
bool adc_channel_config_t::enableInterruptOnConversionCompleted
 Generate an interrupt request once the conversion is completed. More...
 

Initialization

void ADC_Init (ADC_Type *base, const adc_config_t *config)
 Initialize the ADC module. More...
 
void ADC_Deinit (ADC_Type *base)
 De-initializes the ADC module. More...
 
void ADC_GetDefaultConfig (adc_config_t *config)
 Gets an available pre-defined settings for the converter's configuration. More...
 
void ADC_SetChannelConfig (ADC_Type *base, uint32_t channelGroup, const adc_channel_config_t *config)
 Configures the conversion channel. More...
 
static uint32_t ADC_GetChannelConversionValue (ADC_Type *base, uint32_t channelGroup)
 Gets the conversion value. More...
 
static uint32_t ADC_GetChannelStatusFlags (ADC_Type *base, uint32_t channelGroup)
 Gets the status flags of channel. More...
 
status_t ADC_DoAutoCalibration (ADC_Type *base)
 Automates the hardware calibration. More...
 
void ADC_SetOffsetConfig (ADC_Type *base, const adc_offest_config_t *config)
 Set user defined offset. More...
 
static void ADC_EnableDMA (ADC_Type *base, bool enable)
 Enables generating the DMA trigger when the conversion is complete. More...
 
static void ADC_EnableHardwareTrigger (ADC_Type *base, bool enable)
 Enables the hardware trigger mode. More...
 
void ADC_SetHardwareCompareConfig (ADC_Type *base, const adc_hardware_compare_config_t *config)
 Configures the hardware compare mode. More...
 
void ADC_SetHardwareAverageConfig (ADC_Type *base, adc_hardware_average_mode_t mode)
 Configures the hardware average mode. More...
 
static uint32_t ADC_GetStatusFlags (ADC_Type *base)
 Gets the converter's status flags. More...
 
void ADC_ClearStatusFlags (ADC_Type *base, uint32_t mask)
 Clears the converter's status falgs. More...
 

Data Structure Documentation

struct adc_config_t

Data Fields

bool enableOverWrite
 Enable the overwriting. More...
 
bool enableContinuousConversion
 Enable the continuous conversion mode. More...
 
bool enableHighSpeed
 Enable the high-speed mode. More...
 
bool enableLowPower
 Enable the low power mode. More...
 
bool enableLongSample
 Enable the long sample mode. More...
 
bool enableAsynchronousClockOutput
 Enable the asynchronous clock output. More...
 
adc_reference_voltage_source_t referenceVoltageSource
 Select the reference voltage source. More...
 
adc_sample_period_mode_t samplePeriodMode
 Select the sample period in long sample mode or short mode. More...
 
adc_clock_source_t clockSource
 Select the input clock source to generate the internal clock ADCK. More...
 
adc_clock_driver_t clockDriver
 Select the divide ratio used by the ADC to generate the internal clock ADCK. More...
 
adc_resolution_t resolution
 Select the ADC resolution mode. More...
 
struct adc_offest_config_t

Data Fields

bool enableSigned
 if false,The offset value is added with the raw result. More...
 
uint32_t offsetValue
 User configurable offset value(0-4095). More...
 
struct adc_hardware_compare_config_t

In kADC_HardwareCompareMode0, compare true if the result is less than the value1. In kADC_HardwareCompareMode1, compare true if the result is greater than or equal to value1. In kADC_HardwareCompareMode2, Value1 <= Value2, compare true if the result is less than value1 Or the result is Greater than value2. Value1 > Value2, compare true if the result is less than value1 And the result is Greater than value2. In kADC_HardwareCompareMode3, Value1 <= Value2, compare true if the result is greater than or equal to value1 And the result is less than or equal to value2. Value1 > Value2, compare true if the result is greater than or equal to value1 Or the result is less than or equal to value2.

Data Fields

adc_hardware_compare_mode_t hardwareCompareMode
 Select the hardware compare mode. More...
 
uint16_t value1
 Setting value1(0-4095) for hardware compare mode. More...
 
uint16_t value2
 Setting value2(0-4095) for hardware compare mode. More...
 
struct adc_channel_config_t

Data Fields

uint32_t channelNumber
 Setting the conversion channel number. More...
 
bool enableInterruptOnConversionCompleted
 Generate an interrupt request once the conversion is completed. More...
 

Macro Definition Documentation

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

Version 2.0.0.

Enumeration Type Documentation

Enumerator
kADC_ConversionActiveFlag 

Conversion is active,not support w1c.

kADC_CalibrationFailedFlag 

Calibration is failed,support w1c.

kADC_AsynchronousWakeupInterruptFlag 

Asynchronous wakeup interrupt occured, support w1c.

Enumerator
kADC_ReferenceVoltageSourceAlt0 

For external pins pair of VrefH and VrefL.

Enumerator
kADC_SamplePeriod2or12Clocks 

Long sample 12 clocks or short sample 2 clocks.

kADC_SamplePeriod4or16Clocks 

Long sample 16 clocks or short sample 4 clocks.

kADC_SamplePeriod6or20Clocks 

Long sample 20 clocks or short sample 6 clocks.

kADC_SamplePeriod8or24Clocks 

Long sample 24 clocks or short sample 8 clocks.

kADC_SamplePeriodLong12Clcoks 

Long sample 12 clocks.

kADC_SamplePeriodLong16Clcoks 

Long sample 16 clocks.

kADC_SamplePeriodLong20Clcoks 

Long sample 20 clocks.

kADC_SamplePeriodLong24Clcoks 

Long sample 24 clocks.

kADC_SamplePeriodShort2Clocks 

Short sample 2 clocks.

kADC_SamplePeriodShort4Clocks 

Short sample 4 clocks.

kADC_SamplePeriodShort6Clocks 

Short sample 6 clocks.

kADC_SamplePeriodShort8Clocks 

Short sample 8 clocks.

Enumerator
kADC_ClockSourceIPG 

Select IPG clock to generate ADCK.

kADC_ClockSourceIPGDiv2 

Select IPG clock divided by 2 to generate ADCK.

kADC_ClockSourceALT 

Select alternate clock to generate ADCK.

kADC_ClockSourceAD 

Select Asynchronous clock to generate ADCK.

Enumerator
kADC_ClockDriver1 

For divider 1 from the input clock to the module.

kADC_ClockDriver2 

For divider 2 from the input clock to the module.

kADC_ClockDriver4 

For divider 4 from the input clock to the module.

kADC_ClockDriver8 

For divider 8 from the input clock to the module.

Enumerator
kADC_Resolution8Bit 

Single End 8-bit resolution.

kADC_Resolution10Bit 

Single End 10-bit resolution.

kADC_Resolution12Bit 

Single End 12-bit resolution.

Enumerator
kADC_HardwareCompareMode0 

Compare true if the result is less than the value1.

kADC_HardwareCompareMode1 

Compare true if the result is greater than or equal to value1.

kADC_HardwareCompareMode2 

Value1 <= Value2, compare true if the result is less than value1 Or the result is Greater than value2.

Value1 > Value2, compare true if the result is less than value1 And the result is greater than value2

kADC_HardwareCompareMode3 

Value1 <= Value2, compare true if the result is greater than or equal to value1 And the result is less than or equal to value2.

Value1 > Value2, compare true if the result is greater than or equal to value1 Or the result is less than or equal to value2.

Enumerator
kADC_HardwareAverageCount4 

For hardware average with 4 samples.

kADC_HardwareAverageCount8 

For hardware average with 8 samples.

kADC_HardwareAverageCount16 

For hardware average with 16 samples.

kADC_HardwareAverageCount32 

For hardware average with 32 samples.

kADC_HardwareAverageDiasable 

Disable the hardware average function.

Function Documentation

void ADC_Init ( ADC_Type *  base,
const adc_config_t config 
)
Parameters
baseADC peripheral base address.
configPointer to "adc_config_t" structure.
void ADC_Deinit ( ADC_Type *  base)
Parameters
baseADC peripheral base address.
void ADC_GetDefaultConfig ( adc_config_t config)

This function initializes the converter configuration structure with available settings. The default values are:

* config->enableAsynchronousClockOutput = true;
* config->enableOverWrite = false;
* config->enableContinuousConversion = false;
* config->enableHighSpeed = false;
* config->enableLowPower = false;
* config->enableLongSample = false;
* config->referenceVoltageSource = kADC_ReferenceVoltageSourceAlt0;
* config->samplePeriodMode = kADC_SamplePeriod2or12Clocks;
* config->clockSource = kADC_ClockSourceAD;
* config->clockDriver = kADC_ClockDriver1;
* config->resolution = kADC_Resolution12Bit;
*
Parameters
baseADC peripheral base address.
configPointer to the configuration structure.
void ADC_SetChannelConfig ( ADC_Type *  base,
uint32_t  channelGroup,
const adc_channel_config_t config 
)

This operation triggers the conversion when in software trigger mode. When in hardware trigger mode, this API configures the channel while the external trigger source helps to trigger the conversion.

Note that the "Channel Group" has a detailed description. To allow sequential conversions of the ADC to be triggered by internal peripherals, the ADC has more than one group of status and control registers, one for each conversion. The channel group parameter indicates which group of registers are used, for example channel group 0 is for Group A registers and channel group 1 is for Group B registers. The channel groups are used in a "ping-pong" approach to control the ADC operation. At any point, only one of the channel groups is actively controlling ADC conversions. The channel group 0 is used for both software and hardware trigger modes. Channel groups 1 and greater indicate potentially multiple channel group registers for use only in hardware trigger mode. See the chip configuration information in the appropriate MCU reference manual about the number of SC1n registers (channel groups) specific to this device. None of the channel groups 1 or greater are used for software trigger operation. Therefore, writing to these channel groups does not initiate a new conversion. Updating the channel group 0 while a different channel group is actively controlling a conversion is allowed and vice versa. Writing any of the channel group registers while that specific channel group is actively controlling a conversion aborts the current conversion.

Parameters
baseADC peripheral base address.
channelGroupChannel group index.
configPointer to the "adc_channel_config_t" structure for the conversion channel.
static uint32_t ADC_GetChannelConversionValue ( ADC_Type *  base,
uint32_t  channelGroup 
)
inlinestatic
Parameters
baseADC peripheral base address.
channelGroupChannel group index.
Returns
Conversion value.
static uint32_t ADC_GetChannelStatusFlags ( ADC_Type *  base,
uint32_t  channelGroup 
)
inlinestatic

A conversion is completed when the result of the conversion is transferred into the data result registers. (provided the compare function & hardware averaging is disabled), this is indicated by the setting of COCOn. If hardware averaging is enabled, COCOn sets only, if the last of the selected number of conversions is complete. If the compare function is enabled, COCOn sets and conversion result data is transferred only if the compare condition is true. If both hardware averaging and compare functions are enabled, then COCOn sets only if the last of the selected number of conversions is complete and the compare condition is true.

Parameters
baseADC peripheral base address.
channelGroupChannel group index.
Returns
Status flags of channel.return 0 means COCO flag is 0,return 1 means COCOflag is 1.
status_t ADC_DoAutoCalibration ( ADC_Type *  base)

This auto calibration helps to adjust the plus/minus side gain automatically. Execute the calibration before using the converter. Note that the software trigger should be used during calibration.

Parameters
baseADC peripheral base address.
Returns
Execution status.
Return values
kStatus_SuccessCalibration is done successfully.
kStatus_FailCalibration has failed.
void ADC_SetOffsetConfig ( ADC_Type *  base,
const adc_offest_config_t config 
)
Parameters
baseADC peripheral base address.
configPointer to "adc_offest_config_t" structure.
static void ADC_EnableDMA ( ADC_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseADC peripheral base address.
enableSwitcher of the DMA feature. "true" means enabled, "false" means not enabled.
static void ADC_EnableHardwareTrigger ( ADC_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseADC peripheral base address.
enableSwitcher of the trigger mode. "true" means hardware tirgger mode,"false" means software mode.
void ADC_SetHardwareCompareConfig ( ADC_Type *  base,
const adc_hardware_compare_config_t config 
)

The hardware compare mode provides a way to process the conversion result automatically by using hardware. Only the result in the compare range is available. To compare the range, see "adc_hardware_compare_mode_t" or the appopriate reference manual for more information.

Parameters
baseADC peripheral base address.
Pointerto "adc_hardware_compare_config_t" structure.
void ADC_SetHardwareAverageConfig ( ADC_Type *  base,
adc_hardware_average_mode_t  mode 
)

The hardware average mode provides a way to process the conversion result automatically by using hardware. The multiple conversion results are accumulated and averaged internally making them easier to read.

Parameters
baseADC peripheral base address.
modeSetting the hardware average mode. See "adc_hardware_average_mode_t".
static uint32_t ADC_GetStatusFlags ( ADC_Type *  base)
inlinestatic
Parameters
baseADC peripheral base address.
Returns
Flags' mask if indicated flags are asserted. See "adc_status_flags_t".
void ADC_ClearStatusFlags ( ADC_Type *  base,
uint32_t  mask 
)
Parameters
baseADC peripheral base address.
maskMask value for the cleared flags. See "adc_status_flags_t".

Variable Documentation

bool adc_config_t::enableOverWrite
bool adc_config_t::enableContinuousConversion
bool adc_config_t::enableHighSpeed
bool adc_config_t::enableLowPower
bool adc_config_t::enableLongSample
bool adc_config_t::enableAsynchronousClockOutput
adc_reference_voltage_source_t adc_config_t::referenceVoltageSource
adc_sample_period_mode_t adc_config_t::samplePeriodMode
adc_clock_source_t adc_config_t::clockSource
adc_clock_driver_t adc_config_t::clockDriver
adc_resolution_t adc_config_t::resolution
bool adc_offest_config_t::enableSigned

if true,The offset value is subtracted from the raw converted value.

uint32_t adc_offest_config_t::offsetValue
adc_hardware_compare_mode_t adc_hardware_compare_config_t::hardwareCompareMode

See "adc_hardware_compare_mode_t".

uint16_t adc_hardware_compare_config_t::value1
uint16_t adc_hardware_compare_config_t::value2
uint32_t adc_channel_config_t::channelNumber

The available range is 0-31. See channel connection information for each chip in Reference Manual document.

bool adc_channel_config_t::enableInterruptOnConversionCompleted