/* * Copyright (c) 2015, Freescale Semiconductor, Inc. * Copyright 2016-2017 NXP * All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ #include "fsl_debug_console.h" #include "board.h" #include "fsl_ftm.h" #include "pin_mux.h" #include "clock_config.h" /******************************************************************************* * Definitions ******************************************************************************/ #define BOARD_FTM_BASEADDR FTM2 /* FTM channel used for input capture */ #define BOARD_FTM_INPUT_CAPTURE_CHANNEL kFTM_Chnl_3 /* Interrupt number and interrupt handler for the FTM instance used */ #define FTM_INTERRUPT_NUMBER FTM2_IRQn #define FTM_INPUT_CAPTURE_HANDLER FTM2_IRQHandler /* Interrupt to enable and flag to read; depends on the FTM channel pair used */ #define FTM_CHANNEL_INTERRUPT_ENABLE kFTM_Chnl3InterruptEnable #define FTM_CHANNEL_FLAG kFTM_Chnl3Flag /* Get source clock for FTM driver */ #define FTM_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_CoreSysClk) /******************************************************************************* * Prototypes ******************************************************************************/ uint32_t captureVal; /******************************************************************************* * Variables ******************************************************************************/ volatile bool ftmIsrFlag = false; /******************************************************************************* * Code ******************************************************************************/ void FTM_INPUT_CAPTURE_HANDLER(void) { if ((FTM_GetStatusFlags(FTM2) & kFTM_Chnl3Flag) == kFTM_Chnl3Flag) { /* Clear interrupt flag.*/ captureVal = FTM2->CONTROLS[kFTM_Chnl_3].CnV; FTM_ClearStatusFlags(FTM2, kFTM_Chnl3Flag); ftmIsrFlag = true; } __DSB(); } /*! * @brief Main function */ int main(void) { ftm_config_t ftmInfo; ftm_chnl_pwm_signal_param_t ftmParam; /* Board pin, clock, debug console init */ BOARD_InitPins(); #if 0 BOARD_BootClockRUN(); #else BOARD_BootClockHSRUN(); #endif BOARD_InitDebugConsole(); /* Configure ftm params with frequency 24kHZ */ ftmParam.chnlNumber = kFTM_Chnl_4; ftmParam.level = kFTM_HighTrue; ftmParam.dutyCyclePercent = 90U; ftmParam.firstEdgeDelayPercent = 0U; ftmParam.enableDeadtime = false; /* Print a note to terminal */ PRINTF("\r\nFTM input capture example\r\n"); PRINTF("\r\nOnce the input signal is received the input capture value is printed\r\n"); FTM_GetDefaultConfig(&ftmInfo); /* Initialize FTM module */ FTM_Init(FTM2, &ftmInfo); FTM_SetupPwm(FTM2, &ftmParam, 1U, kFTM_EdgeAlignedPwm, 5500U, FTM_SOURCE_CLOCK); PRINTF("Clock Run in %dHz\r\n",FTM_SOURCE_CLOCK); /* Setup dual-edge capture on a FTM channel pair */ FTM_SetupInputCapture(FTM2, kFTM_Chnl_3, kFTM_RisingEdge, 0); /* Set the timer to be in free-running mode */ // BOARD_FTM_BASEADDR->MOD = 0xFFFF; /* Enable channel interrupt when the second edge is detected */ FTM_EnableInterrupts(FTM2, kFTM_Chnl3InterruptEnable); // FTM_EnableInterrupts(FTM2, kFTM_Chnl4InterruptEnable); /* Enable at the NVIC */ EnableIRQ(FTM2_IRQn); FTM_StartTimer(FTM2, kFTM_SystemClock); while (1) { if(ftmIsrFlag == true){ //captureVal = FTM2->CONTROLS[kFTM_Chnl_3].CnV; PRINTF("\r\nCapture value C(n)V=%x\r\n", captureVal); ftmIsrFlag = false; } } } #if 0 /* * Copyright (c) 2015, Freescale Semiconductor, Inc. * Copyright 2016-2017 NXP * All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ #include "fsl_debug_console.h" #include "board.h" #include "fsl_ftm.h" #include "pin_mux.h" #include "clock_config.h" /******************************************************************************* * Definitions ******************************************************************************/ #define BOARD_FTM_BASEADDR FTM2 /* FTM channel used for input capture */ #define BOARD_FTM_INPUT_CAPTURE_CHANNEL kFTM_Chnl_3 /* Interrupt number and interrupt handler for the FTM instance used */ #define FTM_INTERRUPT_NUMBER FTM2_IRQn #define FTM_INPUT_CAPTURE_HANDLER FTM2_IRQHandler /* Interrupt to enable and flag to read; depends on the FTM channel pair used */ #define FTM_CHANNEL_INTERRUPT_ENABLE kFTM_Chnl3InterruptEnable #define FTM_CHANNEL_FLAG kFTM_Chnl3Flag /* Get source clock for FTM driver */ #define FTM_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_CoreSysClk) /******************************************************************************* * Prototypes ******************************************************************************/ /******************************************************************************* * Variables ******************************************************************************/ volatile bool ftmIsrFlag = false; /******************************************************************************* * Code ******************************************************************************/ void FTM_INPUT_CAPTURE_HANDLER(void) { if ((FTM_GetStatusFlags(BOARD_FTM_BASEADDR) & FTM_CHANNEL_FLAG) == FTM_CHANNEL_FLAG) { /* Clear interrupt flag.*/ FTM_ClearStatusFlags(BOARD_FTM_BASEADDR, FTM_CHANNEL_FLAG); } ftmIsrFlag = true; __DSB(); } /*! * @brief Main function */ int main(void) { ftm_config_t ftmInfo; uint32_t captureVal; /* Board pin, clock, debug console init */ BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_InitDebugConsole(); /* Print a note to terminal */ PRINTF("\r\nFTM input capture example\r\n"); PRINTF("\r\nOnce the input signal is received the input capture value is printed\r\n"); FTM_GetDefaultConfig(&ftmInfo); /* Initialize FTM module */ FTM_Init(BOARD_FTM_BASEADDR, &ftmInfo); /* Setup dual-edge capture on a FTM channel pair */ FTM_SetupInputCapture(BOARD_FTM_BASEADDR, BOARD_FTM_INPUT_CAPTURE_CHANNEL, kFTM_FallingEdge, 0); /* Set the timer to be in free-running mode */ BOARD_FTM_BASEADDR->MOD = 0xFFFF; /* Enable channel interrupt when the second edge is detected */ FTM_EnableInterrupts(BOARD_FTM_BASEADDR, FTM_CHANNEL_INTERRUPT_ENABLE); /* Enable at the NVIC */ EnableIRQ(FTM_INTERRUPT_NUMBER); FTM_StartTimer(BOARD_FTM_BASEADDR, kFTM_SystemClock); while (ftmIsrFlag != true) { } captureVal = BOARD_FTM_BASEADDR->CONTROLS[BOARD_FTM_INPUT_CAPTURE_CHANNEL].CnV; PRINTF("\r\nCapture value C(n)V=%x\r\n", captureVal); while (1) { } } #endif