cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 ADC using DMA and oversampling - 16 bit value

LuizGEnger
Associate II

I want to implement the oversampling feature in the ADC read to get a better resolution for my values. Data is transmitted directly using DMA.

If I enable oversampling, I can correctly get an average value when putting a 16x oversampling ratio (the maximum available in STM32CubeMX) and a 4-bit right shift division coefficient.

Still, if I select a 16-bit resolution in the STM32CubeMX, I get an error and can't run the project. It does not accept the line

 hadc1.Init.Resolution = ADC_RESOLUTION_16B;

So how could I obtain the 16-bit resolution value as presented in the product's datasheet / reference manual / etc?

Or the oversampling in this MCU is just to improve SNR with fixed 12-bit value?

Also, when I activate an oversampling ratio, I'm also reducing the effective sampling rate by the same amount, correct?

Thank you.

Kind regards,

Luiz Guilherme Enger

8 REPLIES 8
KnarfB
Principal III

You find details in AN4629 Application note "ADC hardware oversampling for microcontrollers

of the STM32 L0 and L4 series".

So, to get 16-bit you leave 12-bit setting, oversample by 16 and do not right shift the result. The result is the the accumulated sum of all 16 conversions which buys you extra bits.

> I'm also reducing the effective sampling rate by the same amount, correct?

Try it out by measuring the DMA callback frequencies. Don't think so if you select "Single trigger for all oversampled conversions".

hth

KnarfB

TDK
Guru

I don't believe CubeMX should be allowing you to select ADC_RESOLUTION_16B here as it's not a resolution the ADC supports natively. You can only get 16bits with 16x oversampling.

@Sara BEN HADJ YAHYA​ 

If you feel a post has answered your question, please click "Accept as Solution".

So instead of right shifting, I should divide in software?

According to this training resource https://www.st.com/content/ccc/resource/training/technical/product_training/1e/0f/65/34/7e/b0/4e/ca/STM32L4_Analog_ADC.pdf/files/STM32L4_Analog_ADC.pdf/jcr:content/translations/en.STM32L4_Analog_ADC.pdf I have 16 bit only with 256x oversampling, and it does reduce the effective sampling rate.

When creating the project I selected the NUCLEO-STM32L432KC board. The MCU model is STM32L432KC6U. Several firmwares have been uploaded and used so it's all good. CubeMX does allow me to select ADC_RESOLUTION_16B, but then I get an error when building the project.

KnarfB
Principal III

Read AN4629 especially the SNR formula. The "output resolution" in the training resource is the SNR gain you get, not the number of bits in the result. So if you do 16x oversampling, the numerical result is 16x larger, but the SNR gain is only 4x larger which is 2 bit. This is, roughly speaking, because SNR is energy related which is proportional to V² and V is measured.

Without oversampling, you scale the raw ADC value v to something like 3.3*v/4096.0 (for 3.3V max.) and for 16x oversampling to 3.3*v/(16*4096.0).

Really encourage you doing some test here.

hth

KnarfB

I did read the AN4629, but the way it is written does not make it clear for me the information you're providing. Also, the section from RM0394 below makes me think that the oversampling ratio (that in RM0394 is called N and in AN4629 is called OSR, great) does affect the effective sampling rate of the ADC. It takes longer to get a value, so with higher oversampling rate, the longer it takes to obtain a reading in the register.

I did some testing and indeed that is what happens, I could not correctly build a waveform when increasing the oversampling rate.

Thank you.

0693W00000KdbHCQAZ.png

Piranha
Chief II

> So how could I obtain the 16-bit resolution value

To get not only a 16-bit number, but a real 16-bit SNR, one has to oversample 256x times and right-shift the value by 4 bits.

S.Ma
Principal

For each extra bit, the number of oversampling doubles, x2, x4, x16, x256.... rough idea.