mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri May 30 15:30:09 2014 +0100
Revision:
218:44081b78fdc2
Parent:
205:c41fc65bcfb4
Synchronized with git revision d854859072d318241476ccc5f335965444d4c1d8

Full URL: https://github.com/mbedmicro/mbed/commit/d854859072d318241476ccc5f335965444d4c1d8/

[NUCLEO_F072RB] Update CubeF0 HAL driver

Who changed what in which revision?

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