cancel
Showing results for 
Search instead for 
Did you mean: 

How to read ADC for different ADC pins using DMA buffer?

Tejashree
Associate III

0693W00000JNfd6QAD.pngI am have configured 3 ADC pins (PA4,PC4,PB11) with DMA.I want to read individual values at 3 different PINs.

0693W00000JNfi1QAD.pnghow do adjust the size of the buffer for 3 Pins?Or any other way to read it?

12 REPLIES 12
KnarfB
Principal III

The ADC values are interleaved in the buffer. So multiply the buffer size by the number of channels. The last parameter in HAL_ADC_Start_DMA is the size in Bytes. So 17 seems odd. Use sizeof(adc_buf) or calculate by *sizeof(uint32_t) etc..

If unsure: heavily oversize the allocated buffer, fill it with 0 and validate that only the expected number of bytes/values is written by debugging/dumping the buffer.

hth

KnarfB

Tejashree
Associate III

 HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc_buf, sizeofbuf);

Here I tried sizeofbuf as 3.But I cannot read the value .

Can you help me to set the exact value for sizeofbuf.

I am using ADC2.

PA4-ADC2_IN17

PC4-ADC2_IN5

PB11-ADC2_IN14

these are pins used.

What sizeofbuf should i choose in this case?

 HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc_buf, sizeofbuf);

Here I tried sizeofbuf as 3.But I cannot read the value .

Can you help me to set the exact value for sizeofbuf.

I am using ADC2.

PA4-ADC2_IN17

PC4-ADC2_IN5

PB11-ADC2_IN14

these are pins used.

What sizeofbuf should i choose in this case?

Javier1
Principal

Did you checked DMA init was called before ADC_init?

Its a common bug in cumemx autogenerated code

it happened to me

Tejashree
Associate III

Yes, DMA_Init() is called before ADC_Init().

could you post an image of your cubeMX setup for the ADCs?

you should be able to calculate sizeofbuf with that info

match your receiving buffer unit definition (uint8_t/uint16_t/uint32_t) to the one setup n your DMA

0693W00000JNhcZQAT.pngword=32bits

halfword=16bits

byte=8bits

0693W00000JNhgbQAD.pngHalf Word = 16 bits

so what would be my buffer size for following Pins?

PA4-ADC2_IN17

PC4-ADC2_IN5

PB11-ADC2_IN14

ssipa.1
Associate II

simply how many samples you need in buffer for one pin X num pins

for you for example 100 x 3 = 300

uint16_t adc_buf[300];
 
 HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc_buf,300);

but your images is chaos DMA show ADC1 instead ADC2 and checked inputs isnt as you write