You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "Swatantra-66 (via GitHub)" <gi...@apache.org> on 2026/10/13 17:41:00 UTC

[PR] arch/arm/stm32: fix invalid device pointer in adc_sampletime_cfg [nuttx]

Swatantra-66 opened a new pull request, #20642:
URL: https://github.com/apache/nuttx/pull/20642

   ## 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`:
   
   ```c
   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 *`:
   
   ```c
   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] arch/arm/stm32: fix invalid device pointer in adc_sampletime_cfg [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on PR #20642:
URL: https://github.com/apache/nuttx/pull/20642#issuecomment-6155042072

   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] arch/arm/stm32: fix invalid device pointer in adc_sampletime_cfg [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 merged PR #20642:
URL: https://github.com/apache/nuttx/pull/20642


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] arch/arm/stm32: fix invalid device pointer in adc_sampletime_cfg [nuttx]

Posted by "michallenc (via GitHub)" <gi...@apache.org>.
michallenc commented on PR #20642:
URL: https://github.com/apache/nuttx/pull/20642#issuecomment-6154150518

   @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?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] arch/arm/stm32: fix invalid device pointer in adc_sampletime_cfg [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on PR #20642:
URL: https://github.com/apache/nuttx/pull/20642#issuecomment-6214233799

   Done! @jerpelea Rebased onto master and added `Assisted-by: Google Gemini` to the commit message (commit b01cac0014). Thank you for the review!
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] arch/arm/stm32: fix invalid device pointer in adc_sampletime_cfg [nuttx]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #20642:
URL: https://github.com/apache/nuttx/pull/20642#issuecomment-6160248062

   <!-- membrowse-pr-comment -->
   ## <img src="https://membrowse.com/membrowse-logo.svg" height="24" align="top"> MemBrowse Memory Report
   
   *No memory changes detected for:*
   - [arduino-mega2560](https://membrowse.com/public/apache/nuttx/target/arduino-mega2560/compare?head=14c206a9e204feef82f8f62badb94dff9f33ce43&base=665043ccb2d5892deb4b433e98e901d8b541017c)
   - [esp32-devkitc](https://membrowse.com/public/apache/nuttx/target/esp32-devkitc/compare?head=14c206a9e204feef82f8f62badb94dff9f33ce43&base=665043ccb2d5892deb4b433e98e901d8b541017c)
   - [hifive1-revb](https://membrowse.com/public/apache/nuttx/target/hifive1-revb/compare?head=14c206a9e204feef82f8f62badb94dff9f33ce43&base=665043ccb2d5892deb4b433e98e901d8b541017c)
   - [mirtoo](https://membrowse.com/public/apache/nuttx/target/mirtoo/compare?head=14c206a9e204feef82f8f62badb94dff9f33ce43&base=665043ccb2d5892deb4b433e98e901d8b541017c)
   - [qemu-armv8a](https://membrowse.com/public/apache/nuttx/target/qemu-armv8a/compare?head=14c206a9e204feef82f8f62badb94dff9f33ce43&base=665043ccb2d5892deb4b433e98e901d8b541017c)
   - [qemu-intel64](https://membrowse.com/public/apache/nuttx/target/qemu-intel64/compare?head=14c206a9e204feef82f8f62badb94dff9f33ce43&base=bb4d8691759e59aaab6f9fb604a29cf3e6d3705a)
   - [rx65n-rsk2mb](https://membrowse.com/public/apache/nuttx/target/rx65n-rsk2mb/compare?head=14c206a9e204feef82f8f62badb94dff9f33ce43&base=665043ccb2d5892deb4b433e98e901d8b541017c)
   - [s698pm-dkit](https://membrowse.com/public/apache/nuttx/target/s698pm-dkit/compare?head=14c206a9e204feef82f8f62badb94dff9f33ce43&base=665043ccb2d5892deb4b433e98e901d8b541017c)
   - [stm32-nucleo-f103rb](https://membrowse.com/public/apache/nuttx/target/stm32-nucleo-f103rb/compare?head=14c206a9e204feef82f8f62badb94dff9f33ce43&base=665043ccb2d5892deb4b433e98e901d8b541017c)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] arch/arm/stm32: fix invalid device pointer in adc_sampletime_cfg [nuttx]

Posted by "raiden00pl (via GitHub)" <gi...@apache.org>.
raiden00pl commented on PR #20642:
URL: https://github.com/apache/nuttx/pull/20642#issuecomment-6163255550

   @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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] arch/arm/stm32: fix invalid device pointer in adc_sampletime_cfg [nuttx]

Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea commented on PR #20642:
URL: https://github.com/apache/nuttx/pull/20642#issuecomment-6213842953

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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org