11/*
2- * The Clear BSD License
32 * Copyright (c) 2016, Freescale Semiconductor, Inc.
43 * Copyright 2016-2017 NXP
54 * All rights reserved.
6- *
7- * Redistribution and use in source and binary forms, with or without modification,
8- * are permitted (subject to the limitations in the disclaimer below) provided
9- * that the following conditions are met:
105 *
11- * o Redistributions of source code must retain the above copyright notice, this list
12- * of conditions and the following disclaimer.
13- *
14- * o Redistributions in binary form must reproduce the above copyright notice, this
15- * list of conditions and the following disclaimer in the documentation and/or
16- * other materials provided with the distribution.
17- *
18- * o Neither the name of the copyright holder nor the names of its
19- * contributors may be used to endorse or promote products derived from this
20- * software without specific prior written permission.
21- *
22- * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6+ * SPDX-License-Identifier: BSD-3-Clause
337 */
348
359#include "fsl_adc.h"
3913#define FSL_COMPONENT_ID "platform.drivers.adc_12b1msps_sar"
4014#endif
4115
42-
4316/*******************************************************************************
4417 * Prototypes
4518 ******************************************************************************/
@@ -56,8 +29,10 @@ static uint32_t ADC_GetInstance(ADC_Type *base);
5629/*! @brief Pointers to ADC bases for each instance. */
5730static ADC_Type * const s_adcBases [] = ADC_BASE_PTRS ;
5831
32+ #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL )
5933/*! @brief Pointers to ADC clocks for each instance. */
6034static const clock_ip_name_t s_adcClocks [] = ADC_CLOCKS ;
35+ #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
6136
6237/*******************************************************************************
6338 * Code
@@ -80,14 +55,22 @@ static uint32_t ADC_GetInstance(ADC_Type *base)
8055 return instance ;
8156}
8257
58+ /*!
59+ * brief Initialize the ADC module.
60+ *
61+ * param base ADC peripheral base address.
62+ * param config Pointer to "adc_config_t" structure.
63+ */
8364void ADC_Init (ADC_Type * base , const adc_config_t * config )
8465{
8566 assert (NULL != config );
8667
8768 uint32_t tmp32 ;
8869
70+ #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL )
8971 /* Enable the clock. */
9072 CLOCK_EnableClock (s_adcClocks [ADC_GetInstance (base )]);
73+ #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
9174 /* ADCx_CFG */
9275 tmp32 = base -> CFG & (ADC_CFG_AVGS_MASK | ADC_CFG_ADTRG_MASK ); /* Reserve AVGS and ADTRG bits. */
9376 tmp32 |= ADC_CFG_REFSEL (config -> referenceVoltageSource ) | ADC_CFG_ADSTS (config -> samplePeriodMode ) |
@@ -123,33 +106,90 @@ void ADC_Init(ADC_Type *base, const adc_config_t *config)
123106 base -> GC = tmp32 ;
124107}
125108
109+ /*!
110+ * brief De-initializes the ADC module.
111+ *
112+ * param base ADC peripheral base address.
113+ */
126114void ADC_Deinit (ADC_Type * base )
127115{
116+ #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL )
128117 /* Disable the clock. */
129118 CLOCK_DisableClock (s_adcClocks [ADC_GetInstance (base )]);
119+ #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
130120}
131121
122+ /*!
123+ * brief Gets an available pre-defined settings for the converter's configuration.
124+ *
125+ * This function initializes the converter configuration structure with available settings. The default values are:
126+ * code
127+ * config->enableAsynchronousClockOutput = true;
128+ * config->enableOverWrite = false;
129+ * config->enableContinuousConversion = false;
130+ * config->enableHighSpeed = false;
131+ * config->enableLowPower = false;
132+ * config->enableLongSample = false;
133+ * config->referenceVoltageSource = kADC_ReferenceVoltageSourceAlt0;
134+ * config->samplePeriodMode = kADC_SamplePeriod2or12Clocks;
135+ * config->clockSource = kADC_ClockSourceAD;
136+ * config->clockDriver = kADC_ClockDriver1;
137+ * config->resolution = kADC_Resolution12Bit;
138+ * endcode
139+ * param base ADC peripheral base address.
140+ * param config Pointer to the configuration structure.
141+ */
132142void ADC_GetDefaultConfig (adc_config_t * config )
133143{
134144 assert (NULL != config );
135145
146+ /* Initializes the configure structure to zero. */
147+ memset (config , 0 , sizeof (* config ));
148+
136149 config -> enableAsynchronousClockOutput = true;
137- config -> enableOverWrite = false;
138- config -> enableContinuousConversion = false;
139- config -> enableHighSpeed = false;
140- config -> enableLowPower = false;
141- config -> enableLongSample = false;
142- config -> referenceVoltageSource = kADC_ReferenceVoltageSourceAlt0 ;
143- config -> samplePeriodMode = kADC_SamplePeriod2or12Clocks ;
144- config -> clockSource = kADC_ClockSourceAD ;
145- config -> clockDriver = kADC_ClockDriver1 ;
146- config -> resolution = kADC_Resolution12Bit ;
150+ config -> enableOverWrite = false;
151+ config -> enableContinuousConversion = false;
152+ config -> enableHighSpeed = false;
153+ config -> enableLowPower = false;
154+ config -> enableLongSample = false;
155+ config -> referenceVoltageSource = kADC_ReferenceVoltageSourceAlt0 ;
156+ config -> samplePeriodMode = kADC_SamplePeriod2or12Clocks ;
157+ config -> clockSource = kADC_ClockSourceAD ;
158+ config -> clockDriver = kADC_ClockDriver1 ;
159+ config -> resolution = kADC_Resolution12Bit ;
147160}
148161
162+ /*!
163+ * brief Configures the conversion channel.
164+ *
165+ * This operation triggers the conversion when in software trigger mode. When in hardware trigger mode, this API
166+ * configures the channel while the external trigger source helps to trigger the conversion.
167+ *
168+ * Note that the "Channel Group" has a detailed description.
169+ * To allow sequential conversions of the ADC to be triggered by internal peripherals, the ADC has more than one
170+ * group of status and control registers, one for each conversion. The channel group parameter indicates which group of
171+ * registers are used, for example channel group 0 is for Group A registers and channel group 1 is for Group B
172+ * registers. The
173+ * channel groups are used in a "ping-pong" approach to control the ADC operation. At any point, only one of
174+ * the channel groups is actively controlling ADC conversions. The channel group 0 is used for both software and
175+ * hardware
176+ * trigger modes. Channel groups 1 and greater indicate potentially multiple channel group registers for
177+ * use only in hardware trigger mode. See the chip configuration information in the appropriate MCU reference manual
178+ * about the
179+ * number of SC1n registers (channel groups) specific to this device. None of the channel groups 1 or greater are used
180+ * for software trigger operation. Therefore, writing to these channel groups does not initiate a new conversion.
181+ * Updating the channel group 0 while a different channel group is actively controlling a conversion is allowed and
182+ * vice versa. Writing any of the channel group registers while that specific channel group is actively controlling a
183+ * conversion aborts the current conversion.
184+ *
185+ * param base ADC peripheral base address.
186+ * param channelGroup Channel group index.
187+ * param config Pointer to the "adc_channel_config_t" structure for the conversion channel.
188+ */
149189void ADC_SetChannelConfig (ADC_Type * base , uint32_t channelGroup , const adc_channel_config_t * config )
150190{
151191 assert (NULL != config );
152- assert (channelGroup < ADC_HC_COUNT );
192+ assert (channelGroup < FSL_FEATURE_ADC_CONVERSION_CONTROL_COUNT );
153193
154194 uint32_t tmp32 ;
155195
@@ -168,6 +208,19 @@ void ADC_SetChannelConfig(ADC_Type *base, uint32_t channelGroup, const adc_chann
168208 * 3. Check the status of CALF bit in ADC_GS and the CAL bit in ADC_GC.
169209 * 4. When CAL bit becomes '0' then check the CALF status and COCO[0] bit status.
170210 */
211+ /*!
212+ * brief Automates the hardware calibration.
213+ *
214+ * This auto calibration helps to adjust the plus/minus side gain automatically.
215+ * Execute the calibration before using the converter. Note that the software trigger should be used
216+ * during calibration.
217+ *
218+ * param base ADC peripheral base address.
219+ *
220+ * return Execution status.
221+ * retval kStatus_Success Calibration is done successfully.
222+ * retval kStatus_Fail Calibration has failed.
223+ */
171224status_t ADC_DoAutoCalibration (ADC_Type * base )
172225{
173226 status_t status = kStatus_Success ;
@@ -222,6 +275,12 @@ status_t ADC_DoAutoCalibration(ADC_Type *base)
222275 return status ;
223276}
224277
278+ /*!
279+ * brief Set user defined offset.
280+ *
281+ * param base ADC peripheral base address.
282+ * param config Pointer to "adc_offest_config_t" structure.
283+ */
225284void ADC_SetOffsetConfig (ADC_Type * base , const adc_offest_config_t * config )
226285{
227286 assert (NULL != config );
@@ -236,6 +295,19 @@ void ADC_SetOffsetConfig(ADC_Type *base, const adc_offest_config_t *config)
236295 base -> OFS = tmp32 ;
237296}
238297
298+ /*!
299+ * brief Configures the hardware compare mode.
300+ *
301+ * The hardware compare mode provides a way to process the conversion result automatically by using hardware. Only the
302+ * result
303+ * in the compare range is available. To compare the range, see "adc_hardware_compare_mode_t" or the appopriate
304+ * reference
305+ * manual for more information.
306+ *
307+ * param base ADC peripheral base address.
308+ * param Pointer to "adc_hardware_compare_config_t" structure.
309+ *
310+ */
239311void ADC_SetHardwareCompareConfig (ADC_Type * base , const adc_hardware_compare_config_t * config )
240312{
241313 uint32_t tmp32 ;
@@ -269,10 +341,20 @@ void ADC_SetHardwareCompareConfig(ADC_Type *base, const adc_hardware_compare_con
269341 base -> GC = tmp32 ;
270342
271343 /* Load the compare values. */
272- tmp32 = ADC_CV_CV1 (config -> value1 ) | ADC_CV_CV2 (config -> value2 );
344+ tmp32 = ADC_CV_CV1 (config -> value1 ) | ADC_CV_CV2 (config -> value2 );
273345 base -> CV = tmp32 ;
274346}
275347
348+ /*!
349+ * brief Configures the hardware average mode.
350+ *
351+ * The hardware average mode provides a way to process the conversion result automatically by using hardware. The
352+ * multiple
353+ * conversion results are accumulated and averaged internally making them easier to read.
354+ *
355+ * param base ADC peripheral base address.
356+ * param mode Setting the hardware average mode. See "adc_hardware_average_mode_t".
357+ */
276358void ADC_SetHardwareAverageConfig (ADC_Type * base , adc_hardware_average_mode_t mode )
277359{
278360 uint32_t tmp32 ;
@@ -290,6 +372,12 @@ void ADC_SetHardwareAverageConfig(ADC_Type *base, adc_hardware_average_mode_t mo
290372 }
291373}
292374
375+ /*!
376+ * brief Clears the converter's status falgs.
377+ *
378+ * param base ADC peripheral base address.
379+ * param mask Mask value for the cleared flags. See "adc_status_flags_t".
380+ */
293381void ADC_ClearStatusFlags (ADC_Type * base , uint32_t mask )
294382{
295383 uint32_t tmp32 = 0 ;
0 commit comments