STM32F4xx HAL Documentation
Hardware Abstraction Layer for STM32F4 familiy
Loading...
Searching...
No Matches
stm32f4xx_hal_adc.c File Reference

This file provides firmware functions to manage the following functionalities of the Analog to Digital Converter (ADC) peripheral: More...

#include "stm32f4xx_hal.h"
Include dependency graph for stm32f4xx_hal_adc.c:

Go to the source code of this file.

Functions

static void ADC_Init (ADC_HandleTypeDef *hadc)
 Initializes the ADCx peripheral according to the specified parameters in the ADC_InitStruct without initializing the ADC MSP.
 
static void ADC_DMAConvCplt (DMA_HandleTypeDef *hdma)
 DMA transfer complete callback.
 
static void ADC_DMAError (DMA_HandleTypeDef *hdma)
 DMA error callback.
 
static void ADC_DMAHalfConvCplt (DMA_HandleTypeDef *hdma)
 DMA half transfer complete callback.
 
HAL_StatusTypeDef HAL_ADC_Init (ADC_HandleTypeDef *hadc)
 Initializes the ADCx peripheral according to the specified parameters in the ADC_InitStruct and initializes the ADC MSP.
 
HAL_StatusTypeDef HAL_ADC_DeInit (ADC_HandleTypeDef *hadc)
 Deinitializes the ADCx peripheral registers to their default reset values.
 
void HAL_ADC_MspInit (ADC_HandleTypeDef *hadc)
 Initializes the ADC MSP.
 
void HAL_ADC_MspDeInit (ADC_HandleTypeDef *hadc)
 DeInitializes the ADC MSP.
 
HAL_StatusTypeDef HAL_ADC_Start (ADC_HandleTypeDef *hadc)
 Enables ADC and starts conversion of the regular channels.
 
HAL_StatusTypeDef HAL_ADC_Stop (ADC_HandleTypeDef *hadc)
 Disables ADC and stop conversion of regular channels.
 
HAL_StatusTypeDef HAL_ADC_PollForConversion (ADC_HandleTypeDef *hadc, uint32_t Timeout)
 Poll for regular conversion complete.
 
HAL_StatusTypeDef HAL_ADC_PollForEvent (ADC_HandleTypeDef *hadc, uint32_t EventType, uint32_t Timeout)
 Poll for conversion event.
 
HAL_StatusTypeDef HAL_ADC_Start_IT (ADC_HandleTypeDef *hadc)
 Enables the interrupt and starts ADC conversion of regular channels.
 
HAL_StatusTypeDef HAL_ADC_Stop_IT (ADC_HandleTypeDef *hadc)
 Disables the interrupt and stop ADC conversion of regular channels.
 
void HAL_ADC_IRQHandler (ADC_HandleTypeDef *hadc)
 Handles ADC interrupt request.
 
