Question
Using HAL to configure ADC with double buffered DMA on STM32F765VIT
I'm modifying some code to start using double buffered DMA. Everything in the code currently uses the HAL so I'd like to continue this but it seems ill suited for using double buffering with the ADCs.
This code gets me close:
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_samples, total_samples);
HAL_DMA_Abort(&hdma2_stream4);
// Clearing this does nothing ADC1->SR = 0;
// Clear buffers to make sure they're getting filled
memset(adc_samples, 0xff, sizeof(adc_samples));
// Start the DMA with double buffering
bobbyerror = HAL_DMAEx_MultiBufferStart(&hdma2_stream4, // *hdma
(uint32_t) (&(hadc1.Instance->DR)), // SrcAddress
(uint32_t)(&adc_samples[0]), // DstAddress
(uint32_t)(&adc_samples[1]), // SecondMemAddress
total_samples // DataLength
);
The issue after this code is that as soon as HAL_DMAEx_MultiBufferStart() sets EN in S4CR, one word gets copied from ADC1->DR and S4NTDR decrements by one. ADC1 is configured for software to start the conversion so this loop does show both buffers successfully receiving the samples from ADC1, they are just off by one memory location.
for(uint8_t bobbyidx = 0; bobbyidx < NUM_BUFFERS * SAMPLES_PER_CYCLE; bobbyidx++) {
bobbyerror = HAL_ADC_Start(&hadc1);
uint32_t counter = 1000UL * (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));
while (counter != 0) {
counter--;
}
}
