提交 | 用户 | age
|
483170
|
1 |
/** |
Q |
2 |
****************************************************************************** |
|
3 |
* @file stm32f0xx_hal_adc_ex.c |
|
4 |
* @author MCD Application Team |
|
5 |
* @brief This file provides firmware functions to manage the following |
|
6 |
* functionalities of the Analog to Digital Convertor (ADC) |
|
7 |
* peripheral: |
|
8 |
* + Operation functions |
|
9 |
* ++ Calibration (ADC automatic self-calibration) |
|
10 |
* Other functions (generic functions) are available in file |
|
11 |
* "stm32f0xx_hal_adc.c". |
|
12 |
* |
|
13 |
@verbatim |
|
14 |
[..] |
|
15 |
(@) Sections "ADC peripheral features" and "How to use this driver" are |
|
16 |
available in file of generic functions "stm32l1xx_hal_adc.c". |
|
17 |
[..] |
|
18 |
@endverbatim |
|
19 |
****************************************************************************** |
|
20 |
* @attention |
|
21 |
* |
|
22 |
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
|
23 |
* |
|
24 |
* Redistribution and use in source and binary forms, with or without modification, |
|
25 |
* are permitted provided that the following conditions are met: |
|
26 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
27 |
* this list of conditions and the following disclaimer. |
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
29 |
* this list of conditions and the following disclaimer in the documentation |
|
30 |
* and/or other materials provided with the distribution. |
|
31 |
* 3. Neither the name of STMicroelectronics nor the names of its contributors |
|
32 |
* may be used to endorse or promote products derived from this software |
|
33 |
* without specific prior written permission. |
|
34 |
* |
|
35 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
36 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
38 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
39 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
40 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
41 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
42 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
43 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
44 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
45 |
* |
|
46 |
****************************************************************************** |
|
47 |
*/ |
|
48 |
|
|
49 |
/* Includes ------------------------------------------------------------------*/ |
|
50 |
#include "stm32f0xx_hal.h" |
|
51 |
|
|
52 |
/** @addtogroup STM32F0xx_HAL_Driver |
|
53 |
* @{ |
|
54 |
*/ |
|
55 |
|
|
56 |
/** @defgroup ADCEx ADCEx |
|
57 |
* @brief ADC HAL module driver |
|
58 |
* @{ |
|
59 |
*/ |
|
60 |
|
|
61 |
#ifdef HAL_ADC_MODULE_ENABLED |
|
62 |
|
|
63 |
/* Private typedef -----------------------------------------------------------*/ |
|
64 |
/* Private define ------------------------------------------------------------*/ |
|
65 |
/** @defgroup ADCEx_Private_Constants ADCEx Private Constants |
|
66 |
* @{ |
|
67 |
*/ |
|
68 |
|
|
69 |
/* Fixed timeout values for ADC calibration, enable settling time, disable */ |
|
70 |
/* settling time. */ |
|
71 |
/* Values defined to be higher than worst cases: low clock frequency, */ |
|
72 |
/* maximum prescaler. */ |
|
73 |
/* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */ |
|
74 |
/* prescaler 4. */ |
|
75 |
/* Unit: ms */ |
|
76 |
#define ADC_DISABLE_TIMEOUT 2 |
|
77 |
#define ADC_CALIBRATION_TIMEOUT 2U |
|
78 |
/** |
|
79 |
* @} |
|
80 |
*/ |
|
81 |
|
|
82 |
/* Private macros -------------------------------------------------------------*/ |
|
83 |
/* Private variables ---------------------------------------------------------*/ |
|
84 |
/* Private function prototypes -----------------------------------------------*/ |
|
85 |
/* Private functions ---------------------------------------------------------*/ |
|
86 |
|
|
87 |
/** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions |
|
88 |
* @{ |
|
89 |
*/ |
|
90 |
|
|
91 |
/** @defgroup ADCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions |
|
92 |
* @brief Extended Initialization and Configuration functions |
|
93 |
* |
|
94 |
@verbatim |
|
95 |
=============================================================================== |
|
96 |
##### IO operation functions ##### |
|
97 |
=============================================================================== |
|
98 |
[..] This section provides functions allowing to: |
|
99 |
(+) Perform the ADC calibration. |
|
100 |
@endverbatim |
|
101 |
* @{ |
|
102 |
*/ |
|
103 |
|
|
104 |
/** |
|
105 |
* @brief Perform an ADC automatic self-calibration |
|
106 |
* Calibration prerequisite: ADC must be disabled (execute this |
|
107 |
* function before HAL_ADC_Start() or after HAL_ADC_Stop() ). |
|
108 |
* @note Calibration factor can be read after calibration, using function |
|
109 |
* HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]). |
|
110 |
* @param hadc ADC handle |
|
111 |
* @retval HAL status |
|
112 |
*/ |
|
113 |
HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc) |
|
114 |
{ |
|
115 |
HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
|
116 |
uint32_t tickstart = 0U; |
|
117 |
uint32_t backup_setting_adc_dma_transfer = 0; /* Note: Variable not declared as volatile because register read is already declared as volatile */ |
|
118 |
|
|
119 |
/* Check the parameters */ |
|
120 |
assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
|
121 |
|
|
122 |
/* Process locked */ |
|
123 |
__HAL_LOCK(hadc); |
|
124 |
|
|
125 |
/* Calibration prerequisite: ADC must be disabled. */ |
|
126 |
if (ADC_IS_ENABLE(hadc) == RESET) |
|
127 |
{ |
|
128 |
/* Set ADC state */ |
|
129 |
ADC_STATE_CLR_SET(hadc->State, |
|
130 |
HAL_ADC_STATE_REG_BUSY, |
|
131 |
HAL_ADC_STATE_BUSY_INTERNAL); |
|
132 |
|
|
133 |
/* Disable ADC DMA transfer request during calibration */ |
|
134 |
/* Note: Specificity of this STM32 serie: Calibration factor is */ |
|
135 |
/* available in data register and also transfered by DMA. */ |
|
136 |
/* To not insert ADC calibration factor among ADC conversion data */ |
|
137 |
/* in array variable, DMA transfer must be disabled during */ |
|
138 |
/* calibration. */ |
|
139 |
backup_setting_adc_dma_transfer = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG); |
|
140 |
CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG); |
|
141 |
|
|
142 |
/* Start ADC calibration */ |
|
143 |
hadc->Instance->CR |= ADC_CR_ADCAL; |
|
144 |
|
|
145 |
tickstart = HAL_GetTick(); |
|
146 |
|
|
147 |
/* Wait for calibration completion */ |
|
148 |
while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL)) |
|
149 |
{ |
|
150 |
if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT) |
|
151 |
{ |
|
152 |
/* Update ADC state machine to error */ |
|
153 |
ADC_STATE_CLR_SET(hadc->State, |
|
154 |
HAL_ADC_STATE_BUSY_INTERNAL, |
|
155 |
HAL_ADC_STATE_ERROR_INTERNAL); |
|
156 |
|
|
157 |
/* Process unlocked */ |
|
158 |
__HAL_UNLOCK(hadc); |
|
159 |
|
|
160 |
return HAL_ERROR; |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
/* Restore ADC DMA transfer request after calibration */ |
|
165 |
SET_BIT(hadc->Instance->CFGR1, backup_setting_adc_dma_transfer); |
|
166 |
|
|
167 |
/* Set ADC state */ |
|
168 |
ADC_STATE_CLR_SET(hadc->State, |
|
169 |
HAL_ADC_STATE_BUSY_INTERNAL, |
|
170 |
HAL_ADC_STATE_READY); |
|
171 |
} |
|
172 |
else |
|
173 |
{ |
|
174 |
/* Update ADC state machine to error */ |
|
175 |
SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG); |
|
176 |
|
|
177 |
tmp_hal_status = HAL_ERROR; |
|
178 |
} |
|
179 |
|
|
180 |
/* Process unlocked */ |
|
181 |
__HAL_UNLOCK(hadc); |
|
182 |
|
|
183 |
/* Return function status */ |
|
184 |
return tmp_hal_status; |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* @} |
|
189 |
*/ |
|
190 |
|
|
191 |
/** |
|
192 |
* @} |
|
193 |
*/ |
|
194 |
|
|
195 |
#endif /* HAL_ADC_MODULE_ENABLED */ |
|
196 |
/** |
|
197 |
* @} |
|
198 |
*/ |
|
199 |
|
|
200 |
/** |
|
201 |
* @} |
|
202 |
*/ |
|
203 |
|
|
204 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |