2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
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.
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.
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.
31 #include "fsl_adc_hal.h"
33 /*FUNCTION*********************************************************************
35 * Function Name : ADC_HAL_Init
36 * Description :Reset all the registers into a known state for ADC
37 * module. This known state is the default value indicated by the Reference
38 * manual. It is strongly recommended to call this API before any operations
39 * when initializing the ADC module. Note registers for calibration would not
40 * be cleared in this function.
42 *END*************************************************************************/
43 void ADC_HAL_Init(uint32_t baseAddr)
45 HW_ADC_CFG1_WR(baseAddr, 0U);
46 HW_ADC_CFG2_WR(baseAddr, 0U);
47 HW_ADC_CV1_WR(baseAddr, 0U);
48 HW_ADC_CV2_WR(baseAddr, 0U);
49 HW_ADC_SC2_WR(baseAddr, 0U);
50 HW_ADC_SC3_WR(baseAddr, 0U);
51 #if FSL_FEATURE_ADC_HAS_PGA
52 HW_ADC_PGA_WR(baseAddr, 0U);
53 #endif /* FSL_FEATURE_ADC_HAS_PGA */
56 /*FUNCTION*********************************************************************
58 * Function Name : ADC_HAL_SetHwCmpMode
59 * Description :Set the asserted compare range when enabling hardware
60 * compare function. About the selection of range mode, see to the description
61 * for "adc_hw_cmp_range_mode_t".
63 *END*************************************************************************/
64 void ADC_HAL_SetHwCmpMode(uint32_t baseAddr, adc_hw_cmp_range_mode_t mode)
68 case kAdcHwCmpRangeModeOf1:
69 ADC_HAL_SetHwCmpGreaterCmd(baseAddr, false);
70 ADC_HAL_SetHwCmpRangeCmd(baseAddr, false);
72 case kAdcHwCmpRangeModeOf2:
73 ADC_HAL_SetHwCmpGreaterCmd(baseAddr, true);
74 ADC_HAL_SetHwCmpRangeCmd(baseAddr, false);
76 case kAdcHwCmpRangeModeOf3:
77 ADC_HAL_SetHwCmpGreaterCmd(baseAddr, false);
78 ADC_HAL_SetHwCmpRangeCmd(baseAddr, true);
80 case kAdcHwCmpRangeModeOf4:
81 ADC_HAL_SetHwCmpGreaterCmd(baseAddr, true);
82 ADC_HAL_SetHwCmpRangeCmd(baseAddr, true);
89 #if FSL_FEATURE_ADC_HAS_CALIBRATION
91 /*FUNCTION*********************************************************************
93 * Function Name : ADC_HAL_GetAutoPlusSideGainValue
94 * Description : Get the values of CLP0 - CLP4 and CLPS internally,
95 * accumulate them, and return the value that can be used to be set in PG
96 * register directly. Note that this API should be called after the process of
97 * auto calibration has been done.
99 *END*************************************************************************/
100 uint16_t ADC_HAL_GetAutoPlusSideGainValue(uint32_t baseAddr)
104 /* Calculate plus-side calibration */
106 cal_var += BR_ADC_CLP0_CLP0(baseAddr);
107 cal_var += BR_ADC_CLP1_CLP1(baseAddr);
108 cal_var += BR_ADC_CLP2_CLP2(baseAddr);
109 cal_var += BR_ADC_CLP3_CLP3(baseAddr);
110 cal_var += BR_ADC_CLP4_CLP4(baseAddr);
111 cal_var += BR_ADC_CLPS_CLPS(baseAddr);
112 cal_var = 0x8000U | (cal_var>>1U);
117 #if FSL_FEATURE_ADC_HAS_DIFF_MODE
119 /*FUNCTION*********************************************************************
121 * Function Name : ADC_HAL_GetAutoMinusSideGainValue
122 * Description : Get the values of CLM0 - CLM4 and CLMS internally,
123 * accumulate them, and return the value that can be used to be set in MG
124 * register directly. Note that this API should be called after the process of
125 * auto calibration has been done.
127 *END*************************************************************************/
128 uint16_t ADC_HAL_GetAutoMinusSideGainValue(uint32_t baseAddr)
132 /* Calculate minus-side calibration */
134 cal_var += BR_ADC_CLM0_CLM0(baseAddr);
135 cal_var += BR_ADC_CLM1_CLM1(baseAddr);
136 cal_var += BR_ADC_CLM2_CLM2(baseAddr);
137 cal_var += BR_ADC_CLM3_CLM3(baseAddr);
138 cal_var += BR_ADC_CLM4_CLM4(baseAddr);
139 cal_var += BR_ADC_CLMS_CLMS(baseAddr);
140 cal_var = 0x8000U | (cal_var>>1U);
145 #endif /* FSL_FEATURE_ADC_HAS_DIFF_MODE */
147 #endif /* FSL_FEATURE_ADC_HAS_CALIBRATION */
149 /******************************************************************************
151 *****************************************************************************/