STM32G431 - Triggered ADC and DMA
Hi. Setup is simple:
TIM2 creates the ADC_EXTERNALTRIG_T2_TRGO signal, which starts ADC. ADC is configured to use DMA to transfer the data to the buffer.
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.GainCompensation = 0;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T2_TRGO;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
hadc1.Init.OversamplingMode = DISABLE;
DMA works with static number of transfers going to non-circular buffer, and the interrupt on completion, to allow processing of the collected samples:
This actually works fine. When I start the TIM2, the number of samples is transferred to the array, then ADC stops. Unfortunately, the DMA1_Channel1_IRQHandler(void) is executed in the loop, even after disabling TIM2 and clearing the DMA flags. Additional clearing of ADC flags also do not help:
The result is, that ADC flags are cleared correctly, and it's proven that ADC is really stopped, because:
- TIM2 counter do not work (CNT value is freezed so no trigger will fire)
- ADC interrupt flags, are not set again, meaning no conversion completed nor in progress.
Problem is with DMA IRQ flags - even after the IRQHandler execution they are still set, and not cleared. Probably this causes that the IRQ is fired again - but what keeps the IRQ flags set?
