Skip to content

arch/arm/stm32: fix invalid device pointer in adc_sampletime_cfg - #20642

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/stm32-adc-sampletime-priv
Oct 17, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/stm32-adc-sampletime-priv

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Fixes #19554.

In adc_sampletime_cfg() in arch/arm/src/common/stm32/stm32_adc_m0_v1.c, the generic upper-half ADC device pointer dev (struct adc_dev_s *) was passed directly to adc_sampletime_set() and adc_sampletime_write() under CONFIG_STM32_ADC_CHANGE_SAMPLETIME:

adc_sampletime_set((struct stm32_adc_dev_s *)dev, &time_samples);
adc_sampletime_write((struct stm32_adc_dev_s *)dev);

Both helper functions internally cast the received pointer back to struct stm32_dev_s *:

static void adc_sampletime_write(struct stm32_adc_dev_s *dev)
{
  struct stm32_dev_s *priv = (struct stm32_dev_s *)dev;
  ...
  adc_putreg(priv, STM32_ADC_SMPR_OFFSET, smpr);
}

Because struct adc_dev_s (upper-half) and struct stm32_dev_s (lower-half) are distinct structures with different layouts, casting dev resulted in:

  1. Sample-time values being stored into incorrect field offsets.
  2. priv->base in adc_putreg() reading garbage instead of the peripheral register base address, writing to incorrect memory locations instead of configuring hardware registers.

Changes Made

Updated adc_sampletime_cfg() in stm32_adc_m0_v1.c to pass dev->ad_priv (which points to struct stm32_dev_s), matching the implementation in sibling drivers:

  • arch/arm/src/common/stm32/stm32_adc_m3m4_v1v2.c (line 2621)
  • arch/arm/src/stm32f7/stm32_adc.c (line 1591)
  • The existing #else block in stm32_adc_m0_v1.c (line 1469)

Impact

  • Is new feature added? No
  • Is existing feature changed? No (bugfix only)
  • Impact on user: Fixes sample-time configuration and memory corruption on STM32 Cortex-M0 targets when CONFIG_STM32_ADC_CHANGE_SAMPLETIME is enabled.
  • Impact on build: None
  • Impact on hardware: STM32 M0 family with ADCv2 (CONFIG_STM32_STM32F0 / CONFIG_STM32_STM32G0).

Testing

  • Code reviewed against sibling implementations in stm32_adc_m3m4_v1v2.c and stm32_adc.c.
  • Verified struct field offsets and pointer types between struct adc_dev_s and struct stm32_dev_s.
  • Adheres to NuttX C coding style standards.

@michallenc

Copy link
Copy Markdown
Contributor

@raiden00pl I am quite confused by the implementation of ADC driver. You probably have the most experience with STM here, do you know why do we keep two structures, private stm32_dev_s (that in contradiction to its name is for adc only) and public stm32_adc_dev_s with callbacks only and have to cast between them?

@Swatantra-66

Copy link
Copy Markdown
Contributor Author

Hi @michallenc,

To provide some context on the bug this PR fixes:

adc_sampletime_cfg() is called from the upper-half character driver path where the input argument dev is struct adc_dev_s *. The underlying STM32 private hardware state is stored in dev->ad_priv (struct stm32_dev_s *).

stm32_adc_m0_v1.c was mistakenly passing the upper-half pointer dev directly into adc_sampletime_set() and adc_sampletime_write(). Both functions cast their parameter to (struct stm32_dev_s *), reading priv->base from bogus offsets in struct adc_dev_s. Passing (struct stm32_adc_dev_s *)dev->ad_priv aligns this driver with sibling implementations in stm32_adc_m3m4_v1v2.c:2621 and stm32_adc.c:1591.

Regarding the two structures: stm32_dev_s is the actual hardware instance state, while stm32_adc_dev_s serves as the handle for CONFIG_STM32_ADC_LL_OPS (used by lower-level subsystems like motor control / FOC). Unifying or renaming them (e.g. stm32_dev_s -> stm32_adc_priv_s) would certainly improve clarity in a dedicated refactoring PR.

@Swatantra-66
Swatantra-66 force-pushed the fix/stm32-adc-sampletime-priv branch from 2afbb26 to 14c206a Compare October 13, 2026 21:11
@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Size: XS The size of the change in this PR is very small labels Oct 13, 2026
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@raiden00pl

raiden00pl commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

@michallenc as @Swatantra-66 mentioned, the structural mess is from the fact that some ADC functions can be used to implement dedicated drivers with low-level operations. Once I finish unifying the STM32 code, I’ll try to refactor STM32 ADC and split it into proper upper-half and lower-half components, similar to how it’s done for STM32 timers.

The data structure naming in many STM32 drivers is poor (like mentioned stm32_dev_s) and this should be also fixed

@jerpelea

Copy link
Copy Markdown
Contributor

please add Assisted-by: indicating the LLM used according to the community rules

@Swatantra-66
Swatantra-66 force-pushed the fix/stm32-adc-sampletime-priv branch from 7204c05 to 4a8ebe0 Compare October 16, 2026 11:04
In adc_sampletime_cfg() for STM32 M0, the generic upper-half ADC device
pointer 'dev' (struct adc_dev_s *) was passed directly to adc_sampletime_set()
and adc_sampletime_write().

Both functions expect the driver lower-half pointer and cast it to
'struct stm32_dev_s *'. Since the upper-half and lower-half objects are
distinct, this resulted in sample-time configuration values and peripheral
register base addresses being written to and read from incorrect offsets.

This commit updates adc_sampletime_cfg() to pass 'dev->ad_priv' matching
the implementation in sibling drivers (stm32_adc_m3m4_v1v2.c and stm32_adc.c).

Fixes: apache#19554
Assisted-by: Google Gemini
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/stm32-adc-sampletime-priv branch from 4a8ebe0 to b01cac0 Compare October 16, 2026 11:08
@Swatantra-66

Swatantra-66 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Done! @jerpelea Rebased onto master and added Assisted-by: Google Gemini to the commit message (commit b01cac0).

@xiaoxiang781216
xiaoxiang781216 merged commit 01c96f8 into apache:master Oct 17, 2026
35 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

STM32 M0 ADC: adc_sampletime_cfg() passes generic adc_dev_s instead of dev->ad_priv

5 participants