mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Sep 30 17:00:09 2015 +0100
Revision:
636:a11c0372f0ba
Parent:
631:825f75ca301e
Synchronized with git revision d29c98dae61be0946ddf3a3c641c7726056f9452

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

Added support for SAMW25

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