Period/frequency measurement using K02F family

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

Period/frequency measurement using K02F family

1,677 Views
snehalpatil
Contributor II

I want to measure frequency/period of square wave signal which may vary between 40-60Hz. I am trying to use dual capture mode.I have written below initialization code but I am getting confused with following queries:

ftm_init()

{

 

 

 

ftm_dual_edge_capture_param_t param;

 

ftm_user_config_t user_info;

 

user_info.tofFrequency=0; 

 

user_info.BDMMode= kFtmBdmMode_11;

 

 

 

 

 

user_info.isWriteProtection=false;

 

 

user_info.syncMethod=kFtmUseSoftwareTrig;

 

 

FTM_DRV_Init(BOARD_FTM_INSTANCE, &user_info);

 

 

FTM_DRV_SetClock(BOARD_FTM_INSTANCE,kClock_source_FTM_SystemClk, kFtmDividedBy1);

 

param.mode=kFtmContinuous;

 

 

 

param.currChanEdgeMode=kFtmRisingEdge;

 

 

param.nextChanEdgeMode=kFtmRisingEdge;

 

 

FTM_DRV_SetupChnDualEdgeCapture (BOARD_FTM_INSTANCE, &param,BOARD_FTM_X_CHANNEL,0);

 

 

 

 

}

 

Doubts:

1. Is this initialization function correct?

2. How to read status of CHnF and CH(n+1)F to read the channel/counter value. As there is no any function for it in driver. If directly through register address then in which file I could find it. In MK02F12810.h file I find macro

 

FTM_STATUS_CH0F(x)  what is x in this... is this bit position?

3. Through function I can read C(n)V and C(n+1)V registers.

4. Is there any reference code for period measurement ?

5. Is my initialization for structure ftm_user_config_t correct?

 

Note: BOARD_FTM_INSTANCE = 0

         BOARD_FTM_X_CHANNEL = 0

 

 

 

Snehal.

Labels (1)
0 Kudos
9 Replies

1,184 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Snehal,

Regarding your question, I think this is the procedure of the dual capture code. From hardware perspective, you have to connect the captured signal to the even channel of FTM, for example FTM_CH0, FTM_CH2,..., the odd channel is ignored. When the first edge of captured signal is detected, the current FTM counter value is latched to the FTM_CnV reg, when the second edge of captured signal is detected, the current FTM counter value is latched to the FTM_Cn+1V reg, when the FTM_CHn+1 channel interrupt is enabled, an interrupt occurs, you can read the FTM_CnV/FTM_Cn+1V register to variables in ISR of odd channel, figure out the difference, it is okay.

so you can use the snippet for the ISR of FTM.

unsigned int c2v,c3v,diff_cycle;

void FTM0_IRQHandler()

{

    //checking the interrupt flag source

    //clear interrupt flag

if(FTM_HAL_HasChnEventOccurred(g_ftmBase[FTM_INSTANCE],CHAN2_IDX))

    {

    FTM_HAL_ClearChnEventFlag (g_ftmBase[FTM_INSTANCE],CHAN2_IDX);

    c2v=FTM_HAL_GetChnCountVal(g_ftmBase[FTM_INSTANCE],CHAN2_IDX);

    }

if(FTM_HAL_HasChnEventOccurred(g_ftmBase[FTM_INSTANCE],CHAN3_IDX))

    {

    FTM_HAL_ClearChnEventFlag (g_ftmBase[FTM_INSTANCE],CHAN3_IDX);

c3v=FTM_HAL_GetChnCountVal(g_ftmBase[FTM_INSTANCE],CHAN3_IDX);

    }

    //figuring out the difference of FTM0_C2V and FTM0_C3V

    diff_cycle=c3v-c2v;

}


for the initialization code, I think this is the procedure:

FTM_DRV_Init(BOARD_FTM_INSTANCE, &user_info);

FTM_DRV_SetupChnDualEdgeCapture (BOARD_FTM_INSTANCE, &param,BOARD_FTM_X_CHANNEL,0);

TM_HAL_EnableChnInt(g_ftmBase[FTM_INSTANCE],CHAN3_IDX); //enable odd channel interrupt

FTM_DRV_SetClock(BOARD_FTM_INSTANCE,kClock_source_FTM_SystemClk, kFtmDividedBy1);

FTM_HAL_SetDualEdgeCaptureCmd(g_ftmBase[FTM_INSTANCE],CHAN2_IDX,true); //set the  DECAPx in FTM_COMBINE register to launch the capturing

for(;;) {} //waiting interrupt

I think it is okay, but I do not test the code.

Hope it can help you

BR

XiangJun Rong

0 Kudos

1,184 Views
snehalpatil
Contributor II

Plz respond for Confirmation of CLKOUT frequency value. Please see below mail chain.

