2

I use ADC with DMA (STM32F4, ide STM32CubeIDE) and I think that I understand how it works but still have one dilema. From my understanding MCU is called only when DMA transfer is completed, basically MCU go into this function when DMA ADC is finished

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)

Is that right?

So if this is true, I have next dilema: for example adc_clock is 10MHz, sample time is 480 adc_cycles, 12 bit adc resolution. adc_sample_period = 1/10Mhz * (480 + 12 + 3) = 49.5uS. That mean MCU will go into HAL_ADC_ConvCpltCallback() every 49.5uS ??? For my perspective that is hard intensive, especially in bigger projects. Did anyone have idea how to solve this "problem"? I want to read ADC results for example every 1mS but also want to implement DMA into ADC. Any idea is welcome

subavet995
  • 139
  • 14
  • You need to start with a spec. What are you to do with the ADC values, what resolutions and real-time deadlines are actually needed and so on. It doesn't make sense to catch every ADC conversion at high clock speed if you don't need to decode a signal in hard real-time. – Lundin Mar 27 '20 at 11:52
  • @Lundin I know what I will do with adc values. It will be used in formula to calculate current. Resolution is 12bit (0-4095) , I emphasize that in text above. I agree with constatation that reading adc every is 49.5uS is wastaing of MCU resources. I need adc valuse every 1mS, maybe to engage timer to triger ADC DMA? – subavet995 Mar 27 '20 at 12:06
  • Start by turning down the ADC conversion clock? – Lundin Mar 27 '20 at 12:07
  • Min adc clock is 5Mhz. (APB2 / add_prescaler) = (40Mhz / 8) = 5Mhz. So 1/5Mhz*(480 + 12 + 3) = 99uS which is too hard intensive for my perspective. I will try will timer, I will report results – subavet995 Mar 27 '20 at 12:18

2 Answers2

2

Read the uC documentation - do not start from the "magic" HAL functions.

1ms period between the the ADC conversions is absolutely nothing. I have many projects where I use double or triple ADC modes with resulting sampling rate of 18MSPS.

Generally speaking if you want poor, slow, inefficient and working "by accident" code - use HAL. Otherwise learn your hardware and use registers instead.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Thanks for answer. I want to use HAL I don't have time for detailed reading registers. I use triple ADC DMA with ~9.7MSPS (1 / 49.5uS * 480). – subavet995 Mar 27 '20 at 11:55
  • 2
    @subavet995 you do not. if conversion takes 49.5us then you conversion rate is 20kSPS. The number you show is a ADC clock not the sample rate. My advice - start from the basics. BTW HAL will take you much more time than the registers. \ – 0___________ Mar 27 '20 at 12:07
  • Yea you are right. I will try to solve this with timer – subavet995 Mar 27 '20 at 12:13
0

This is how I solve this problem: I change DMA configuration. DMA was configurated to work in circular mode, that mean when ADC finish one conversion DMA store data and MCU is notified via

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)

Basically MCU was notified every 49.5uS and for my purpose that was too intensive (I need ADC result every 1mS). I create timer which is used to indicate ADC when need to start sample and with DMA in normal mode (ADC will do only 1 measurement) that solve my problem. Every 1mS got result from ADC. So trick was in DMA modes (circular/normal mode).

subavet995
  • 139
  • 14