MK66F microsecond timer or DWT

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

MK66F microsecond timer or DWT

Jump to solution
557 Views
aaronm
Contributor III

µcontroller: MK66F.  SDK: 2.11.0.  MCUXpresso v11.9.0

I'm writing an app on the MK66F that uses an HC-SR04 and part of the process is to determine the delay from the echo signal.  I'm using an LPTMR instance to count how long it takes for a rising and falling interrupt to occur on the GPIO input pin.  That part all works.  But the delay between rising and falling, according the values I get from LPTMR_GetCurrentTimerCount( LPTMR0 ); can be too small to measure in integers: the difference between finish and start can come back as 0, so I'm thinking my timer is counting in milliseconds, instead of microseconds.  Depending on the distance, the echo delay can be less than a millisecond, and I need this resolution.

By default, the DWT register isn't available in this SDK.  Ok, no worries, but how do I get microsecond level in my LPTMR?

 LPTMR_GetDefaultConfig( &tmrConfig );
LPTMR_Init( LPTMR0, &tmrConfig );
LPTMR_SetTimerPeriod( LPTMR0, 0xFFFF );
LPTMR_StartTimer( LPTMR0 );

This is the code I'm using to configure and start the LPTMR instance.  Or do I implement SDK_DELAY_USE_DWT as a project define and use getCpuCycleCount()?

Tags (1)
0 Kudos
Reply
1 Solution
510 Views
aaronm
Contributor III

XiangJun, thank you for this insight.

My GPIO interrupt handler is already handling the rise and fall events on the input pin, so I didn't really want "capture" from from FlexTimer.  I looked through the Mbed OS code for the MK66F, and found that they use the Periodic Interrupt Timer (PIT) to generate a cycle count, and I was able to graft much of their code to mine - particularly the us_ticker_init() and pit_isr() routines from mbed-os/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/us_ticker.c - and it seems to work.  My GPIO handler will read the PIT_GetCurrentTimerCount() on signal rise, and then another count on signal fall so I can count microseconds.

But I thank you for pointing me in this direction.

Aaron

View solution in original post

0 Kudos
Reply
2 Replies
511 Views
aaronm
Contributor III

XiangJun, thank you for this insight.

My GPIO interrupt handler is already handling the rise and fall events on the input pin, so I didn't really want "capture" from from FlexTimer.  I looked through the Mbed OS code for the MK66F, and found that they use the Periodic Interrupt Timer (PIT) to generate a cycle count, and I was able to graft much of their code to mine - particularly the us_ticker_init() and pit_isr() routines from mbed-os/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/us_ticker.c - and it seems to work.  My GPIO handler will read the PIT_GetCurrentTimerCount() on signal rise, and then another count on signal fall so I can count microseconds.

But I thank you for pointing me in this direction.

Aaron

0 Kudos
Reply
549 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I have checked the LPTMR module of K66, it does not have capture function, so it is not suitable for your application.

I suggest you use TPM or FTM modules to test the signal duty cycle or cycle time. You can connect the tested signal to the TPM_CHx pin(capture signal), when the rising/falling edge of the capture signal is detected, the current counter value is loaded into _CnV register, and interrupt is fired. Secondly, the TPM and FTM have high driving clock, so you can get high resolution. The LPTMR module is designed to work in low power mode, so it's driving clock is lowerer than that of TPM/FTM, so resolution is lower, btw, it does not support capture function.

I suggest you download SDK from the link:

https://mcuxpresso.nxp.com/en/select

 

In the SDK, there is input_capture example, located at:

............\driver_examples\ftm\input_capture

Hope it can help you

BR

XiangJun Rong