ftm dual edge capture How to keep capturing

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

ftm dual edge capture How to keep capturing

Jump to solution
1,864 Views
erichsu
Contributor I

FRDM KV11Z ftm dual edge capture How to keep capturing?

 

/*
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016-2017, 2020 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_ftm.h"

/*******************************************************************************
* Definitions
******************************************************************************/
/* The Flextimer instance/channel used for board */
#define DEMO_FTM_BASEADDR FTM0

/* FTM channel pair used for the dual-edge capture, channel pair 1 uses channels 2 and 3 */
#define BOARD_FTM_INPUT_CAPTURE_CHANNEL_PAIR kFTM_Chnl_1

/* Interrupt number and interrupt handler for the FTM instance used */
#define FTM_INTERRUPT_NUMBER FTM0_IRQn
#define FTM_INPUT_CAPTURE_HANDLER FTM0_IRQHandler

/* Interrupt to enable and flag to read; depends on the FTM channel used for dual-edge capture */
#define FTM_FIRST_CHANNEL_INTERRUPT_ENABLE kFTM_Chnl2InterruptEnable
#define FTM_FIRST_CHANNEL_FLAG kFTM_Chnl2Flag
#define FTM_SECOND_CHANNEL_INTERRUPT_ENABLE kFTM_Chnl3InterruptEnable
#define FTM_SECOND_CHANNEL_FLAG kFTM_Chnl3Flag

/* Get source clock for FTM driver */
#define FTM_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_CoreSysClk)

/*******************************************************************************
* Prototypes
******************************************************************************/

/*******************************************************************************
* Variables
******************************************************************************/
volatile bool ftmFirstChannelInterruptFlag = false;
volatile bool ftmSecondChannelInterruptFlag = false;
/* Record FTM TOF interrupt times */
volatile uint32_t g_timerOverflowInterruptCount = 0u;
volatile uint32_t g_firstChannelOverflowCount = 0u;
volatile uint32_t g_secondChannelOverflowCount = 0u;

/*******************************************************************************
* Code
******************************************************************************/
void FTM_INPUT_CAPTURE_HANDLER(void)
{
if ((FTM_GetStatusFlags(DEMO_FTM_BASEADDR) & kFTM_TimeOverflowFlag) == kFTM_TimeOverflowFlag)
{
/* Clear overflow interrupt flag.*/
FTM_ClearStatusFlags(DEMO_FTM_BASEADDR, kFTM_TimeOverflowFlag);
g_timerOverflowInterruptCount++;
}
else if (((FTM_GetStatusFlags(DEMO_FTM_BASEADDR) & FTM_FIRST_CHANNEL_FLAG) == FTM_FIRST_CHANNEL_FLAG) &&
(ftmFirstChannelInterruptFlag == false))
{
/* Disable first channel interrupt.*/
FTM_DisableInterrupts(DEMO_FTM_BASEADDR, FTM_FIRST_CHANNEL_INTERRUPT_ENABLE);
g_firstChannelOverflowCount = g_timerOverflowInterruptCount;
ftmFirstChannelInterruptFlag = true;
}
else if ((FTM_GetStatusFlags(DEMO_FTM_BASEADDR) & FTM_SECOND_CHANNEL_FLAG) == FTM_SECOND_CHANNEL_FLAG)
{
/* Clear second channel interrupt flag.*/
FTM_ClearStatusFlags(DEMO_FTM_BASEADDR, FTM_SECOND_CHANNEL_FLAG);
/* Disable second channel interrupt.*/
FTM_DisableInterrupts(DEMO_FTM_BASEADDR, FTM_SECOND_CHANNEL_INTERRUPT_ENABLE);
g_secondChannelOverflowCount = g_timerOverflowInterruptCount;
ftmSecondChannelInterruptFlag = true;
}
else
{
}
__DSB();
}

