stm32/adc: add support for differential mode (DIFSEL) and calibration - #20152
Merged
Merged
Conversation
daniel-p-carvalho
requested review from
gustavonihei,
hartmannathan,
jerpelea,
raiden00pl and
xiaoxiang781216
as code owners
September 15, 2026 09:15
jerpelea
previously approved these changes
Sep 15, 2026
raiden00pl
previously requested changes
Sep 15, 2026
raiden00pl
left a comment
Member
There was a problem hiding this comment.
you should use approach like in STM32H5:
nuttx/arch/arm/src/stm32h5/stm32_adc.c
Lines 308 to 312 in 0276c6a
xiaoxiang781216
previously approved these changes
Sep 15, 2026
daniel-p-carvalho
dismissed stale reviews from xiaoxiang781216 and jerpelea
via
September 16, 2026 00:45
befeb10
daniel-p-carvalho
force-pushed
the
feat/stm32-adc-difsel
branch
from
September 16, 2026 00:45
26f8477 to
befeb10
Compare
Contributor
Author
|
Updated as suggested. Refactored to follow the STM32H5 approach:
|
Add board-level configuration and driver support for differential input
channels and auto-calibration on STM32 ADC IPv2 and IPv2G4 (STM32F3,
STM32G4), following the STM32H5 architecture pattern requested in review:
- In hardware headers (stm32_adc_v2.h and stm32_adc_v2g4.h), fix register
comments and define ADC_DIFSEL register shift and masks.
- In stm32_adc_m3m4_v1v2.c:
- Add difsel field to struct stm32_dev_s initialized from
BOARD_ADCx_DIFSEL if defined, falling back to ADC_DIFSEL_DEFAULT (0).
- Configure DIFSEL before enabling the ADC in adc_configure().
- Fix and enable adc_calibrate() for HAVE_IP_ADC_V2, supporting both
single-ended calibration (ADCALDIF=0) and differential calibration
(ADCALDIF=1) based on the channel selection.
Verified on Nucleo-G431KB (single-ended and differential channel 1).
Assisted-by: Gemini:gemini-2.5-pro
Signed-off-by: Daniel P. Carvalho <danieloak@gmail.com>
daniel-p-carvalho
force-pushed
the
feat/stm32-adc-difsel
branch
from
September 16, 2026 23:34
befeb10 to
ad1fdfe
Compare
xiaoxiang781216
approved these changes
Sep 17, 2026
acassis
approved these changes
Sep 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for ADC differential mode selection (DIFSEL) and fixes auto-calibration for STM32 ADC IPv2 and IPv2G4 (STM32F3 and STM32G4 families).
stm32_adc_v2.handstm32_adc_v2g4.h):ADC_DIFSELshift/masks.Kconfig.adc):menuconfig STM32_ADC_DIFSELunderSTM32_HAVE_IP_ADC_M3M4_V2.CONFIG_STM32_ADCx_DIFSELandCONFIG_STM32_ADCx_DIFSEL_VALUEbitmask (bitstm32_adc_m3m4_v1v2.h/stm32_adc_m3m4_v1v2.c):difselcfgto ADC private structure anddifselcfg_setto lower-half operations.DIFSELinadc_configure()prior to enabling the ADC (adc_enable(priv, true)), strictly adhering to the ST Reference Manual (RM0440 / RM0316).adc_calibrate()forHAVE_IP_ADC_V2: performs single-ended calibration (ADCALDIF=0) and, if differential channels are configured, differential calibration (ADCALDIF=1).nxstyleissues across the driver.Impact
menuconfigor board headers.CONFIG_STM32_ADC_DIFSELis disabled.Testing
nucleo-g431rb:adc./tools/checkpatch.sh -m -g upstream/master..HEADpasses with 0 errors and 0 warnings (✔️ All checks pass.)Hardware Validation Details
Tested on Nucleo-G431KB with ADC1 Channel 1 configured as differential input:
2015(expected 12-bit bipolar midpoint 2048 with minor board noise).4095(maximum positive full-scale).0(minimum negative full-scale).