STM32-Mat / Target: Determine PWM Duty Cycle

Question asked by leuser.marc on May 20, 2014
Hello,

from the documentation of the STM32-Mat/Target i have read that there is currently no implementation to determine the duty cycle available.
however, since the frequency measurement implementation just starts a timer during one interrupt and stops it to compute T in the next, it is possible to create an IRQ on both edges and just divide the resulting frequency by the actual frequency to determine the duty cycle.
Example:
- f is 10 kHz at 33% duty cycle
- T-actual = 0.1 ms (corresponds to 10 kHz)
- T-measured = 0.033ms (corresponds to 30 kHz)
- f-actual / f-measured = 10 kHz / 30 kHz = 0.33 = 33%
so far so good, i tried to implement thhe routine by generating a PWM with one timer and using another for measurement. this is already working.

it might be useful to check out the PWM_Read_Write example and analyze the interrupt routine in order to understand why this works. it's basically a bit being toggled and the frequency is computed after 2 interrupts. i.e. if you have an interrupt on both edges you will calculate the ON or OFF time rather than the whole period.

however my goal is to determine the duty cycle of an external pwm. i do know the frequency, but i cannot guarantee that the rising/falling edges are in sync with the software switch of the time measurement. in other words, i can not be certain wether my computed duty cycle represents the ON or OFF period.

i already found the tlc file responsible for the timer routines and i managed to insert some random comments into the code, so in general i found a way to customize the interrupt routines. here is my actual question: is it possible to determine what edge the interrupt came from? i want to sync the software switch on either positive or negative edge so i can be certain that i measure on or off period in ANY GIVEN CASE.

my thought was to try and put the I/O in input mode during the time of the interrupt being called and then evaluate the bit to be 1 or 0. however i'm not completely certain if that is the way to go.
any input is highly appreciated :-) it is also not a problem for me to use a simple hardware hack if you can come up with an idea to sync the pwm on a rising or falling edge