Hi Ping, I show you my pseudo-code:
TPM_ISR{
if((TPM0_C0SC & 0x4)){//if coming from a rising edge....
TPM0_CNT = 0X0;//reset counter
/* I'm not sure if that is correct,but the manual says:"When switching from one
channel mode to a different channel mode, the channel must first be disabled and this
must be acknowledged in the LPTPM counter clock domain." So I do this little code to switch from rising edge interrupt to falling edge interrupt.
If I don't do this, the code lose edges.
*/
TPM0_C0SC = 0x00U;
status = TPM0_C0SC;
while(status){
status = (TPM0_C0SC & 0x3C);
}
TPM0_C0SC |= 0x48;//falling edge interrupt enable
}else{//if coming from a falling edge
pulse_period = TPM0_CNT;//pulse measure
TPM0_C0SC = 0x00U;
status = TPM0_C0SC;
while(status){
status = (TPM0_C0SC & 0x3C);
}
TPM0_C0SC = (TPM_CnSC_CHIE_MASK | TPM_CnSC_ELSA_MASK);//rising edge interrupt enable
TPM0_CNT = 0;
counter = 0;
guard_time = 1000;
while(counter < guard_time){
counter = TPM0_CNT;
}
}
TPM0_C0SC |= (0x80);//reset interrupt flag
}//end TPM_ISR
As you can see, after the guard time I reset the possible pending (rising/falling)interrupt but as soon as I exit from ISR, the ISR is called again like there were a pending interrupt.
That happens only if the situation is like that showed in the image: two unwanted pulse; but if there is only one unwanted pulse all goes well. It is like in the guard time the pending interrupts were queued and TPM0_C0SC |= (0x80); only deletes one.
Only work if i disable interrupt by TPM0_C0SC &= (~0x40); before the guard time and then I re-enable it when guard time elapsed.
I'm getting crazy.
Thank you.