AnsweredAssumed Answered

Quadrature decoder initialization with KSDK1.2 & KDS3.0

Question asked by PETER SHIH on Oct 12, 2015
Latest reply on Oct 16, 2015 by PETER SHIH

Hi :

 

I am working on quadrature decoder on FTM with TWR-K64F120M by using KDS3.0 and KSDK1.2. I have encoder connected to PTB0 and PTB1 and declare as

FTM1_QD_PHA and FTM1_QD_PHB, and able to see the signal on the scope. However, I am not able to initialize the FTM1 properly. The initialization always get stuck at FTM_HAL_SetSyncMode() in FTM_DRV_Init(). Please let me know if you have any idea or recommendation for me to clear the problem. Thanks!

main.c

//----------------------------------------------------------------------- // Standard C/C++ Includes //----------------------------------------------------------------------- #include <stdio.h>  //----------------------------------------------------------------------- // KSDK Includes //----------------------------------------------------------------------- #include "main.h"  //----------------------------------------------------------------------- // Application Includes //-----------------------------------------------------------------------  //----------------------------------------------------------------------- // Constants //----------------------------------------------------------------------- #define    ENCX_OVERFLOW        4000    //1000 CPR(cycle/rev) #define    ENCY_OVERFLOW        4000    //1000 CPR(cycle/rev)  const gpio_input_pin_user_config_t encoderPins[] = {   {     .pinName = kFTM1B0,     .config.isPullEnable = false,     .config.pullSelect = kPortPullUp,     .config.isPassiveFilterEnabled = false,     .config.isDigitalFilterEnabled = false,     .config.interrupt = kPortIntDisabled   },   {     .pinName = kFTM1B1,     .config.isPullEnable = false,     .config.pullSelect = kPortPullUp,     .config.isPassiveFilterEnabled = false,     .config.isDigitalFilterEnabled = false,     .config.interrupt = kPortIntDisabled   },   {     .pinName = kFTM2B18,     .config.isPullEnable = false,     .config.pullSelect = kPortPullUp,     .config.isPassiveFilterEnabled = false,     .config.isDigitalFilterEnabled = false,     .config.interrupt = kPortIntDisabled   },   {     .pinName = kFTM2B19,     .config.isPullEnable = false,     .config.pullSelect = kPortPullUp,     .config.isPassiveFilterEnabled = false,     .config.isDigitalFilterEnabled = false,     .config.interrupt = kPortIntDisabled   },   {     .pinName = GPIO_PINS_OUT_OF_RANGE,   } };  //----------------------------------------------------------------------- // Typedefs //-----------------------------------------------------------------------  //----------------------------------------------------------------------- // Global Variables //-----------------------------------------------------------------------  //----------------------------------------------------------------------- // Macros //-----------------------------------------------------------------------  //----------------------------------------------------------------------- // Function Prototypes //----------------------------------------------------------------------- void encoderX_Init(void); void configure_ftm1_pins(void);  //----------------------------------------------------------------------- // Main Function //----------------------------------------------------------------------- int main(void) {      // Configure board specific pin muxing     hardware_init();      // Initialize UART terminal     dbg_uart_init();      encoderX_Init();      PRINTF("\r\nRunning the TwoLED_ProjGen project.\n");      for (;;)                                         // Forever loop     {         __asm("NOP");     } }  void encoderX_Init(void) {     FTM_Type *ftmBase;      // Configure FTM1,2 encoder pins     ftmBase = g_ftmBase[FTM2_IDX];     configure_ftm1_pins();     GPIO_DRV_Init(encoderPins, NULL);      //FTM_DRV_QuadDecodeStop(FTM2_IDX);     //OSA_InstallIntHandler(FTM1_IRQn, FlexTimer1_IRQHandler);     ftm_user_config_t flexTimer1_InitConfig0 = {       .tofFrequency      = 0U,       .isWriteProtection = false,                // Disable write protection       .BDMMode           = kFtmBdmMode_11,       .syncMethod        = 0U     };     FTM_DRV_Init(FTM2_IDX,&flexTimer1_InitConfig0);      FTM_HAL_SetMod(ftmBase, ENCX_OVERFLOW);     FTM_HAL_SetCounterInitVal(ftmBase, 0);      FTM_DRV_SetClock(FTM2_IDX, kClock_source_FTM_ExternalClk, kFtmDividedBy1);    //start the timer clock, source is the the phase A and B input signals     ftm_phase_params_t flexTimer1_QdConfig0 = {       .kFtmPhaseInputFilter = false,       .kFtmPhaseFilterVal = 0U,       .kFtmPhasePolarity = kFtmQuadPhaseNormal,     };     FTM_DRV_QuadDecodeStart(FTM2_IDX, &flexTimer1_QdConfig0, &flexTimer1_QdConfig0, kFtmQuadPhaseEncode);     FTM_HAL_ClearTimerOverflow(ftmBase);     FTM_DRV_SetTimeOverflowIntCmd(FTM2_IDX,true); }  void configure_ftm1_pins(void) {     PORT_HAL_SetMuxMode(PORTB,0UL,kPortMuxAlt6);     PORT_HAL_SetPullMode(PORTB,0UL,kPortPullUp);     PORT_HAL_SetPullCmd(PORTB,0UL,false);     PORT_HAL_SetMuxMode(PORTB,1UL,kPortMuxAlt6);     PORT_HAL_SetPullMode(PORTB,1UL,kPortPullUp);     PORT_HAL_SetPullCmd(PORTB,1UL,false); }

 

gpio_pin.h

enum _gpio_pins_pinNames{   kGpioSW1                    = GPIO_MAKE_PIN(GPIOC_IDX, 6U),   kGpioSW3                    = GPIO_MAKE_PIN(GPIOA_IDX, 4U),   kGpioSdhc0Cd                = GPIO_MAKE_PIN(GPIOB_IDX, 20U),   kGpioLED1                   = GPIO_MAKE_PIN(GPIOE_IDX, 6U),   kGpioLED2                   = GPIO_MAKE_PIN(GPIOE_IDX, 7U),   kGpioLED3                   = GPIO_MAKE_PIN(GPIOE_IDX, 8U),   kGpioLED4                   = GPIO_MAKE_PIN(GPIOE_IDX, 9U),   kGpioSdcardCardDetection    = GPIO_MAKE_PIN(PORTD_IDX, 15U),  /* Connects the TWR_MEM, this's SPI Card detector pin */        kFTM1B0                        = GPIO_MAKE_PIN(GPIOB_IDX, 0U),            //FTM1_QD_PHA     kFTM1B1                        = GPIO_MAKE_PIN(GPIOB_IDX, 1U),            //FTM1_QD_PHB     kFTM2B18                    = GPIO_MAKE_PIN(GPIOB_IDX, 18U),        //FTM2_QD_PHA     kFTM2B19                    = GPIO_MAKE_PIN(GPIOB_IDX, 19U),        //FTM2_QD_PHA };

 

Peter Shih

Outcomes