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:
- Basic Converter - This part handles the mechanism that converts the external analog voltage to a digital value. API functions configure the converter.
- Channel Mux - Multiple channels share the converter in each ADC instance because of the time division multiplexing. However, the converter can only handle one channel at a time. To get the value of an indicated channel, the channel mux should be set to the connection between an indicated pad and the converter's input. The conversion value during this period is for the channel only. API functions configure the channel.
- Advance Feature Group - The advanced feature group covers optional features for applications. These features includes some that are already implemented by hardware, such as the calibration, hardware average, hardware compare, different power, and speed mode. APIs configure the advanced features. Although these features are optional, they are recommended to ensure that the ADC performs better, especially for calibration.
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:
- One-Time-Trigger Mode. One-Time-Trigger works without an interrupt and continuous mode. After it is triggered by channel configuration, the conversion launches and the application gets the result data. There is no auto operation to make the conversion continuous. The application should trigger the conversion again if another conversion is needed.
- Interrupt mode - Interrupt mode works only with continuous conversion or one-time conversion. To use the interrupt mode, the user enables the interrupt when configuring for each conversion. Note that the conversion value should be read out by calling the API of the "ADC16_DRV_GetConvValueRAW()" in the user-defined ISR. Use the interrupt mode carefully with the continuous conversion because too many interrupts may affect the main routine. Using the low conversion speed is recommended in the continuous interrupt mode.
- Blocking mode. Blocking mode is based on polling mode and works only with the continuous conversion mode. The conversion is triggered by the channel configuration. When the conversion is completed, it gets blocked because the result data is not read. The application obtains the result data by calling API. After this, the conversion becomes continuous.
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;
uint16_t sampleValue;
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16_calibration_param_t userCalConfig;
#endif
For Interrupt use:
static uint32_t ADC16_DRV_TEST_OneTimeConvInt(uint32_t instance)
{
uint32_t errCounter = 0U;
uint16_t sampleValue;
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16_calibration_param_t userCalConfig;
#endif
For Blocking use:
static uint32_t ADC16_DRV_TEST_ContinuousConvBlocking(uint32_t instance)
{
uint32_t errCounter = 0U;
uint16_t sampleValue;
uint32_t i;
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16_calibration_param_t userCalConfig;
#endif
|
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...
|
|
|
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...
|
|
Enumerator |
---|
kAdcConvActiveFlag |
Indicates if a conversion or hardware averaging is in progress.
|
kAdcChnConvCompleteFlag |
Indicates if the channel group A is ready.
|
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
-
userConfigPtr | Pointer to the user configuration structure. See the "adc16_converter_config_t". |
- Returns
- Execution status.
This function initializes the converter in the ADC module.
- Parameters
-
instance | ADC16 instance ID. |
userConfigPtr | Pointer to the initialization structure. See the "adc16_converter_config_t". |
- Returns
- Execution status.
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
-
instance | ADC16 instance ID. |
- Returns
- Execution status.
This function configures the hardware compare feature with indicated configuration.
- Parameters
-
instance | ADC16 instance ID. |
configPtr | Pointer to configuration structure. See the "adc16_hw_cmp_config_t". |
- Returns
- Execution status.
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
-
instance | ADC16 instance ID. |
chnGroup | Selection of the configuration group. |
configPtr | Pointer 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
-
instance | ADC16 instance ID. |
chnGroup | Selection of configuration group. |
void ADC16_DRV_PauseConv |
( |
uint32_t |
instance, |
|
|
uint32_t |
chnGroup |
|
) |
| |
This function pauses the current conversion setting by software.
- Parameters
-
instance | ADC16 instance ID. |
chnGroup | Selection 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
-
instance | ADC16 instance ID. |
chnGroup | Selection 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
-
instance | ADC16 instance ID. |
chnGroup | Selection 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
-
instance | ADC16 instance ID. |
flag | Indicated 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
-
instance | ADC16 instance ID. |
chnGroup | ADC16 channel group number. |
flag | Indicated event. |
- Returns
- Assertion of event flag.
ADC_Type* const g_adcBase[] |
const IRQn_Type g_adcIrqId[ADC_INSTANCE_COUNT] |