cancel
Showing results for 
Search instead for 
Did you mean: 

Edge source of TIM Input Capture?

DPaul.2
Associate II

I need to measure the On and Off times of a possibly asymmetric signal.

Measuring frequency is a classic TIM Input Capture function, and in this case one is tempted to set the capture for both edges. Within the ISR, however, is there a HAL approved way to know if the interrupt was triggered by a rising edge or a falling edge?

I suppose one could initially configure the Input Capture for rising edge, then on-the-fly reconfigure with each interrupt for the alternate edge, but there ought to be a better way. Can one perhaps execute a HAL_GPIO_ReadPin even though the pin is otherwise configured? Direct register access is also acceptable (but not preferred)....

I happen to be dealing with a bare metal STM32G0C1CE, but I'm sure any proper solution would be STM32 generic.

Thanks for any pointers!

doug

4 REPLIES 4
KnarfB
Principal III

If you can use two timer channels, there is a combined timer channel feature, called "PWM input mode" in the ref. man. One channel is programmed for rising edge, the other for falling. Works very precisely without relying on IRQ timings or software in general (other than init).

If the signal edges do not follow each other too quickly and you want to use an interrupt, you can use an EXTI interrupt for both edges and read back the pin in the IRQ handler as you suggested.

hth

KnarfB

TDK
Guru

> Within the ISR, however, is there a HAL approved way to know if the interrupt was triggered by a rising edge or a falling edge?

There's no 100% way to know which exactly triggered it, but you can read the state of the pin and assume the ISR call is from the last transition. Technically, this could be wrong, but if the pin is toggling so fast that your ISR can't keep up, you're dead in the water anyway.

HAL_GPIO_ReadPin returns the correct/expected result on pins configured in AF mode.

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

HI @DPaul.2​,

First of all, Input capture can work on rising, falling or both edges event. At each edge detection the shadow CNT register is copied in CCRx register. This register needs to be read before the next event occurs to avoid overflow.

In case, the ISR is too slow to capture the second event, you should consider a DMA transfer and probably compute your data when DMA interrupt occurs.

I don't advise you to use HAL_GPIO_Read pin for this such of application.

You could refer to AN4776 from section 2.3 to 2.6. As mentioned by @KnarfB​, the PWM input mode is a feature you also could use.

Feel free to ask for more details.

Best Regards,

Gwénolé

Javier1
Principal

I used this video to develop a PWM width and freq detector and it works like a charm

https://www.youtube.com/watch?v=qqzZ9C0umQ4

we dont need to firmware by ourselves, lets talk