Electronic – STM32F303K8 Nucleo Restart ADC For Continuous Scans (Using Interrupt?) STM22CubeMX

nucleostm32stm32f3

I have a project generated from CubeMX for my STM32Nucleo F303K8.
My IDE is SystemWorkbench.

I am using a total of 8 Analog inputs split between two ADC's

ADC1: 1, 2, 4, 12.

ADC2: 1, 2, 3, 4.

I am not sure how to properly read these. I am using DMA and the frequency in which I check the value is much slower than the scan time. But even if it wasn't, I wouldn't care if I read the same value twice.

I just want to have a continuous scan, when one finishes the next starts.

But whatever I try to do, it seems that the only way I can get continuous scans is by manually starting the ADC scans over and over in my main loop using:

HAL_ADC_Start(&hadc1);
HAL_ADC_Start(&hadc2);

or the _IT variant.

I have tried enabling continuous conversion and then starting the ADC once outside my main loop. But that just does one scan and stops there.

I thought maybe I could just use the scan complete interrupt to restart the scan when it finishes. This works when I restart only one ADC inside the interrupt. However as soon as I add the second to the callback then the system locks up. Obviously there is an interrupt not being handled somewhere.

 /**
 * @brief Callback when an ADC scan completes.
 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    if (hadc->Instance == ADC1)
    {
        //HAL_ADC_Start_IT(&hadc1);
    }
    if (hadc->Instance == ADC2)
    {
        //HAL_ADC_Start_IT(&hadc2);
    }

}

My full code is here: Current commit refid: ef11437

https://github.com/c-herring/Sumo2017_NucleoF303/tree/ef11437f2f21d1beaa2ead0d9f5393c295cf9981

If anyone has any insight for me that would be greatly appreciated.

Best Answer

I have managed to find a solution. Although as PeterJ_01 mentions, this may just be a workaround to a much larger problem.

The cause of my problem appears to be that I had the cycle time set to 61.5 cycles per channel. Which seems to be too fast when doing 8 conversions. My suspicion was that I was saturating the µC. Which is why restarting only one ADC in the callback would work. but not both. But I guess it is something deeper than that.

I have upped the cycle time to 601.5 cycles and it all works!

Also, I realize that I do not need ADC interrupts enabled to restart the conversion. Since the DMA automatically attaches HAL_ADC_ConvCpltCallback to the conversion complete DMA interrupt.