mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
targets/cmsis/TARGET_STM/TARGET_NUCLEO_L053R8/stm32l0xx_hal_adc.c@181:a4cbdfbbd2f4, 2014-05-07 (annotated)
- Committer:
- mbed_official
- Date:
- Wed May 07 13:15:08 2014 +0100
- Revision:
- 181:a4cbdfbbd2f4
Synchronized with git revision 7751e759576c6fd68deccb81ea82bac19ed41745
Full URL: https://github.com/mbedmicro/mbed/commit/7751e759576c6fd68deccb81ea82bac19ed41745/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 181:a4cbdfbbd2f4 | 1 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 2 | ****************************************************************************** |
mbed_official | 181:a4cbdfbbd2f4 | 3 | * @file stm32l0xx_hal_adc.c |
mbed_official | 181:a4cbdfbbd2f4 | 4 | * @author MCD Application Team |
mbed_official | 181:a4cbdfbbd2f4 | 5 | * @version V1.0.0 |
mbed_official | 181:a4cbdfbbd2f4 | 6 | * @date 22-April-2014 |
mbed_official | 181:a4cbdfbbd2f4 | 7 | * @brief This file provides firmware functions to manage the following |
mbed_official | 181:a4cbdfbbd2f4 | 8 | * functionalities of the Analog to Digital Convertor (ADC) |
mbed_official | 181:a4cbdfbbd2f4 | 9 | * peripheral: |
mbed_official | 181:a4cbdfbbd2f4 | 10 | * + Initialization and de-initialization functions |
mbed_official | 181:a4cbdfbbd2f4 | 11 | * ++ Initialization and Configuration of ADC |
mbed_official | 181:a4cbdfbbd2f4 | 12 | * + Operation functions |
mbed_official | 181:a4cbdfbbd2f4 | 13 | * ++ Start, stop, get result of conversions of regular |
mbed_official | 181:a4cbdfbbd2f4 | 14 | * groups, using 3 possible modes : polling, interruption or DMA. |
mbed_official | 181:a4cbdfbbd2f4 | 15 | * ++ Calibration feature |
mbed_official | 181:a4cbdfbbd2f4 | 16 | * + Control functions |
mbed_official | 181:a4cbdfbbd2f4 | 17 | * ++ Analog Watchdog configuration |
mbed_official | 181:a4cbdfbbd2f4 | 18 | * ++ Regular Channels Configuration |
mbed_official | 181:a4cbdfbbd2f4 | 19 | * + State functions |
mbed_official | 181:a4cbdfbbd2f4 | 20 | * ++ ADC state machine management |
mbed_official | 181:a4cbdfbbd2f4 | 21 | * ++ Interrupts and flags management |
mbed_official | 181:a4cbdfbbd2f4 | 22 | * |
mbed_official | 181:a4cbdfbbd2f4 | 23 | @verbatim |
mbed_official | 181:a4cbdfbbd2f4 | 24 | ============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 25 | ##### ADC specific features ##### |
mbed_official | 181:a4cbdfbbd2f4 | 26 | ============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 27 | [..] |
mbed_official | 181:a4cbdfbbd2f4 | 28 | (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution. |
mbed_official | 181:a4cbdfbbd2f4 | 29 | |
mbed_official | 181:a4cbdfbbd2f4 | 30 | (#) A built-in hardware oversampler allows to improve analog performances |
mbed_official | 181:a4cbdfbbd2f4 | 31 | while off-loading the related computational burden from the CPU. |
mbed_official | 181:a4cbdfbbd2f4 | 32 | |
mbed_official | 181:a4cbdfbbd2f4 | 33 | (#) Interrupt generation at the end of conversionand in case of analog |
mbed_official | 181:a4cbdfbbd2f4 | 34 | watchdog or overrun events. |
mbed_official | 181:a4cbdfbbd2f4 | 35 | |
mbed_official | 181:a4cbdfbbd2f4 | 36 | (#) Single and continuous conversion modes. |
mbed_official | 181:a4cbdfbbd2f4 | 37 | |
mbed_official | 181:a4cbdfbbd2f4 | 38 | (#) Scan or dicontinuous mode conversion of channel 0 to channel 18. |
mbed_official | 181:a4cbdfbbd2f4 | 39 | |
mbed_official | 181:a4cbdfbbd2f4 | 40 | (#) Configurable scan direction (Upward from channel 0 to 18 or Backward from |
mbed_official | 181:a4cbdfbbd2f4 | 41 | channel 18 to channel 0) |
mbed_official | 181:a4cbdfbbd2f4 | 42 | |
mbed_official | 181:a4cbdfbbd2f4 | 43 | (#) Data alignment with in-built data coherency. |
mbed_official | 181:a4cbdfbbd2f4 | 44 | |
mbed_official | 181:a4cbdfbbd2f4 | 45 | (#) Channel-wise programmable sampling time. |
mbed_official | 181:a4cbdfbbd2f4 | 46 | |
mbed_official | 181:a4cbdfbbd2f4 | 47 | (#) External trigger option with configurable polarity. |
mbed_official | 181:a4cbdfbbd2f4 | 48 | |
mbed_official | 181:a4cbdfbbd2f4 | 49 | (#) DMA request generation during regular channel conversion. |
mbed_official | 181:a4cbdfbbd2f4 | 50 | |
mbed_official | 181:a4cbdfbbd2f4 | 51 | (#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at |
mbed_official | 181:a4cbdfbbd2f4 | 52 | slower speed. |
mbed_official | 181:a4cbdfbbd2f4 | 53 | |
mbed_official | 181:a4cbdfbbd2f4 | 54 | (#) ADC input range: VREF=VIN =VREF+. |
mbed_official | 181:a4cbdfbbd2f4 | 55 | |
mbed_official | 181:a4cbdfbbd2f4 | 56 | (#) ADC self-calibration. |
mbed_official | 181:a4cbdfbbd2f4 | 57 | |
mbed_official | 181:a4cbdfbbd2f4 | 58 | (#) ADC is automatically powered off (AutoOff mode) except during the active |
mbed_official | 181:a4cbdfbbd2f4 | 59 | conversion phase. This dramatically reduces the power consumption of the |
mbed_official | 181:a4cbdfbbd2f4 | 60 | ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 61 | |
mbed_official | 181:a4cbdfbbd2f4 | 62 | (#) Wait mode to prevent ADC overrun in applications with low frequency. |
mbed_official | 181:a4cbdfbbd2f4 | 63 | |
mbed_official | 181:a4cbdfbbd2f4 | 64 | |
mbed_official | 181:a4cbdfbbd2f4 | 65 | ##### How to use this driver ##### |
mbed_official | 181:a4cbdfbbd2f4 | 66 | ============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 67 | [..] |
mbed_official | 181:a4cbdfbbd2f4 | 68 | |
mbed_official | 181:a4cbdfbbd2f4 | 69 | (#) Enable the ADC interface |
mbed_official | 181:a4cbdfbbd2f4 | 70 | As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured |
mbed_official | 181:a4cbdfbbd2f4 | 71 | at RCC top level. |
mbed_official | 181:a4cbdfbbd2f4 | 72 | |
mbed_official | 181:a4cbdfbbd2f4 | 73 | Depending on both possible clock sources: PCLK clock or ADC asynchronous |
mbed_official | 181:a4cbdfbbd2f4 | 74 | clock. |
mbed_official | 181:a4cbdfbbd2f4 | 75 | __ADC1_CLK_ENABLE(); |
mbed_official | 181:a4cbdfbbd2f4 | 76 | |
mbed_official | 181:a4cbdfbbd2f4 | 77 | |
mbed_official | 181:a4cbdfbbd2f4 | 78 | (#) ADC pins configuration |
mbed_official | 181:a4cbdfbbd2f4 | 79 | (++) Enable the clock for the ADC GPIOs using the following function: |
mbed_official | 181:a4cbdfbbd2f4 | 80 | __GPIOx_CLK_ENABLE(); |
mbed_official | 181:a4cbdfbbd2f4 | 81 | (++) Configure these ADC pins in analog mode using HAL_GPIO_Init(); |
mbed_official | 181:a4cbdfbbd2f4 | 82 | |
mbed_official | 181:a4cbdfbbd2f4 | 83 | (#) Configure the ADC parameters (conversion resolution, oversampler, |
mbed_official | 181:a4cbdfbbd2f4 | 84 | data alignment, continuous mode,...) using the HAL_ADC_Init() function. |
mbed_official | 181:a4cbdfbbd2f4 | 85 | |
mbed_official | 181:a4cbdfbbd2f4 | 86 | (#) Activate the ADC peripheral using one of the start functions: |
mbed_official | 181:a4cbdfbbd2f4 | 87 | HAL_ADC_Start(), HAL_ADC_Start_IT() or HAL_ADC_Start_DMA() |
mbed_official | 181:a4cbdfbbd2f4 | 88 | |
mbed_official | 181:a4cbdfbbd2f4 | 89 | *** Channels configuration *** |
mbed_official | 181:a4cbdfbbd2f4 | 90 | =============================== |
mbed_official | 181:a4cbdfbbd2f4 | 91 | [..] |
mbed_official | 181:a4cbdfbbd2f4 | 92 | (+) To configure the ADC channels group, use HAL_ADC_ConfigChannel() function. |
mbed_official | 181:a4cbdfbbd2f4 | 93 | (+) To read the ADC converted values, use the HAL_ADC_GetValue() function. |
mbed_official | 181:a4cbdfbbd2f4 | 94 | |
mbed_official | 181:a4cbdfbbd2f4 | 95 | *** DMA feature configuration *** |
mbed_official | 181:a4cbdfbbd2f4 | 96 | ================================= |
mbed_official | 181:a4cbdfbbd2f4 | 97 | [..] |
mbed_official | 181:a4cbdfbbd2f4 | 98 | (+) To enable the DMA mode, use the HAL_ADC_Start_DMA() function. |
mbed_official | 181:a4cbdfbbd2f4 | 99 | (+) To enable the generation of DMA requests continuously at the end of |
mbed_official | 181:a4cbdfbbd2f4 | 100 | the last DMA transfer, set .Init.DMAContinuousRequests to ENABLE and |
mbed_official | 181:a4cbdfbbd2f4 | 101 | call HAL_ADC_Init() function. |
mbed_official | 181:a4cbdfbbd2f4 | 102 | |
mbed_official | 181:a4cbdfbbd2f4 | 103 | |
mbed_official | 181:a4cbdfbbd2f4 | 104 | @endverbatim |
mbed_official | 181:a4cbdfbbd2f4 | 105 | ****************************************************************************** |
mbed_official | 181:a4cbdfbbd2f4 | 106 | * @attention |
mbed_official | 181:a4cbdfbbd2f4 | 107 | * |
mbed_official | 181:a4cbdfbbd2f4 | 108 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
mbed_official | 181:a4cbdfbbd2f4 | 109 | * |
mbed_official | 181:a4cbdfbbd2f4 | 110 | * Redistribution and use in source and binary forms, with or without modification, |
mbed_official | 181:a4cbdfbbd2f4 | 111 | * are permitted provided that the following conditions are met: |
mbed_official | 181:a4cbdfbbd2f4 | 112 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 181:a4cbdfbbd2f4 | 113 | * this list of conditions and the following disclaimer. |
mbed_official | 181:a4cbdfbbd2f4 | 114 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 181:a4cbdfbbd2f4 | 115 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 181:a4cbdfbbd2f4 | 116 | * and/or other materials provided with the distribution. |
mbed_official | 181:a4cbdfbbd2f4 | 117 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 181:a4cbdfbbd2f4 | 118 | * may be used to endorse or promote products derived from this software |
mbed_official | 181:a4cbdfbbd2f4 | 119 | * without specific prior written permission. |
mbed_official | 181:a4cbdfbbd2f4 | 120 | * |
mbed_official | 181:a4cbdfbbd2f4 | 121 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 181:a4cbdfbbd2f4 | 122 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 181:a4cbdfbbd2f4 | 123 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 181:a4cbdfbbd2f4 | 124 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 181:a4cbdfbbd2f4 | 125 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 181:a4cbdfbbd2f4 | 126 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 181:a4cbdfbbd2f4 | 127 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 181:a4cbdfbbd2f4 | 128 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 181:a4cbdfbbd2f4 | 129 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 181:a4cbdfbbd2f4 | 130 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 181:a4cbdfbbd2f4 | 131 | * |
mbed_official | 181:a4cbdfbbd2f4 | 132 | ****************************************************************************** |
mbed_official | 181:a4cbdfbbd2f4 | 133 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 134 | |
mbed_official | 181:a4cbdfbbd2f4 | 135 | /* Includes ------------------------------------------------------------------*/ |
mbed_official | 181:a4cbdfbbd2f4 | 136 | #include "stm32l0xx_hal.h" |
mbed_official | 181:a4cbdfbbd2f4 | 137 | |
mbed_official | 181:a4cbdfbbd2f4 | 138 | /** @addtogroup STM32L0xx_HAL_Driver |
mbed_official | 181:a4cbdfbbd2f4 | 139 | * @{ |
mbed_official | 181:a4cbdfbbd2f4 | 140 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 141 | |
mbed_official | 181:a4cbdfbbd2f4 | 142 | /** @addtogroup ADC |
mbed_official | 181:a4cbdfbbd2f4 | 143 | * @brief ADC driver modules |
mbed_official | 181:a4cbdfbbd2f4 | 144 | * @{ |
mbed_official | 181:a4cbdfbbd2f4 | 145 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 146 | |
mbed_official | 181:a4cbdfbbd2f4 | 147 | #ifdef HAL_ADC_MODULE_ENABLED |
mbed_official | 181:a4cbdfbbd2f4 | 148 | |
mbed_official | 181:a4cbdfbbd2f4 | 149 | /* Private typedef -----------------------------------------------------------*/ |
mbed_official | 181:a4cbdfbbd2f4 | 150 | /* Private define ------------------------------------------------------------*/ |
mbed_official | 181:a4cbdfbbd2f4 | 151 | /* Private macro -------------------------------------------------------------*/ |
mbed_official | 181:a4cbdfbbd2f4 | 152 | /* Private variables ---------------------------------------------------------*/ |
mbed_official | 181:a4cbdfbbd2f4 | 153 | /* Private function prototypes -----------------------------------------------*/ |
mbed_official | 181:a4cbdfbbd2f4 | 154 | static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 155 | static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 181:a4cbdfbbd2f4 | 156 | static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 181:a4cbdfbbd2f4 | 157 | static void ADC_DMAError(DMA_HandleTypeDef *hdma); |
mbed_official | 181:a4cbdfbbd2f4 | 158 | static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc, uint32_t ConversionGroup); |
mbed_official | 181:a4cbdfbbd2f4 | 159 | static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 160 | |
mbed_official | 181:a4cbdfbbd2f4 | 161 | /* Private functions ---------------------------------------------------------*/ |
mbed_official | 181:a4cbdfbbd2f4 | 162 | |
mbed_official | 181:a4cbdfbbd2f4 | 163 | /** @defgroup ADC_Private_Functions |
mbed_official | 181:a4cbdfbbd2f4 | 164 | * @{ |
mbed_official | 181:a4cbdfbbd2f4 | 165 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 166 | |
mbed_official | 181:a4cbdfbbd2f4 | 167 | /** @defgroup ADC_Group1 Initialization/de-initialization functions |
mbed_official | 181:a4cbdfbbd2f4 | 168 | * @brief Initialization and Configuration functions |
mbed_official | 181:a4cbdfbbd2f4 | 169 | * |
mbed_official | 181:a4cbdfbbd2f4 | 170 | @verbatim |
mbed_official | 181:a4cbdfbbd2f4 | 171 | =============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 172 | ##### Initialization and de-initialization functions ##### |
mbed_official | 181:a4cbdfbbd2f4 | 173 | =============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 174 | [..] This section provides functions allowing to: |
mbed_official | 181:a4cbdfbbd2f4 | 175 | (+) Initialize and configure the ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 176 | (+) De-initialize the ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 177 | |
mbed_official | 181:a4cbdfbbd2f4 | 178 | @endverbatim |
mbed_official | 181:a4cbdfbbd2f4 | 179 | * @{ |
mbed_official | 181:a4cbdfbbd2f4 | 180 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 181 | |
mbed_official | 181:a4cbdfbbd2f4 | 182 | |
mbed_official | 181:a4cbdfbbd2f4 | 183 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 184 | * @brief Initializes the ADCx peripheral according to the specified parameters |
mbed_official | 181:a4cbdfbbd2f4 | 185 | * in the ADC_InitStruct. |
mbed_official | 181:a4cbdfbbd2f4 | 186 | * @note This function is used to configure the global features of the ADC |
mbed_official | 181:a4cbdfbbd2f4 | 187 | * (ClockPrescaler, Resolution, Data Alignment and number of conversion), however, |
mbed_official | 181:a4cbdfbbd2f4 | 188 | * the rest of the configuration parameters are specific to the regular |
mbed_official | 181:a4cbdfbbd2f4 | 189 | * channels group (scan mode activation, continuous mode activation, |
mbed_official | 181:a4cbdfbbd2f4 | 190 | * External trigger source and edge, DMA continuous request after the |
mbed_official | 181:a4cbdfbbd2f4 | 191 | * last transfer and End of conversion selection). |
mbed_official | 181:a4cbdfbbd2f4 | 192 | * |
mbed_official | 181:a4cbdfbbd2f4 | 193 | * As prerequisite, into HAL_ADC_MspInit(), ADC clock must be |
mbed_official | 181:a4cbdfbbd2f4 | 194 | * configured at RCC top level. |
mbed_official | 181:a4cbdfbbd2f4 | 195 | * See commented example code below that can be copied |
mbed_official | 181:a4cbdfbbd2f4 | 196 | * and uncommented into HAL_ADC_MspInit(). |
mbed_official | 181:a4cbdfbbd2f4 | 197 | * |
mbed_official | 181:a4cbdfbbd2f4 | 198 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 199 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 200 | * @retval HAL status |
mbed_official | 181:a4cbdfbbd2f4 | 201 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 202 | HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 203 | { |
mbed_official | 181:a4cbdfbbd2f4 | 204 | uint32_t tickstart = 0x00; |
mbed_official | 181:a4cbdfbbd2f4 | 205 | |
mbed_official | 181:a4cbdfbbd2f4 | 206 | /* Check ADC handle */ |
mbed_official | 181:a4cbdfbbd2f4 | 207 | if(hadc == NULL) |
mbed_official | 181:a4cbdfbbd2f4 | 208 | { |
mbed_official | 181:a4cbdfbbd2f4 | 209 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 210 | } |
mbed_official | 181:a4cbdfbbd2f4 | 211 | |
mbed_official | 181:a4cbdfbbd2f4 | 212 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 213 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 214 | assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler)); |
mbed_official | 181:a4cbdfbbd2f4 | 215 | assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution)); |
mbed_official | 181:a4cbdfbbd2f4 | 216 | assert_param(IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTime)); |
mbed_official | 181:a4cbdfbbd2f4 | 217 | assert_param(IS_ADC_SCAN_DIRECTION(hadc->Init.ScanDirection)); |
mbed_official | 181:a4cbdfbbd2f4 | 218 | assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign)); |
mbed_official | 181:a4cbdfbbd2f4 | 219 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode)); |
mbed_official | 181:a4cbdfbbd2f4 | 220 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode)); |
mbed_official | 181:a4cbdfbbd2f4 | 221 | assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge)); |
mbed_official | 181:a4cbdfbbd2f4 | 222 | assert_param(IS_ADC_EXTERNAL_TRIG_CONV(hadc->Init.ExternalTrigConv)); |
mbed_official | 181:a4cbdfbbd2f4 | 223 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests)); |
mbed_official | 181:a4cbdfbbd2f4 | 224 | assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection)); |
mbed_official | 181:a4cbdfbbd2f4 | 225 | assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun)); |
mbed_official | 181:a4cbdfbbd2f4 | 226 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait)); |
mbed_official | 181:a4cbdfbbd2f4 | 227 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerFrequencyMode)); |
mbed_official | 181:a4cbdfbbd2f4 | 228 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoOff)); |
mbed_official | 181:a4cbdfbbd2f4 | 229 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode)); |
mbed_official | 181:a4cbdfbbd2f4 | 230 | |
mbed_official | 181:a4cbdfbbd2f4 | 231 | if(hadc->State == HAL_ADC_STATE_RESET) |
mbed_official | 181:a4cbdfbbd2f4 | 232 | { |
mbed_official | 181:a4cbdfbbd2f4 | 233 | /* Init the low level hardware */ |
mbed_official | 181:a4cbdfbbd2f4 | 234 | HAL_ADC_MspInit(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 235 | } |
mbed_official | 181:a4cbdfbbd2f4 | 236 | |
mbed_official | 181:a4cbdfbbd2f4 | 237 | /* Initialize the ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 238 | hadc->State = HAL_ADC_STATE_BUSY; |
mbed_official | 181:a4cbdfbbd2f4 | 239 | |
mbed_official | 181:a4cbdfbbd2f4 | 240 | /* Configuration of ADC clock: clock source PCLK or asynchronous with |
mbed_official | 181:a4cbdfbbd2f4 | 241 | selectable prescaler */ |
mbed_official | 181:a4cbdfbbd2f4 | 242 | __HAL_ADC_CLOCK_PRESCALER(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 243 | |
mbed_official | 181:a4cbdfbbd2f4 | 244 | /* Set the Low Frequency mode */ |
mbed_official | 181:a4cbdfbbd2f4 | 245 | ADC->CCR &= (uint32_t)~ADC_CCR_LFMEN; |
mbed_official | 181:a4cbdfbbd2f4 | 246 | ADC->CCR |=__HAL_ADC_CCR_LOWFREQUENCY(hadc->Init.LowPowerFrequencyMode); |
mbed_official | 181:a4cbdfbbd2f4 | 247 | |
mbed_official | 181:a4cbdfbbd2f4 | 248 | /* Enable voltage regulator (if disabled at this step) */ |
mbed_official | 181:a4cbdfbbd2f4 | 249 | if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN)) |
mbed_official | 181:a4cbdfbbd2f4 | 250 | { |
mbed_official | 181:a4cbdfbbd2f4 | 251 | /* Note: The software must wait for the startup time of the ADC voltage */ |
mbed_official | 181:a4cbdfbbd2f4 | 252 | /* regulator before launching a calibration or enabling the ADC. */ |
mbed_official | 181:a4cbdfbbd2f4 | 253 | /* This temporization must be implemented by software and is equal */ |
mbed_official | 181:a4cbdfbbd2f4 | 254 | /* to 10 µs in the worst case process/temperature/power supply. */ |
mbed_official | 181:a4cbdfbbd2f4 | 255 | |
mbed_official | 181:a4cbdfbbd2f4 | 256 | /* Disable the ADC (if not already disabled) */ |
mbed_official | 181:a4cbdfbbd2f4 | 257 | if (__HAL_ADC_IS_ENABLED(hadc) != RESET ) |
mbed_official | 181:a4cbdfbbd2f4 | 258 | { |
mbed_official | 181:a4cbdfbbd2f4 | 259 | /* Check if conditions to disable the ADC are fulfilled */ |
mbed_official | 181:a4cbdfbbd2f4 | 260 | if (__HAL_ADC_DISABLING_CONDITIONS(hadc) != RESET) |
mbed_official | 181:a4cbdfbbd2f4 | 261 | { |
mbed_official | 181:a4cbdfbbd2f4 | 262 | __HAL_ADC_DISABLE(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 263 | } |
mbed_official | 181:a4cbdfbbd2f4 | 264 | else |
mbed_official | 181:a4cbdfbbd2f4 | 265 | { |
mbed_official | 181:a4cbdfbbd2f4 | 266 | hadc->State= HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 267 | |
mbed_official | 181:a4cbdfbbd2f4 | 268 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 269 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 270 | |
mbed_official | 181:a4cbdfbbd2f4 | 271 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 272 | } |
mbed_official | 181:a4cbdfbbd2f4 | 273 | |
mbed_official | 181:a4cbdfbbd2f4 | 274 | /* Get timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 275 | tickstart = HAL_GetTick(); |
mbed_official | 181:a4cbdfbbd2f4 | 276 | |
mbed_official | 181:a4cbdfbbd2f4 | 277 | /* Wait for disabling completion */ |
mbed_official | 181:a4cbdfbbd2f4 | 278 | while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN)) |
mbed_official | 181:a4cbdfbbd2f4 | 279 | { |
mbed_official | 181:a4cbdfbbd2f4 | 280 | /* Check for the Timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 281 | if(ADC_ENABLE_TIMEOUT != HAL_MAX_DELAY) |
mbed_official | 181:a4cbdfbbd2f4 | 282 | { |
mbed_official | 181:a4cbdfbbd2f4 | 283 | if((HAL_GetTick() - tickstart ) > ADC_DISABLE_TIMEOUT) |
mbed_official | 181:a4cbdfbbd2f4 | 284 | { |
mbed_official | 181:a4cbdfbbd2f4 | 285 | hadc->State= HAL_ADC_STATE_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 286 | |
mbed_official | 181:a4cbdfbbd2f4 | 287 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 288 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 289 | |
mbed_official | 181:a4cbdfbbd2f4 | 290 | return HAL_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 291 | } |
mbed_official | 181:a4cbdfbbd2f4 | 292 | } |
mbed_official | 181:a4cbdfbbd2f4 | 293 | } |
mbed_official | 181:a4cbdfbbd2f4 | 294 | |
mbed_official | 181:a4cbdfbbd2f4 | 295 | } |
mbed_official | 181:a4cbdfbbd2f4 | 296 | |
mbed_official | 181:a4cbdfbbd2f4 | 297 | /* Set ADVREGEN bit */ |
mbed_official | 181:a4cbdfbbd2f4 | 298 | hadc->Instance->CR |= ADC_CR_ADVREGEN; |
mbed_official | 181:a4cbdfbbd2f4 | 299 | /* Delay of 10 µs minimum (value from design, cf reference manual) */ |
mbed_official | 181:a4cbdfbbd2f4 | 300 | /* Delay fixed to worst case: maximum CPU frequency */ |
mbed_official | 181:a4cbdfbbd2f4 | 301 | HAL_Delay(10); |
mbed_official | 181:a4cbdfbbd2f4 | 302 | } |
mbed_official | 181:a4cbdfbbd2f4 | 303 | |
mbed_official | 181:a4cbdfbbd2f4 | 304 | /* Configuration of ADC: */ |
mbed_official | 181:a4cbdfbbd2f4 | 305 | /* - Resolution */ |
mbed_official | 181:a4cbdfbbd2f4 | 306 | /* - Data alignment */ |
mbed_official | 181:a4cbdfbbd2f4 | 307 | /* - Scan directio */ |
mbed_official | 181:a4cbdfbbd2f4 | 308 | /* - External trigger to start conversion */ |
mbed_official | 181:a4cbdfbbd2f4 | 309 | /* - External trigger polarity */ |
mbed_official | 181:a4cbdfbbd2f4 | 310 | /* - Continuous conversion mode */ |
mbed_official | 181:a4cbdfbbd2f4 | 311 | /* - DMA continuous request */ |
mbed_official | 181:a4cbdfbbd2f4 | 312 | /* - Overrun */ |
mbed_official | 181:a4cbdfbbd2f4 | 313 | /* - AutoDelay feature */ |
mbed_official | 181:a4cbdfbbd2f4 | 314 | /* - Discontinuous mode */ |
mbed_official | 181:a4cbdfbbd2f4 | 315 | hadc->Instance->CFGR1 &= ~( ADC_CFGR1_RES | |
mbed_official | 181:a4cbdfbbd2f4 | 316 | ADC_CFGR1_ALIGN | |
mbed_official | 181:a4cbdfbbd2f4 | 317 | ADC_CFGR1_SCANDIR | |
mbed_official | 181:a4cbdfbbd2f4 | 318 | ADC_CFGR1_EXTSEL | |
mbed_official | 181:a4cbdfbbd2f4 | 319 | ADC_CFGR1_EXTEN | |
mbed_official | 181:a4cbdfbbd2f4 | 320 | ADC_CFGR1_CONT | |
mbed_official | 181:a4cbdfbbd2f4 | 321 | ADC_CFGR1_DMACFG | |
mbed_official | 181:a4cbdfbbd2f4 | 322 | ADC_CFGR1_OVRMOD | |
mbed_official | 181:a4cbdfbbd2f4 | 323 | ADC_CFGR1_AUTDLY | |
mbed_official | 181:a4cbdfbbd2f4 | 324 | ADC_CFGR1_AUTOFF | |
mbed_official | 181:a4cbdfbbd2f4 | 325 | ADC_CFGR1_DISCEN); |
mbed_official | 181:a4cbdfbbd2f4 | 326 | |
mbed_official | 181:a4cbdfbbd2f4 | 327 | hadc->Instance->CFGR1 |= ( hadc->Init.Resolution | |
mbed_official | 181:a4cbdfbbd2f4 | 328 | hadc->Init.DataAlign | |
mbed_official | 181:a4cbdfbbd2f4 | 329 | hadc->Init.ScanDirection | |
mbed_official | 181:a4cbdfbbd2f4 | 330 | hadc->Init.ExternalTrigConvEdge | |
mbed_official | 181:a4cbdfbbd2f4 | 331 | __HAL_ADC_CFGR1_CONTINUOUS(hadc->Init.ContinuousConvMode) | |
mbed_official | 181:a4cbdfbbd2f4 | 332 | __HAL_ADC_CFGR1_DMAContReq(hadc->Init.DMAContinuousRequests) | |
mbed_official | 181:a4cbdfbbd2f4 | 333 | hadc->Init.Overrun | |
mbed_official | 181:a4cbdfbbd2f4 | 334 | __HAL_ADC_CFGR1_AutoDelay(hadc->Init.LowPowerAutoWait) | |
mbed_official | 181:a4cbdfbbd2f4 | 335 | __HAL_ADC_CFGR1_AUTOFF(hadc->Init.LowPowerAutoOff)); |
mbed_official | 181:a4cbdfbbd2f4 | 336 | |
mbed_official | 181:a4cbdfbbd2f4 | 337 | /* Configure the external trigger only if Conversion edge is not "NONE" */ |
mbed_official | 181:a4cbdfbbd2f4 | 338 | if (hadc->Init.ExternalTrigConvEdge != ADC_EXTERNALTRIG_EDGE_NONE) |
mbed_official | 181:a4cbdfbbd2f4 | 339 | { |
mbed_official | 181:a4cbdfbbd2f4 | 340 | hadc->Instance->CFGR1 |= hadc->Init.ExternalTrigConv; |
mbed_official | 181:a4cbdfbbd2f4 | 341 | } |
mbed_official | 181:a4cbdfbbd2f4 | 342 | |
mbed_official | 181:a4cbdfbbd2f4 | 343 | /* Enable discontinuous mode only if continuous mode is disabled */ |
mbed_official | 181:a4cbdfbbd2f4 | 344 | if ((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == DISABLE)) |
mbed_official | 181:a4cbdfbbd2f4 | 345 | { |
mbed_official | 181:a4cbdfbbd2f4 | 346 | /* Enable the selected ADC discontinuous mode */ |
mbed_official | 181:a4cbdfbbd2f4 | 347 | hadc->Instance->CFGR1 |= ( ADC_CFGR1_DISCEN); |
mbed_official | 181:a4cbdfbbd2f4 | 348 | } |
mbed_official | 181:a4cbdfbbd2f4 | 349 | |
mbed_official | 181:a4cbdfbbd2f4 | 350 | if (hadc->Init.OversamplingMode == ENABLE) |
mbed_official | 181:a4cbdfbbd2f4 | 351 | { |
mbed_official | 181:a4cbdfbbd2f4 | 352 | assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversample.Ratio)); |
mbed_official | 181:a4cbdfbbd2f4 | 353 | assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversample.RightBitShift)); |
mbed_official | 181:a4cbdfbbd2f4 | 354 | assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversample.TriggeredMode)); |
mbed_official | 181:a4cbdfbbd2f4 | 355 | |
mbed_official | 181:a4cbdfbbd2f4 | 356 | /* Configuration of Oversampler: */ |
mbed_official | 181:a4cbdfbbd2f4 | 357 | /* - Oversampling Ratio */ |
mbed_official | 181:a4cbdfbbd2f4 | 358 | /* - Right bit shift */ |
mbed_official | 181:a4cbdfbbd2f4 | 359 | /* - Triggered mode */ |
mbed_official | 181:a4cbdfbbd2f4 | 360 | |
mbed_official | 181:a4cbdfbbd2f4 | 361 | hadc->Instance->CFGR2 &= ~( ADC_CFGR2_OVSR | |
mbed_official | 181:a4cbdfbbd2f4 | 362 | ADC_CFGR2_OVSS | |
mbed_official | 181:a4cbdfbbd2f4 | 363 | ADC_CFGR2_TOVS ); |
mbed_official | 181:a4cbdfbbd2f4 | 364 | |
mbed_official | 181:a4cbdfbbd2f4 | 365 | hadc->Instance->CFGR2 |= ( hadc->Init.Oversample.Ratio | |
mbed_official | 181:a4cbdfbbd2f4 | 366 | hadc->Init.Oversample.RightBitShift | |
mbed_official | 181:a4cbdfbbd2f4 | 367 | hadc->Init.Oversample.TriggeredMode ); |
mbed_official | 181:a4cbdfbbd2f4 | 368 | |
mbed_official | 181:a4cbdfbbd2f4 | 369 | /* Enable OverSampling mode */ |
mbed_official | 181:a4cbdfbbd2f4 | 370 | hadc->Instance->CFGR2 |= ADC_CFGR2_OVSE; |
mbed_official | 181:a4cbdfbbd2f4 | 371 | } |
mbed_official | 181:a4cbdfbbd2f4 | 372 | else |
mbed_official | 181:a4cbdfbbd2f4 | 373 | { |
mbed_official | 181:a4cbdfbbd2f4 | 374 | /* Disable OverSampling mode */ |
mbed_official | 181:a4cbdfbbd2f4 | 375 | hadc->Instance->CFGR2 &= ~ADC_CFGR2_OVSE; |
mbed_official | 181:a4cbdfbbd2f4 | 376 | } |
mbed_official | 181:a4cbdfbbd2f4 | 377 | |
mbed_official | 181:a4cbdfbbd2f4 | 378 | /* Clear the old sampling time */ |
mbed_official | 181:a4cbdfbbd2f4 | 379 | hadc->Instance->SMPR &= (uint32_t)(~ADC_SMPR_SMPR); |
mbed_official | 181:a4cbdfbbd2f4 | 380 | |
mbed_official | 181:a4cbdfbbd2f4 | 381 | /* Set the new sample time */ |
mbed_official | 181:a4cbdfbbd2f4 | 382 | hadc->Instance->SMPR |= hadc->Init.SamplingTime; |
mbed_official | 181:a4cbdfbbd2f4 | 383 | |
mbed_official | 181:a4cbdfbbd2f4 | 384 | |
mbed_official | 181:a4cbdfbbd2f4 | 385 | /* Set ADC error code to none */ |
mbed_official | 181:a4cbdfbbd2f4 | 386 | hadc->ErrorCode = HAL_ADC_ERROR_NONE; |
mbed_official | 181:a4cbdfbbd2f4 | 387 | |
mbed_official | 181:a4cbdfbbd2f4 | 388 | /* Initialize the ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 389 | hadc->State = HAL_ADC_STATE_READY; |
mbed_official | 181:a4cbdfbbd2f4 | 390 | |
mbed_official | 181:a4cbdfbbd2f4 | 391 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 392 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 393 | } |
mbed_official | 181:a4cbdfbbd2f4 | 394 | |
mbed_official | 181:a4cbdfbbd2f4 | 395 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 396 | * @brief Deinitialize the ADC peripheral registers to its default reset values. |
mbed_official | 181:a4cbdfbbd2f4 | 397 | * @note To not impact other ADCs, reset of common ADC registers have been |
mbed_official | 181:a4cbdfbbd2f4 | 398 | * left commented below. |
mbed_official | 181:a4cbdfbbd2f4 | 399 | * If needed, the example code can be copied and uncommented into |
mbed_official | 181:a4cbdfbbd2f4 | 400 | * function HAL_ADC_MspDeInit(). |
mbed_official | 181:a4cbdfbbd2f4 | 401 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 402 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 403 | * @retval HAL status |
mbed_official | 181:a4cbdfbbd2f4 | 404 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 405 | HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 406 | { |
mbed_official | 181:a4cbdfbbd2f4 | 407 | uint32_t tickstart = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 408 | |
mbed_official | 181:a4cbdfbbd2f4 | 409 | /* Check ADC handle */ |
mbed_official | 181:a4cbdfbbd2f4 | 410 | if(hadc == NULL) |
mbed_official | 181:a4cbdfbbd2f4 | 411 | { |
mbed_official | 181:a4cbdfbbd2f4 | 412 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 413 | } |
mbed_official | 181:a4cbdfbbd2f4 | 414 | |
mbed_official | 181:a4cbdfbbd2f4 | 415 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 416 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 417 | |
mbed_official | 181:a4cbdfbbd2f4 | 418 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 419 | hadc->State = HAL_ADC_STATE_BUSY; |
mbed_official | 181:a4cbdfbbd2f4 | 420 | |
mbed_official | 181:a4cbdfbbd2f4 | 421 | /* Stop potential conversion ongoing */ |
mbed_official | 181:a4cbdfbbd2f4 | 422 | if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) && HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS)) |
mbed_official | 181:a4cbdfbbd2f4 | 423 | { |
mbed_official | 181:a4cbdfbbd2f4 | 424 | /* Stop regular conversion */ |
mbed_official | 181:a4cbdfbbd2f4 | 425 | hadc->Instance->CR |= ADC_CR_ADSTP; |
mbed_official | 181:a4cbdfbbd2f4 | 426 | } |
mbed_official | 181:a4cbdfbbd2f4 | 427 | |
mbed_official | 181:a4cbdfbbd2f4 | 428 | /* Disable ADC: Solution to recover from an unknown ADC state (for example, */ |
mbed_official | 181:a4cbdfbbd2f4 | 429 | /* in case of forbidden action on register bits) */ |
mbed_official | 181:a4cbdfbbd2f4 | 430 | /* Procedure to disable the ADC peripheral: wait for conversions */ |
mbed_official | 181:a4cbdfbbd2f4 | 431 | /* effectively stopped, then disable ADC */ |
mbed_official | 181:a4cbdfbbd2f4 | 432 | /* 1. Wait until ADSTART = 0 */ |
mbed_official | 181:a4cbdfbbd2f4 | 433 | |
mbed_official | 181:a4cbdfbbd2f4 | 434 | /* Get timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 435 | tickstart = HAL_GetTick(); |
mbed_official | 181:a4cbdfbbd2f4 | 436 | |
mbed_official | 181:a4cbdfbbd2f4 | 437 | while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART)) |
mbed_official | 181:a4cbdfbbd2f4 | 438 | { |
mbed_official | 181:a4cbdfbbd2f4 | 439 | /* Check for the Timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 440 | if(ADC_STOP_CONVERSION_TIMEOUT != HAL_MAX_DELAY) |
mbed_official | 181:a4cbdfbbd2f4 | 441 | { |
mbed_official | 181:a4cbdfbbd2f4 | 442 | if((HAL_GetTick() - tickstart ) > ADC_STOP_CONVERSION_TIMEOUT) |
mbed_official | 181:a4cbdfbbd2f4 | 443 | { |
mbed_official | 181:a4cbdfbbd2f4 | 444 | hadc->State= HAL_ADC_STATE_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 445 | |
mbed_official | 181:a4cbdfbbd2f4 | 446 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 447 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 448 | |
mbed_official | 181:a4cbdfbbd2f4 | 449 | return HAL_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 450 | } |
mbed_official | 181:a4cbdfbbd2f4 | 451 | } |
mbed_official | 181:a4cbdfbbd2f4 | 452 | } |
mbed_official | 181:a4cbdfbbd2f4 | 453 | |
mbed_official | 181:a4cbdfbbd2f4 | 454 | /* 2. Disable the ADC peripheral */ |
mbed_official | 181:a4cbdfbbd2f4 | 455 | __HAL_ADC_DISABLE(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 456 | |
mbed_official | 181:a4cbdfbbd2f4 | 457 | |
mbed_official | 181:a4cbdfbbd2f4 | 458 | /* Reset ADC registers****************/ |
mbed_official | 181:a4cbdfbbd2f4 | 459 | /* Reset register IER */ |
mbed_official | 181:a4cbdfbbd2f4 | 460 | __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD | ADC_IT_OVR | ADC_IT_EOCAL | ADC_IT_EOS | \ |
mbed_official | 181:a4cbdfbbd2f4 | 461 | ADC_IT_EOC | ADC_IT_RDY | ADC_IT_EOSMP )); |
mbed_official | 181:a4cbdfbbd2f4 | 462 | |
mbed_official | 181:a4cbdfbbd2f4 | 463 | /* Reset register ISR */ |
mbed_official | 181:a4cbdfbbd2f4 | 464 | __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_EOCAL | ADC_FLAG_OVR | ADC_FLAG_EOS | \ |
mbed_official | 181:a4cbdfbbd2f4 | 465 | ADC_FLAG_EOC | ADC_FLAG_EOSMP | ADC_FLAG_RDY)); |
mbed_official | 181:a4cbdfbbd2f4 | 466 | |
mbed_official | 181:a4cbdfbbd2f4 | 467 | /* Reset register CR */ |
mbed_official | 181:a4cbdfbbd2f4 | 468 | /* Disable voltage regulator */ |
mbed_official | 181:a4cbdfbbd2f4 | 469 | /* Note: Regulator disable useful for power saving */ |
mbed_official | 181:a4cbdfbbd2f4 | 470 | /* Reset ADVREGEN bit */ |
mbed_official | 181:a4cbdfbbd2f4 | 471 | hadc->Instance->CR &= ~ADC_CR_ADVREGEN; |
mbed_official | 181:a4cbdfbbd2f4 | 472 | |
mbed_official | 181:a4cbdfbbd2f4 | 473 | /* Bits ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode "read-set": no direct reset applicable */ |
mbed_official | 181:a4cbdfbbd2f4 | 474 | /* No action */ |
mbed_official | 181:a4cbdfbbd2f4 | 475 | |
mbed_official | 181:a4cbdfbbd2f4 | 476 | /* Reset register CFGR1 */ |
mbed_official | 181:a4cbdfbbd2f4 | 477 | hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | \ |
mbed_official | 181:a4cbdfbbd2f4 | 478 | ADC_CFGR1_DISCEN | ADC_CFGR1_AUTOFF | ADC_CFGR1_AUTDLY | \ |
mbed_official | 181:a4cbdfbbd2f4 | 479 | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD | ADC_CFGR1_EXTEN | \ |
mbed_official | 181:a4cbdfbbd2f4 | 480 | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RES | \ |
mbed_official | 181:a4cbdfbbd2f4 | 481 | ADC_CFGR1_SCANDIR| ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN); |
mbed_official | 181:a4cbdfbbd2f4 | 482 | |
mbed_official | 181:a4cbdfbbd2f4 | 483 | /* Reset register CFGR2 */ |
mbed_official | 181:a4cbdfbbd2f4 | 484 | hadc->Instance->CFGR2 &= ~(ADC_CFGR2_TOVS | ADC_CFGR2_OVSS | ADC_CFGR2_OVSR | \ |
mbed_official | 181:a4cbdfbbd2f4 | 485 | ADC_CFGR2_OVSE | ADC_CFGR2_CKMODE ); |
mbed_official | 181:a4cbdfbbd2f4 | 486 | |
mbed_official | 181:a4cbdfbbd2f4 | 487 | /* Reset register SMPR */ |
mbed_official | 181:a4cbdfbbd2f4 | 488 | hadc->Instance->SMPR &= ~(ADC_SMPR_SMPR); |
mbed_official | 181:a4cbdfbbd2f4 | 489 | |
mbed_official | 181:a4cbdfbbd2f4 | 490 | /* Reset register TR */ |
mbed_official | 181:a4cbdfbbd2f4 | 491 | hadc->Instance->TR &= ~(ADC_TR_LT | ADC_TR_HT); |
mbed_official | 181:a4cbdfbbd2f4 | 492 | |
mbed_official | 181:a4cbdfbbd2f4 | 493 | /* Reset register CALFACT */ |
mbed_official | 181:a4cbdfbbd2f4 | 494 | hadc->Instance->CALFACT &= ~(ADC_CALFACT_CALFACT); |
mbed_official | 181:a4cbdfbbd2f4 | 495 | |
mbed_official | 181:a4cbdfbbd2f4 | 496 | /* Reset register DR */ |
mbed_official | 181:a4cbdfbbd2f4 | 497 | /* bits in access mode read only, no direct reset applicable*/ |
mbed_official | 181:a4cbdfbbd2f4 | 498 | |
mbed_official | 181:a4cbdfbbd2f4 | 499 | /* Reset register CALFACT */ |
mbed_official | 181:a4cbdfbbd2f4 | 500 | hadc->Instance->CALFACT &= ~(ADC_CALFACT_CALFACT); |
mbed_official | 181:a4cbdfbbd2f4 | 501 | |
mbed_official | 181:a4cbdfbbd2f4 | 502 | |
mbed_official | 181:a4cbdfbbd2f4 | 503 | /* DeInit the low level hardware */ |
mbed_official | 181:a4cbdfbbd2f4 | 504 | HAL_ADC_MspDeInit(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 505 | |
mbed_official | 181:a4cbdfbbd2f4 | 506 | /* Set ADC error code to none */ |
mbed_official | 181:a4cbdfbbd2f4 | 507 | hadc->ErrorCode = HAL_ADC_ERROR_NONE; |
mbed_official | 181:a4cbdfbbd2f4 | 508 | |
mbed_official | 181:a4cbdfbbd2f4 | 509 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 510 | hadc->State = HAL_ADC_STATE_RESET; |
mbed_official | 181:a4cbdfbbd2f4 | 511 | |
mbed_official | 181:a4cbdfbbd2f4 | 512 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 513 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 514 | } |
mbed_official | 181:a4cbdfbbd2f4 | 515 | |
mbed_official | 181:a4cbdfbbd2f4 | 516 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 517 | * @brief Initializes the ADC MSP. |
mbed_official | 181:a4cbdfbbd2f4 | 518 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 519 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 520 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 521 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 522 | __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 523 | { |
mbed_official | 181:a4cbdfbbd2f4 | 524 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 181:a4cbdfbbd2f4 | 525 | the HAL_ADC_MspInit could be implemented in the user file |
mbed_official | 181:a4cbdfbbd2f4 | 526 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 527 | } |
mbed_official | 181:a4cbdfbbd2f4 | 528 | |
mbed_official | 181:a4cbdfbbd2f4 | 529 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 530 | * @brief DeInitializes the ADC MSP. |
mbed_official | 181:a4cbdfbbd2f4 | 531 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 532 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 533 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 534 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 535 | __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 536 | { |
mbed_official | 181:a4cbdfbbd2f4 | 537 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 181:a4cbdfbbd2f4 | 538 | the HAL_ADC_MspDeInit could be implemented in the user file |
mbed_official | 181:a4cbdfbbd2f4 | 539 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 540 | } |
mbed_official | 181:a4cbdfbbd2f4 | 541 | |
mbed_official | 181:a4cbdfbbd2f4 | 542 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 543 | * @} |
mbed_official | 181:a4cbdfbbd2f4 | 544 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 545 | |
mbed_official | 181:a4cbdfbbd2f4 | 546 | /** @defgroup ADC_Group2 I/O operation functions |
mbed_official | 181:a4cbdfbbd2f4 | 547 | * @brief I/O operation functions |
mbed_official | 181:a4cbdfbbd2f4 | 548 | * |
mbed_official | 181:a4cbdfbbd2f4 | 549 | @verbatim |
mbed_official | 181:a4cbdfbbd2f4 | 550 | =============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 551 | ##### IO operation functions ##### |
mbed_official | 181:a4cbdfbbd2f4 | 552 | =============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 553 | [..] This section provides functions allowing to: |
mbed_official | 181:a4cbdfbbd2f4 | 554 | (+) Start conversion. |
mbed_official | 181:a4cbdfbbd2f4 | 555 | (+) Stop conversion. |
mbed_official | 181:a4cbdfbbd2f4 | 556 | (+) poll for conversion complete. |
mbed_official | 181:a4cbdfbbd2f4 | 557 | (+) poll for conversion event. |
mbed_official | 181:a4cbdfbbd2f4 | 558 | (+) Start conversion and enable interrupt. |
mbed_official | 181:a4cbdfbbd2f4 | 559 | (+) Stop conversion and disable interrupt. |
mbed_official | 181:a4cbdfbbd2f4 | 560 | (+) handle ADC interrupt request. |
mbed_official | 181:a4cbdfbbd2f4 | 561 | (+) Start conversion of regular channel and enable DMA transfer. |
mbed_official | 181:a4cbdfbbd2f4 | 562 | (+) Stop conversion of regular channel and disable DMA transfer. |
mbed_official | 181:a4cbdfbbd2f4 | 563 | (+) Get result of regular channel conversion. |
mbed_official | 181:a4cbdfbbd2f4 | 564 | (+) Handle ADC interrupt request. |
mbed_official | 181:a4cbdfbbd2f4 | 565 | |
mbed_official | 181:a4cbdfbbd2f4 | 566 | @endverbatim |
mbed_official | 181:a4cbdfbbd2f4 | 567 | * @{ |
mbed_official | 181:a4cbdfbbd2f4 | 568 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 569 | |
mbed_official | 181:a4cbdfbbd2f4 | 570 | |
mbed_official | 181:a4cbdfbbd2f4 | 571 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 572 | * @brief Enables ADC and starts conversion of the regular channels. |
mbed_official | 181:a4cbdfbbd2f4 | 573 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 574 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 575 | * @retval HAL status |
mbed_official | 181:a4cbdfbbd2f4 | 576 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 577 | HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 578 | { |
mbed_official | 181:a4cbdfbbd2f4 | 579 | HAL_StatusTypeDef tmpHALStatus = HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 580 | |
mbed_official | 181:a4cbdfbbd2f4 | 581 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 582 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 583 | |
mbed_official | 181:a4cbdfbbd2f4 | 584 | /* Process locked */ |
mbed_official | 181:a4cbdfbbd2f4 | 585 | __HAL_LOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 586 | |
mbed_official | 181:a4cbdfbbd2f4 | 587 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 588 | hadc->State = HAL_ADC_STATE_BUSY_REG; |
mbed_official | 181:a4cbdfbbd2f4 | 589 | |
mbed_official | 181:a4cbdfbbd2f4 | 590 | /* Set ADC error code to none */ |
mbed_official | 181:a4cbdfbbd2f4 | 591 | hadc->ErrorCode = HAL_ADC_ERROR_NONE; |
mbed_official | 181:a4cbdfbbd2f4 | 592 | |
mbed_official | 181:a4cbdfbbd2f4 | 593 | /* Enable ADC */ |
mbed_official | 181:a4cbdfbbd2f4 | 594 | tmpHALStatus = ADC_Enable(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 595 | |
mbed_official | 181:a4cbdfbbd2f4 | 596 | /* Start conversion if ADC is effectively enabled */ |
mbed_official | 181:a4cbdfbbd2f4 | 597 | if (tmpHALStatus != HAL_ERROR) |
mbed_official | 181:a4cbdfbbd2f4 | 598 | { |
mbed_official | 181:a4cbdfbbd2f4 | 599 | /* ADC start conversion command */ |
mbed_official | 181:a4cbdfbbd2f4 | 600 | hadc->Instance->CR |= ADC_CR_ADSTART; |
mbed_official | 181:a4cbdfbbd2f4 | 601 | } |
mbed_official | 181:a4cbdfbbd2f4 | 602 | |
mbed_official | 181:a4cbdfbbd2f4 | 603 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 604 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 605 | |
mbed_official | 181:a4cbdfbbd2f4 | 606 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 607 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 608 | } |
mbed_official | 181:a4cbdfbbd2f4 | 609 | |
mbed_official | 181:a4cbdfbbd2f4 | 610 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 611 | * @brief Stop ADC conversion of regular channels, disable ADC peripheral. |
mbed_official | 181:a4cbdfbbd2f4 | 612 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 613 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 614 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 615 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 616 | HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 617 | { |
mbed_official | 181:a4cbdfbbd2f4 | 618 | HAL_StatusTypeDef tmpHALStatus = HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 619 | |
mbed_official | 181:a4cbdfbbd2f4 | 620 | /* Process locked */ |
mbed_official | 181:a4cbdfbbd2f4 | 621 | __HAL_LOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 622 | |
mbed_official | 181:a4cbdfbbd2f4 | 623 | /* 1. Stop potential conversion ongoing (regular conversion) */ |
mbed_official | 181:a4cbdfbbd2f4 | 624 | tmpHALStatus = ADC_ConversionStop(hadc, REGULAR_GROUP); |
mbed_official | 181:a4cbdfbbd2f4 | 625 | |
mbed_official | 181:a4cbdfbbd2f4 | 626 | /* 2. Disable ADC peripheral if conversions are effectively stopped */ |
mbed_official | 181:a4cbdfbbd2f4 | 627 | if (tmpHALStatus != HAL_ERROR) |
mbed_official | 181:a4cbdfbbd2f4 | 628 | { |
mbed_official | 181:a4cbdfbbd2f4 | 629 | /* Disable the ADC peripheral */ |
mbed_official | 181:a4cbdfbbd2f4 | 630 | ADC_Disable(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 631 | |
mbed_official | 181:a4cbdfbbd2f4 | 632 | /* Check if ADC is effectively disabled */ |
mbed_official | 181:a4cbdfbbd2f4 | 633 | if (hadc->State != HAL_ADC_STATE_ERROR) |
mbed_official | 181:a4cbdfbbd2f4 | 634 | { |
mbed_official | 181:a4cbdfbbd2f4 | 635 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 636 | hadc->State = HAL_ADC_STATE_READY; |
mbed_official | 181:a4cbdfbbd2f4 | 637 | } |
mbed_official | 181:a4cbdfbbd2f4 | 638 | else |
mbed_official | 181:a4cbdfbbd2f4 | 639 | { |
mbed_official | 181:a4cbdfbbd2f4 | 640 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 641 | } |
mbed_official | 181:a4cbdfbbd2f4 | 642 | } |
mbed_official | 181:a4cbdfbbd2f4 | 643 | else |
mbed_official | 181:a4cbdfbbd2f4 | 644 | { |
mbed_official | 181:a4cbdfbbd2f4 | 645 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 646 | } |
mbed_official | 181:a4cbdfbbd2f4 | 647 | |
mbed_official | 181:a4cbdfbbd2f4 | 648 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 649 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 650 | |
mbed_official | 181:a4cbdfbbd2f4 | 651 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 652 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 653 | } |
mbed_official | 181:a4cbdfbbd2f4 | 654 | |
mbed_official | 181:a4cbdfbbd2f4 | 655 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 656 | * @brief Poll for conversion complete. |
mbed_official | 181:a4cbdfbbd2f4 | 657 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 658 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 659 | * @param Timeout: Timeout value in millisecond. |
mbed_official | 181:a4cbdfbbd2f4 | 660 | * @retval HAL status |
mbed_official | 181:a4cbdfbbd2f4 | 661 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 662 | HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout) |
mbed_official | 181:a4cbdfbbd2f4 | 663 | { |
mbed_official | 181:a4cbdfbbd2f4 | 664 | uint32_t tickstart = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 665 | uint32_t tmp_Flag_EOC; |
mbed_official | 181:a4cbdfbbd2f4 | 666 | |
mbed_official | 181:a4cbdfbbd2f4 | 667 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 668 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 669 | |
mbed_official | 181:a4cbdfbbd2f4 | 670 | /* If interruption after each sequence */ |
mbed_official | 181:a4cbdfbbd2f4 | 671 | if (hadc->Init.EOCSelection == EOC_SEQ_CONV) |
mbed_official | 181:a4cbdfbbd2f4 | 672 | { |
mbed_official | 181:a4cbdfbbd2f4 | 673 | tmp_Flag_EOC = ADC_FLAG_EOS; |
mbed_official | 181:a4cbdfbbd2f4 | 674 | } |
mbed_official | 181:a4cbdfbbd2f4 | 675 | /* If interruption after each conversion */ |
mbed_official | 181:a4cbdfbbd2f4 | 676 | else /* EOC_SINGLE_CONV */ |
mbed_official | 181:a4cbdfbbd2f4 | 677 | { |
mbed_official | 181:a4cbdfbbd2f4 | 678 | tmp_Flag_EOC = (ADC_FLAG_EOC | ADC_FLAG_EOS); |
mbed_official | 181:a4cbdfbbd2f4 | 679 | } |
mbed_official | 181:a4cbdfbbd2f4 | 680 | |
mbed_official | 181:a4cbdfbbd2f4 | 681 | /* Get timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 682 | tickstart = HAL_GetTick(); |
mbed_official | 181:a4cbdfbbd2f4 | 683 | |
mbed_official | 181:a4cbdfbbd2f4 | 684 | /* Wait until End of Conversion flag is raised */ |
mbed_official | 181:a4cbdfbbd2f4 | 685 | while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC)) |
mbed_official | 181:a4cbdfbbd2f4 | 686 | { |
mbed_official | 181:a4cbdfbbd2f4 | 687 | /* Check for the Timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 688 | if(Timeout != HAL_MAX_DELAY) |
mbed_official | 181:a4cbdfbbd2f4 | 689 | { |
mbed_official | 181:a4cbdfbbd2f4 | 690 | if((int32_t) (HAL_GetTick() - tickstart ) > Timeout) |
mbed_official | 181:a4cbdfbbd2f4 | 691 | { |
mbed_official | 181:a4cbdfbbd2f4 | 692 | hadc->State= HAL_ADC_STATE_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 693 | |
mbed_official | 181:a4cbdfbbd2f4 | 694 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 695 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 696 | |
mbed_official | 181:a4cbdfbbd2f4 | 697 | return HAL_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 698 | } |
mbed_official | 181:a4cbdfbbd2f4 | 699 | } |
mbed_official | 181:a4cbdfbbd2f4 | 700 | } |
mbed_official | 181:a4cbdfbbd2f4 | 701 | |
mbed_official | 181:a4cbdfbbd2f4 | 702 | /* Clear end of conversion flag of regular group if low power feature */ |
mbed_official | 181:a4cbdfbbd2f4 | 703 | /* "LowPowerAutoWait " is disabled, to not interfere with this feature */ |
mbed_official | 181:a4cbdfbbd2f4 | 704 | /* until data register is read using function HAL_ADC_GetValue(). */ |
mbed_official | 181:a4cbdfbbd2f4 | 705 | if (hadc->Init.LowPowerAutoWait == DISABLE) |
mbed_official | 181:a4cbdfbbd2f4 | 706 | { |
mbed_official | 181:a4cbdfbbd2f4 | 707 | /* Clear regular group conversion flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 708 | __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS)); |
mbed_official | 181:a4cbdfbbd2f4 | 709 | } |
mbed_official | 181:a4cbdfbbd2f4 | 710 | |
mbed_official | 181:a4cbdfbbd2f4 | 711 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 712 | hadc->State = HAL_ADC_STATE_EOC; |
mbed_official | 181:a4cbdfbbd2f4 | 713 | |
mbed_official | 181:a4cbdfbbd2f4 | 714 | /* Return ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 715 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 716 | } |
mbed_official | 181:a4cbdfbbd2f4 | 717 | |
mbed_official | 181:a4cbdfbbd2f4 | 718 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 719 | * @brief Poll for conversion event. |
mbed_official | 181:a4cbdfbbd2f4 | 720 | * @param hadc: ADC handle. |
mbed_official | 181:a4cbdfbbd2f4 | 721 | * @param EventType: the ADC event type. |
mbed_official | 181:a4cbdfbbd2f4 | 722 | * This parameter can be one of the following values: |
mbed_official | 181:a4cbdfbbd2f4 | 723 | * @arg AWD_EVENT: ADC Analog watchdog event. |
mbed_official | 181:a4cbdfbbd2f4 | 724 | * @arg OVR_EVENT: ADC Overrun event. |
mbed_official | 181:a4cbdfbbd2f4 | 725 | * @param Timeout: Timeout value in millisecond. |
mbed_official | 181:a4cbdfbbd2f4 | 726 | * @retval HAL status |
mbed_official | 181:a4cbdfbbd2f4 | 727 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 728 | HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout) |
mbed_official | 181:a4cbdfbbd2f4 | 729 | { |
mbed_official | 181:a4cbdfbbd2f4 | 730 | uint32_t tickstart = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 731 | |
mbed_official | 181:a4cbdfbbd2f4 | 732 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 733 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 734 | assert_param(IS_ADC_EVENT_TYPE(EventType)); |
mbed_official | 181:a4cbdfbbd2f4 | 735 | |
mbed_official | 181:a4cbdfbbd2f4 | 736 | /* Get timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 737 | tickstart = HAL_GetTick(); |
mbed_official | 181:a4cbdfbbd2f4 | 738 | |
mbed_official | 181:a4cbdfbbd2f4 | 739 | /* Check selected event flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 740 | while(!(__HAL_ADC_GET_FLAG(hadc,EventType))) |
mbed_official | 181:a4cbdfbbd2f4 | 741 | { |
mbed_official | 181:a4cbdfbbd2f4 | 742 | /* Check if timeout is disabled (set to infinite wait) */ |
mbed_official | 181:a4cbdfbbd2f4 | 743 | if(Timeout != HAL_MAX_DELAY) |
mbed_official | 181:a4cbdfbbd2f4 | 744 | { |
mbed_official | 181:a4cbdfbbd2f4 | 745 | if((HAL_GetTick() - tickstart ) > Timeout) |
mbed_official | 181:a4cbdfbbd2f4 | 746 | { |
mbed_official | 181:a4cbdfbbd2f4 | 747 | /* Update ADC state machine to timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 748 | hadc->State = HAL_ADC_STATE_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 749 | |
mbed_official | 181:a4cbdfbbd2f4 | 750 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 751 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 752 | |
mbed_official | 181:a4cbdfbbd2f4 | 753 | return HAL_TIMEOUT; |
mbed_official | 181:a4cbdfbbd2f4 | 754 | } |
mbed_official | 181:a4cbdfbbd2f4 | 755 | } |
mbed_official | 181:a4cbdfbbd2f4 | 756 | } |
mbed_official | 181:a4cbdfbbd2f4 | 757 | |
mbed_official | 181:a4cbdfbbd2f4 | 758 | switch(EventType) |
mbed_official | 181:a4cbdfbbd2f4 | 759 | { |
mbed_official | 181:a4cbdfbbd2f4 | 760 | /* Check analog watchdog flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 761 | case AWD_EVENT: |
mbed_official | 181:a4cbdfbbd2f4 | 762 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 763 | hadc->State = HAL_ADC_STATE_AWD; |
mbed_official | 181:a4cbdfbbd2f4 | 764 | |
mbed_official | 181:a4cbdfbbd2f4 | 765 | /* Clear ADC analog watchdog flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 766 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD); |
mbed_official | 181:a4cbdfbbd2f4 | 767 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 768 | |
mbed_official | 181:a4cbdfbbd2f4 | 769 | /* Case OVR_EVENT */ |
mbed_official | 181:a4cbdfbbd2f4 | 770 | default: |
mbed_official | 181:a4cbdfbbd2f4 | 771 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 772 | hadc->State = HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 773 | |
mbed_official | 181:a4cbdfbbd2f4 | 774 | /* Clear ADC Overrun flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 775 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR); |
mbed_official | 181:a4cbdfbbd2f4 | 776 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 777 | } |
mbed_official | 181:a4cbdfbbd2f4 | 778 | |
mbed_official | 181:a4cbdfbbd2f4 | 779 | /* Return ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 780 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 781 | } |
mbed_official | 181:a4cbdfbbd2f4 | 782 | |
mbed_official | 181:a4cbdfbbd2f4 | 783 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 784 | * @brief Enables the interrupt and starts ADC conversion of regular channels. |
mbed_official | 181:a4cbdfbbd2f4 | 785 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 786 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 787 | * @retval HAL status. |
mbed_official | 181:a4cbdfbbd2f4 | 788 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 789 | HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 790 | { |
mbed_official | 181:a4cbdfbbd2f4 | 791 | HAL_StatusTypeDef tmpHALStatus = HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 792 | |
mbed_official | 181:a4cbdfbbd2f4 | 793 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 794 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 795 | |
mbed_official | 181:a4cbdfbbd2f4 | 796 | /* Process locked */ |
mbed_official | 181:a4cbdfbbd2f4 | 797 | __HAL_LOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 798 | |
mbed_official | 181:a4cbdfbbd2f4 | 799 | /* State machine update: Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 800 | hadc->State = HAL_ADC_STATE_BUSY_REG; |
mbed_official | 181:a4cbdfbbd2f4 | 801 | |
mbed_official | 181:a4cbdfbbd2f4 | 802 | /* Set ADC error code to none */ |
mbed_official | 181:a4cbdfbbd2f4 | 803 | hadc->ErrorCode = HAL_ADC_ERROR_NONE; |
mbed_official | 181:a4cbdfbbd2f4 | 804 | |
mbed_official | 181:a4cbdfbbd2f4 | 805 | /* Enable the ADC peripheral */ |
mbed_official | 181:a4cbdfbbd2f4 | 806 | tmpHALStatus = ADC_Enable(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 807 | |
mbed_official | 181:a4cbdfbbd2f4 | 808 | /* Start conversion if ADC is effectively enabled */ |
mbed_official | 181:a4cbdfbbd2f4 | 809 | if (tmpHALStatus != HAL_ERROR) |
mbed_official | 181:a4cbdfbbd2f4 | 810 | { |
mbed_official | 181:a4cbdfbbd2f4 | 811 | /* Enable ADC overrun interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 812 | __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR); |
mbed_official | 181:a4cbdfbbd2f4 | 813 | |
mbed_official | 181:a4cbdfbbd2f4 | 814 | /* Enable ADC end of conversion interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 815 | switch(hadc->Init.EOCSelection) |
mbed_official | 181:a4cbdfbbd2f4 | 816 | { |
mbed_official | 181:a4cbdfbbd2f4 | 817 | case EOC_SEQ_CONV: |
mbed_official | 181:a4cbdfbbd2f4 | 818 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC); |
mbed_official | 181:a4cbdfbbd2f4 | 819 | __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS); |
mbed_official | 181:a4cbdfbbd2f4 | 820 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 821 | /* case EOC_SINGLE_CONV */ |
mbed_official | 181:a4cbdfbbd2f4 | 822 | default: |
mbed_official | 181:a4cbdfbbd2f4 | 823 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOS); |
mbed_official | 181:a4cbdfbbd2f4 | 824 | __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC); |
mbed_official | 181:a4cbdfbbd2f4 | 825 | break; |
mbed_official | 181:a4cbdfbbd2f4 | 826 | } |
mbed_official | 181:a4cbdfbbd2f4 | 827 | |
mbed_official | 181:a4cbdfbbd2f4 | 828 | /* ADC start conversion command */ |
mbed_official | 181:a4cbdfbbd2f4 | 829 | hadc->Instance->CR |= ADC_CR_ADSTART; |
mbed_official | 181:a4cbdfbbd2f4 | 830 | } |
mbed_official | 181:a4cbdfbbd2f4 | 831 | |
mbed_official | 181:a4cbdfbbd2f4 | 832 | else |
mbed_official | 181:a4cbdfbbd2f4 | 833 | { |
mbed_official | 181:a4cbdfbbd2f4 | 834 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 835 | } |
mbed_official | 181:a4cbdfbbd2f4 | 836 | |
mbed_official | 181:a4cbdfbbd2f4 | 837 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 838 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 839 | |
mbed_official | 181:a4cbdfbbd2f4 | 840 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 841 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 842 | } |
mbed_official | 181:a4cbdfbbd2f4 | 843 | |
mbed_official | 181:a4cbdfbbd2f4 | 844 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 845 | * @brief Stop ADC conversion of regular channels, disable interruptions |
mbed_official | 181:a4cbdfbbd2f4 | 846 | * EOC/EOS/OVR, disable ADC peripheral. |
mbed_official | 181:a4cbdfbbd2f4 | 847 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 848 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 849 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 850 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 851 | HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 852 | { |
mbed_official | 181:a4cbdfbbd2f4 | 853 | HAL_StatusTypeDef tmpHALStatus = HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 854 | |
mbed_official | 181:a4cbdfbbd2f4 | 855 | /* Process locked */ |
mbed_official | 181:a4cbdfbbd2f4 | 856 | __HAL_LOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 857 | |
mbed_official | 181:a4cbdfbbd2f4 | 858 | /* 1. Stop potential conversion ongoing (regular conversion) */ |
mbed_official | 181:a4cbdfbbd2f4 | 859 | tmpHALStatus = ADC_ConversionStop(hadc, REGULAR_GROUP); |
mbed_official | 181:a4cbdfbbd2f4 | 860 | |
mbed_official | 181:a4cbdfbbd2f4 | 861 | /* 2. Disable ADC peripheral if conversions are effectively stopped */ |
mbed_official | 181:a4cbdfbbd2f4 | 862 | if (tmpHALStatus != HAL_ERROR) |
mbed_official | 181:a4cbdfbbd2f4 | 863 | { |
mbed_official | 181:a4cbdfbbd2f4 | 864 | /* Disable ADC interrupts */ |
mbed_official | 181:a4cbdfbbd2f4 | 865 | __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR)); |
mbed_official | 181:a4cbdfbbd2f4 | 866 | |
mbed_official | 181:a4cbdfbbd2f4 | 867 | /* Disable the ADC peripheral */ |
mbed_official | 181:a4cbdfbbd2f4 | 868 | ADC_Disable(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 869 | |
mbed_official | 181:a4cbdfbbd2f4 | 870 | /* Check if ADC is effectively disabled */ |
mbed_official | 181:a4cbdfbbd2f4 | 871 | if (hadc->State != HAL_ADC_STATE_ERROR) |
mbed_official | 181:a4cbdfbbd2f4 | 872 | { |
mbed_official | 181:a4cbdfbbd2f4 | 873 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 874 | hadc->State = HAL_ADC_STATE_READY; |
mbed_official | 181:a4cbdfbbd2f4 | 875 | } |
mbed_official | 181:a4cbdfbbd2f4 | 876 | else |
mbed_official | 181:a4cbdfbbd2f4 | 877 | { |
mbed_official | 181:a4cbdfbbd2f4 | 878 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 879 | } |
mbed_official | 181:a4cbdfbbd2f4 | 880 | } |
mbed_official | 181:a4cbdfbbd2f4 | 881 | else |
mbed_official | 181:a4cbdfbbd2f4 | 882 | { |
mbed_official | 181:a4cbdfbbd2f4 | 883 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 884 | } |
mbed_official | 181:a4cbdfbbd2f4 | 885 | |
mbed_official | 181:a4cbdfbbd2f4 | 886 | |
mbed_official | 181:a4cbdfbbd2f4 | 887 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 888 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 889 | |
mbed_official | 181:a4cbdfbbd2f4 | 890 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 891 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 892 | } |
mbed_official | 181:a4cbdfbbd2f4 | 893 | |
mbed_official | 181:a4cbdfbbd2f4 | 894 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 895 | * @brief Handles ADC interrupt request |
mbed_official | 181:a4cbdfbbd2f4 | 896 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 897 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 898 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 899 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 900 | void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 901 | { |
mbed_official | 181:a4cbdfbbd2f4 | 902 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 903 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 904 | assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection)); |
mbed_official | 181:a4cbdfbbd2f4 | 905 | |
mbed_official | 181:a4cbdfbbd2f4 | 906 | |
mbed_official | 181:a4cbdfbbd2f4 | 907 | /* Check End of Conversion flag for regular channels */ |
mbed_official | 181:a4cbdfbbd2f4 | 908 | if( (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC)) || \ |
mbed_official | 181:a4cbdfbbd2f4 | 909 | (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOS)) ) |
mbed_official | 181:a4cbdfbbd2f4 | 910 | { |
mbed_official | 181:a4cbdfbbd2f4 | 911 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 912 | hadc->State = HAL_ADC_STATE_EOC; |
mbed_official | 181:a4cbdfbbd2f4 | 913 | |
mbed_official | 181:a4cbdfbbd2f4 | 914 | |
mbed_official | 181:a4cbdfbbd2f4 | 915 | /* Disable interruption if no further conversion upcoming by continuous mode or external trigger */ |
mbed_official | 181:a4cbdfbbd2f4 | 916 | if((hadc->Init.ContinuousConvMode == DISABLE) && \ |
mbed_official | 181:a4cbdfbbd2f4 | 917 | (hadc->Init.ExternalTrigConvEdge == ADC_EXTERNALTRIG_EDGE_NONE) |
mbed_official | 181:a4cbdfbbd2f4 | 918 | ) |
mbed_official | 181:a4cbdfbbd2f4 | 919 | { |
mbed_official | 181:a4cbdfbbd2f4 | 920 | /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit ADSTART==0 (no conversion on going) */ |
mbed_official | 181:a4cbdfbbd2f4 | 921 | if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADSTART)) |
mbed_official | 181:a4cbdfbbd2f4 | 922 | { |
mbed_official | 181:a4cbdfbbd2f4 | 923 | /* Cases of interruption after each conversion or after each sequence */ |
mbed_official | 181:a4cbdfbbd2f4 | 924 | /* If interruption after each sequence */ |
mbed_official | 181:a4cbdfbbd2f4 | 925 | if (hadc->Init.EOCSelection == EOC_SEQ_CONV) |
mbed_official | 181:a4cbdfbbd2f4 | 926 | { |
mbed_official | 181:a4cbdfbbd2f4 | 927 | /* If End of Sequence is reached, disable interrupts */ |
mbed_official | 181:a4cbdfbbd2f4 | 928 | if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) ) |
mbed_official | 181:a4cbdfbbd2f4 | 929 | { |
mbed_official | 181:a4cbdfbbd2f4 | 930 | /* DISABLE ADC end of sequence conversion interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 931 | /* DISABLE ADC overrun interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 932 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR); |
mbed_official | 181:a4cbdfbbd2f4 | 933 | } |
mbed_official | 181:a4cbdfbbd2f4 | 934 | } |
mbed_official | 181:a4cbdfbbd2f4 | 935 | /* If interruption after each conversion */ |
mbed_official | 181:a4cbdfbbd2f4 | 936 | else |
mbed_official | 181:a4cbdfbbd2f4 | 937 | { |
mbed_official | 181:a4cbdfbbd2f4 | 938 | /* DISABLE ADC end of single conversion interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 939 | /* DISABLE ADC overrun interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 940 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_OVR); |
mbed_official | 181:a4cbdfbbd2f4 | 941 | } |
mbed_official | 181:a4cbdfbbd2f4 | 942 | } |
mbed_official | 181:a4cbdfbbd2f4 | 943 | else |
mbed_official | 181:a4cbdfbbd2f4 | 944 | { |
mbed_official | 181:a4cbdfbbd2f4 | 945 | /* Change ADC state to error state */ |
mbed_official | 181:a4cbdfbbd2f4 | 946 | hadc->State = HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 947 | } |
mbed_official | 181:a4cbdfbbd2f4 | 948 | } |
mbed_official | 181:a4cbdfbbd2f4 | 949 | |
mbed_official | 181:a4cbdfbbd2f4 | 950 | /* Conversion complete callback */ |
mbed_official | 181:a4cbdfbbd2f4 | 951 | /* Note: into callback, to determine if callback has been triggered from EOC or EOS, */ |
mbed_official | 181:a4cbdfbbd2f4 | 952 | /* it is possible to use: if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS)) */ |
mbed_official | 181:a4cbdfbbd2f4 | 953 | HAL_ADC_ConvCpltCallback(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 954 | |
mbed_official | 181:a4cbdfbbd2f4 | 955 | /* Clear regular channels conversion flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 956 | if (hadc->Init.LowPowerAutoWait != ENABLE) |
mbed_official | 181:a4cbdfbbd2f4 | 957 | { |
mbed_official | 181:a4cbdfbbd2f4 | 958 | __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) ); |
mbed_official | 181:a4cbdfbbd2f4 | 959 | } |
mbed_official | 181:a4cbdfbbd2f4 | 960 | } |
mbed_official | 181:a4cbdfbbd2f4 | 961 | |
mbed_official | 181:a4cbdfbbd2f4 | 962 | |
mbed_official | 181:a4cbdfbbd2f4 | 963 | /* Check Analog watchdog flags */ |
mbed_official | 181:a4cbdfbbd2f4 | 964 | if( (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD))) |
mbed_official | 181:a4cbdfbbd2f4 | 965 | { |
mbed_official | 181:a4cbdfbbd2f4 | 966 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 967 | hadc->State = HAL_ADC_STATE_AWD; |
mbed_official | 181:a4cbdfbbd2f4 | 968 | |
mbed_official | 181:a4cbdfbbd2f4 | 969 | /* Level out of window callback */ |
mbed_official | 181:a4cbdfbbd2f4 | 970 | HAL_ADC_LevelOutOfWindowCallback(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 971 | |
mbed_official | 181:a4cbdfbbd2f4 | 972 | /* Clear ADC Analog watchdog flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 973 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD); |
mbed_official | 181:a4cbdfbbd2f4 | 974 | } |
mbed_official | 181:a4cbdfbbd2f4 | 975 | |
mbed_official | 181:a4cbdfbbd2f4 | 976 | /* Check Overrun flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 977 | if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR)) |
mbed_official | 181:a4cbdfbbd2f4 | 978 | { |
mbed_official | 181:a4cbdfbbd2f4 | 979 | /* Change ADC state to overrun state */ |
mbed_official | 181:a4cbdfbbd2f4 | 980 | hadc->State = HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 981 | |
mbed_official | 181:a4cbdfbbd2f4 | 982 | /* Set ADC error code to overrun */ |
mbed_official | 181:a4cbdfbbd2f4 | 983 | hadc->ErrorCode |= HAL_ADC_ERROR_OVR; |
mbed_official | 181:a4cbdfbbd2f4 | 984 | |
mbed_official | 181:a4cbdfbbd2f4 | 985 | /* Clear the Overrun flag */ |
mbed_official | 181:a4cbdfbbd2f4 | 986 | __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_OVR); |
mbed_official | 181:a4cbdfbbd2f4 | 987 | |
mbed_official | 181:a4cbdfbbd2f4 | 988 | /* Error callback */ |
mbed_official | 181:a4cbdfbbd2f4 | 989 | HAL_ADC_ErrorCallback(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 990 | } |
mbed_official | 181:a4cbdfbbd2f4 | 991 | } |
mbed_official | 181:a4cbdfbbd2f4 | 992 | |
mbed_official | 181:a4cbdfbbd2f4 | 993 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 994 | * @brief Enables ADC DMA request after last transfer (Single-ADC mode) and enables ADC peripheral |
mbed_official | 181:a4cbdfbbd2f4 | 995 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 996 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 997 | * @param pData: The destination Buffer address. |
mbed_official | 181:a4cbdfbbd2f4 | 998 | * @param Length: The length of data to be transferred from ADC peripheral to memory. |
mbed_official | 181:a4cbdfbbd2f4 | 999 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 1000 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1001 | HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length) |
mbed_official | 181:a4cbdfbbd2f4 | 1002 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1003 | HAL_StatusTypeDef tmpHALStatus = HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 1004 | |
mbed_official | 181:a4cbdfbbd2f4 | 1005 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 1006 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 1007 | |
mbed_official | 181:a4cbdfbbd2f4 | 1008 | /* Process locked */ |
mbed_official | 181:a4cbdfbbd2f4 | 1009 | __HAL_LOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1010 | |
mbed_official | 181:a4cbdfbbd2f4 | 1011 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 1012 | hadc->State = HAL_ADC_STATE_BUSY_REG; |
mbed_official | 181:a4cbdfbbd2f4 | 1013 | |
mbed_official | 181:a4cbdfbbd2f4 | 1014 | /* Set ADC error code to none */ |
mbed_official | 181:a4cbdfbbd2f4 | 1015 | hadc->ErrorCode = HAL_ADC_ERROR_NONE; |
mbed_official | 181:a4cbdfbbd2f4 | 1016 | |
mbed_official | 181:a4cbdfbbd2f4 | 1017 | /* Enable ADC */ |
mbed_official | 181:a4cbdfbbd2f4 | 1018 | tmpHALStatus = ADC_Enable(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1019 | |
mbed_official | 181:a4cbdfbbd2f4 | 1020 | /* Start conversion if ADC is effectively enabled */ |
mbed_official | 181:a4cbdfbbd2f4 | 1021 | if (tmpHALStatus != HAL_ERROR) |
mbed_official | 181:a4cbdfbbd2f4 | 1022 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1023 | /* Enable ADC DMA mode */ |
mbed_official | 181:a4cbdfbbd2f4 | 1024 | hadc->Instance->CFGR1 |= ADC_CFGR1_DMAEN; |
mbed_official | 181:a4cbdfbbd2f4 | 1025 | |
mbed_official | 181:a4cbdfbbd2f4 | 1026 | /* Set the DMA transfer complete callback */ |
mbed_official | 181:a4cbdfbbd2f4 | 1027 | hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt; |
mbed_official | 181:a4cbdfbbd2f4 | 1028 | |
mbed_official | 181:a4cbdfbbd2f4 | 1029 | /* Set the DMA half transfer complete callback */ |
mbed_official | 181:a4cbdfbbd2f4 | 1030 | hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt; |
mbed_official | 181:a4cbdfbbd2f4 | 1031 | |
mbed_official | 181:a4cbdfbbd2f4 | 1032 | /* Set the DMA error callback */ |
mbed_official | 181:a4cbdfbbd2f4 | 1033 | hadc->DMA_Handle->XferErrorCallback = ADC_DMAError; |
mbed_official | 181:a4cbdfbbd2f4 | 1034 | |
mbed_official | 181:a4cbdfbbd2f4 | 1035 | /* Manage ADC and DMA start: ADC overrun interruption, DMA start, |
mbed_official | 181:a4cbdfbbd2f4 | 1036 | ADC start (in case of SW start) */ |
mbed_official | 181:a4cbdfbbd2f4 | 1037 | |
mbed_official | 181:a4cbdfbbd2f4 | 1038 | /* Enable ADC overrun interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 1039 | __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR); |
mbed_official | 181:a4cbdfbbd2f4 | 1040 | |
mbed_official | 181:a4cbdfbbd2f4 | 1041 | /* Enable the DMA Stream */ |
mbed_official | 181:a4cbdfbbd2f4 | 1042 | HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length); |
mbed_official | 181:a4cbdfbbd2f4 | 1043 | |
mbed_official | 181:a4cbdfbbd2f4 | 1044 | |
mbed_official | 181:a4cbdfbbd2f4 | 1045 | /* ADC start conversion command */ |
mbed_official | 181:a4cbdfbbd2f4 | 1046 | hadc->Instance->CR |= ADC_CR_ADSTART; |
mbed_official | 181:a4cbdfbbd2f4 | 1047 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1048 | |
mbed_official | 181:a4cbdfbbd2f4 | 1049 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 1050 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1051 | |
mbed_official | 181:a4cbdfbbd2f4 | 1052 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 1053 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 1054 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1055 | |
mbed_official | 181:a4cbdfbbd2f4 | 1056 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1057 | * @brief Disable ADC DMA (Single-ADC mode), disable ADC peripheral |
mbed_official | 181:a4cbdfbbd2f4 | 1058 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1059 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1060 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 1061 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1062 | HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1063 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1064 | HAL_StatusTypeDef tmpHALStatus = HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 1065 | |
mbed_official | 181:a4cbdfbbd2f4 | 1066 | /* Process locked */ |
mbed_official | 181:a4cbdfbbd2f4 | 1067 | __HAL_LOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1068 | |
mbed_official | 181:a4cbdfbbd2f4 | 1069 | /* 1. Stop potential conversion ongoing (regular conversion) */ |
mbed_official | 181:a4cbdfbbd2f4 | 1070 | tmpHALStatus = ADC_ConversionStop(hadc, REGULAR_GROUP); |
mbed_official | 181:a4cbdfbbd2f4 | 1071 | |
mbed_official | 181:a4cbdfbbd2f4 | 1072 | /* 2. Disable ADC peripheral if conversions are effectively stopped */ |
mbed_official | 181:a4cbdfbbd2f4 | 1073 | if (tmpHALStatus != HAL_ERROR) |
mbed_official | 181:a4cbdfbbd2f4 | 1074 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1075 | /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */ |
mbed_official | 181:a4cbdfbbd2f4 | 1076 | hadc->Instance->CFGR1 &= ~ADC_CFGR1_DMAEN; |
mbed_official | 181:a4cbdfbbd2f4 | 1077 | |
mbed_official | 181:a4cbdfbbd2f4 | 1078 | /* Disable the DMA Stream */ |
mbed_official | 181:a4cbdfbbd2f4 | 1079 | if (HAL_DMA_Abort(hadc->DMA_Handle) != HAL_OK) |
mbed_official | 181:a4cbdfbbd2f4 | 1080 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1081 | /* Update ADC state machine to error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1082 | hadc->State = HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1083 | |
mbed_official | 181:a4cbdfbbd2f4 | 1084 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 1085 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1086 | |
mbed_official | 181:a4cbdfbbd2f4 | 1087 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1088 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1089 | |
mbed_official | 181:a4cbdfbbd2f4 | 1090 | /* Disable ADC overrun interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 1091 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR); |
mbed_official | 181:a4cbdfbbd2f4 | 1092 | |
mbed_official | 181:a4cbdfbbd2f4 | 1093 | /* Disable the ADC peripheral */ |
mbed_official | 181:a4cbdfbbd2f4 | 1094 | ADC_Disable(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1095 | |
mbed_official | 181:a4cbdfbbd2f4 | 1096 | /* Check if ADC is effectively disabled */ |
mbed_official | 181:a4cbdfbbd2f4 | 1097 | if (hadc->State != HAL_ADC_STATE_ERROR) |
mbed_official | 181:a4cbdfbbd2f4 | 1098 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1099 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 1100 | hadc->State = HAL_ADC_STATE_READY; |
mbed_official | 181:a4cbdfbbd2f4 | 1101 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1102 | else |
mbed_official | 181:a4cbdfbbd2f4 | 1103 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1104 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1105 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1106 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1107 | else |
mbed_official | 181:a4cbdfbbd2f4 | 1108 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1109 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1110 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1111 | |
mbed_official | 181:a4cbdfbbd2f4 | 1112 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 1113 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1114 | |
mbed_official | 181:a4cbdfbbd2f4 | 1115 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 1116 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 1117 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1118 | |
mbed_official | 181:a4cbdfbbd2f4 | 1119 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1120 | * @brief Gets the converted value from data register of regular channel. |
mbed_official | 181:a4cbdfbbd2f4 | 1121 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1122 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1123 | * @retval Converted value |
mbed_official | 181:a4cbdfbbd2f4 | 1124 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1125 | uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1126 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1127 | /* Return the selected ADC converted value */ |
mbed_official | 181:a4cbdfbbd2f4 | 1128 | return hadc->Instance->DR; |
mbed_official | 181:a4cbdfbbd2f4 | 1129 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1130 | |
mbed_official | 181:a4cbdfbbd2f4 | 1131 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1132 | * @brief Regular conversion complete callback in non blocking mode |
mbed_official | 181:a4cbdfbbd2f4 | 1133 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1134 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1135 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 1136 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1137 | __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1138 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1139 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 181:a4cbdfbbd2f4 | 1140 | the HAL_ADC_ConvCpltCallback could be implemented in the user file |
mbed_official | 181:a4cbdfbbd2f4 | 1141 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1142 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1143 | |
mbed_official | 181:a4cbdfbbd2f4 | 1144 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1145 | * @brief Regular conversion half DMA transfer callback in non blocking mode |
mbed_official | 181:a4cbdfbbd2f4 | 1146 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1147 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1148 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 1149 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1150 | __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1151 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1152 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 181:a4cbdfbbd2f4 | 1153 | the HAL_ADC_ConvHalfCpltCallback could be implemented in the user file |
mbed_official | 181:a4cbdfbbd2f4 | 1154 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1155 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1156 | |
mbed_official | 181:a4cbdfbbd2f4 | 1157 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1158 | * @brief Analog watchdog callback in non blocking mode |
mbed_official | 181:a4cbdfbbd2f4 | 1159 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1160 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1161 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 1162 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1163 | __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1164 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1165 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 181:a4cbdfbbd2f4 | 1166 | the HAL_ADC_LevelOoutOfWindowCallback could be implemented in the user file |
mbed_official | 181:a4cbdfbbd2f4 | 1167 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1168 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1169 | |
mbed_official | 181:a4cbdfbbd2f4 | 1170 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1171 | * @brief Error ADC callback. |
mbed_official | 181:a4cbdfbbd2f4 | 1172 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1173 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1174 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 1175 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1176 | __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1177 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1178 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 181:a4cbdfbbd2f4 | 1179 | the HAL_ADC_ErrorCallback could be implemented in the user file |
mbed_official | 181:a4cbdfbbd2f4 | 1180 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1181 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1182 | |
mbed_official | 181:a4cbdfbbd2f4 | 1183 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1184 | * @} |
mbed_official | 181:a4cbdfbbd2f4 | 1185 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1186 | |
mbed_official | 181:a4cbdfbbd2f4 | 1187 | /** @defgroup ADC_Group3 Peripheral Control functions |
mbed_official | 181:a4cbdfbbd2f4 | 1188 | * @brief Peripheral Control functions |
mbed_official | 181:a4cbdfbbd2f4 | 1189 | * |
mbed_official | 181:a4cbdfbbd2f4 | 1190 | @verbatim |
mbed_official | 181:a4cbdfbbd2f4 | 1191 | =============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 1192 | ##### Peripheral Control functions ##### |
mbed_official | 181:a4cbdfbbd2f4 | 1193 | =============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 1194 | [..] This section provides functions allowing to: |
mbed_official | 181:a4cbdfbbd2f4 | 1195 | (+) Configure channels. |
mbed_official | 181:a4cbdfbbd2f4 | 1196 | (+) Configure the analog watch dog. |
mbed_official | 181:a4cbdfbbd2f4 | 1197 | |
mbed_official | 181:a4cbdfbbd2f4 | 1198 | @endverbatim |
mbed_official | 181:a4cbdfbbd2f4 | 1199 | * @{ |
mbed_official | 181:a4cbdfbbd2f4 | 1200 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1201 | |
mbed_official | 181:a4cbdfbbd2f4 | 1202 | |
mbed_official | 181:a4cbdfbbd2f4 | 1203 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1204 | * @brief Configures the selected ADC regular channel: sampling time, |
mbed_official | 181:a4cbdfbbd2f4 | 1205 | * offset,. |
mbed_official | 181:a4cbdfbbd2f4 | 1206 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1207 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1208 | * @param sConfig: ADC regular channel configuration structure. |
mbed_official | 181:a4cbdfbbd2f4 | 1209 | * @retval HAL status |
mbed_official | 181:a4cbdfbbd2f4 | 1210 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1211 | HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig) |
mbed_official | 181:a4cbdfbbd2f4 | 1212 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1213 | |
mbed_official | 181:a4cbdfbbd2f4 | 1214 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 1215 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 1216 | assert_param(IS_ADC_CHANNEL(sConfig->Channel)); |
mbed_official | 181:a4cbdfbbd2f4 | 1217 | |
mbed_official | 181:a4cbdfbbd2f4 | 1218 | /* Process locked */ |
mbed_official | 181:a4cbdfbbd2f4 | 1219 | __HAL_LOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1220 | |
mbed_official | 181:a4cbdfbbd2f4 | 1221 | /* Enable selected channels */ |
mbed_official | 181:a4cbdfbbd2f4 | 1222 | hadc->Instance->CHSELR |= (uint32_t)(sConfig->Channel & ADC_CHANNEL_MASK); |
mbed_official | 181:a4cbdfbbd2f4 | 1223 | |
mbed_official | 181:a4cbdfbbd2f4 | 1224 | /* Management of internal measurement channels: Vlcd/VrefInt/TempSensor */ |
mbed_official | 181:a4cbdfbbd2f4 | 1225 | /* internal measurement paths enable: If internal channel selected, enable */ |
mbed_official | 181:a4cbdfbbd2f4 | 1226 | /* dedicated internal buffers and path. */ |
mbed_official | 181:a4cbdfbbd2f4 | 1227 | |
mbed_official | 181:a4cbdfbbd2f4 | 1228 | /* If Temperature sensor channel is selected, then enable the internal */ |
mbed_official | 181:a4cbdfbbd2f4 | 1229 | /* buffers and path */ |
mbed_official | 181:a4cbdfbbd2f4 | 1230 | if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_TEMPSENSOR ) == (ADC_CHANNEL_TEMPSENSOR & ADC_CHANNEL_MASK)) |
mbed_official | 181:a4cbdfbbd2f4 | 1231 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1232 | ADC->CCR |= ADC_CCR_TSEN; |
mbed_official | 181:a4cbdfbbd2f4 | 1233 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1234 | |
mbed_official | 181:a4cbdfbbd2f4 | 1235 | /* If VRefInt channel is selected, then enable the internal buffers and path */ |
mbed_official | 181:a4cbdfbbd2f4 | 1236 | if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VREFINT) == (ADC_CHANNEL_VREFINT & ADC_CHANNEL_MASK)) |
mbed_official | 181:a4cbdfbbd2f4 | 1237 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1238 | ADC->CCR |= ADC_CCR_VREFEN; |
mbed_official | 181:a4cbdfbbd2f4 | 1239 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1240 | |
mbed_official | 181:a4cbdfbbd2f4 | 1241 | /* If Vlcd channel is selected, then enable the internal buffers and path */ |
mbed_official | 181:a4cbdfbbd2f4 | 1242 | if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VLCD) == (ADC_CHANNEL_VLCD & ADC_CHANNEL_MASK)) |
mbed_official | 181:a4cbdfbbd2f4 | 1243 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1244 | ADC->CCR |= ADC_CCR_VLCDEN; |
mbed_official | 181:a4cbdfbbd2f4 | 1245 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1246 | |
mbed_official | 181:a4cbdfbbd2f4 | 1247 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 1248 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1249 | |
mbed_official | 181:a4cbdfbbd2f4 | 1250 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 1251 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 1252 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1253 | |
mbed_official | 181:a4cbdfbbd2f4 | 1254 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1255 | * @brief Configures the analog watchdog. |
mbed_official | 181:a4cbdfbbd2f4 | 1256 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1257 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1258 | * @param AnalogWDGConfig : pointer to an ADC_AnalogWDGConfTypeDef structure |
mbed_official | 181:a4cbdfbbd2f4 | 1259 | * that contains the configuration information of ADC analog watchdog. |
mbed_official | 181:a4cbdfbbd2f4 | 1260 | * @retval HAL status |
mbed_official | 181:a4cbdfbbd2f4 | 1261 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1262 | HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig) |
mbed_official | 181:a4cbdfbbd2f4 | 1263 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1264 | uint32_t tmpAWDHighThresholdShifted; |
mbed_official | 181:a4cbdfbbd2f4 | 1265 | uint32_t tmpAWDLowThresholdShifted; |
mbed_official | 181:a4cbdfbbd2f4 | 1266 | |
mbed_official | 181:a4cbdfbbd2f4 | 1267 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 1268 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 1269 | assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode)); |
mbed_official | 181:a4cbdfbbd2f4 | 1270 | assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel)); |
mbed_official | 181:a4cbdfbbd2f4 | 1271 | assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode)); |
mbed_official | 181:a4cbdfbbd2f4 | 1272 | |
mbed_official | 181:a4cbdfbbd2f4 | 1273 | assert_param(IS_ADC_RANGE(__HAL_ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold)); |
mbed_official | 181:a4cbdfbbd2f4 | 1274 | assert_param(IS_ADC_RANGE(__HAL_ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold)); |
mbed_official | 181:a4cbdfbbd2f4 | 1275 | |
mbed_official | 181:a4cbdfbbd2f4 | 1276 | /* Process locked */ |
mbed_official | 181:a4cbdfbbd2f4 | 1277 | __HAL_LOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1278 | |
mbed_official | 181:a4cbdfbbd2f4 | 1279 | /* Configure ADC Analog watchdog interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 1280 | if(AnalogWDGConfig->ITMode == ENABLE) |
mbed_official | 181:a4cbdfbbd2f4 | 1281 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1282 | /* Enable the ADC Analog watchdog interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 1283 | __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD); |
mbed_official | 181:a4cbdfbbd2f4 | 1284 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1285 | else |
mbed_official | 181:a4cbdfbbd2f4 | 1286 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1287 | /* Disable the ADC Analog watchdog interrupt */ |
mbed_official | 181:a4cbdfbbd2f4 | 1288 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD); |
mbed_official | 181:a4cbdfbbd2f4 | 1289 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1290 | |
mbed_official | 181:a4cbdfbbd2f4 | 1291 | /* Configuration of analog watchdog: */ |
mbed_official | 181:a4cbdfbbd2f4 | 1292 | /* - Set the analog watchdog mode */ |
mbed_official | 181:a4cbdfbbd2f4 | 1293 | /* - Set the Analog watchdog channel (is not used if watchdog */ |
mbed_official | 181:a4cbdfbbd2f4 | 1294 | /* mode "all channels": ADC_CFGR1_AWD1SGL=0) */ |
mbed_official | 181:a4cbdfbbd2f4 | 1295 | hadc->Instance->CFGR1 &= ~( ADC_CFGR1_AWDSGL | |
mbed_official | 181:a4cbdfbbd2f4 | 1296 | ADC_CFGR1_AWDEN | |
mbed_official | 181:a4cbdfbbd2f4 | 1297 | ADC_CFGR1_AWDCH ); |
mbed_official | 181:a4cbdfbbd2f4 | 1298 | |
mbed_official | 181:a4cbdfbbd2f4 | 1299 | hadc->Instance->CFGR1 |= ( AnalogWDGConfig->WatchdogMode | |
mbed_official | 181:a4cbdfbbd2f4 | 1300 | (AnalogWDGConfig->Channel & ADC_CHANNEL_AWD_MASK)); |
mbed_official | 181:a4cbdfbbd2f4 | 1301 | |
mbed_official | 181:a4cbdfbbd2f4 | 1302 | |
mbed_official | 181:a4cbdfbbd2f4 | 1303 | /* Shift the offset in function of the selected ADC resolution: Thresholds */ |
mbed_official | 181:a4cbdfbbd2f4 | 1304 | /* have to be left-aligned on bit 11, the LSB (right bits) are set to 0 */ |
mbed_official | 181:a4cbdfbbd2f4 | 1305 | tmpAWDHighThresholdShifted = __HAL_ADC_AWD1Threshold_shift_resolution(hadc, AnalogWDGConfig->HighThreshold); |
mbed_official | 181:a4cbdfbbd2f4 | 1306 | tmpAWDLowThresholdShifted = __HAL_ADC_AWD1Threshold_shift_resolution(hadc, AnalogWDGConfig->LowThreshold); |
mbed_official | 181:a4cbdfbbd2f4 | 1307 | |
mbed_official | 181:a4cbdfbbd2f4 | 1308 | /* Clear High & Low high thresholds */ |
mbed_official | 181:a4cbdfbbd2f4 | 1309 | hadc->Instance->TR &= (uint32_t) ~ (ADC_TR_HT | ADC_TR_LT); |
mbed_official | 181:a4cbdfbbd2f4 | 1310 | |
mbed_official | 181:a4cbdfbbd2f4 | 1311 | /* Set the high threshold */ |
mbed_official | 181:a4cbdfbbd2f4 | 1312 | hadc->Instance->TR = __HAL_ADC_TRx_HighThreshold (tmpAWDHighThresholdShifted); |
mbed_official | 181:a4cbdfbbd2f4 | 1313 | /* Set the low threshold */ |
mbed_official | 181:a4cbdfbbd2f4 | 1314 | hadc->Instance->TR |= tmpAWDLowThresholdShifted; |
mbed_official | 181:a4cbdfbbd2f4 | 1315 | |
mbed_official | 181:a4cbdfbbd2f4 | 1316 | /* Process unlocked */ |
mbed_official | 181:a4cbdfbbd2f4 | 1317 | __HAL_UNLOCK(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1318 | |
mbed_official | 181:a4cbdfbbd2f4 | 1319 | /* Return function status */ |
mbed_official | 181:a4cbdfbbd2f4 | 1320 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 1321 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1322 | |
mbed_official | 181:a4cbdfbbd2f4 | 1323 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1324 | * @} |
mbed_official | 181:a4cbdfbbd2f4 | 1325 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1326 | |
mbed_official | 181:a4cbdfbbd2f4 | 1327 | /** @defgroup ADC_Group4 ADC Peripheral State functions |
mbed_official | 181:a4cbdfbbd2f4 | 1328 | * @brief ADC Peripheral State functions |
mbed_official | 181:a4cbdfbbd2f4 | 1329 | * |
mbed_official | 181:a4cbdfbbd2f4 | 1330 | @verbatim |
mbed_official | 181:a4cbdfbbd2f4 | 1331 | =============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 1332 | ##### ADC Peripheral State functions ##### |
mbed_official | 181:a4cbdfbbd2f4 | 1333 | =============================================================================== |
mbed_official | 181:a4cbdfbbd2f4 | 1334 | [..] |
mbed_official | 181:a4cbdfbbd2f4 | 1335 | This subsection provides functions allowing to |
mbed_official | 181:a4cbdfbbd2f4 | 1336 | (+) Check the ADC state. |
mbed_official | 181:a4cbdfbbd2f4 | 1337 | (+) handle ADC interrupt request. |
mbed_official | 181:a4cbdfbbd2f4 | 1338 | |
mbed_official | 181:a4cbdfbbd2f4 | 1339 | @endverbatim |
mbed_official | 181:a4cbdfbbd2f4 | 1340 | * @{ |
mbed_official | 181:a4cbdfbbd2f4 | 1341 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1342 | |
mbed_official | 181:a4cbdfbbd2f4 | 1343 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1344 | * @brief return the ADC state |
mbed_official | 181:a4cbdfbbd2f4 | 1345 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1346 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1347 | * @retval HAL state |
mbed_official | 181:a4cbdfbbd2f4 | 1348 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1349 | HAL_ADC_StateTypeDef HAL_ADC_GetState(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1350 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1351 | /* Return ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 1352 | return hadc->State; |
mbed_official | 181:a4cbdfbbd2f4 | 1353 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1354 | |
mbed_official | 181:a4cbdfbbd2f4 | 1355 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1356 | * @brief Return the ADC error code |
mbed_official | 181:a4cbdfbbd2f4 | 1357 | * @param hadc: pointer to a ADC_HandleTypeDef structure that contains |
mbed_official | 181:a4cbdfbbd2f4 | 1358 | * the configuration information for the specified ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1359 | * @retval ADC Error Code |
mbed_official | 181:a4cbdfbbd2f4 | 1360 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1361 | uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1362 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1363 | return hadc->ErrorCode; |
mbed_official | 181:a4cbdfbbd2f4 | 1364 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1365 | |
mbed_official | 181:a4cbdfbbd2f4 | 1366 | |
mbed_official | 181:a4cbdfbbd2f4 | 1367 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1368 | * @} |
mbed_official | 181:a4cbdfbbd2f4 | 1369 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1370 | |
mbed_official | 181:a4cbdfbbd2f4 | 1371 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1372 | * @brief Enable the selected ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1373 | * @note Prerequisite condition to use this function: ADC must be disabled |
mbed_official | 181:a4cbdfbbd2f4 | 1374 | * and voltage regulator must be enabled (done into HAL_ADC_Init()). |
mbed_official | 181:a4cbdfbbd2f4 | 1375 | * @param hadc: ADC handle |
mbed_official | 181:a4cbdfbbd2f4 | 1376 | * @retval HAL status. |
mbed_official | 181:a4cbdfbbd2f4 | 1377 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1378 | static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1379 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1380 | uint32_t tickstart = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 1381 | |
mbed_official | 181:a4cbdfbbd2f4 | 1382 | /* ADC enable and wait for ADC ready (in case of ADC is disabled or */ |
mbed_official | 181:a4cbdfbbd2f4 | 1383 | /* enabling phase not yet completed: flag ADC ready not yet set). */ |
mbed_official | 181:a4cbdfbbd2f4 | 1384 | /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */ |
mbed_official | 181:a4cbdfbbd2f4 | 1385 | /* causes: ADC clock not running, ...). */ |
mbed_official | 181:a4cbdfbbd2f4 | 1386 | if (__HAL_ADC_IS_ENABLED(hadc) == RESET) |
mbed_official | 181:a4cbdfbbd2f4 | 1387 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1388 | /* Check if conditions to enable the ADC are fulfilled */ |
mbed_official | 181:a4cbdfbbd2f4 | 1389 | if (__HAL_ADC_ENABLING_CONDITIONS(hadc) == RESET) |
mbed_official | 181:a4cbdfbbd2f4 | 1390 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1391 | /* Update ADC state machine to error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1392 | hadc->State = HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1393 | |
mbed_official | 181:a4cbdfbbd2f4 | 1394 | /* Set ADC error code to ADC IP internal error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1395 | hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL; |
mbed_official | 181:a4cbdfbbd2f4 | 1396 | |
mbed_official | 181:a4cbdfbbd2f4 | 1397 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1398 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1399 | |
mbed_official | 181:a4cbdfbbd2f4 | 1400 | /* Enable the ADC peripheral */ |
mbed_official | 181:a4cbdfbbd2f4 | 1401 | __HAL_ADC_ENABLE(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1402 | |
mbed_official | 181:a4cbdfbbd2f4 | 1403 | /* Wait for ADC effectively enabled */ |
mbed_official | 181:a4cbdfbbd2f4 | 1404 | /* Get timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 1405 | tickstart = HAL_GetTick(); |
mbed_official | 181:a4cbdfbbd2f4 | 1406 | |
mbed_official | 181:a4cbdfbbd2f4 | 1407 | while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET) |
mbed_official | 181:a4cbdfbbd2f4 | 1408 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1409 | /* Check for the Timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 1410 | if(ADC_ENABLE_TIMEOUT != HAL_MAX_DELAY) |
mbed_official | 181:a4cbdfbbd2f4 | 1411 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1412 | if((HAL_GetTick() - tickstart ) > ADC_ENABLE_TIMEOUT) |
mbed_official | 181:a4cbdfbbd2f4 | 1413 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1414 | /* Update ADC state machine to error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1415 | hadc->State = HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1416 | |
mbed_official | 181:a4cbdfbbd2f4 | 1417 | /* Set ADC error code to ADC IP internal error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1418 | hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL; |
mbed_official | 181:a4cbdfbbd2f4 | 1419 | |
mbed_official | 181:a4cbdfbbd2f4 | 1420 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1421 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1422 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1423 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1424 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1425 | |
mbed_official | 181:a4cbdfbbd2f4 | 1426 | /* Return HAL status */ |
mbed_official | 181:a4cbdfbbd2f4 | 1427 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 1428 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1429 | |
mbed_official | 181:a4cbdfbbd2f4 | 1430 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1431 | * @brief Disable the selected ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1432 | * @note Prerequisite condition to use this function: ADC conversions must be |
mbed_official | 181:a4cbdfbbd2f4 | 1433 | * stopped to disable the ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1434 | * @param hadc: ADC handle |
mbed_official | 181:a4cbdfbbd2f4 | 1435 | * @retval HAL status. |
mbed_official | 181:a4cbdfbbd2f4 | 1436 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1437 | static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc) |
mbed_official | 181:a4cbdfbbd2f4 | 1438 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1439 | uint32_t tickstart = 0; |
mbed_official | 181:a4cbdfbbd2f4 | 1440 | |
mbed_official | 181:a4cbdfbbd2f4 | 1441 | /* Verification if ADC is not already disabled: */ |
mbed_official | 181:a4cbdfbbd2f4 | 1442 | /* forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */ |
mbed_official | 181:a4cbdfbbd2f4 | 1443 | /* disabled. */ |
mbed_official | 181:a4cbdfbbd2f4 | 1444 | if (__HAL_ADC_IS_ENABLED(hadc) != RESET ) |
mbed_official | 181:a4cbdfbbd2f4 | 1445 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1446 | /* Check if conditions to disable the ADC are fulfilled */ |
mbed_official | 181:a4cbdfbbd2f4 | 1447 | if (__HAL_ADC_DISABLING_CONDITIONS(hadc) != RESET) |
mbed_official | 181:a4cbdfbbd2f4 | 1448 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1449 | /* Disable the ADC peripheral */ |
mbed_official | 181:a4cbdfbbd2f4 | 1450 | __HAL_ADC_DISABLE(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1451 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1452 | else |
mbed_official | 181:a4cbdfbbd2f4 | 1453 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1454 | /* Update ADC state machine to error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1455 | hadc->State = HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1456 | |
mbed_official | 181:a4cbdfbbd2f4 | 1457 | /* Set ADC error code to ADC internal error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1458 | hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL; |
mbed_official | 181:a4cbdfbbd2f4 | 1459 | |
mbed_official | 181:a4cbdfbbd2f4 | 1460 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1461 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1462 | |
mbed_official | 181:a4cbdfbbd2f4 | 1463 | /* Wait for ADC effectively disabled */ |
mbed_official | 181:a4cbdfbbd2f4 | 1464 | /* Get timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 1465 | tickstart = HAL_GetTick(); |
mbed_official | 181:a4cbdfbbd2f4 | 1466 | |
mbed_official | 181:a4cbdfbbd2f4 | 1467 | while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN)) |
mbed_official | 181:a4cbdfbbd2f4 | 1468 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1469 | /* Check for the Timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 1470 | if(ADC_ENABLE_TIMEOUT != HAL_MAX_DELAY) |
mbed_official | 181:a4cbdfbbd2f4 | 1471 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1472 | if((HAL_GetTick() - tickstart ) > ADC_DISABLE_TIMEOUT) |
mbed_official | 181:a4cbdfbbd2f4 | 1473 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1474 | /* Update ADC state machine to error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1475 | hadc->State = HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1476 | |
mbed_official | 181:a4cbdfbbd2f4 | 1477 | /* Set ADC error code to ADC internal error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1478 | hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL; |
mbed_official | 181:a4cbdfbbd2f4 | 1479 | |
mbed_official | 181:a4cbdfbbd2f4 | 1480 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1481 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1482 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1483 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1484 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1485 | |
mbed_official | 181:a4cbdfbbd2f4 | 1486 | /* Return HAL status */ |
mbed_official | 181:a4cbdfbbd2f4 | 1487 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 1488 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1489 | |
mbed_official | 181:a4cbdfbbd2f4 | 1490 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1491 | * @brief Stop ADC conversion. |
mbed_official | 181:a4cbdfbbd2f4 | 1492 | * @note Prerequisite condition to use this function: ADC conversions must be |
mbed_official | 181:a4cbdfbbd2f4 | 1493 | * stopped to disable the ADC. |
mbed_official | 181:a4cbdfbbd2f4 | 1494 | * @param hadc: ADC handle |
mbed_official | 181:a4cbdfbbd2f4 | 1495 | * @param ConversionGroup: Only ADC group regular. |
mbed_official | 181:a4cbdfbbd2f4 | 1496 | * This parameter can be one of the following values: |
mbed_official | 181:a4cbdfbbd2f4 | 1497 | * @arg REGULAR_GROUP: ADC regular conversion type. |
mbed_official | 181:a4cbdfbbd2f4 | 1498 | * @retval HAL status. |
mbed_official | 181:a4cbdfbbd2f4 | 1499 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1500 | static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc, uint32_t ConversionGroup) |
mbed_official | 181:a4cbdfbbd2f4 | 1501 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1502 | uint32_t tickstart = 0 ; |
mbed_official | 181:a4cbdfbbd2f4 | 1503 | |
mbed_official | 181:a4cbdfbbd2f4 | 1504 | /* Check the parameters */ |
mbed_official | 181:a4cbdfbbd2f4 | 1505 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
mbed_official | 181:a4cbdfbbd2f4 | 1506 | assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup)); |
mbed_official | 181:a4cbdfbbd2f4 | 1507 | |
mbed_official | 181:a4cbdfbbd2f4 | 1508 | /* Verification: if ADC is not already stopped, bypass this function */ |
mbed_official | 181:a4cbdfbbd2f4 | 1509 | if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART)) |
mbed_official | 181:a4cbdfbbd2f4 | 1510 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1511 | /* Stop potential conversion on regular group */ |
mbed_official | 181:a4cbdfbbd2f4 | 1512 | if (ConversionGroup == REGULAR_GROUP) |
mbed_official | 181:a4cbdfbbd2f4 | 1513 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1514 | /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */ |
mbed_official | 181:a4cbdfbbd2f4 | 1515 | if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) && \ |
mbed_official | 181:a4cbdfbbd2f4 | 1516 | HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) ) |
mbed_official | 181:a4cbdfbbd2f4 | 1517 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1518 | /* Stop conversions on regular group */ |
mbed_official | 181:a4cbdfbbd2f4 | 1519 | hadc->Instance->CR |= ADC_CR_ADSTP; |
mbed_official | 181:a4cbdfbbd2f4 | 1520 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1521 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1522 | |
mbed_official | 181:a4cbdfbbd2f4 | 1523 | /* Wait for conversion effectively stopped */ |
mbed_official | 181:a4cbdfbbd2f4 | 1524 | /* Get timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 1525 | tickstart = HAL_GetTick(); |
mbed_official | 181:a4cbdfbbd2f4 | 1526 | |
mbed_official | 181:a4cbdfbbd2f4 | 1527 | while((hadc->Instance->CR & ADC_CR_ADSTART) != RESET) |
mbed_official | 181:a4cbdfbbd2f4 | 1528 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1529 | /* Check for the Timeout */ |
mbed_official | 181:a4cbdfbbd2f4 | 1530 | if(ADC_STOP_CONVERSION_TIMEOUT != HAL_MAX_DELAY) |
mbed_official | 181:a4cbdfbbd2f4 | 1531 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1532 | if((HAL_GetTick() - tickstart ) > ADC_STOP_CONVERSION_TIMEOUT) |
mbed_official | 181:a4cbdfbbd2f4 | 1533 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1534 | /* Update ADC state machine to error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1535 | hadc->State = HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1536 | |
mbed_official | 181:a4cbdfbbd2f4 | 1537 | /* Set ADC error code to ADC IP internal error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1538 | hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL; |
mbed_official | 181:a4cbdfbbd2f4 | 1539 | |
mbed_official | 181:a4cbdfbbd2f4 | 1540 | return HAL_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1541 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1542 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1543 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1544 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1545 | |
mbed_official | 181:a4cbdfbbd2f4 | 1546 | /* Return HAL status */ |
mbed_official | 181:a4cbdfbbd2f4 | 1547 | return HAL_OK; |
mbed_official | 181:a4cbdfbbd2f4 | 1548 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1549 | |
mbed_official | 181:a4cbdfbbd2f4 | 1550 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1551 | * @brief DMA transfer complete callback. |
mbed_official | 181:a4cbdfbbd2f4 | 1552 | * @param hdma: pointer to DMA handle. |
mbed_official | 181:a4cbdfbbd2f4 | 1553 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 1554 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1555 | static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 181:a4cbdfbbd2f4 | 1556 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1557 | ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
mbed_official | 181:a4cbdfbbd2f4 | 1558 | |
mbed_official | 181:a4cbdfbbd2f4 | 1559 | /* Change ADC state */ |
mbed_official | 181:a4cbdfbbd2f4 | 1560 | hadc->State = HAL_ADC_STATE_EOC; |
mbed_official | 181:a4cbdfbbd2f4 | 1561 | |
mbed_official | 181:a4cbdfbbd2f4 | 1562 | HAL_ADC_ConvCpltCallback(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1563 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1564 | |
mbed_official | 181:a4cbdfbbd2f4 | 1565 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1566 | * @brief DMA half transfer complete callback. |
mbed_official | 181:a4cbdfbbd2f4 | 1567 | * @param hdma: pointer to DMA handle. |
mbed_official | 181:a4cbdfbbd2f4 | 1568 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 1569 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1570 | static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 181:a4cbdfbbd2f4 | 1571 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1572 | ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
mbed_official | 181:a4cbdfbbd2f4 | 1573 | |
mbed_official | 181:a4cbdfbbd2f4 | 1574 | /* Conversion complete callback */ |
mbed_official | 181:a4cbdfbbd2f4 | 1575 | HAL_ADC_ConvHalfCpltCallback(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1576 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1577 | |
mbed_official | 181:a4cbdfbbd2f4 | 1578 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1579 | * @brief DMA error callback |
mbed_official | 181:a4cbdfbbd2f4 | 1580 | * @param hdma: pointer to DMA handle. |
mbed_official | 181:a4cbdfbbd2f4 | 1581 | * @retval None |
mbed_official | 181:a4cbdfbbd2f4 | 1582 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1583 | static void ADC_DMAError(DMA_HandleTypeDef *hdma) |
mbed_official | 181:a4cbdfbbd2f4 | 1584 | { |
mbed_official | 181:a4cbdfbbd2f4 | 1585 | ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
mbed_official | 181:a4cbdfbbd2f4 | 1586 | hadc->State= HAL_ADC_STATE_ERROR; |
mbed_official | 181:a4cbdfbbd2f4 | 1587 | /* Set ADC error code to DMA error */ |
mbed_official | 181:a4cbdfbbd2f4 | 1588 | hadc->ErrorCode |= HAL_ADC_ERROR_DMA; |
mbed_official | 181:a4cbdfbbd2f4 | 1589 | HAL_ADC_ErrorCallback(hadc); |
mbed_official | 181:a4cbdfbbd2f4 | 1590 | } |
mbed_official | 181:a4cbdfbbd2f4 | 1591 | |
mbed_official | 181:a4cbdfbbd2f4 | 1592 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1593 | * @} |
mbed_official | 181:a4cbdfbbd2f4 | 1594 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1595 | |
mbed_official | 181:a4cbdfbbd2f4 | 1596 | #endif /* HAL_ADC_MODULE_ENABLED */ |
mbed_official | 181:a4cbdfbbd2f4 | 1597 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1598 | * @} |
mbed_official | 181:a4cbdfbbd2f4 | 1599 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1600 | |
mbed_official | 181:a4cbdfbbd2f4 | 1601 | /** |
mbed_official | 181:a4cbdfbbd2f4 | 1602 | * @} |
mbed_official | 181:a4cbdfbbd2f4 | 1603 | */ |
mbed_official | 181:a4cbdfbbd2f4 | 1604 | |
mbed_official | 181:a4cbdfbbd2f4 | 1605 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |