cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F429: How to correctly read timer counter register during ISR?

timistof
Associate III

I'm converting the CubeMX usb audio device example to be audio class 2.0 compliant, with high-speed external phy.

I have to implement a feedback endpoint to report the current number of samples per microframe. For this, I have tim2 set up as external timer connected to the audio codec's I2C_SCK bitclock. This gives me a 26.6 fixed point frame count (64 clock pulses per audio frame).

The problem lies when I try to calculate the delta time between SOF 's in my USBD_AUDIO_SOF callback (coming from HAL_PCD_IRQHandler -> HAL_PCD_SOFCallback -> USBD_LL_SOF).

The callback happens every 125 uS (I can verify this by toggling a gpio), so with a samplerate of 96khz, i'd expect the delta to be about (96000 / 8000 = ) 12 samples, or 768 SCK pulses.

The actual delta is nowhere near that.

There seems to be a problem reading the timer during the USB interrupt. When reading the timer every second during the main program loop, the delta is as expected (around 96000 samples, or 6144000 pulses).

I have another timer (TIM5), set up to use the APB1 clock (90 Mhz). This timer exhibits the same issue during the isr, but works fine in the main loop.

Is there a workaround for this?

**UPDATE:**

The timer delta also seems to be inaccurate in my audio callback function (coming from HAL_DMA_IRQHandler)

1 ACCEPTED SOLUTION

Accepted Solutions
timistof
Associate III

Derp! It turns out setting a breakpoint in an interrupt slowed down things enough to add a massive delay to the measured delta time.

I ended up filling a "history" buffer with the measured deltas. When breaking inside the interrupt and expecting the buffer I can see the delta time is consistent and with the expected value.

View solution in original post

3 REPLIES 3
TDK
Guru

Reading the timer is the same wherever you are. Just read TIMx->CNT. It doesn't make sense that it would read correctly in the main loop but not within an ISR. I would recheck your assumptions. Perhaps they are not getting called at the rate you think they are.

If you feel a post has answered your question, please click "Accept as Solution".

I'm sure the SOF callback is called every 125 uS, I'm pulsing a gpio and see this showing up correctly on the scope. The measured delta is not only a bit off, the delta is completely wrong. Orders of magintude wrong.

timistof
Associate III

Derp! It turns out setting a breakpoint in an interrupt slowed down things enough to add a massive delay to the measured delta time.

I ended up filling a "history" buffer with the measured deltas. When breaking inside the interrupt and expecting the buffer I can see the delta time is consistent and with the expected value.