Tim5 in Capture mode to measure the time period between rising edge on 2 different GPIOs
Hi,
I am trying to measure the amount of time between the rising edge on two different GPIOs using TIM5. one assumption I make is that one of those GPIOs will happen first.
Actually 3 gpios are involved in the application, PA0, PA1 and PA2, PA2 is configured as an output, PA0 and PA1 are in alternate mode to be used as capture triggers for the TIM. PA0 and PA1 are both connected to some other system which Is the system I want to measure "response time of". PA2 is connected to PA0.
The Tim is configured to fire an interrupt on the event on PA1. When this happens the ISR sends a message to a mailbox, containing the contents of CCR[0] and CCR[1].
So the approach goes like this:
- write a zero on PA2 and wait until PA0 and PA1 both read 0 (the SUT has to obey the protocol)
- clear the CCR registers and restart timer from 0
- write a one to PA2
- blocking wait on the mailbox for the message from ISR.
So the intent here is to measure the time from writing a one to some external "SUT" and wait for it to reply by writing a one on another pin.
After receiving the message from the ISR, the number of ticks can be calculated by subtraction stop_ticks - start_ticks essentially. and the time in ms can be obtained by dividing that value by 84000. (I am on an STM32f407)
This seems to work quite well to measure time periods of 1,2,3,4,5... and upwards ms. But when measuring a timeperiod that on the oscilloscope shows up as approx 1.5us, this system measures a time of about 3us. (so big percentual error at small time periods).
These periods (1.5 - 3us) are some number of hundreds of clock cycles at 84MHz so I assume a rising edge can be detected a lot faster than that. An 84MHz timer should have a period of slightly above 10ns, so I was hoping to get approximately that precision on the measurements.
So I wonder, what might cause this error offset?
How long does it take to detect a rising edge in clock cycles of the timer?
Any thoughts about this approach? are there some common pitfalls?
Thanks a lot everyone.
