FTM Settings

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

FTM Settings

1,820 Views
john71
Senior Contributor I

I need a free run timer ticking milliseconds.

FTM clocked from the System clock (120Mhz)  and the prescalers I have 1 - 128. So I can not get 1 millisecond tick.

Is there any way to do it?

As I see we have another option in CLKS - Fixed frequency clock. How do I configure this option?


12 Replies

1,216 Views
john71
Senior Contributor I

Hi Mark

I configured the timer

void SYSTIME_Init(void)
{
    #if FLEXTIMER_ENA
    SIM_SCGC6 |= SIM_SCGC6_FTM0_MASK;

    /* Turn off FlexTimer 3 and clear any pending Timer Overflow Flag
    (TOF) using the FTM3_SC register (Status and Control register for FlexTimer 3)*/
    FTM0_SC;
    FTM0_SC = 0;
                        //ovf int ena,            fixed freq clock - 32K,         prescaler - 32
    FTM0_SC = FTM_SC_TOIE_MASK | FTM_SC_CLKS(2) | FTM_SC_PS(5);

    /* Disable write protection for FlexTimer 0 using the FTM3_MODE register*/
    FTM0_MODE = FTM_MODE_WPDIS_MASK;

     enable_irq(62);  
    #endif
}

and I test it

 time_before = FTM0_CNT;
 Delay_ms(100);
 time_after = FTM0_CNT;

So I should see the difference between timestamps - 100. But I see some random numbers < 100.

0 Kudos

1,216 Views
mjbcswitzerland
Specialist V

Evgeny

You need to check that the fixed clock is set up correctly to give the 32kHz that you are expecting.
Also you need to verify that your delay for 100ms is correct since this may also be inaccurate/random.

Regards

Mark

0 Kudos

1,216 Views
john71
Senior Contributor I

Mark

I wish I know how to set up the fixed clock. Browsing over the registers I didn't find where I do it.

0 Kudos

1,216 Views
mjbcswitzerland
Specialist V

Evgeny

pastedImage_1.png

All you need to do is set IREFS in MCG_C1. Note that there is a divide by 2 in the path!

If you don't set this it will use the external clock (divided by FRDIV) which gives a more accurate/stable reference but may not allow 32kHz unless the clock happens to be a multiple of it.

Regards

Mark

1,216 Views
mjbcswitzerland
Specialist V

Hi

The fixed clock option depends on the Kinetsi part being used - it is often the MCGFFCK and so can be set to the 32kHz IRC, which allows you to use a 32 prescaler to derive a 1ms counting rate (although the 32kHz IRC clock has some tolerance).
It can be selected in the CLKS field of the FTMx_SC register.

Regards

Mark

0 Kudos

1,216 Views
john71
Senior Contributor I

Hi Mark

32kHz would be great.

I'm using K10. I went through all MCG and didn't find the FTM mapping to 32kHz.

0 Kudos

1,216 Views
mjbcswitzerland
Specialist V

Evgeny

K10:

pastedImage_1.png

pastedImage_3.png

Regards

Mark

0 Kudos

1,216 Views
john71
Senior Contributor I

So if choose CLKS = 10 - Fixed frequency clock - MCGFFCLK will be routed as  FTM clock source?



 

0 Kudos

1,216 Views
mjbcswitzerland
Specialist V

Yes.

1,216 Views
mjbcswitzerland
Specialist V

Evgeny

If you can connect the output of one FTM to the clock input of another you can set the first to generate a 1ms square wave and use that by the second one to count at exactly 1ms rate.

Regards

Mark


uTasker developer and supporter (+5'000 hours experience on +60 Kinetis derivatives in +80 product developments)
Kinetis: http://www.utasker.com/kinetis.html

0 Kudos

1,216 Views
john71
Senior Contributor I

If you can connect the output of one FTM to the clock input of another you can set the first to generate a 1ms square wave

Mark, how do I set the first one to generate a 1ms?

SystemClock(120Mhz) / prescaler(1~128)  - I can not get 1ms.

0 Kudos

1,216 Views
mjbcswitzerland
Specialist V

Hi Evgeny

To configure a PWM output at 1kHz (1ms clocking rate for another FTM) I do (from http://www.utasker.com/docs/uTasker/uTaskerHWTimers.PDF)

    PWM_INTERRUPT_SETUP pwm_setup;
    pwm_setup.int_type = PWM_INTERRUPT;
    pwm_setup.pwm_mode = (PWM_SYS_CLK | PWM_PRESCALER_16 | PWM_EDGE_ALIGNED); // clock PWM timer from the system clock with /16 pre-scaler
    pwm_setup.int_handler = 0;                                           // no user interrupt call-back on PWM cycle
    pwm_setup.pwm_frequency = PWM_FREQUENCY(1000, 16);                   // generate 1kHz on PWM output
    pwm_setup.pwm_value = _PWM_PERCENT(50, pwm_setup.pwm_frequency);     // 50% PWM (high/low)
    pwm_setup.pwm_reference = (_TIMER_0 | 3);                            // timer module 0, channel 3
    fnConfigureInterrupt((void *)&pwm_setup);

This essentially does
- powers up the FTM
- set FTM_SCS to FTM_CSC_MS_ELS_PWM_LOW_TRUE_PULSES
- sets the period to FTM_MOD (the rate of 1ms)
- sets FTM_CV for the channel to 50% (half the FTM_MOD value)
- starts the FTM withot interrupt with teh defined prescaler value

It also configures the output pin (eg. on PTA6) using
_CONFIG_PERIPHERAL(A, 6, (PA_6_FTM0_CH3 | (PORT_SRE_FAST | PORT_DSE_HIGH))); // FTM0_CH3 on PA.6 (alt. function 3)

The other FTM needs to be set up to use a FTM clock input pin, connected to this output.

I do it with
    pwm_setup.pwm_mode = (PWM_EXTERNAL_CLK | PWM_PRESCALER_1 | PWM_NO_OUTPUT);
    pwm_setup.pwm_frequency = 0xffff;
    pwm_setup.pwm_reference = (_TIMER_1 | 0);                            // timer module 1, channel 0
    fnConfigureInterrupt((void *)&pwm_setup);
to get the second one counting from it (it must use a different FTM)

which then also configures the clock input cnnection (which depends on the exact Kinetis part since they are not all the same in this respect - some have 2 pins that can be used by all timers and some have dedicated pins for each timer).


You can get full code reference from the uTasker Open Source project to get some ideas or just use the entire project to quickly complete your development.

Regards

Mark

0 Kudos