mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Parent:
441:d2c15dda23c1
Child:
630:825f75ca301e
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

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

Initial version of drivers for SAMR21

Who changed what in which revision?

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