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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to