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 ADC16 Peripheral driver. The ADC16 peripheral driver configures the ADC16 (16-bit SAR Analog-to-Digital Converter). It handles calibration, initialization, and configuration of 16-bit SAR ADC module.

ADC16 Driver model building

ADC16 driver has three parts:

ADC16 Initialization

Note that the calibration should be done before any other operation.

To initialize the ADC16 driver, prepare a configuration structure and populate it with an available configuration. API functions are designed for typical use cases and facilitate populating the structure. See the "Call diagram" section for typical use cases. Additionally, the application should provide a block of memory to keep the state while the driver operates. After the configuration structure is available and memory is allocated to keep state, the ADC module can be initialized by calling the API of ADC16_DRV_Init() function.

ADC16 Call diagram

There are three kinds of typical use cases for the ADC module:

These use cases are based on the software trigger. However, they can easily be ported to use the hardware trigger. If using the hardware trigger, enable it when initializing the converter.

Complex use cases, such as the DMA and the multiple channel scan, require another module to work correctly. They can be customized according to the application.

These are the examples to initialize and configure the ADC driver for typical use cases.

For One-Time-Trigger use:

uint32_t ADC16_DRV_TEST_OneTimeConv(uint32_t instance)
{
uint32_t errCounter = 0U;
adc16_converter_config_t userConvConfig;
adc16_chn_config_t userChnConfig;
uint16_t sampleValue;
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16_calibration_param_t userCalConfig;
#endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION
/* Execute the auto-calibration.
userConvConfig.lowPowerEnable = false;
userConvConfig.clkDividerMode = kAdc16ClkDividerOf8;
userConvConfig.longSampleTimeEnable = true;
userConvConfig.resolution = kAdc16ResolutionBitOf12or13;
userConvConfig.clkSrc = kAdc16ClkSrcOfAsynClk;
userConvConfig.asyncClkEnable = true;
userConvConfig.highSpeedEnable = false;
userConvConfig.longSampleCycleMode = kAdc16LongSampleCycleOf24;
userConvConfig.hwTriggerEnable = false;
userConvConfig.refVoltSrc = kAdc16RefVoltSrcOfVref;
userConvConfig.continuousConvEnable = false;
#if FSL_FEATURE_ADC16_HAS_DMA
userConvConfig.dmaEnable = false;
#endif /* FSL_FEATURE_ADC16_HAS_DMA
if (kStatus_ADC16_Success != ADC16_DRV_Init(instance, &userConvConfig))
{
errCounter++;
}
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
if (kStatus_ADC16_Success != ADC16_DRV_GetAutoCalibrationParam(instance, &userCalConfig) )
{
errCounter++;
}
ADC16_DRV_SetCalibrationParam(instance, &userCalConfig);
#endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION
userChnConfig.chnIdx = ADC16_DRV_TEST_SAMPLE_CHN;
userChnConfig.convCompletedIntEnable = false;
userChnConfig.diffConvEnable = false;
ADC16_DRV_ConfigConvChn(instance, ADC16_DRV_TEST_SAMPLE_CHNGROUP, &userChnConfig);
ADC16_DRV_WaitConvDone(instance, ADC16_DRV_TEST_SAMPLE_CHNGROUP);
sampleValue = ADC16_DRV_GetConvValueRAW(instance, ADC16_DRV_TEST_SAMPLE_CHNGROUP);
printf("ADC16_DRV_TEST_OneTimeConv Sample Value: %d\r\n", sampleValue);
ADC16_DRV_Deinit(instance);
return errCounter;
}

For Interrupt use:

static uint32_t ADC16_DRV_TEST_OneTimeConvInt(uint32_t instance)
{
uint32_t errCounter = 0U;
adc16_converter_config_t userConvConfig;
adc16_chn_config_t userChnConfig;
uint16_t sampleValue;
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16_calibration_param_t userCalConfig;
#endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION
/* Execute the auto-calibration.
userConvConfig.lowPowerEnable = false;
userConvConfig.clkDividerMode = kAdc16ClkDividerOf8;
userConvConfig.longSampleTimeEnable = true;
userConvConfig.resolution = kAdc16ResolutionBitOf12or13;
userConvConfig.clkSrc = kAdc16ClkSrcOfAsynClk;
userConvConfig.asyncClkEnable = true;
userConvConfig.highSpeedEnable = false;
userConvConfig.longSampleCycleMode = kAdc16LongSampleCycleOf24;
userConvConfig.hwTriggerEnable = false;
userConvConfig.refVoltSrc = kAdc16RefVoltSrcOfVref;
userConvConfig.continuousConvEnable = false;
#if FSL_FEATURE_ADC16_HAS_DMA
userConvConfig.dmaEnable = false;
#endif /* FSL_FEATURE_ADC16_HAS_DMA
if (kStatus_ADC16_Success != ADC16_DRV_Init(instance, &userConvConfig))
{
errCounter++;
}
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
if (kStatus_ADC16_Success != ADC16_DRV_GetAutoCalibrationParam(instance, &userCalConfig) )
{
errCounter++;
}
ADC16_DRV_SetCalibrationParam(instance, &userCalConfig);
#endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION
gAdc16IntSampleFlag = false;
userChnConfig.chnIdx = ADC16_DRV_TEST_SAMPLE_CHN;
userChnConfig.convCompletedIntEnable = true;
userChnConfig.diffConvEnable = false;
ADC16_DRV_ConfigConvChn(instance, ADC16_DRV_TEST_SAMPLE_CHNGROUP, &userChnConfig);
while (!gAdc16IntSampleFlag) {}
printf("ADC16_DRV_TEST_OneTimeConvInt Sample Value: %d\r\n", gAdc16IntSampleValue);
gAdc16IntSampleFlag = false;
ADC16_DRV_Deinit(instance);
return errCounter;
}

For Blocking use:

static uint32_t ADC16_DRV_TEST_ContinuousConvBlocking(uint32_t instance)
{
uint32_t errCounter = 0U;
adc16_converter_config_t userConvConfig;
adc16_chn_config_t userChnConfig;
uint16_t sampleValue;
uint32_t i;
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16_calibration_param_t userCalConfig;
#endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION
/* Execute the auto-calibration.
userConvConfig.lowPowerEnable = false;
userConvConfig.clkDividerMode = kAdc16ClkDividerOf8;
userConvConfig.longSampleTimeEnable = true;
userConvConfig.resolution = kAdc16ResolutionBitOf12or13;
userConvConfig.clkSrc = kAdc16ClkSrcOfAsynClk;
userConvConfig.asyncClkEnable = true;
userConvConfig.highSpeedEnable = false;
userConvConfig.longSampleCycleMode = kAdc16LongSampleCycleOf24;
userConvConfig.hwTriggerEnable = false;
userConvConfig.refVoltSrc = kAdc16RefVoltSrcOfVref;
userConvConfig.continuousConvEnable = false;
#if FSL_FEATURE_ADC16_HAS_DMA
userConvConfig.dmaEnable = false;
#endif /* FSL_FEATURE_ADC16_HAS_DMA
if (kStatus_ADC16_Success != ADC16_DRV_Init(instance, &userConvConfig))
{
errCounter++;
}
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
if (kStatus_ADC16_Success != ADC16_DRV_GetAutoCalibrationParam(instance, &userCalConfig) )
{
errCounter++;
}
ADC16_DRV_SetCalibrationParam(instance, &userCalConfig);
#endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION
userChnConfig.chnIdx = ADC16_DRV_TEST_SAMPLE_CHN;
userChnConfig.convCompletedIntEnable = false;
userChnConfig.diffConvEnable = false;
ADC16_DRV_ConfigConvChn(instance, ADC16_DRV_TEST_SAMPLE_CHNGROUP, &userChnConfig);
for (i = 0U; i < 4U; i++)
{
sampleValue = ADC16_DRV_GetConvValueRAW(instance, ADC16_DRV_TEST_SAMPLE_CHNGROUP);
printf("ADC16_DRV_TEST_ContinuousConvBlocking %dst Sample Value: %d\r\n", i, sampleValue);
}
ADC16_DRV_Deinit(instance);
return errCounter;
}

Enumerations

enum  adc16_flag_t {
  kAdcConvActiveFlag = 0U,
  kAdcChnConvCompleteFlag = 3U
}
 Defines the type of event flags. More...
 

Functions

adc16_status_t ADC16_DRV_StructInitUserConfigDefault (adc16_converter_config_t *userConfigPtr)
 Fills the initial user configuration by default for a one-time trigger mode. More...
 
adc16_status_t ADC16_DRV_Init (uint32_t instance, const adc16_converter_config_t *userConfigPtr)
 Initializes the ADC module converter. More...
 
adc16_status_t ADC16_DRV_Deinit (uint32_t instance)
 De-initializes the ADC module converter. More...
 
adc16_status_t ADC16_DRV_ConfigHwCompare (uint32_t instance, const adc16_hw_cmp_config_t *configPtr)
 Configures the hardware compare feature. More...
 
adc16_status_t ADC16_DRV_ConfigConvChn (uint32_t instance, uint32_t chnGroup, const adc16_chn_config_t *configPtr)
 Configure the conversion channel by software. More...
 
void ADC16_DRV_WaitConvDone (uint32_t instance, uint32_t chnGroup)
 Waits for the latest conversion to be complete. More...
 
void ADC16_DRV_PauseConv (uint32_t instance, uint32_t chnGroup)
 Pauses the current conversion by software. More...
 
uint16_t ADC16_DRV_GetConvValueRAW (uint32_t instance, uint32_t chnGroup)
 Gets the latest conversion value with no format. More...
 
int16_t ADC16_DRV_GetConvValueSigned (uint32_t instance, uint32_t chnGroup)
 Gets the latest conversion value with signed. More...
 
bool ADC16_DRV_GetConvFlag (uint32_t instance, adc16_flag_t flag)
 Gets the event status of the ADC16 module. More...
 
bool ADC16_DRV_GetChnFlag (uint32_t instance, uint32_t chnGroup, adc16_flag_t flag)
 Gets the event status of each channel group. More...
 

Variables

ADC_Type *const g_adcBase []
 Table of base addresses for ADC16 instances. More...
 
const IRQn_Type g_adcIrqId [ADC_INSTANCE_COUNT]
 Table to save ADC IRQ enum numbers defined in the CMSIS header file. More...
 

Enumeration Type Documentation

Enumerator
kAdcConvActiveFlag 

Indicates if a conversion or hardware averaging is in progress.

kAdcChnConvCompleteFlag 

Indicates if the channel group A is ready.

Function Documentation

adc16_status_t ADC16_DRV_StructInitUserConfigDefault ( adc16_converter_config_t userConfigPtr)

This function fills the initial user configuration by default for a one-time trigger mode. Calling the initialization function with the filled parameter configures the ADC module work as one-time trigger mode. The settings are:

  • .lowPowerEnable = true;
  • .clkDividerMode = kAdc16ClkDividerOf8;
  • .longSampleTimeEnable = true;
  • .resolutionMode = kAdc16ResolutionBitOfSingleEndAs12;
  • .clkSrc = kAdc16ClkSrcOfAsynClk
  • .asyncClkEnable = true;
  • .highSpeedEnable = false;
  • .longSampleCycleMode = kAdc16LongSampleCycleOf24;
  • .hwTriggerEnable = false;
  • .refVoltSrc = kAdcRefVoltSrcOfVref;
  • .continuousConvEnable = false;
  • .dmaEnable = false;
Parameters
userConfigPtrPointer to the user configuration structure. See the "adc16_converter_config_t".
Returns
Execution status.
adc16_status_t ADC16_DRV_Init ( uint32_t  instance,
const adc16_converter_config_t userConfigPtr 
)

This function initializes the converter in the ADC module.

Parameters
instanceADC16 instance ID.
userConfigPtrPointer to the initialization structure. See the "adc16_converter_config_t".
Returns
Execution status.
adc16_status_t ADC16_DRV_Deinit ( uint32_t  instance)

This function de-initializes and gates the ADC module. When ADC is no longer used, calling this API function shuts down the device to reduce the power consumption.

Parameters
instanceADC16 instance ID.
Returns
Execution status.
adc16_status_t ADC16_DRV_ConfigHwCompare ( uint32_t  instance,
const adc16_hw_cmp_config_t configPtr 
)

This function configures the hardware compare feature with indicated configuration.

Parameters
instanceADC16 instance ID.
configPtrPointer to configuration structure. See the "adc16_hw_cmp_config_t".
Returns
Execution status.
adc16_status_t ADC16_DRV_ConfigConvChn ( uint32_t  instance,
uint32_t  chnGroup,
const adc16_chn_config_t configPtr 
)

This function configures the conversion channel. When the ADC16 module has been initialized by enabling the software trigger (disable hardware trigger), calling this API triggers the conversion.

Parameters
instanceADC16 instance ID.
chnGroupSelection of the configuration group.
configPtrPointer to configuration structure. See the "adc16_chn_config_t".
Returns
Execution status.
void ADC16_DRV_WaitConvDone ( uint32_t  instance,
uint32_t  chnGroup 
)

This function waits for the latest conversion to be complete. When triggering the conversion by configuring the available channel, the converter is launched. This API function should be called to wait for the conversion to be complete when no interrupt or DMA mode is used for the ADC16 module. After the waiting period, the available data from the conversion result are fetched. The complete flag is not cleared until the result data is read.

Parameters
instanceADC16 instance ID.
chnGroupSelection of configuration group.
void ADC16_DRV_PauseConv ( uint32_t  instance,
uint32_t  chnGroup 
)

This function pauses the current conversion setting by software.

Parameters
instanceADC16 instance ID.
chnGroupSelection of configuration group.
uint16_t ADC16_DRV_GetConvValueRAW ( uint32_t  instance,
uint32_t  chnGroup 
)

This function gets the conversion value from the ADC16 module.

Parameters
instanceADC16 instance ID.
chnGroupSelection of configuration group.
Returns
Unformatted conversion value.
int16_t ADC16_DRV_GetConvValueSigned ( uint32_t  instance,
uint32_t  chnGroup 
)

This function gets the conversion value from the ADC16 module with signed.

Parameters
instanceADC16 instance ID.
chnGroupSelection of configuration group.
Returns
Signed conversion value.
bool ADC16_DRV_GetConvFlag ( uint32_t  instance,
adc16_flag_t  flag 
)

This function gets the event status of the ADC16 converter. If the event is asserted, it returns "true". Otherwise, it is "false".

Parameters
instanceADC16 instance ID.
flagIndicated event.
Returns
Assertion of event flag.
bool ADC16_DRV_GetChnFlag ( uint32_t  instance,
uint32_t  chnGroup,
adc16_flag_t  flag 
)

This function gets the event status of each channel group. If the event is asserted, it returns "true". Otherwise, it is "false".

Parameters
instanceADC16 instance ID.
chnGroupADC16 channel group number.
flagIndicated event.
Returns
Assertion of event flag.

Variable Documentation

ADC_Type* const g_adcBase[]
const IRQn_Type g_adcIrqId[ADC_INSTANCE_COUNT]