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 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:
Configure system
Initialize HAL ADC handle using HAL_ADC_Init()
Case of multiple ADC instances used: Link HAL ADC handles using HAL_ADC_SetLinkNextHandle() (for more details, refer to function description).
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:
Activate ADC using HAL_ADC_Start()
Perform ADC calibration using HAL_ADC_Calibrate()
Start ADC conversions using functions HAL_ADC_[INJ|REG|MM]_StartConv…() (refer to list above)
Process conversion data using HAL_ADC_[INJ|REG|MM]_GetValue(), IRQ handler and callback functions, DMA buffers
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().
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:
HAL_ADC_ErrorCallback() : ADC error callback
HAL_ADC_REG_EndOfSamplingCallback() : ADC group regular end of sampling phase callback
HAL_ADC_REG_UnitaryConvCpltCallback() : ADC group regular end of unitary conversion callback
HAL_ADC_REG_SequenceConvCpltCallback() : ADC group regular end of sequence conversions callback
HAL_ADC_REG_DataTransferHalfCallback() : ADC group regular conversion data buffer half transfer callback (under condition of USE_HAL_ADC_DMA activated)
HAL_ADC_REG_DataTransferCpltCallback() : ADC group regular conversion data buffer transfer complete callback (under condition of USE_HAL_ADC_DMA activated)
HAL_ADC_REG_DataTransferStopCallback() : ADC group regular conversion data transfer abort callback (under condition of USE_HAL_ADC_DMA activated)
HAL_ADC_INJ_UnitaryConvCpltCallback() : ADC group injected end of unitary conversion callback
HAL_ADC_INJ_SequenceConvCpltCallback() : ADC group injected end of sequence conversions callback
HAL_ADC_AnalogWD_OutOfWindowCallback() : ADC analog watchdog out of window event callback
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.