1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef __ADC_IMX6SX_H__
32 #define __ADC_IMX6SX_H__
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <assert.h>
37 #include "device_imx.h"
38
39 /*!
40 * @addtogroup adc_imx6sx_driver
41 * @{
42 */
43
44 /*******************************************************************************
45 * Definitions
46 ******************************************************************************/
47
48 /*! @brief ADC module initialize structure. */
49 typedef struct _adc_init_config
50 {
51 uint8_t clockSource; /*!< Select input clock source to generate the internal conversion clock.*/
52 uint8_t divideRatio; /*!< Selects divide ratio used to generate the internal conversion clock.*/
53 uint8_t averageNumber; /*!< The average number for hardware average function.*/
54 uint8_t resolutionMode; /*!< Set ADC resolution mode.*/
55 } adc_init_config_t;
56
57 /*! @brief ADC hardware average number. */
58 enum _adc_average_number
59 {
60 adcAvgNum4 = 0U, /*!< ADC Hardware Average Number is set to 4.*/
61 adcAvgNum8 = 1U, /*!< ADC Hardware Average Number is set to 8.*/
62 adcAvgNum16 = 2U, /*!< ADC Hardware Average Number is set to 16.*/
63 adcAvgNum32 = 3U, /*!< ADC Hardware Average Number is set to 32.*/
64 adcAvgNumNone = 4U, /*!< Disable ADC Hardware Average.*/
65 };
66
67 /*! @brief ADC conversion trigger select. */
68 enum _adc_convert_trigger_mode
69 {
70 adcSoftwareTrigger = 0U, /*!< ADC software trigger a conversion.*/
71 adcHardwareTrigger = 1U, /*!< ADC hardware trigger a conversion.*/
72 };
73
74 /*! @brief ADC conversion speed configure. */
75 enum _adc_convert_speed_config
76 {
77 adcNormalSpeed = 0U, /*!< ADC set as normal conversion speed.*/
78 adcHighSpeed = 1U, /*!< ADC set as high conversion speed.*/
79 };
80
81 /*! @brief ADC sample time duration. */
82 enum _adc_sample_time_duration
83 {
84 adcSamplePeriodClock2, /*!< The sample time duration is set as 2 ADC clocks.*/
85 adcSamplePeriodClock4, /*!< The sample time duration is set as 4 ADC clocks.*/
86 adcSamplePeriodClock6, /*!< The sample time duration is set as 6 ADC clocks.*/
87 adcSamplePeriodClock8, /*!< The sample time duration is set as 8 ADC clocks.*/
88 adcSamplePeriodClock12, /*!< The sample time duration is set as 12 ADC clocks.*/
89 adcSamplePeriodClock16, /*!< The sample time duration is set as 16 ADC clocks.*/
90 adcSamplePeriodClock20, /*!< The sample time duration is set as 20 ADC clocks.*/
91 adcSamplePeriodClock24, /*!< The sample time duration is set as 24 ADC clocks.*/
92 };
93
94 /*! @brief ADC low power configure. */
95 enum _adc_power_mode
96 {
97 adcNormalPowerMode = 0U, /*!< ADC hard block set as normal power mode.*/
98 adcLowPowerMode = 1U, /*!< ADC hard block set as low power mode.*/
99 };
100
101 /*! @brief ADC conversion resolution mode. */
102 enum _adc_resolution_mode
103 {
104 adcResolutionBit8 = 0U, /*!< ADC resolution set as 8 bit conversion mode.*/
105 adcResolutionBit10 = 1U, /*!< ADC resolution set as 10 bit conversion mode.*/
106 adcResolutionBit12 = 2U, /*!< ADC resolution set as 12 bit conversion mode.*/
107 };
108
109 /*! @brief ADC input clock divide. */
110 enum _adc_clock_divide
111 {
112 adcInputClockDiv1 = 0U, /*!< Input clock divide 1 to generate internal clock.*/
113 adcInputClockDiv2 = 1U, /*!< Input clock divide 2 to generate internal clock.*/
114 adcInputClockDiv4 = 2U, /*!< Input clock divide 4 to generate internal clock.*/
115 adcInputClockDiv8 = 3U, /*!< Input clock divide 8 to generate internal clock.*/
116 };
117
118 /*! @brief ADC clock source. */
119 enum _adc_clock_source
120 {
121 adcIpgClock = 0U, /*!< Select ipg clock as input clock source.*/
122 adcIpgClockDivide2 = 1U, /*!< Select ipg clock divide 2 as input clock source.*/
123 adcAsynClock = 3U, /*!< Select asynchronous clock as input clock source.*/
124 };
125
126 /*! @brief ADC comparer work mode configuration. */
127 enum _adc_compare_mode
128 {
129 adcCmpModeLessThanCmpVal1, /*!< Compare true if the result is less than compare value 1.*/
130 adcCmpModeGreaterThanCmpVal1, /*!< Compare true if the result is greater than or equal to compare value 1.*/
131 adcCmpModeOutRangNotInclusive, /*!< Compare true if the result is less than compare value 1 or the result is Greater than compare value 2.*/
132 adcCmpModeInRangNotInclusive, /*!< Compare true if the result is less than compare value 1 and the result is greater than compare value 2.*/
133 adcCmpModeInRangInclusive, /*!< Compare true if the result is greater than or equal to compare value 1 and the result is less than or equal to compare value 2.*/
134 adcCmpModeOutRangInclusive, /*!< Compare true if the result is greater than or equal to compare value 1 or the result is less than or equal to compare value 2.*/
135 adcCmpModeDisable, /*!< ADC compare function disable.*/
136 };
137
138 /*! @brief ADC general status flag. */
139 enum _adc_general_status_flag
140 {
141 adcFlagAsynWakeUpInt = 1U << 0, /*!< Indicate asynchronous wake up interrupt occurred in stop mode.*/
142 adcFlagCalibrateFailed = 1U << 1, /*!< Indicate the result of the calibration sequence.*/
143 adcFlagConvertActive = 1U << 2, /*!< Indicate a conversion is in the process.*/
144 };
145
146 /*******************************************************************************
147 * API
148 ******************************************************************************/
149
150 #if defined(__cplusplus)
151 extern "C" {
152 #endif
153
154 /*!
155 * @name ADC Module Initialization and Configuration Functions.
156 * @{
157 */
158
159 /*!
160 * @brief Initialize ADC to reset state and initialize with initialize structure.
161 *
162 * @param base ADC base pointer.
163 * @param initConfig ADC initialize structure.
164 */
165 void ADC_Init(ADC_Type* base, const adc_init_config_t* initConfig);
166
167 /*!
168 * @brief This function reset ADC module register content to its default value.
169 *
170 * @param base ADC base pointer.
171 */
172 void ADC_Deinit(ADC_Type* base);
173
174 /*!
175 * @brief Enable or disable ADC module overwrite conversion result.
176 *
177 * @param base ADC base pointer.
178 * @param enable Enable/Disable conversion result overwire function.
179 * - true: Enable conversion result overwire.
180 * - false: Disable conversion result overwrite.
181 */
182 void ADC_SetConvertResultOverwrite(ADC_Type* base, bool enable);
183
184 /*!
185 * @brief This function set ADC module conversion trigger mode.
186 *
187 * @param base ADC base pointer.
188 * @param mode Conversion trigger (see @ref _adc_convert_trigger_mode enumeration).
189 */
190 void ADC_SetConvertTrigMode(ADC_Type* base, uint8_t mode);
191
192 /*!
193 * @brief This function is used to get conversion trigger mode.
194 *
195 * @param base ADC base pointer.
196 * @return Conversion trigger mode (see @ref _adc_convert_trigger_mode enumeration).
197 */
ADC_GetConvertTrigMode(ADC_Type * base)198 static inline uint8_t ADC_GetConvertTrigMode(ADC_Type* base)
199 {
200 return (uint8_t)((ADC_CFG_REG(base) & ADC_CFG_ADTRG_MASK) >> ADC_CFG_ADTRG_SHIFT);
201 }
202
203 /*!
204 * @brief This function set ADC module conversion speed mode.
205 *
206 * @param base ADC base pointer.
207 * @param mode Conversion speed mode (see @ref _adc_convert_speed_config enumeration).
208 */
209 void ADC_SetConvertSpeed(ADC_Type* base, uint8_t mode);
210
211 /*!
212 * @brief This function get ADC module conversion speed mode.
213 *
214 * @param base ADC base pointer.
215 * @return Conversion speed mode.
216 */
ADC_GetConvertSpeed(ADC_Type * base)217 static inline uint8_t ADC_GetConvertSpeed(ADC_Type* base)
218 {
219 return (uint8_t)((ADC_CFG_REG(base) & ADC_CFG_ADHSC_MASK) >> ADC_CFG_ADHSC_SHIFT);
220 }
221
222 /*!
223 * @brief This function set ADC module sample time duration.
224 *
225 * @param base ADC base pointer.
226 * @param duration Sample time duration (see @ref _adc_sample_time_duration enumeration).
227 */
228 void ADC_SetSampleTimeDuration(ADC_Type* base, uint8_t duration);
229
230 /*!
231 * @brief This function set ADC module power mode.
232 *
233 * @param base ADC base pointer.
234 * @param powerMode power mode (see @ref _adc_power_mode enumeration).
235 */
236 void ADC_SetPowerMode(ADC_Type* base, uint8_t powerMode);
237
238 /*!
239 * @brief This function get ADC module power mode.
240 *
241 * @param base ADC base pointer.
242 * @return Power mode.
243 */
ADC_GetPowerMode(ADC_Type * base)244 static inline uint8_t ADC_GetPowerMode(ADC_Type* base)
245 {
246 return (uint8_t)((ADC_CFG_REG(base) & ADC_CFG_ADLPC_MASK) >> ADC_CFG_ADLPC_SHIFT);
247 }
248
249 /*!
250 * @brief This function set ADC module clock source.
251 *
252 * @param base ADC base pointer.
253 * @param source Conversion clock source (see @ref _adc_clock_source enumeration).
254 * @param div Input clock divide ratio (see @ref _adc_clock_divide enumeration).
255 */
256 void ADC_SetClockSource(ADC_Type* base, uint8_t source, uint8_t div);
257
258 /*!
259 * @brief This function enable asynchronous clock source output regardless of the
260 * state of ADC and input clock select of ADC module. Setting this bit
261 * allows the clock to be used even while the ADC is idle or operating from
262 * a different clock source.
263 *
264 * @param base ADC base pointer.
265 * @param enable Asynchronous clock output enable.
266 * - true: Enable asynchronous clock output regardless of the state of ADC;
267 * - false: Only enable if selected as ADC input clock source and a
268 * ADC conversion is active.
269 */
270 void ADC_SetAsynClockOutput(ADC_Type* base, bool enable);
271
272 /*@}*/
273
274 /*!
275 * @name ADC Calibration Control Functions.
276 * @{
277 */
278
279 /*!
280 * @brief This function is used to enable or disable calibration function.
281 *
282 * @param base ADC base pointer.
283 * @param enable Enable/Disable calibration function.
284 * - true: Enable calibration function.
285 * - false: Disable calibration function.
286 */
287 void ADC_SetCalibration(ADC_Type* base, bool enable);
288
289 /*!
290 * @brief This function is used to get calibrate result value.
291 *
292 * @param base ADC base pointer.
293 * @return Calibration result value.
294 */
ADC_GetCalibrationResult(ADC_Type * base)295 static inline uint8_t ADC_GetCalibrationResult(ADC_Type* base)
296 {
297 return (uint8_t)((ADC_CAL_REG(base) & ADC_CAL_CAL_CODE_MASK) >> ADC_CAL_CAL_CODE_SHIFT);
298 }
299
300 /*@}*/
301
302 /*!
303 * @name ADC Module Conversion Control Functions.
304 * @{
305 */
306
307 /*!
308 * @brief Enable continuous conversion and start a conversion on target channel.
309 * This function is only used for software trigger mode. If configured as
310 * hardware trigger mode, this function just enable continuous conversion mode
311 * and not start the conversion.
312 *
313 * @param base ADC base pointer.
314 * @param channel Input channel selection.
315 * @param enable Enable/Disable continuous conversion.
316 * - true: Enable and start continuous conversion.
317 * - false: Disable continuous conversion.
318 */
319 void ADC_SetConvertCmd(ADC_Type* base, uint8_t channel, bool enable);
320
321 /*!
322 * @brief Enable single conversion and trigger single time conversion
323 * on target input channel.If configured as hardware trigger
324 * mode, this function just set input channel and not start a
325 * conversion.
326 *
327 * @param base ADC base pointer.
328 * @param channel Input channel selection.
329 */
330 void ADC_TriggerSingleConvert(ADC_Type* base, uint8_t channel);
331
332 /*!
333 * @brief Enable hardware average function and set hardware average number.
334 *
335 * @param base ADC base pointer.
336 * @param avgNum Hardware average number (see @ref _adc_average_number enumeration).
337 * If avgNum is equal to adcAvgNumNone, it means disable hardware
338 * average function.
339 */
340 void ADC_SetAverageNum(ADC_Type* base, uint8_t avgNum);
341
342 /*!
343 * @brief Set conversion resolution mode.
344 *
345 * @param base ADC base pointer.
346 * @param mode resolution mode (see @ref _adc_resolution_mode enumeration).
347 */
ADC_SetResolutionMode(ADC_Type * base,uint8_t mode)348 static inline void ADC_SetResolutionMode(ADC_Type* base, uint8_t mode)
349 {
350 assert(mode <= adcResolutionBit12);
351
352 ADC_CFG_REG(base) = (ADC_CFG_REG(base) & (~ADC_CFG_MODE_MASK)) |
353 ADC_CFG_MODE(mode);
354 }
355
356 /*!
357 * @brief Set conversion resolution mode.
358 *
359 * @param base ADC base pointer.
360 * @return Resolution mode (see @ref _adc_resolution_mode enumeration).
361 */
ADC_GetResolutionMode(ADC_Type * base)362 static inline uint8_t ADC_GetResolutionMode(ADC_Type* base)
363 {
364 return (uint8_t)((ADC_CFG_REG(base) & ADC_CFG_MODE_MASK) >> ADC_CFG_MODE_SHIFT);
365 }
366
367 /*!
368 * @brief Set conversion disabled.
369 *
370 * @param base ADC base pointer.
371 */
372 void ADC_StopConvert(ADC_Type* base);
373
374 /*!
375 * @brief Get right aligned conversion result.
376 *
377 * @param base ADC base pointer.
378 * @return Conversion result.
379 */
380 uint16_t ADC_GetConvertResult(ADC_Type* base);
381
382 /*@}*/
383
384 /*!
385 * @name ADC Comparer Control Functions.
386 * @{
387 */
388
389 /*!
390 * @brief Enable compare function and set the compare work mode of ADC module.
391 * If cmpMode is equal to adcCmpModeDisable, it means to disable the compare function.
392 * @param base ADC base pointer.
393 * @param cmpMode Comparer work mode selected (see @ref _adc_compare_mode enumeration).
394 * - adcCmpModeLessThanCmpVal1: only set compare value 1;
395 * - adcCmpModeGreaterThanCmpVal1: only set compare value 1;
396 * - adcCmpModeOutRangNotInclusive: set compare value 1 less than or equal to compare value 2;
397 * - adcCmpModeInRangNotInclusive: set compare value 1 greater than compare value 2;
398 * - adcCmpModeInRangInclusive: set compare value 1 less than or equal to compare value 2;
399 * - adcCmpModeOutRangInclusive: set compare value 1 greater than compare value 2;
400 * - adcCmpModeDisable: unnecessary to set compare value 1 and compare value 2.
401 * @param cmpVal1 Compare threshold 1.
402 * @param cmpVal2 Compare threshold 2.
403 */
404 void ADC_SetCmpMode(ADC_Type* base, uint8_t cmpMode, uint16_t cmpVal1, uint16_t cmpVal2);
405
406 /*@}*/
407
408 /*!
409 * @name Offset Correction Control Functions.
410 * @{
411 */
412
413 /*!
414 * @brief Set ADC module offset correct mode.
415 *
416 * @param base ADC base pointer.
417 * @param correctMode Offset correct mode.
418 * - true: The offset value is subtracted from the raw converted value;
419 * - false: The offset value is added with the raw result.
420 */
421 void ADC_SetCorrectionMode(ADC_Type* base, bool correctMode);
422
423 /*!
424 * @brief Set ADC module offset value.
425 *
426 * @param base ADC base pointer.
427 * @param val Offset value.
428 */
ADC_SetOffsetVal(ADC_Type * base,uint16_t val)429 static inline void ADC_SetOffsetVal(ADC_Type* base, uint16_t val)
430 {
431 ADC_OFS_REG(base) = (ADC_OFS_REG(base) & (~ADC_OFS_OFS_MASK)) |
432 ADC_OFS_OFS(val);
433 }
434
435 /*@}*/
436
437 /*!
438 * @name Interrupt and Flag Control Functions.
439 * @{
440 */
441
442 /*!
443 * @brief Enables or disables ADC conversion complete interrupt request.
444 *
445 * @param base ADC base pointer.
446 * @param enable Enable/Disable ADC conversion complete interrupt.
447 * - true: Enable conversion complete interrupt.
448 * - false: Disable conversion complete interrupt.
449 */
450 void ADC_SetIntCmd(ADC_Type* base, bool enable);
451
452 /*!
453 * @brief Gets the ADC module conversion complete status flag state.
454 *
455 * @param base ADC base pointer.
456 * @retval true: A conversion is completed.
457 * @retval false: A conversion is not completed.
458 */
459 bool ADC_IsConvertComplete(ADC_Type* base);
460
461 /*!
462 * @brief Gets the ADC module general status flag state.
463 *
464 * @param base ADC base pointer.
465 * @param flags ADC status flag mask (see @ref _adc_general_status_flag enumeration).
466 * @return ADC status, each bit represents one status flag.
467 */
ADC_GetStatusFlag(ADC_Type * base,uint32_t flags)468 static inline uint32_t ADC_GetStatusFlag(ADC_Type* base, uint32_t flags)
469 {
470 return (uint32_t)(ADC_GS_REG(base) & flags);
471 }
472
473 /*!
474 * @brief Clear one or more ADC status flag state.
475 *
476 * @param base ADC base pointer.
477 * @param flags ADC status flag mask (see @ref _adc_general_status_flag enumeration).
478 */
ADC_ClearStatusFlag(ADC_Type * base,uint32_t flags)479 static inline void ADC_ClearStatusFlag(ADC_Type* base, uint32_t flags)
480 {
481 assert(flags < adcFlagConvertActive);
482 ADC_GS_REG(base) = flags;
483 }
484
485 /*@}*/
486
487 /*!
488 * @name DMA Control Functions.
489 * @{
490 */
491
492 /*!
493 * @brief Enable or Disable DMA request.
494 *
495 * @param base ADC base pointer.
496 * @param enable Enable/Disable ADC DMA request.
497 * - true: Enable DMA request.
498 * - false: Disable DMA request.
499 */
500 void ADC_SetDmaCmd(ADC_Type* base, bool enable);
501
502 /*@}*/
503
504 #ifdef __cplusplus
505 }
506 #endif
507
508 /*! @}*/
509
510 #endif /* __ADC_IMX6SX_H__ */
511 /*******************************************************************************
512 * EOF
513 ******************************************************************************/
514