Skip to main content
Explorer
February 22, 2024
Solved

Help with ADC + DMA integration.

  • February 22, 2024
  • 1 reply
  • 817 views

Hello.

I want to integrate ADC with DMA, and calculate the average count of all convertions.

I'm using the following code:

In adc.c:

```

if(HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) {
uint16_t value = HAL_ADC_GetValue(&hadc1);
USART_Send("%.3f", value);
HAL_ADC_Start(&hadc1);
}

```

In main.c:

```

HAL_ADC_Start_DMA(&hadc1, (uint8_t*) AVG_BUF, AVG_BUF_SIZE);

```

 

However HAL_ADC_GetValue() seems to not use DMA.

I want it to explicitely use DMA to read.

    This topic has been closed for replies.
    Best answer by TDK

    HAL_ADC_Start_DMA will put values into the buffer you specify, in this case AVG_BUG. So that is where you should read values from. Implement the HAL_ADC_ConvCpltCallback call to read out the values after they're all transferred. Refer to the documentaiton in stm32f4xx_hal_adc.c:

     (++) ADC conversion with transfer by DMA:
     (+++) Activate the ADC peripheral and start conversions
     using function HAL_ADC_Start_DMA()
     (+++) Wait for ADC conversion completion by call of function
     HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
     (these functions must be implemented in user program)
     (+++) Conversion results are automatically transferred by DMA into
     destination variable address.
     (+++) Stop conversion and disable the ADC peripheral
     using function HAL_ADC_Stop_DMA()

     

    1 reply

    TDKAnswer
    Super User
    February 22, 2024

    HAL_ADC_Start_DMA will put values into the buffer you specify, in this case AVG_BUG. So that is where you should read values from. Implement the HAL_ADC_ConvCpltCallback call to read out the values after they're all transferred. Refer to the documentaiton in stm32f4xx_hal_adc.c:

     (++) ADC conversion with transfer by DMA:
     (+++) Activate the ADC peripheral and start conversions
     using function HAL_ADC_Start_DMA()
     (+++) Wait for ADC conversion completion by call of function
     HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
     (these functions must be implemented in user program)
     (+++) Conversion results are automatically transferred by DMA into
     destination variable address.
     (+++) Stop conversion and disable the ADC peripheral
     using function HAL_ADC_Stop_DMA()