RIOT-OS / RIOT

RIOT - The friendly OS for IoT
https://riot-os.org
GNU Lesser General Public License v2.1
4.91k stars 1.99k forks source link

STM32 f4_f7 ADC sample time register configuration #20261

Closed sada45 closed 8 months ago

sada45 commented 9 months ago

Hi~ I am learning the ADC sampling in on STM32F722, and I think I find a bug in the file cpu/stm32/periph/adc_f4_f7.c. In function adc_init(), the code setting the sampling time is follows:

  if (adc_config[line].chan >= 10) {
      dev(line)->SMPR1 &= ~(MAX_ADC_SMP << (3 * (adc_config[line].chan - 10)));
      dev(line)->SMPR1 |= MAX_ADC_SMP << (3 * (adc_config[line].chan - 10));
  }
  else {
      dev(line)->SMPR1 &= ~(MAX_ADC_SMP << (3 * adc_config[line].chan));
      dev(line)->SMPR2 |= MAX_ADC_SMP << (3 * adc_config[line].chan);
  }

To my understanding, the code first clear the SMP<chan> bits in the SMPR1 and SMPR2, then set the new value.

When the chan<10, the SMP are all in the SMPR2 register, so the correct code for chan<10 should be:

  if (adc_config[line].chan >= 10) {
          ........
  }
else {
    dev(line)->SMPR2 &= ~(MAX_ADC_SMP << (3 * adc_config[line].chan));
    dev(line)->SMPR2 |= MAX_ADC_SMP << (3 * adc_config[line].chan);
}
maribu commented 9 months ago

Thx for spotting the issue :)

Would you mind to open a pull request with the fix? Feel free to ping me in the pull request