HAL ADC How to Use

group ADC_How_To_Use

How to use the ADC HAL module driver

HAL ADC driver usage

  • ADC configuration

    • System configuration (out of HAL ADC driver)

      • RCC to provide ADC kernel clock

      • GPIO to connect ADC channels to device pins (if ADC usage with channel other than internal ones)

      • CPU Cortex NVIC to configure interrupts lines (if ADC usage with interrupt)

      • DMA channel (if ADC usage with data transfer by DMA)

    • ADC peripheral configuration

      • ADC peripheral is structured in subblocks with each a specific scope. HAL ADC follows this structure with a configuration structure and associated function for each subblock.

        • Mandatory subblocks, all must be configured:

          • ADC instance

          • ADC channel

        • Mandatory subblocks, at least one must be configured:

          • ADC group regular (prefix REG): main group available on all STM32 series, all ADC instances. Intended for regular data stream.

          • ADC group injected (prefix INJ): alternate group, availability depending on STM32 series and ADC instances. Intended for occasional or as a priority conversions.

        • Optional subblocks

          • Analog watchdog

          • Oversampling

          • Offset

          • Multimode (prefix MM): encompass multiple ADC instances (one master, some slaves) for synchronized operation.

      • ADC instances can belong to an ADC common instance, in this case they can share features (clock configuration, multimode capability, …). HAL ADC driver provides a mechanism to link HAL ADC handles and manage shared features.

    • HAL ADC configuration steps:

      1. Configure system

      2. Initialize HAL ADC handle using HAL_ADC_Init()

      3. Case of multiple ADC instances used: Link HAL ADC handles using HAL_ADC_SetLinkNextHandle() (for more details, refer to function description).

      4. Configure ADC subblocks using functions HAL_ADC_[INJ|REG|MM]_SetConfig{Features}()

  • ADC operation

    • Activation and deactivation

      • ADC peripheral requires a specific procedure for activation (internal analog circuitry control, delay for stabilization time). Note: From activation step, ADC internal analog hardware is enabled, inducing current consumption. Therefore, after ADC usage, ADC must be deactivated for power optimization.

    • Calibration

      • ADC conversion accuracy can be improved by running a self calibration.

    • ADC conversions management

      • Conversions can be performed using three programming models:

        • Polling mode (blocking): using HAL_ADC_[INJ|REG|MM]_StartConv(), HAL_ADC_[INJ|REG|MM]_PollForConv()

        • Interrupt mode: using HAL_ADC_[INJ|REG|MM]_StartConv_IT(), HAL_ADC_IRQHandler_[INJ|REG|AWD]() and callback functions

        • Data transfer by DMA mode: using HAL_ADC_[INJ|REG|MM]_StartConv_DMA()

        • Note: IT and DMA mode can be used with optional interruptions (analog watchdog, …) using functions HAL_ADC_[INJ|REG|MM]_StartConv_IT_Opt(), HAL_ADC_[INJ|REG|MM]_StartConv_DMA_Opt()

    • HAL ADC operation steps:

      1. Activate ADC using HAL_ADC_Start()

      2. Perform ADC calibration using HAL_ADC_Calibrate()

      3. Start ADC conversions using functions HAL_ADC_[INJ|REG|MM]_StartConv…() (refer to list above)

      4. Process conversion data using HAL_ADC_[INJ|REG|MM]_GetValue(), IRQ handler and callback functions, DMA buffers

      5. Stop ADC conversions using functions HAL_ADC_[INJ|REG|MM]_StopConv…() Note: With trigger SW start, conversions iterations without conversion stop operation is possible using function HAL_ADC_[INJ|REG|MM]_TrigNextConv().

      6. Deactivate ADC using HAL_ADC_Stop()

Callback registration

When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 1, functions HAL_ADC_Register…Callback() allow to register following callbacks:

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.