Regards,

Snehal.

0 Kudos

1,184 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Snehal,

I am sorry for the delay, I just come back after a long festival. Regarding the CLKOUT, of course, the CLKOUT signal can be outputted from the PTC3 pin. From hardware perspective, this is the procedure:

1)enable PortC gate clock. You can use the macro code: CLOCK_SYS_EnablePortClock(PORTC_IDX);

2)set the PTC3 as CLKOUT function pin  with the following code:

PORT_HAL_SetMuxMode(PORTC,3u,kPortMuxAlt6);

3)select the CLKOUTSEL bits with the following code:

       CLOCK_HAL_SetClkOutSel(g_simBase[0],ClockClkoutSelIrc48M);

/*! @brief SIM CLKOUT_SEL clock source select */

typedef enum _clock_clkout_src

{

kClockClkoutSelFlashClk    = 2U,  /*!< Flash clock    */

kClockClkoutSelLpoClk      = 3U,  /*!< LPO clock      */

kClockClkoutSelMcgIrClk    = 4U,  /*!< MCGIRCLK       */

kClockClkoutSelOsc0erClk   = 6U,  /*!< OSC0ERCLK      */

kClockClkoutSelIrc48M      = 7U   /*!< IRC48MCLK      */

#if (defined(DOXYGEN_OUTPUT) && (DOXYGEN_OUTPUT))

} clock_clkout_src_k02f12810_t;

#else

} clock_clkout_src_t;

#endif

Hope it can help  you.

BR

Xiangjun Rong

0 Kudos

1,184 Views
snehalpatil
Contributor II

Hi,

My development board is on the way I need this info as early as possible so I would be ready with code for testing..

Snehal.

0 Kudos

1,184 Views
snehalpatil
Contributor II

Hi,

Thanks for reply,

I want to set CLKOUT as 20 MHz.

System clock what we are using is 72Mhz. Which clockclkout_src parameter for function CLOCK_HAL_SetClkOutSel() would be ok to set CLKOUT as 20Mhz?

Snehal.

0 Kudos

1,184 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Snehal,

I think you misunderstand the function of CLKOUT pin. The CLKOUT pin can output the clock source from the specified clock source for example flash clock, LPO, MCGIRCLK,OSCERCLK, IRC48M, but it has not the dividing function. when you run the core/system clock in 72mhz, the flash clock is about 18MHz, which is close to 20MHz, I think you can output the 18MHz flash clock to the CLKOUT pin. As an alternative, you can run the  K02 in High Speed Run Mode, in the case, the core/system clcok frequency is 100MHz, set the OUTDIV4 in SIM_CLKDIV1 register as 3, the flash clock will be 20MHz, output the flash clock to CLKOUT pin, it is okay.

If you use 72MHz system clock, it is impossible to get 20MHz clock no matter whatever you use as a divider.

BR

XianmgJun Rong

0 Kudos

1,184 Views
snehalpatil
Contributor II

Hi,

I would like to know how much is CLKOUT on GPIO pin 24 (ALT5) for MK02FN128VFM10? In fsl_sim_hal_MK02F12810.h there is function

static inline void CLOCK_HAL_SetClkOutSel(SIM_Type * base, clock_clkout_src_t setting) but this is not used. Is there any clockout set? Please tell value.

I find macro #define DEFAULT_SYSTEM_CLOCK 20971520u /* Default System clock value */. Does this mean CLKOUT value for MK02FN128VFM10 is approx. 20MHz.

Regards,

Snehal.

0 Kudos

1,184 Views
snehalpatil
Contributor II

Hey thanks,

I will test and reply results.

I am missing one thing how should I assign GPIO pin to any alternate function like SPI,ADC etc.

I didn’t get any function for this in gpio_driver file. In reference manual I got table( section 10.3.1 K02F Signal Multiplexing and Pin Assignments ) which alternate function should be selected for GPIO pin but how to assign that alternate function to GPIO pin.

Regards,

Snehal.

0 Kudos

1,184 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Snahal,

Regarding the question how to assign a pin as a function for Kinetis K02 family, this is the procedure:

For example, the PTC5 is multiplexed with SPI0_SCK(ALT2), configure the PTC5 as SPI0_SCK function.

1)As you know, each pin is  a Port pin, Enable the gated Port clock. For example,   CLOCK_SYS_EnablePortClock(PORTC_IDX); //write SIM_SCGCx reg.

2)configure the pin as SPI0_SCK. PORT_HAL_SetMuxMode(PORTC,5u,kPortMuxAlt2); //write the MUX bits of PORTx_PCRy register.

it is okay that the PTC5 function as SPI0_SCK.

Hope it can help you.

BR

XiangJun Rong

0 Kudos