1
1
/*
2
- * The Clear BSD License
3
2
* Copyright (c) 2016, Freescale Semiconductor, Inc.
4
3
* Copyright 2016-2017 NXP
5
4
* 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:
10
5
*
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
33
7
*/
34
8
35
9
#include "fsl_adc.h"
39
13
#define FSL_COMPONENT_ID "platform.drivers.adc_12b1msps_sar"
40
14
#endif
41
15
42
-
43
16
/*******************************************************************************
44
17
* Prototypes
45
18
******************************************************************************/
@@ -56,8 +29,10 @@ static uint32_t ADC_GetInstance(ADC_Type *base);
56
29
/*! @brief Pointers to ADC bases for each instance. */
57
30
static ADC_Type * const s_adcBases [] = ADC_BASE_PTRS ;
58
31
32
+ #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL )
59
33
/*! @brief Pointers to ADC clocks for each instance. */
60
34
static const clock_ip_name_t s_adcClocks [] = ADC_CLOCKS ;
35
+ #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
61
36
62
37
/*******************************************************************************
63
38
* Code
@@ -80,14 +55,22 @@ static uint32_t ADC_GetInstance(ADC_Type *base)
80
55
return instance ;
81
56
}
82
57
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
+ */
83
64
void ADC_Init (ADC_Type * base , const adc_config_t * config )
84
65
{
85
66
assert (NULL != config );
86
67
87
68
uint32_t tmp32 ;
88
69
70
+ #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL )
89
71
/* Enable the clock. */
90
72
CLOCK_EnableClock (s_adcClocks [ADC_GetInstance (base )]);
73
+ #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
91
74
/* ADCx_CFG */
92
75
tmp32 = base -> CFG & (ADC_CFG_AVGS_MASK | ADC_CFG_ADTRG_MASK ); /* Reserve AVGS and ADTRG bits. */
93
76
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)
123
106
base -> GC = tmp32 ;
124
107
}
125
108
109
+ /*!
110
+ * brief De-initializes the ADC module.
111
+ *
112
+ * param base ADC peripheral base address.
113
+ */
126
114
void ADC_Deinit (ADC_Type * base )
127
115
{
116
+ #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL )
128
117
/* Disable the clock. */
129
118
CLOCK_DisableClock (s_adcClocks [ADC_GetInstance (base )]);
119
+ #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
130
120
}
131
121
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
+ */
132
142
void ADC_GetDefaultConfig (adc_config_t * config )
133
143
{
134
144
assert (NULL != config );
135
145
146
+ /* Initializes the configure structure to zero. */
147
+ memset (config , 0 , sizeof (* config ));
148
+
136
149
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 ;
147
160
}
148
161
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
+ */
149
189
void ADC_SetChannelConfig (ADC_Type * base , uint32_t channelGroup , const adc_channel_config_t * config )
150
190
{
151
191
assert (NULL != config );
152
- assert (channelGroup < ADC_HC_COUNT );
192
+ assert (channelGroup < FSL_FEATURE_ADC_CONVERSION_CONTROL_COUNT );
153
193
154
194
uint32_t tmp32 ;
155
195
@@ -168,6 +208,19 @@ void ADC_SetChannelConfig(ADC_Type *base, uint32_t channelGroup, const adc_chann
168
208
* 3. Check the status of CALF bit in ADC_GS and the CAL bit in ADC_GC.
169
209
* 4. When CAL bit becomes '0' then check the CALF status and COCO[0] bit status.
170
210
*/
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
+ */
171
224
status_t ADC_DoAutoCalibration (ADC_Type * base )
172
225
{
173
226
status_t status = kStatus_Success ;
@@ -222,6 +275,12 @@ status_t ADC_DoAutoCalibration(ADC_Type *base)
222
275
return status ;
223
276
}
224
277
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
+ */
225
284
void ADC_SetOffsetConfig (ADC_Type * base , const adc_offest_config_t * config )
226
285
{
227
286
assert (NULL != config );
@@ -236,6 +295,19 @@ void ADC_SetOffsetConfig(ADC_Type *base, const adc_offest_config_t *config)
236
295
base -> OFS = tmp32 ;
237
296
}
238
297
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
+ */
239
311
void ADC_SetHardwareCompareConfig (ADC_Type * base , const adc_hardware_compare_config_t * config )
240
312
{
241
313
uint32_t tmp32 ;
@@ -269,10 +341,20 @@ void ADC_SetHardwareCompareConfig(ADC_Type *base, const adc_hardware_compare_con
269
341
base -> GC = tmp32 ;
270
342
271
343
/* 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 );
273
345
base -> CV = tmp32 ;
274
346
}
275
347
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
+ */
276
358
void ADC_SetHardwareAverageConfig (ADC_Type * base , adc_hardware_average_mode_t mode )
277
359
{
278
360
uint32_t tmp32 ;
@@ -290,6 +372,12 @@ void ADC_SetHardwareAverageConfig(ADC_Type *base, adc_hardware_average_mode_t mo
290
372
}
291
373
}
292
374
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
+ */
293
381
void ADC_ClearStatusFlags (ADC_Type * base , uint32_t mask )
294
382
{
295
383
uint32_t tmp32 = 0 ;
0 commit comments