/*!
* @brief Main function
*/
int main(void)
{
ftm_config_t ftmInfo;
ftm_dual_edge_capture_param_t edgeParam;
uint32_t capture1Val;
uint32_t capture2Val;
float pulseWidth;

/* Board pin, clock, debug console init */
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();

/* Print a note to terminal */
PRINTF("\r\nFTM dual-edge capture example\r\n");
PRINTF("\r\nOnce the input signal is received the input capture values are printed");
PRINTF("\r\nThe input signal's pulse width is calculated from the capture values & printed\r\n");

FTM_GetDefaultConfig(&ftmInfo);
/* Initialize FTM module */
FTM_Init(DEMO_FTM_BASEADDR, &ftmInfo);

edgeParam.mode = kFTM_OneShot;
/* Set capture edges to calculate the pulse width of input signal */
edgeParam.currChanEdgeMode = kFTM_RisingEdge;
edgeParam.nextChanEdgeMode = kFTM_FallingEdge;

/* Setup dual-edge capture on a FTM channel pair */
FTM_SetupDualEdgeCapture(DEMO_FTM_BASEADDR, BOARD_FTM_INPUT_CAPTURE_CHANNEL_PAIR, &edgeParam, 0);

/* Set the timer to be in free-running mode */
DEMO_FTM_BASEADDR->MOD = 0xFFFF;

/* Enable first channel interrupt */
FTM_EnableInterrupts(DEMO_FTM_BASEADDR, FTM_FIRST_CHANNEL_INTERRUPT_ENABLE);

/* Enable second channel interrupt when the second edge is detected */
FTM_EnableInterrupts(DEMO_FTM_BASEADDR, FTM_SECOND_CHANNEL_INTERRUPT_ENABLE);

/* Enable overflow interrupt */
FTM_EnableInterrupts(DEMO_FTM_BASEADDR, kFTM_TimeOverflowInterruptEnable);

/* Enable at the NVIC */
EnableIRQ(FTM_INTERRUPT_NUMBER);

FTM_StartTimer(DEMO_FTM_BASEADDR, kFTM_SystemClock);

while (ftmFirstChannelInterruptFlag != true)
{
}

while (ftmSecondChannelInterruptFlag != true)
{
}

/* Clear first channel interrupt flag after the second edge is detected.*/
FTM_ClearStatusFlags(DEMO_FTM_BASEADDR, FTM_FIRST_CHANNEL_FLAG);

/* Clear overflow interrupt flag.*/
FTM_ClearStatusFlags(DEMO_FTM_BASEADDR, kFTM_TimeOverflowFlag);
/* Disable overflow interrupt.*/
FTM_DisableInterrupts(DEMO_FTM_BASEADDR, kFTM_TimeOverflowInterruptEnable);

capture1Val = DEMO_FTM_BASEADDR->CONTROLS[BOARD_FTM_INPUT_CAPTURE_CHANNEL_PAIR * 2].CnV;
capture2Val = DEMO_FTM_BASEADDR->CONTROLS[(BOARD_FTM_INPUT_CAPTURE_CHANNEL_PAIR * 2) + 1].CnV;
PRINTF("\r\nCapture value C(n)V=%x\r\n", capture1Val);
PRINTF("\r\nCapture value C(n+1)V=%x\r\n", capture2Val);

/* FTM clock source is not prescaled and is
* divided by 1000000 as the output is printed in microseconds
*/
pulseWidth =
(float)(((g_secondChannelOverflowCount - g_firstChannelOverflowCount) * 65536 + capture2Val - capture1Val) +
1) /
((float)FTM_SOURCE_CLOCK / 1000000);

PRINTF("\r\nInput signals pulse width = %f us\r\n", pulseWidth);

while (1)
{
}
}

0 Kudos
1 Solution
1,773 Views
nxf56274
NXP Employee
NXP Employee

Hi,

Please refer the attachment. It works on frdm-kv11.

Have a great day,
TIC

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

- We are following threads for 7 days after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

View solution in original post

0 Kudos
10 Replies
1,519 Views
le27
Contributor I

I am currently learning to use the KV11Z board for related development, thank you for the above instructions.

0 Kudos
1,848 Views
erichsu
Contributor I

But the interrupts are all disabled. Where can I restart?
Because the captured value will not change.

erichsu_0-1618886584414.png

 

But use the restart button:

erichsu_1-1618886730295.png

 

 

0 Kudos
1,844 Views
nxf56274
NXP Employee
NXP Employee

Hi,

Comment that code.

Have a great day,
TIC

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

- We are following threads for 7 days after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
1,841 Views
erichsu
Contributor I

erichsu_0-1618887970058.pngerichsu_1-1618888008064.png

The interrupts are not disabled again in the while loop.

Where do I need to enable the interrupt again?

0 Kudos
1,822 Views
nxf56274
NXP Employee
NXP Employee

Hi,

You can enable the channel interrupt in the while loop in main function.

Have a great day,
TIC

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

- We are following threads for 7 days after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
1,819 Views
erichsu
Contributor I

I have done this, but the interrupt program repeats the judgment.

erichsu_0-1618898010621.png

Put the code here:

erichsu_1-1618898078362.png

 

0 Kudos
1,805 Views
nxf56274
NXP Employee
NXP Employee

Hi,

Due to this code edgeParam.mode = kFTM_OneShot, the capture will only run one time.

Change it to kFTM_Continuous.

Have a great day,
TIC

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

- We are following threads for 7 days after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
1,794 Views
erichsu
Contributor I

erichsu_0-1619003698114.pngerichsu_1-1619003741799.png

 

erichsu_2-1619003791752.png

result:

erichsu_3-1619003821061.png

The rising and falling edge values are not captured?

0 Kudos
1,774 Views
nxf56274
NXP Employee
NXP Employee

Hi,

Please refer the attachment. It works on frdm-kv11.

Have a great day,
TIC

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

- We are following threads for 7 days after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
1,853 Views
nxf56274
NXP Employee
NXP Employee

Hi,

Use a while loop.

2.PNG

Have a great day,
TIC

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

- We are following threads for 7 days after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos