STM32CubeU5 icon indicating copy to clipboard operation
STM32CubeU5 copied to clipboard

ADC oversampling macros incompatible with STM32U575 ADC1 (they are compatible with ADC4)

Open sdt99 opened this issue 2 years ago • 1 comments

I discovered this issue while modifying the Nucleo example project: STM32CubeU5\Projects\NUCLEO-U575ZI-Q\Examples\ADC\ADC_DifferentialMode

The ADC conversion returns correct 14 bit values when oversampling is disabled: hadc1.Init.OversamplingMode = DISABLE;

When I try to enable oversampling I get incorrect values - to a different extent depending on which oversampling I select.

These settings under-read by factor of 3  (0.33)
  hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_64;
  hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_6;

These settings under-read by factor of 3  (0.83)
  hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16;
  hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_4;

I discovered the problem is that the macros ADC_OVERSAMPLING_RATIO_16, ADC_OVERSAMPLING_RATIO_64, and their corresponding macros LL_ADC_OVS_RATIO_X are copied from earlier HAL, which is compatible with the STM32L4+, but not compatible with the STM32U575 ADC1.

The STM32L4 ADC oversampling ratio can only be set as a power of 2 (which corresponds to LL_ADC_OVS_RATIO_X). From RM0432 section 21.6.5 ADC configuration register 2 (ADC_CFGR2):

Bits 4:2 OVSR[2:0]: Oversampling ratio
This bitfield is set and cleared by software to define the oversampling ratio.
000: 2x
001: 4x
010: 8x
011: 16x
100: 32x
101: 64x
110: 128x
111: 256x

The STM32U5 oversampling ratio is set to any number between 1 and 1024. From RM0456 section 29.6.5 ADC configuration register 2 (ADC_CFGR2):

Bits 25:16 OSR[9:0]: Oversampling ratio
This bitfield is set and cleared by software to define the oversampling ratio.
0: 1x (no oversampling)
1: 2x
2: 3x
...
1023: 1024x

Therefore the macros ADC_OVERSAMPLING_RATIO_X defined in stm32u5xx_hal_adc.h and LL_ADC_OVS_RATIO_X defined in stm32u5xx_ll_adc.h are not compatible with the STM32U575 ADC1 ADC_CRGR2 register - which can cause confusion when porting a project from an older STM32.

Note that these macros do appear to be compatible with ADC4 on the STM32U575 - (according to RM0456 30.7.5 ADC configuration register 2 (ADC_CFGR2)) - so perhaps simply renaming the macros in stm32u5xx_hal_adc.h to ADC4_OVERSAMPLING_RATIO_X would make this clearer.

sdt99 avatar Sep 30 '22 14:09 sdt99

ST Internal Reference: 135967

RJMSTM avatar Oct 11 '22 14:10 RJMSTM