CMP0 to TMP1_CH0 input capture (KL05)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CMP0 to TMP1_CH0 input capture (KL05)

852 Views
phatpaul
Contributor III

I'm using KL05 with Processor Expert on KDS 3.2.0.

I need to measure the time between two rising edges without the error caused by interrupt latency.  The level must be variable using the DAC and Comparator.

I'm having trouble capturing pulses using the CMP0 to trigger TPM1, channel 0 capture.

The capture value is not changing (it does infrequently), however the interrupt flag is being set (the CMP0 interrupt is enabled, but not the TPM1 capture interrupt).

In PE, I have TimerUnit_LDD component with Channel 0 set for Capture mode, and the "Capture input pin" is set to "CMP0_output". 

I already found this was missing from PE generated code: (from the reference manual table 3-1, page 44)

SIM_SOPT4 |= SIM_SOPT4_TPM1CH0SRC_MASK; // Enable CMP0 -> capture TPM1CH0

But even after adding that, the result of TPM_PDD_ReadChannelValueReg(TPM1_BASE_PTR, 0) doesn't change after every pulse.

Any ideas? Any example programs that do this?

If capture is broken, can I use DMA to achieve period measurement (seems over-complicated)? 

Labels (1)
0 Kudos
3 Replies

658 Views
phatpaul
Contributor III

I thought I had a working solution, but unfortunately it is still unreliable.

It seems that sometimes the TMP1_CH0 interrupt flag is not set (even though I entered the ISR).  And sometimes the flag is set but the capture value I read is garbage (or unchanged).

Some clues:

It happens most often when TPM1_CH1 is set to compare and it interrupts at the same time as the capture is supposed to happen.  Is it possible that TPM1_CH1 compare could corrupt TPM1_CH0 capture?

0 Kudos

658 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

The input capture will happen after the CMP trigger TPM, so you should read capture d value in TPM interrupt. And you can disable the interrupt of CMP if there is no other needs in CMP interrupt.
I didn't find Cap1_Reset function in your example.(If TPM1 CH1 is used as Output Compare Mode, then the Cap1_Reset will affect the CH1.) If you don't take TPM overflow into account, it would be better two rising edges happens in one TPM MOD period.(See diff error 1)

diff error.jpg

Q1: It seems that sometimes the TMP1_CH0 interrupt flag is not set (even though I entered the ISR).
A1:  If you TPM1_CH1 interrupt happen before TPM1_CH0, then Cap1_OnCapture may not be called but still you will enter PE_ISR(TU1_Interrupt) and TU1_OnChannel1.

cmp02tpmtest.jpg

Q2: And sometimes the flag is set but the capture value I read is garbage (or unchanged).
A2: If push_debug16 take too much time and the input captured several times during UART printf, then the captured value is the latest one and the diff will not reflect the real time between two rising edges.(See diff error 2)

I don't think the TPM1_CH1 compare could corrupt TPM1_CH0 capture. Maybe you can attached this example here so that I can test it.

Best Regards,

Robin

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

658 Views
phatpaul
Contributor III

I created a bare bones project to demonstrate the issue.  In doing so, I think I've found a work-around.  I moved the code which reads the captured value into the Cap1_OnCapture ISR (was in the AC1_OnCompare ISR).  Now the capture values look great!

So there are potentially two bugs. 

 - The module-to-module interconnect SIM_SOPT4_TPM1CH0SRC is not being set by Processor Expert.

 - For some reason, it's not reliable to read the capture value from the CMP0 interrupt.  The work-around is to read it in the Cap1_OnCapture (a.k.a. TU1_OnChannel0).

I'll post the project if anyone is interested.  Comment-out the code in the AC1_OnCompare ISR and un-comment the same code in the Cap1_OnCapture ISR to see the working solution.  The input is a square wave on pin 26 (CMP0_IN0), and the measured values are dumped out the UART pin 23 (UART0_TX).

0 Kudos