]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_adc_ex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F0 / stm32f0xx_hal_adc_ex.c
1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_hal_adc_ex.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    11-December-2014
7   * @brief   This file provides firmware functions to manage the following 
8   *          functionalities of the Analog to Digital Convertor (ADC)
9   *          peripheral:
10   *           + Operation functions
11   *             ++ Calibration (ADC automatic self-calibration)
12   *         
13   @verbatim
14   ==============================================================================
15                     ##### ADC specific features #####
16   ==============================================================================
17   [..] 
18   (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
19
20   (#) Interrupt generation at the end of regular conversion and in case of 
21       analog watchdog or overrun events.
22   
23   (#) Single and continuous conversion modes.
24   
25   (#) Scan mode for automatic conversion of channel 0 to channel 'n'.
26   
27   (#) Data alignment with in-built data coherency.
28   
29   (#) Programmable sampling time.
30   
31   (#) ADC conversion group Regular.
32
33   (#) External trigger (timer or EXTI) with configurable polarity.
34
35   (#) DMA request generation for transfer of conversions data of regular group.
36
37   (#) ADC calibration
38   
39   (#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at 
40       slower speed.
41   
42   (#) ADC input range: from Vref minud (connected to Vssa) to Vref plus(connected to 
43       Vdda or to an external voltage reference).
44
45
46                      ##### How to use this driver #####
47   ==============================================================================
48     [..]
49
50     (#) Enable the ADC interface 
51         As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured  
52         at RCC top level: clock source and clock prescaler.
53         Two possible clock sources: synchronous clock derived from APB clock
54         or asynchronous clock derived from ADC dedicated HSI RC oscillator
55         14MHz.
56         Example:
57           __ADC1_CLK_ENABLE();                         (mandatory)
58           
59           HI14 enable or let under control of ADC:     (optional)
60
61           RCC_OscInitTypeDef   RCC_OscInitStructure;
62           RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
63           RCC_OscInitStructure.HSI14CalibrationValue = RCC_HSI14CALIBRATION_DEFAULT;
64           RCC_OscInitStructure.HSI14State = RCC_HSI14_ADC_CONTROL;
65           RCC_OscInitStructure.PLL...   (optional if used for system clock)
66           HAL_RCC_OscConfig(&RCC_OscInitStructure);
67           
68           Parameter "HSI14State" must be set either:
69            - to "...HSI14State = RCC_HSI14_ADC_CONTROL" to let the ADC control 
70              the HSI14 oscillator enable/disable (if not used to supply the main 
71              system clock): feature used if ADC mode LowPowerAutoPowerOff is 
72              enabled.
73            - to "...HSI14State = RCC_HSI14_ON" to maintain the HSI14 oscillator
74              always enabled: can be used to supply the main system clock.
75
76     (#) ADC pins configuration
77          (++) Enable the clock for the ADC GPIOs using the following function:
78              __GPIOx_CLK_ENABLE();   
79          (++) Configure these ADC pins in analog mode using HAL_GPIO_Init();  
80   
81      (#) Configure the ADC parameters (conversion resolution, data alignment,  
82          continuous mode, ...) using the HAL_ADC_Init() function.
83
84      (#) Activate the ADC peripheral using one of the start functions: 
85          HAL_ADC_Start(), HAL_ADC_Start_IT(), HAL_ADC_Start_DMA().
86   
87      *** Regular channels group configuration ***
88      ============================================
89      [..]    
90        (+) To configure the ADC regular channels group features, use 
91            HAL_ADC_Init() and HAL_ADC_ConfigChannel() functions.
92        (+) To activate the continuous mode, use the HAL_ADC_Init() function.   
93        (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.
94               
95      *** DMA for Regular channels group features configuration ***
96      ============================================================= 
97      [..]
98        (+) To enable the DMA mode for regular channels group, use the  
99            HAL_ADC_Start_DMA() function.
100        (+) To enable the generation of DMA requests continuously at the end of 
101            the last DMA transfer, use the HAL_ADC_Init() function.
102   
103     @endverbatim
104   ******************************************************************************
105   * @attention
106   *
107   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
108   *
109   * Redistribution and use in source and binary forms, with or without modification,
110   * are permitted provided that the following conditions are met:
111   *   1. Redistributions of source code must retain the above copyright notice,
112   *      this list of conditions and the following disclaimer.
113   *   2. Redistributions in binary form must reproduce the above copyright notice,
114   *      this list of conditions and the following disclaimer in the documentation
115   *      and/or other materials provided with the distribution.
116   *   3. Neither the name of STMicroelectronics nor the names of its contributors
117   *      may be used to endorse or promote products derived from this software
118   *      without specific prior written permission.
119   *
120   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
121   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
122   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
123   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
124   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
125   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
126   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
127   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
128   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
129   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
130   *
131   ******************************************************************************  
132   */
133
134 /* Includes ------------------------------------------------------------------*/
135 #include "stm32f0xx_hal.h"
136
137 /** @addtogroup STM32F0xx_HAL_Driver
138   * @{
139   */
140
141 /** @defgroup ADCEx ADCEx Extended HAL Module Driver
142   * @brief ADC HAL module driver
143   * @{
144   */
145
146 #ifdef HAL_ADC_MODULE_ENABLED
147
148 /* Private typedef -----------------------------------------------------------*/
149 /* Private define ------------------------------------------------------------*/
150 /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
151   * @{
152   */ 
153  
154 /* Fixed timeout values for ADC calibration, enable settling time, disable  */
155   /* settling time.                                                           */
156   /* Values defined to be higher than worst cases: low clock frequency,       */
157   /* maximum prescaler.                                                       */
158   /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock         */
159   /* prescaler 4.                                                             */
160   /* Unit: ms                                                                 */
161   #define ADC_DISABLE_TIMEOUT           2
162   #define ADC_CALIBRATION_TIMEOUT       2      
163 /**
164   * @}
165   */
166   
167 /* Private macro -------------------------------------------------------------*/
168 /* Private variables ---------------------------------------------------------*/
169 /* Private function prototypes -----------------------------------------------*/
170 /* Private functions ---------------------------------------------------------*/
171
172 /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
173   * @{
174   */
175
176 /** @defgroup ADCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions 
177  *  @brief    Extended Initialization and Configuration functions 
178  *
179 @verbatim    
180  ===============================================================================
181                       ##### IO operation functions #####
182  ===============================================================================
183     [..]  This section provides functions allowing to:
184       (+) Perform the ADC calibration. 
185 @endverbatim
186   * @{
187   */
188
189 /**
190   * @brief  Perform an ADC automatic self-calibration
191   *         Calibration prerequisite: ADC must be disabled (execute this
192   *         function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
193   * @note   Calibration factor can be read after calibration, using function
194   *         HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]).
195   * @param  hadc: ADC handle
196   * @retval HAL status
197   */
198 HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
199 {
200   HAL_StatusTypeDef tmpHALStatus = HAL_OK;
201   uint32_t tickstart=0;
202   
203   /* Check the parameters */
204   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
205
206   /* Process locked */
207   __HAL_LOCK(hadc);
208        
209   /* Calibration prerequisite: ADC must be disabled.                          */
210   if (__HAL_ADC_IS_ENABLED(hadc) == RESET )
211   {
212     /* Change ADC state */
213     hadc->State = HAL_ADC_STATE_READY;
214     
215     /* Start ADC calibration */
216     hadc->Instance->CR |= ADC_CR_ADCAL;
217
218     tickstart = HAL_GetTick();  
219
220     /* Wait for calibration completion */
221     while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
222     {
223       if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
224       {
225         /* Update ADC state machine to error */
226         hadc->State = HAL_ADC_STATE_ERROR;
227         
228         /* Process unlocked */
229         __HAL_UNLOCK(hadc);
230         
231         return HAL_ERROR;
232       }
233     }
234   }
235   else
236   {
237     /* Update ADC state machine to error */
238     hadc->State = HAL_ADC_STATE_ERROR;
239   }
240   
241   /* Process unlocked */
242   __HAL_UNLOCK(hadc);
243   
244   /* Return function status */
245   return tmpHALStatus;
246 }
247
248 /**
249   * @}
250   */  
251
252 /**
253   * @}
254   */
255
256 #endif /* HAL_ADC_MODULE_ENABLED */
257 /**
258   * @}
259   */
260
261 /**
262   * @}
263   */
264
265 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/