cancel
Showing results for 
Search instead for 
Did you mean: 

Weird latency inside the interrupts and DMA

hesam moshiri
Associate III
Posted on July 12, 2018 at 19:05

Hi,

I enabled the ADC DMA inside the Timer2 interrupt, and Put '1' for a GPIO PIN, as follows:

void TIM2_IRQHandler(void)

{

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, 1);

HAL_ADC_Start_DMA(&hadc1, (uint32_t *) buffer, 100);

HAL_TIM_IRQHandler(&htim2);

}

Then inside the ADC interrupt, I wrote '0' for the GPIO PIN and disabled the ADC DMA.

void DMA2_Stream0_IRQHandler(void)

{

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, 0);

HAL_ADC_Stop_DMA(&hadc1);

HAL_DMA_IRQHandler(&hdma_adc1);

}

An interrupts latency is around 3 to 4uS. I confirmed it by toggling a PIN inside the timer interrupt with no other code.

ADC DMA holds 100 samples which takes around 50uS. In this code the PIN value change is above any code, BUT why the ADC conversion values gets added to the generated pulse?

I mean after firing the Timer interrupt + 4uS, I should see a rising edge on the oscilloscope screen, but I see that rising edge after 50uS. The GPIO code is ABOVE ADC code.

I test this phase shift with another clean signal with the same frequency by an oscilloscope, then I realized this. The frequency of the ISR pulse is low and 100Hz.

What's important for me here is beginning. because I want to make sure that the ADC conversion starts on-time, not too late.

it's weird, why??????? 

:((

2 REPLIES 2
Posted on July 12, 2018 at 20:08

If timing is important use the Hardware trigger for the ADC.

Have the TIMx_CHx output also a pulse to the pin, as well as the ADC sample. You could also toggle a secondary channel of the TIM if that;s easier to review.

Using the interrupt as you have now has a whole bunch of latency, and then a lot of ADC/DMA configuration time

I'd use the TIM to mark time, and setup the DMA to do a continuous/circular mode, doubling the buffer so I had Ping/Pong buffers which I could then process in the DMA HT and TC interrupts without the data underneath me changing dynamically.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on July 12, 2018 at 21:01

The reason why I did this was to grab a specific part of the input signal to the ADC and I had used the timer interrupt for the timing match to start the conversion. My problem with the above code is that the PIN changing code is above any other code, then this delay comes from where!!!

I use CubeMX which should not make any difference in defining the configurations. There are few options: Continues/discontinues DMA, and Continues/Discontinues conversions.

I tried the timer triggering for the ADC, but it seems like a clock source for the conversion, not a starting point for the ADC conversions.