cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 DMA Normal Mode number of bytes transferred check

abotha
Associate III
Posted on February 01, 2018 at 09:07

Good Day

If I call:

HAL_ADC_Start_DMA(&hadc1, (uint32_t*)getADCBuffer, adcBufferLength); //uint16_t getADCBuffer[adcBufferLength] and adcBufferLenght is defined as 1000�?

and then call:

HAL_ADC_Stop_DMA(&hadc1);�?

before the 'adcBufferLength' was reached, how can I check how many new bytes I have in my getADCBuffer for every iteration of the code?

Can I do this:

adcNumSamplesCaptured = (adcBufferLength - (uint16_t)(DMA1_Channel1->CNDTR));�?

where obviously DMA1_Channel1 is associated with the ADC Configuration.

In normal mode, will CNDTR return to 'adcBufferLength' automatically every time? The Reference Manual for my MCU says:

'

If the channel is configured in non-circular mode, no DMA request is served after the last

transfer (that is once the number of data items to be transferred has reached zero). In orderto reload a new number of data items to be transferred into the DMA_CNDTRx register, theDMA channel must be disabled.'

I should never reach '0' items left to transfer,is there something that I must do?

ShouldI call 'CLEAR_BIT(DMA1_Channel1->CCR,DMA_CCR_EN);' before calling

'HAL_ADC_Start_DMA(&hadc1, (uint32_t*)getADCBuffer, adcBufferLength);' ?

I find that sometimes 'adcNumSamplesCaptured is larger than the number of non-zero bytes in 'getADCBuffer', and can't quite figure out why.

My ADC DMA Configuration:

 /* ADC1 DMA Init */
 /* ADC1 Init */
 hdma_adc1.Instance = DMA1_Channel1;
 hdma_adc1.Init.Request = DMA_REQUEST_0;
 hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
 hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
 hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
 hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
 hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
 hdma_adc1.Init.Mode = DMA_NORMAL;
 hdma_adc1.Init.Priority = DMA_PRIORITY_HIGH;
 if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Kind Regards

#dma #dma-adc #dma-normal #adc #stm32-adc
1 REPLY 1
Kurt Alber
Associate III
Posted on April 11, 2018 at 11:54

Hi! You have to disable the dma and write again to CNDTR of your dma channel. When you enable the channel again it will work!

Cheers