Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:58

0001 /*
0002  * Copyright (c) 2016, Freescale Semiconductor, Inc.
0003  * Copyright 2016-2020 NXP
0004  * All rights reserved.
0005  *
0006  * SPDX-License-Identifier: BSD-3-Clause
0007  */
0008 
0009 #ifndef _FSL_ADC_H_
0010 #define _FSL_ADC_H_
0011 
0012 #include "fsl_common.h"
0013 
0014 /*!
0015  *  @addtogroup adc_12b1msps_sar
0016  *  @{
0017  */
0018 
0019 /*******************************************************************************
0020  * Definitions
0021  ******************************************************************************/
0022 /*! @brief ADC driver version */
0023 #define FSL_ADC_DRIVER_VERSION (MAKE_VERSION(2, 0, 4)) /*!< Version 2.0.4. */
0024 
0025 /*!
0026  * @brief Converter's status flags.
0027  */
0028 typedef enum _adc_status_flags
0029 {
0030     kADC_ConversionActiveFlag  = ADC_GS_ADACT_MASK, /*!< Conversion is active,not support w1c. */
0031     kADC_CalibrationFailedFlag = ADC_GS_CALF_MASK,  /*!< Calibration is failed,support w1c. */
0032     kADC_AsynchronousWakeupInterruptFlag =
0033         ADC_GS_AWKST_MASK, /*!< Asynchronous wakeup interrupt occurred, support w1c. */
0034 } adc_status_flags_t;
0035 
0036 /*!
0037  * @brief Reference voltage source.
0038  */
0039 typedef enum _adc_reference_voltage_source
0040 {
0041     kADC_ReferenceVoltageSourceAlt0 = 0U, /*!< For external pins pair of VrefH and VrefL. */
0042 } adc_reference_voltage_source_t;
0043 
0044 /*!
0045  * @brief Sample time duration.
0046  */
0047 typedef enum _adc_sample_period_mode
0048 {
0049     /* This group of enumeration is for internal use which is related to register setting. */
0050     kADC_SamplePeriod2or12Clocks = 0U, /*!< Long sample 12 clocks or short sample 2 clocks. */
0051     kADC_SamplePeriod4or16Clocks = 1U, /*!< Long sample 16 clocks or short sample 4 clocks. */
0052     kADC_SamplePeriod6or20Clocks = 2U, /*!< Long sample 20 clocks or short sample 6 clocks. */
0053     kADC_SamplePeriod8or24Clocks = 3U, /*!< Long sample 24 clocks or short sample 8 clocks. */
0054     /* This group of enumeration is for a public user. */
0055     /* For long sample mode. */
0056     kADC_SamplePeriodLong12Clcoks = kADC_SamplePeriod2or12Clocks, /*!< Long sample 12 clocks. */
0057     kADC_SamplePeriodLong16Clcoks = kADC_SamplePeriod4or16Clocks, /*!< Long sample 16 clocks. */
0058     kADC_SamplePeriodLong20Clcoks = kADC_SamplePeriod6or20Clocks, /*!< Long sample 20 clocks. */
0059     kADC_SamplePeriodLong24Clcoks = kADC_SamplePeriod8or24Clocks, /*!< Long sample 24 clocks. */
0060     /* For short sample mode. */
0061     kADC_SamplePeriodShort2Clocks = kADC_SamplePeriod2or12Clocks, /*!< Short sample 2 clocks. */
0062     kADC_SamplePeriodShort4Clocks = kADC_SamplePeriod4or16Clocks, /*!< Short sample 4 clocks. */
0063     kADC_SamplePeriodShort6Clocks = kADC_SamplePeriod6or20Clocks, /*!< Short sample 6 clocks. */
0064     kADC_SamplePeriodShort8Clocks = kADC_SamplePeriod8or24Clocks, /*!< Short sample 8 clocks. */
0065 } adc_sample_period_mode_t;
0066 
0067 /*!
0068  * @brief Clock source.
0069  */
0070 typedef enum _adc_clock_source
0071 {
0072     kADC_ClockSourceIPG     = 0U, /*!< Select IPG clock to generate ADCK. */
0073     kADC_ClockSourceIPGDiv2 = 1U, /*!< Select IPG clock divided by 2 to generate ADCK. */
0074 #if !(defined(FSL_FEATURE_ADC_SUPPORT_ALTCLK_REMOVE) && FSL_FEATURE_ADC_SUPPORT_ALTCLK_REMOVE)
0075     kADC_ClockSourceALT = 2U, /*!< Select alternate clock to generate ADCK. */
0076 #endif
0077     kADC_ClockSourceAD = 3U, /*!< Select Asynchronous clock to generate ADCK. */
0078 } adc_clock_source_t;
0079 
0080 /*!
0081  * @brief Clock divider for the converter.
0082  */
0083 typedef enum _adc_clock_drvier
0084 {
0085     kADC_ClockDriver1 = 0U, /*!< For divider 1 from the input clock to the module. */
0086     kADC_ClockDriver2 = 1U, /*!< For divider 2 from the input clock to the module. */
0087     kADC_ClockDriver4 = 2U, /*!< For divider 4 from the input clock to the module. */
0088     kADC_ClockDriver8 = 3U, /*!< For divider 8 from the input clock to the module. */
0089 } adc_clock_driver_t;
0090 
0091 /*!
0092  * @brief Converter's resolution.
0093  */
0094 typedef enum _adc_resolution
0095 {
0096     kADC_Resolution8Bit  = 0U, /*!< Single End 8-bit resolution. */
0097     kADC_Resolution10Bit = 1U, /*!< Single End 10-bit resolution. */
0098     kADC_Resolution12Bit = 2U, /*!< Single End 12-bit resolution. */
0099 } adc_resolution_t;
0100 
0101 /*!
0102  * @brief Converter hardware compare mode.
0103  */
0104 typedef enum _adc_hardware_compare_mode
0105 {
0106     kADC_HardwareCompareMode0 = 0U, /*!< Compare true if the result is less than the value1. */
0107     kADC_HardwareCompareMode1 = 1U, /*!< Compare true if the result is greater than or equal to value1. */
0108     kADC_HardwareCompareMode2 = 2U, /*!< Value1 <= Value2, compare true if the result is less than value1 Or
0109                                                           the result is Greater than value2.
0110                                          Value1 >  Value2, compare true if the result is less than value1 And the
0111                                                           result is greater than value2*/
0112     kADC_HardwareCompareMode3 = 3U, /*!< Value1 <= Value2, compare true if the result is greater than or equal
0113                                                           to value1 And the result is less than or equal to value2.
0114                                          Value1 >  Value2, compare true if the result is greater than or equal to
0115                                                           value1 Or the result is less than or equal to value2. */
0116 } adc_hardware_compare_mode_t;
0117 
0118 /*!
0119  * @brief Converter hardware average mode.
0120  */
0121 typedef enum _adc_hardware_average_mode
0122 {
0123     kADC_HardwareAverageCount4   = 0U, /*!< For hardware average with 4 samples. */
0124     kADC_HardwareAverageCount8   = 1U, /*!< For hardware average with 8 samples. */
0125     kADC_HardwareAverageCount16  = 2U, /*!< For hardware average with 16 samples. */
0126     kADC_HardwareAverageCount32  = 3U, /*!< For hardware average with 32 samples. */
0127     kADC_HardwareAverageDiasable = 4U, /*!< Disable the hardware average function. */
0128 } adc_hardware_average_mode_t;
0129 
0130 /*!
0131  * @brief Converter configuration.
0132  */
0133 typedef struct _adc_config
0134 {
0135     bool enableOverWrite;                                  /*!< Enable the overwriting. */
0136     bool enableContinuousConversion;                       /*!< Enable the continuous conversion mode. */
0137     bool enableHighSpeed;                                  /*!< Enable the high-speed mode. */
0138     bool enableLowPower;                                   /*!< Enable the low power mode. */
0139     bool enableLongSample;                                 /*!< Enable the long sample mode. */
0140     bool enableAsynchronousClockOutput;                    /*!< Enable the asynchronous clock output. */
0141     adc_reference_voltage_source_t referenceVoltageSource; /*!< Select the reference voltage source. */
0142     adc_sample_period_mode_t samplePeriodMode; /*!< Select the sample period in long sample mode or short mode. */
0143     adc_clock_source_t clockSource; /*!< Select the input clock source to generate the internal clock ADCK. */
0144     adc_clock_driver_t clockDriver; /*!< Select the divide ratio used by the ADC to generate the internal clock ADCK. */
0145     adc_resolution_t resolution;    /*!< Select the ADC resolution mode. */
0146 } adc_config_t;
0147 
0148 /*!
0149  * @brief Converter Offset configuration.
0150  */
0151 typedef struct _adc_offest_config
0152 {
0153     bool enableSigned;    /*!< if false,The offset value is added with the raw result.
0154                                if true,The offset value is subtracted from the raw converted value. */
0155     uint32_t offsetValue; /*!< User configurable offset value(0-4095). */
0156 } adc_offest_config_t;
0157 
0158 /*!
0159  * @brief ADC hardware compare configuration.
0160  *
0161  * In kADC_HardwareCompareMode0, compare true if the result is less than the value1.
0162  * In kADC_HardwareCompareMode1, compare true if the result is greater than or equal to value1.
0163  * In kADC_HardwareCompareMode2, Value1 <= Value2, compare true if the result is less than value1 Or the result is
0164  * Greater than value2.
0165  *                               Value1 >  Value2, compare true if the result is less than value1 And the result is
0166  * Greater than value2.
0167  * In kADC_HardwareCompareMode3, Value1 <= Value2, compare true if the result is greater than or equal to value1 And the
0168  * result is less than or equal to value2.
0169  *                               Value1 >  Value2, compare true if the result is greater than or equal to value1 Or the
0170  * result is less than or equal to value2.
0171  */
0172 typedef struct _adc_hardware_compare_config
0173 {
0174     adc_hardware_compare_mode_t hardwareCompareMode; /*!< Select the hardware compare mode.
0175                                                             See "adc_hardware_compare_mode_t". */
0176     uint16_t value1;                                 /*!< Setting value1(0-4095) for hardware compare mode. */
0177     uint16_t value2;                                 /*!< Setting value2(0-4095) for hardware compare mode. */
0178 } adc_hardware_compare_config_t;
0179 
0180 /*!
0181  * @brief ADC channel conversion configuration.
0182  */
0183 typedef struct _adc_channel_config
0184 {
0185     uint32_t channelNumber;                    /*!< Setting the conversion channel number. The available range is 0-31.
0186                                                     See channel connection information for each chip in Reference
0187                                                     Manual document. */
0188     bool enableInterruptOnConversionCompleted; /*!< Generate an interrupt request once the conversion is completed. */
0189 } adc_channel_config_t;
0190 /*******************************************************************************
0191  * API
0192  ******************************************************************************/
0193 #if defined(__cplusplus)
0194 extern "C" {
0195 #endif
0196 
0197 /*!
0198  * @name Initialization
0199  * @{
0200  */
0201 
0202 /*!
0203  * @brief Initialize the ADC module.
0204  *
0205  * @param base ADC peripheral base address.
0206  * @param config Pointer to "adc_config_t" structure.
0207  */
0208 void ADC_Init(ADC_Type *base, const adc_config_t *config);
0209 
0210 /*!
0211  * @brief De-initializes the ADC module.
0212  *
0213  * @param base ADC peripheral base address.
0214  */
0215 void ADC_Deinit(ADC_Type *base);
0216 
0217 /*!
0218  * @brief Gets an available pre-defined settings for the converter's configuration.
0219  *
0220  * This function initializes the converter configuration structure with available settings. The default values are:
0221  * @code
0222  *  config->enableAsynchronousClockOutput = true;
0223  *  config->enableOverWrite =               false;
0224  *  config->enableContinuousConversion =    false;
0225  *  config->enableHighSpeed =               false;
0226  *  config->enableLowPower =                false;
0227  *  config->enableLongSample =              false;
0228  *  config->referenceVoltageSource =        kADC_ReferenceVoltageSourceAlt0;
0229  *  config->samplePeriodMode =              kADC_SamplePeriod2or12Clocks;
0230  *  config->clockSource =                   kADC_ClockSourceAD;
0231  *  config->clockDriver =                   kADC_ClockDriver1;
0232  *  config->resolution =                    kADC_Resolution12Bit;
0233  * @endcode
0234  * @param config Pointer to the configuration structure.
0235  */
0236 void ADC_GetDefaultConfig(adc_config_t *config);
0237 
0238 /*!
0239  * @brief Configures the conversion channel.
0240  *
0241  * This operation triggers the conversion when in software trigger mode. When in hardware trigger mode, this API
0242  * configures the channel while the external trigger source helps to trigger the conversion.
0243  *
0244  * Note that the "Channel Group" has a detailed description.
0245  * To allow sequential conversions of the ADC to be triggered by internal peripherals, the ADC has more than one
0246  * group of status and control registers, one for each conversion. The channel group parameter indicates which group of
0247  * registers are used, for example channel group 0 is for Group A registers and channel group 1 is for Group B
0248  * registers. The
0249  * channel groups are used in a "ping-pong" approach to control the ADC operation.  At any point, only one of
0250  * the channel groups is actively controlling ADC conversions. The channel group 0 is used for both software and
0251  * hardware
0252  * trigger modes. Channel groups 1 and greater indicate potentially multiple channel group registers for
0253  * use only in hardware trigger mode. See the chip configuration information in the appropriate MCU reference manual
0254  * about the
0255  * number of SC1n registers (channel groups) specific to this device.  None of the channel groups 1 or greater are used
0256  * for software trigger operation. Therefore, writing to these channel groups does not initiate a new conversion.
0257  * Updating the channel group 0 while a different channel group is actively controlling a conversion is allowed and
0258  * vice versa. Writing any of the channel group registers while that specific channel group is actively controlling a
0259  * conversion aborts the current conversion.
0260  *
0261  * @param base          ADC peripheral base address.
0262  * @param channelGroup  Channel group index.
0263  * @param config        Pointer to the "adc_channel_config_t" structure for the conversion channel.
0264  */
0265 void ADC_SetChannelConfig(ADC_Type *base, uint32_t channelGroup, const adc_channel_config_t *config);
0266 
0267 /*!
0268  * @brief  Gets the conversion value.
0269  *
0270  * @param  base         ADC peripheral base address.
0271  * @param  channelGroup Channel group index.
0272  *
0273  * @return              Conversion value.
0274  */
0275 static inline uint32_t ADC_GetChannelConversionValue(ADC_Type *base, uint32_t channelGroup)
0276 {
0277     assert(channelGroup < (uint32_t)FSL_FEATURE_ADC_CONVERSION_CONTROL_COUNT);
0278 
0279     return base->R[channelGroup];
0280 }
0281 
0282 /*!
0283  * @brief Gets the status flags of channel.
0284  *
0285  * A conversion is completed when the result of the conversion is transferred into the data
0286  * result registers. (provided the compare function & hardware averaging is disabled), this is
0287  * indicated by the setting of COCOn. If hardware averaging is enabled, COCOn sets only,
0288  * if the last of the selected number of conversions is complete. If the compare function is
0289  * enabled, COCOn sets and conversion result data is transferred only if the compare
0290  * condition is true. If both hardware averaging and compare functions are enabled, then
0291  * COCOn sets only if the last of the selected number of conversions is complete and the
0292  * compare condition is true.
0293  *
0294  * @param base         ADC peripheral base address.
0295  * @param channelGroup Channel group index.
0296  *
0297  * @return             Status flags of channel.return 0 means COCO flag is 0,return 1 means COCOflag is 1.
0298  */
0299 static inline uint32_t ADC_GetChannelStatusFlags(ADC_Type *base, uint32_t channelGroup)
0300 {
0301     assert(channelGroup < (uint32_t)FSL_FEATURE_ADC_CONVERSION_CONTROL_COUNT);
0302 
0303     /* If flag is set,return 1,otherwise, return 0. */
0304     return (((base->HS) & (1UL << channelGroup)) >> channelGroup);
0305 }
0306 
0307 /*!
0308  * @brief  Automates the hardware calibration.
0309  *
0310  * This auto calibration helps to adjust the plus/minus side gain automatically.
0311  * Execute the calibration before using the converter. Note that the software trigger should be used
0312  * during calibration.
0313  *
0314  * @param  base ADC peripheral base address.
0315  *
0316  * @return                 Execution status.
0317  * @retval kStatus_Success Calibration is done successfully.
0318  * @retval kStatus_Fail    Calibration has failed.
0319  */
0320 status_t ADC_DoAutoCalibration(ADC_Type *base);
0321 
0322 /*!
0323  * @brief Set user defined offset.
0324  *
0325  * @param base   ADC peripheral base address.
0326  * @param config Pointer to "adc_offest_config_t" structure.
0327  */
0328 void ADC_SetOffsetConfig(ADC_Type *base, const adc_offest_config_t *config);
0329 
0330 /*!
0331  * @brief Enables generating the DMA trigger when the conversion is complete.
0332  *
0333  * @param base   ADC peripheral base address.
0334  * @param enable Switcher of the DMA feature. "true" means enabled, "false" means not enabled.
0335  */
0336 static inline void ADC_EnableDMA(ADC_Type *base, bool enable)
0337 {
0338     if (enable)
0339     {
0340         base->GC |= ADC_GC_DMAEN_MASK;
0341     }
0342     else
0343     {
0344         base->GC &= ~ADC_GC_DMAEN_MASK;
0345     }
0346 }
0347 
0348 /*!
0349  * @brief Enables the hardware trigger mode.
0350  *
0351  * @param base ADC peripheral base address.
0352  * @param enable Switcher of the trigger mode. "true" means hardware tirgger mode,"false" means software mode.
0353  */
0354 #if !(defined(FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE) && FSL_FEATURE_ADC_SUPPORT_HARDWARE_TRIGGER_REMOVE)
0355 static inline void ADC_EnableHardwareTrigger(ADC_Type *base, bool enable)
0356 {
0357     if (enable)
0358     {
0359         base->CFG |= ADC_CFG_ADTRG_MASK;
0360     }
0361     else
0362     {
0363         base->CFG &= ~ADC_CFG_ADTRG_MASK;
0364     }
0365 }
0366 #endif
0367 
0368 /*!
0369  * @brief Configures the hardware compare mode.
0370  *
0371  * The hardware compare mode provides a way to process the conversion result automatically by using hardware. Only the
0372  * result
0373  * in the compare range is available. To compare the range, see "adc_hardware_compare_mode_t" or the appopriate
0374  * reference
0375  * manual for more information.
0376  *
0377  * @param base ADC peripheral base address.
0378  * @param config Pointer to "adc_hardware_compare_config_t" structure.
0379  *
0380  */
0381 void ADC_SetHardwareCompareConfig(ADC_Type *base, const adc_hardware_compare_config_t *config);
0382 
0383 /*!
0384  * @brief Configures the hardware average mode.
0385  *
0386  * The hardware average mode provides a way to process the conversion result automatically by using hardware. The
0387  * multiple
0388  * conversion results are accumulated and averaged internally making them easier to read.
0389  *
0390  * @param base ADC peripheral base address.
0391  * @param mode Setting the hardware average mode. See "adc_hardware_average_mode_t".
0392  */
0393 void ADC_SetHardwareAverageConfig(ADC_Type *base, adc_hardware_average_mode_t mode);
0394 
0395 /*!
0396  * @brief Gets the converter's status flags.
0397  *
0398  * @param base ADC peripheral base address.
0399  *
0400  * @return Flags' mask if indicated flags are asserted. See "adc_status_flags_t".
0401  */
0402 static inline uint32_t ADC_GetStatusFlags(ADC_Type *base)
0403 {
0404     return base->GS;
0405 }
0406 
0407 /*!
0408  * @brief Clears the converter's status falgs.
0409  *
0410  * @param base ADC peripheral base address.
0411  * @param mask Mask value for the cleared flags. See "adc_status_flags_t".
0412  */
0413 void ADC_ClearStatusFlags(ADC_Type *base, uint32_t mask);
0414 
0415 /*!
0416  *@}
0417  */
0418 
0419 #if defined(__cplusplus)
0420 }
0421 #endif
0422 
0423 /*!
0424  *@}
0425  */
0426 
0427 #endif /* _FSL_ADC_H_ */