Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
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. |
||
23 | * All rights reserved.</center></h2> |
||
24 | * |
||
25 | * This software component is licensed by ST under BSD 3-Clause license, |
||
26 | * the "License"; You may not use this file except in compliance with the |
||
27 | * License. You may obtain a copy of the License at: |
||
28 | * opensource.org/licenses/BSD-3-Clause |
||
29 | * |
||
30 | ****************************************************************************** |
||
31 | */ |
||
32 | |||
33 | /* Includes ------------------------------------------------------------------*/ |
||
34 | #include "stm32f0xx_hal.h" |
||
35 | |||
36 | /** @addtogroup STM32F0xx_HAL_Driver |
||
37 | * @{ |
||
38 | */ |
||
39 | |||
40 | /** @defgroup ADCEx ADCEx |
||
41 | * @brief ADC HAL module driver |
||
42 | * @{ |
||
43 | */ |
||
44 | |||
45 | #ifdef HAL_ADC_MODULE_ENABLED |
||
46 | |||
47 | /* Private typedef -----------------------------------------------------------*/ |
||
48 | /* Private define ------------------------------------------------------------*/ |
||
49 | /** @defgroup ADCEx_Private_Constants ADCEx Private Constants |
||
50 | * @{ |
||
51 | */ |
||
52 | |||
53 | /* Fixed timeout values for ADC calibration, enable settling time, disable */ |
||
54 | /* settling time. */ |
||
55 | /* Values defined to be higher than worst cases: low clock frequency, */ |
||
56 | /* maximum prescaler. */ |
||
57 | /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */ |
||
58 | /* prescaler 4. */ |
||
59 | /* Unit: ms */ |
||
60 | #define ADC_DISABLE_TIMEOUT 2 |
||
61 | #define ADC_CALIBRATION_TIMEOUT 2U |
||
62 | /** |
||
63 | * @} |
||
64 | */ |
||
65 | |||
66 | /* Private macros -------------------------------------------------------------*/ |
||
67 | /* Private variables ---------------------------------------------------------*/ |
||
68 | /* Private function prototypes -----------------------------------------------*/ |
||
69 | /* Private functions ---------------------------------------------------------*/ |
||
70 | |||
71 | /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions |
||
72 | * @{ |
||
73 | */ |
||
74 | |||
75 | /** @defgroup ADCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions |
||
76 | * @brief Extended Initialization and Configuration functions |
||
77 | * |
||
78 | @verbatim |
||
79 | =============================================================================== |
||
80 | ##### IO operation functions ##### |
||
81 | =============================================================================== |
||
82 | [..] This section provides functions allowing to: |
||
83 | (+) Perform the ADC calibration. |
||
84 | @endverbatim |
||
85 | * @{ |
||
86 | */ |
||
87 | |||
88 | /** |
||
89 | * @brief Perform an ADC automatic self-calibration |
||
90 | * Calibration prerequisite: ADC must be disabled (execute this |
||
91 | * function before HAL_ADC_Start() or after HAL_ADC_Stop() ). |
||
92 | * @note Calibration factor can be read after calibration, using function |
||
93 | * HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]). |
||
94 | * @param hadc ADC handle |
||
95 | * @retval HAL status |
||
96 | */ |
||
97 | HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc) |
||
98 | { |
||
99 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
100 | uint32_t tickstart = 0U; |
||
101 | uint32_t backup_setting_adc_dma_transfer = 0; /* Note: Variable not declared as volatile because register read is already declared as volatile */ |
||
102 | |||
103 | /* Check the parameters */ |
||
104 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
105 | |||
106 | /* Process locked */ |
||
107 | __HAL_LOCK(hadc); |
||
108 | |||
109 | /* Calibration prerequisite: ADC must be disabled. */ |
||
110 | if (ADC_IS_ENABLE(hadc) == RESET) |
||
111 | { |
||
112 | /* Set ADC state */ |
||
113 | ADC_STATE_CLR_SET(hadc->State, |
||
114 | HAL_ADC_STATE_REG_BUSY, |
||
115 | HAL_ADC_STATE_BUSY_INTERNAL); |
||
116 | |||
117 | /* Disable ADC DMA transfer request during calibration */ |
||
118 | /* Note: Specificity of this STM32 serie: Calibration factor is */ |
||
119 | /* available in data register and also transfered by DMA. */ |
||
120 | /* To not insert ADC calibration factor among ADC conversion data */ |
||
121 | /* in array variable, DMA transfer must be disabled during */ |
||
122 | /* calibration. */ |
||
123 | backup_setting_adc_dma_transfer = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG); |
||
124 | CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG); |
||
125 | |||
126 | /* Start ADC calibration */ |
||
127 | hadc->Instance->CR |= ADC_CR_ADCAL; |
||
128 | |||
129 | tickstart = HAL_GetTick(); |
||
130 | |||
131 | /* Wait for calibration completion */ |
||
132 | while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL)) |
||
133 | { |
||
134 | if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT) |
||
135 | { |
||
6 | mjames | 136 | /* New check to avoid false timeout detection in case of preemption */ |
137 | if(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL)) |
||
138 | { |
||
139 | /* Update ADC state machine to error */ |
||
140 | ADC_STATE_CLR_SET(hadc->State, |
||
141 | HAL_ADC_STATE_BUSY_INTERNAL, |
||
142 | HAL_ADC_STATE_ERROR_INTERNAL); |
||
143 | |||
144 | /* Process unlocked */ |
||
145 | __HAL_UNLOCK(hadc); |
||
146 | |||
147 | return HAL_ERROR; |
||
148 | } |
||
2 | mjames | 149 | } |
150 | } |
||
151 | |||
152 | /* Restore ADC DMA transfer request after calibration */ |
||
153 | SET_BIT(hadc->Instance->CFGR1, backup_setting_adc_dma_transfer); |
||
154 | |||
155 | /* Set ADC state */ |
||
156 | ADC_STATE_CLR_SET(hadc->State, |
||
157 | HAL_ADC_STATE_BUSY_INTERNAL, |
||
158 | HAL_ADC_STATE_READY); |
||
159 | } |
||
160 | else |
||
161 | { |
||
162 | /* Update ADC state machine to error */ |
||
163 | SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG); |
||
164 | |||
165 | tmp_hal_status = HAL_ERROR; |
||
166 | } |
||
167 | |||
168 | /* Process unlocked */ |
||
169 | __HAL_UNLOCK(hadc); |
||
170 | |||
171 | /* Return function status */ |
||
172 | return tmp_hal_status; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @} |
||
177 | */ |
||
178 | |||
179 | /** |
||
180 | * @} |
||
181 | */ |
||
182 | |||
183 | #endif /* HAL_ADC_MODULE_ENABLED */ |
||
184 | /** |
||
185 | * @} |
||
186 | */ |
||
187 | |||
188 | /** |
||
189 | * @} |
||
190 | */ |
||
191 | |||
192 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |