]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/adc/fsl_adc_hal.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / hal / adc / fsl_adc_hal.c
1 /*
2  * Copyright (c) 2013 - 2014, 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 #include "fsl_adc_hal.h"
32
33 /*FUNCTION*********************************************************************
34  *
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.
41  *
42  *END*************************************************************************/
43 void ADC_HAL_Init(uint32_t baseAddr)
44 {
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 */
54 }
55
56 /*FUNCTION*********************************************************************
57  *
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".
62  *
63  *END*************************************************************************/
64 void ADC_HAL_SetHwCmpMode(uint32_t baseAddr, adc_hw_cmp_range_mode_t mode)
65 {
66     switch (mode)
67     {
68     case kAdcHwCmpRangeModeOf1:
69         ADC_HAL_SetHwCmpGreaterCmd(baseAddr, false);
70         ADC_HAL_SetHwCmpRangeCmd(baseAddr, false);
71         break;
72     case kAdcHwCmpRangeModeOf2:
73         ADC_HAL_SetHwCmpGreaterCmd(baseAddr, true);
74         ADC_HAL_SetHwCmpRangeCmd(baseAddr, false);
75         break;
76     case kAdcHwCmpRangeModeOf3:
77         ADC_HAL_SetHwCmpGreaterCmd(baseAddr, false);
78         ADC_HAL_SetHwCmpRangeCmd(baseAddr, true);
79         break;
80     case kAdcHwCmpRangeModeOf4:
81         ADC_HAL_SetHwCmpGreaterCmd(baseAddr, true);
82         ADC_HAL_SetHwCmpRangeCmd(baseAddr, true);
83         break;
84     default:
85         break;
86     }
87 }
88
89 #if FSL_FEATURE_ADC_HAS_CALIBRATION
90
91 /*FUNCTION*********************************************************************
92  *
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.
98  *
99  *END*************************************************************************/
100 uint16_t ADC_HAL_GetAutoPlusSideGainValue(uint32_t baseAddr)
101 {
102     uint16_t cal_var;
103
104     /* Calculate plus-side calibration */
105     cal_var = 0U;
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);
113
114     return cal_var;
115 }
116
117 #if FSL_FEATURE_ADC_HAS_DIFF_MODE
118
119 /*FUNCTION*********************************************************************
120  *
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.
126  *
127  *END*************************************************************************/
128 uint16_t ADC_HAL_GetAutoMinusSideGainValue(uint32_t baseAddr)
129 {
130     uint16_t cal_var;
131
132     /* Calculate minus-side calibration */
133     cal_var = 0U;
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);
141
142     return cal_var;
143 }
144
145 #endif /* FSL_FEATURE_ADC_HAS_DIFF_MODE */
146
147 #endif /* FSL_FEATURE_ADC_HAS_CALIBRATION */
148
149 /******************************************************************************
150  * EOF
151  *****************************************************************************/
152