mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
180:96ed750bd169
mbed library release version 165

Who changed what in which revision?

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