Kinetis MK12 - FTM PWM

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

Kinetis MK12 - FTM PWM

1,134 Views
mylim
Contributor IV

Hi, I am using MK12DX256VLH5.

I am trying to generate a 20kHz tone using FTM0 channel 2.

I tried with my code below but doesn't seem to work. 

Is someone able to help to advise if I did any mistake? 

Thank you.

 

 

 

 

void Init_FTM(void)
{
ftm_config_t ftmConfig
ftm_chnl_pwm_signal_param_t ftmParam;

/* Configure ftm params with frequency 24kHZ */
ftmParam.chnlNumber = (ftm_chnl_t)kFTM_Chnl_2;
ftmParam.level = kFTM_LowTrue;
ftmParam.dutyCyclePercent = 50U;
ftmParam.firstEdgeDelayPercent = 0U;

/* Get FTM Config & Init FTM */
FTM_GetDefaultConfig(&ftmConfig);
ftmConfig.pwmSyncMode = kFTM_HardwareTrigger_0;
FTM_Init(FTM0, &ftmConfig);

FTM_SetupPwm(FTM0, &ftmParam, 1U, kFTM_CenterAlignedPwm, 20000U, kFTM_SystemClock);
FTM_EnableInterrupts(FTM0, kFTM_Chnl_2);
FTM_StartTimer(FTM0, kFTM_SystemClock);
}

void FTM_0_HANDLER(void)
{
/* Clear interrupt flag.*/
FTM_ClearStatusFlags(FTM0, kFTM_TimeOverflowFlag);
ftmIsrFlag = true;
}

int main(void) {

    /* Init board hardware. */
    BOARD_InitBootPins();
    BOARD_InitBootClocks();
    BOARD_InitBootPeripherals();

    Init_FTM();

    while(1) {

    	if (ftmIsrFlag)
    	{
    		ftmIsrFlag = false;
            printf(" -- ");
    	}

    }

 

 

 

 

Labels (1)
Tags (1)
0 Kudos
4 Replies

1,102 Views
mjbcswitzerland
Specialist V

Hi

You can get a reference from the open source uTasker project at  https://github.com/uTasker/uTasker-Kinetis

FlexTimers are independent of the OS, but the project also supports a FreeRTOS configuration if needed.

This is the code to generate a 50% MSR output on PTC3 (FlexTimer 0, Channel 2) on the K21D50, which is equivalent to your K12.

    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_reference = (_TIMER_0 | 2);                            // timer module 0, channel 2
    pwm_setup.pwm_frequency = PWM_FREQUENCY(20000, 16);                  // generate 20000Hz on PWM output
    pwm_setup.pwm_value   = _PWM_PERCENT(50, pwm_setup.pwm_frequency);   // 50% PWM (high/low)
    fnConfigureInterrupt((void *)&pwm_setup);                            // enter configuration for PWM test


Note that the uTasker FlexTimer interface automatically does all background work like ensuring the used peripherals and ports are clocked and the pin correctly multiplexed to its required peripheral function so that is all that is needed (also supporting interrupt and DMA if needed - here I have set the interrupt handler to 0, which avoids interrupts being generated but a call-back can be entered if a 20kHz interrupt were required).
When using other code you will need to carefully handled these other requirements since they are typically needed to be performed separately.

The uTasker project includes a Kinetis simulator that will emulate all peripherals and make testing and verification simple (without needing HW) and below is a screen shot of it running, with the mouse hovered over PTC3 so that it displays is possible functions and programmed function, as well as showing the signal that the FlexTimer is generating on this particular output - confirming that its frequency is accurate and the 50% MSR was achieved fairly well (with a /16 prescaler).

Regards

Mark

mjbcswitzerland_0-1648338607061.png

 

0 Kudos

1,066 Views
brock
Contributor II

(Sorry for reposting, I originally sent it to a 2021 thread)

@mjbcswitzerland

Hi Mark,

Thank you for all your posts on this forum.  We need more of that kind of honesty.

Have you had any experience with the low power modes in the i.MX family?

We're in big trouble with no KL27 stock for (yet) another year.  All our stuff is battery powered and we need to get down to ~10uA while sleeping and able to wake up on interrupt.

Honestly, I'd rather abandon NXP like they abandoned us, but the ease of portability with FlexIO etc. would be nice.  We have about 20 different boards with Kinetis products, I really can't believe they have done this to us.

Thanks for your time.

Brock

0 Kudos

1,110 Views
vicentegomez
NXP TechSupport
NXP TechSupport

We do not have a sample using FreeRTOS, but in this application note you can find the way to use the FTM, you can check the configuration that we recommend on this application note and used on your code

 

https://www.nxp.com/docs/en/application-note/AN5142.pdf

 

Regards

 

1,099 Views
mylim
Contributor IV

Hi Vincent,

I think made a confusion here. I am using the fsl_ftm.h & fsl_ftm.c for the flextimer.

Thanks.

0 Kudos