HAL_StatusTypeDef HAL_ADC_Start_DMA (ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
 Enables ADC DMA request after last transfer (Single-ADC mode) and enables ADC peripheral.
 
HAL_StatusTypeDef HAL_ADC_Stop_DMA (ADC_HandleTypeDef *hadc)
 Disables ADC DMA (Single-ADC mode) and disables ADC peripheral.
 
uint32_t HAL_ADC_GetValue (ADC_HandleTypeDef *hadc)
 Gets the converted value from data register of regular channel.
 
void HAL_ADC_ConvCpltCallback (ADC_HandleTypeDef *hadc)
 Regular conversion complete callback in non blocking mode.
 
void HAL_ADC_ConvHalfCpltCallback (ADC_HandleTypeDef *hadc)
 Regular conversion half DMA transfer callback in non blocking mode.
 
void HAL_ADC_LevelOutOfWindowCallback (ADC_HandleTypeDef *hadc)
 Analog watchdog callback in non blocking mode.
 
void HAL_ADC_ErrorCallback (ADC_HandleTypeDef *hadc)
 Error ADC callback.
 
HAL_StatusTypeDef HAL_ADC_ConfigChannel (ADC_HandleTypeDef *hadc, ADC_ChannelConfTypeDef *sConfig)
 Configures for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
 
HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig (ADC_HandleTypeDef *hadc, ADC_AnalogWDGConfTypeDef *AnalogWDGConfig)
 Configures the analog watchdog.
 
uint32_t HAL_ADC_GetState (ADC_HandleTypeDef *hadc)
 return the ADC state
 
uint32_t HAL_ADC_GetError (ADC_HandleTypeDef *hadc)
 Return the ADC error code.
 

Detailed Description

This file provides firmware functions to manage the following functionalities of the Analog to Digital Converter (ADC) peripheral:

Author
MCD Application Team
  • Initialization and de-initialization functions
  • Peripheral Control functions
  • Peripheral State functions
Attention

Copyright (c) 2017 STMicroelectronics. All rights reserved.

This software is licensed under terms that can be found in the LICENSE file in the root directory of this software component. If no LICENSE file comes with this software, it is provided AS-IS.

==============================================================================
                  ##### ADC Peripheral features #####
==============================================================================
[..]
(#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
(#) Interrupt generation at the end of conversion, end of injected conversion,
    and in case of analog watchdog or overrun events
(#) Single and continuous conversion modes.
(#) Scan mode for automatic conversion of channel 0 to channel x.
(#) Data alignment with in-built data coherency.
(#) Channel-wise programmable sampling time.
(#) External trigger option with configurable polarity for both regular and
    injected conversion.
(#) Dual/Triple mode (on devices with 2 ADCs or more).
(#) Configurable DMA data storage in Dual/Triple ADC mode.
(#) Configurable delay between conversions in Dual/Triple interleaved mode.
(#) ADC conversion type (refer to the datasheets).
(#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
    slower speed.
(#) ADC input range: VREF(minus) = VIN = VREF(plus).
(#) DMA request generation during regular channel conversion.


                   ##### How to use this driver #####
==============================================================================
[..]
(#)Initialize the ADC low level resources by implementing the HAL_ADC_MspInit():
     (##) Enable the ADC interface clock using __HAL_RCC_ADC_CLK_ENABLE()
     (##) ADC pins configuration
           (+++) Enable the clock for the ADC GPIOs using the following function:
                 __HAL_RCC_GPIOx_CLK_ENABLE()
           (+++) Configure these ADC pins in analog mode using HAL_GPIO_Init()
     (##) In case of using interrupts (e.g. HAL_ADC_Start_IT())
           (+++) Configure the ADC interrupt priority using HAL_NVIC_SetPriority()
           (+++) Enable the ADC IRQ handler using HAL_NVIC_EnableIRQ()
           (+++) In ADC IRQ handler, call HAL_ADC_IRQHandler()
     (##) In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA())
           (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE()
           (+++) Configure and enable two DMA streams stream for managing data
               transfer from peripheral to memory (output stream)
           (+++) Associate the initialized DMA handle to the CRYP DMA handle
               using  __HAL_LINKDMA()
           (+++) Configure the priority and enable the NVIC for the transfer complete
               interrupt on the two DMA Streams. The output stream should have higher
               priority than the input stream.

  *** Configuration of ADC, groups regular/injected, channels parameters ***
==============================================================================
[..]
(#) Configure the ADC parameters (resolution, data alignment, ...)
    and regular group parameters (conversion trigger, sequencer, ...)
    using function HAL_ADC_Init().

(#) Configure the channels for regular group parameters (channel number,
    channel rank into sequencer, ..., into regular group)
    using function HAL_ADC_ConfigChannel().

(#) Optionally, configure the injected group parameters (conversion trigger,
    sequencer, ..., of injected group)
    and the channels for injected group parameters (channel number,
    channel rank into sequencer, ..., into injected group)
    using function HAL_ADCEx_InjectedConfigChannel().

(#) Optionally, configure the analog watchdog parameters (channels
    monitored, thresholds, ...) using function HAL_ADC_AnalogWDGConfig().

(#) Optionally, for devices with several ADC instances: configure the
    multimode parameters using function HAL_ADCEx_MultiModeConfigChannel().

                     *** Execution of ADC conversions ***
==============================================================================
[..]
(#) ADC driver can be used among three modes: polling, interruption,
    transfer by DMA.

   *** Polling mode IO operation ***
   =================================
   [..]
     (+) Start the ADC peripheral using HAL_ADC_Start()
     (+) Wait for end of conversion using HAL_ADC_PollForConversion(), at this stage
         user can specify the value of timeout according to his end application
     (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.
     (+) Stop the ADC peripheral using HAL_ADC_Stop()

   *** Interrupt mode IO operation ***
   ===================================
   [..]
     (+) Start the ADC peripheral using HAL_ADC_Start_IT()
     (+) Use HAL_ADC_IRQHandler() called under ADC_IRQHandler() Interrupt subroutine
     (+) At ADC end of conversion HAL_ADC_ConvCpltCallback() function is executed and user can
         add his own code by customization of function pointer HAL_ADC_ConvCpltCallback
     (+) In case of ADC Error, HAL_ADC_ErrorCallback() function is executed and user can
         add his own code by customization of function pointer HAL_ADC_ErrorCallback
     (+) Stop the ADC peripheral using HAL_ADC_Stop_IT()

   *** DMA mode IO operation ***
   ==============================
   [..]
     (+) Start the ADC peripheral using HAL_ADC_Start_DMA(), at this stage the user specify the length
         of data to be transferred at each end of conversion
     (+) At The end of data transfer by HAL_ADC_ConvCpltCallback() function is executed and user can
         add his own code by customization of function pointer HAL_ADC_ConvCpltCallback
     (+) In case of transfer Error, HAL_ADC_ErrorCallback() function is executed and user can
         add his own code by customization of function pointer HAL_ADC_ErrorCallback
     (+) Stop the ADC peripheral using HAL_ADC_Stop_DMA()

   *** ADC HAL driver macros list ***
   =============================================
   [..]
     Below the list of most used macros in ADC HAL driver.

    (+) __HAL_ADC_ENABLE : Enable the ADC peripheral
    (+) __HAL_ADC_DISABLE : Disable the ADC peripheral
    (+) __HAL_ADC_ENABLE_IT: Enable the ADC end of conversion interrupt
    (+) __HAL_ADC_DISABLE_IT: Disable the ADC end of conversion interrupt
    (+) __HAL_ADC_GET_IT_SOURCE: Check if the specified ADC interrupt source is enabled or disabled
    (+) __HAL_ADC_CLEAR_FLAG: Clear the ADC's pending flags
    (+) __HAL_ADC_GET_FLAG: Get the selected ADC's flag status
    (+) ADC_GET_RESOLUTION: Return resolution bits in CR1 register

   [..]
     (@) You can refer to the ADC HAL driver header file for more useful macros

                    *** Deinitialization of ADC ***
==============================================================================
[..]
(#) Disable the ADC interface
   (++) ADC clock can be hard reset and disabled at RCC top level.
   (++) Hard reset of ADC peripherals
        using macro __HAL_RCC_ADC_FORCE_RESET(), __HAL_RCC_ADC_RELEASE_RESET().
   (++) ADC clock disable using the equivalent macro/functions as configuration step.
             (+++) Example:
                 Into HAL_ADC_MspDeInit() (recommended code location) or with
                 other device clock parameters configuration:
             (+++) HAL_RCC_GetOscConfig(&RCC_OscInitStructure);
             (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;
             (+++) RCC_OscInitStructure.HSIState = RCC_HSI_OFF; (if not used for system clock)
             (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);

(#) ADC pins configuration
   (++) Disable the clock for the ADC GPIOs using macro __HAL_RCC_GPIOx_CLK_DISABLE()

(#) Optionally, in case of usage of ADC with interruptions:
   (++) Disable the NVIC for ADC using function HAL_NVIC_DisableIRQ(ADCx_IRQn)

(#) Optionally, in case of usage of DMA:
      (++) Deinitialize the DMA using function HAL_DMA_DeInit().
      (++) Disable the NVIC for DMA using function HAL_NVIC_DisableIRQ(DMAx_Channelx_IRQn)
                    *** Callback registration ***
==============================================================================
  [..]

   The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
   allows the user to configure dynamically the driver callbacks.
   Use Functions HAL_ADC_RegisterCallback()
   to register an interrupt callback.
  [..]

   Function HAL_ADC_RegisterCallback() allows to register following callbacks:
     (+) ConvCpltCallback               : ADC conversion complete callback
     (+) ConvHalfCpltCallback           : ADC conversion DMA half-transfer callback
     (+) LevelOutOfWindowCallback       : ADC analog watchdog 1 callback
     (+) ErrorCallback                  : ADC error callback
     (+) InjectedConvCpltCallback       : ADC group injected conversion complete callback
     (+) InjectedQueueOverflowCallback  : ADC group injected context queue overflow callback
     (+) LevelOutOfWindow2Callback      : ADC analog watchdog 2 callback
     (+) LevelOutOfWindow3Callback      : ADC analog watchdog 3 callback
     (+) EndOfSamplingCallback          : ADC end of sampling callback
     (+) MspInitCallback                : ADC Msp Init callback
     (+) MspDeInitCallback              : ADC Msp DeInit callback
   This function takes as parameters the HAL peripheral handle, the Callback ID
   and a pointer to the user callback function.
  [..]

   Use function HAL_ADC_UnRegisterCallback to reset a callback to the default
   weak function.
  [..]

   HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
   and the Callback ID.
   This function allows to reset following callbacks:
     (+) ConvCpltCallback               : ADC conversion complete callback
     (+) ConvHalfCpltCallback           : ADC conversion DMA half-transfer callback
     (+) LevelOutOfWindowCallback       : ADC analog watchdog 1 callback
     (+) ErrorCallback                  : ADC error callback
     (+) InjectedConvCpltCallback       : ADC group injected conversion complete callback
     (+) InjectedQueueOverflowCallback  : ADC group injected context queue overflow callback
     (+) LevelOutOfWindow2Callback      : ADC analog watchdog 2 callback
     (+) LevelOutOfWindow3Callback      : ADC analog watchdog 3 callback
     (+) EndOfSamplingCallback          : ADC end of sampling callback
     (+) MspInitCallback                : ADC Msp Init callback
     (+) MspDeInitCallback              : ADC Msp DeInit callback
   [..]

   By default, after the HAL_ADC_Init() and when the state is HAL_ADC_STATE_RESET
   all callbacks are set to the corresponding weak functions:
   examples HAL_ADC_ConvCpltCallback(), HAL_ADC_ErrorCallback().
   Exception done for MspInit and MspDeInit functions that are
   reset to the legacy weak functions in the HAL_ADC_Init()/ HAL_ADC_DeInit() only when
   these callbacks are null (not registered beforehand).
  [..]

   If MspInit or MspDeInit are not null, the HAL_ADC_Init()/ HAL_ADC_DeInit()
   keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
   [..]

   Callbacks can be registered/unregistered in HAL_ADC_STATE_READY state only.
   Exception done MspInit/MspDeInit functions that can be registered/unregistered
   in HAL_ADC_STATE_READY or HAL_ADC_STATE_RESET state,
   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  [..]

   Then, the user first registers the MspInit/MspDeInit user callbacks
   using HAL_ADC_RegisterCallback() before calling HAL_ADC_DeInit()
   or HAL_ADC_Init() function.
   [..]

   When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
   not defined, the callback registration feature is not available and all callbacks
   are set to the corresponding weak functions.

Definition in file stm32f4xx_hal_adc.c.