AnsweredAssumed Answered

TRK-KEA64 PWM Generation with FTM

Question asked by C. on May 22, 2018
Latest reply on May 24, 2018 by C.

Hi there!

 

I am trying to generate a PWM signal using a TRK-KEA64 board. My project is using SKEAZN642 headers.

 

The approach was to use the FTM driver provided in Quick Start Package. But I can't really make it works!

 

My main.c code:

#include "FTM/FTM.h"
#include "GPIO/GPIO.h"
#include "CLK/CLK.h"

void ftm_callback()
{
     FTM_ClrOverFlowFlag(FTM2);
     OUTPUT_TOGGLE(PTC, PTC0);
}

void pwmSetDutyCycle(FTM_Type* pFTM, uint8_t ftmChannel, uint16_t dutyCycle /*0-1000*/)
{
     uint32_t dc = pFTM->MOD - (pFTM->MOD) / (1000 / dutyCycle);
     FTM_SetChannelValue(pFTM, ftmChannel, dc);
}

void PWM_Init(FTM_Type* pFTM, uint8_t ftmChannel, uint32_t clkFreqHz, uint32_t freqHz)
{
     /* FTM Config */
     FTM_ConfigType FTM_Config = { 0 };
     FTM_Config.modulo = 10000;                                   /* Timer count */
     FTM_Config.clk_source = FTM_CLOCK_SYSTEMCLOCK;          /* Set clocking source */
     FTM_Config.prescaler = FTM_CLOCK_PS_DIV1;               /* Clock divider */
     FTM_Config.mode = 1;                                        /* Edge aligned PWM */
     FTM_Config.toie = 0;                                        /* enable channel interrupt */

     /* FTM Channel Config */
     FTM_ChParamsType FTM_CH_Config = { 0 };
     FTM_CH_Config.ctrl.bits.bMode = FTM_PWMMODE_EDGEALLIGNED;     /* PWM Setting */
     FTM_CH_Config.ctrl.bits.bPWMPol = FTM_PWM_HIGHTRUEPULSE;     /* Another PWM Setting */
     FTM_CH_Config.u16CnV = (FTM_Config.modulo);                         /* 0% dutycycle by default */

     /* Init FTM module */
     FTM_Init(pFTM, &FTM_Config);

     /* Set interrupt */
     FTM_SetCallback(pFTM, ftm_callback);
     /* Init channel */
     FTM_ChannelInit(pFTM, ftmChannel, FTM_CH_Config);
}

int main(void)
{
     FTM_Type *pFTM = FTM2;
     uint8_t ftmChannel = 0;

     /* Config pin as output */
     CONFIG_PIN_AS_GPIO(PTC, PTC0, OUTPUT);

     /* Select Pins corresponds to the PTC0 for output */
     SIM_PINSEL |= SIM_PINSEL_FTM2PS0_MASK;

     /* Init PWM driver */
     PWM_Init(pFTM, ftmChannel, DEFAULT_SYSTEM_CLOCK, 10000 /*Hz*/);

     /* Set duty cycle */
     pwmSetDutyCycle(pFTM, ftmChannel, 500);     /* DutyCycle - 0 is 0% and 1000 is 100% */

     while (1)
     {
     }
}

 

The interrupt was disabled intentionally!

 

The thing is that the code works perfectly on FRDM-KEAZ64 and the duty-cycle function is also working like a charm! But on TRK-KEA64 it does not! I observed actually that anything it's harder to do on TRK-KEA64 board than the others.

 

When interrupts are enabled, I can toogle the pin to generate an PWM with 50% duty-cycle but this is not what I want and I think doing this I will just misunderstand the purpose of FTM driver.

 

Any hint or idea? A working example of this would be highly appreciated!

 

PS: The example from QSP works on FRDM-KEAZ64 but not on TRK-KEA64!

PS2: I have also attached and example project!

PS3: It only seems to work with processor expert but I can't really figure out how and I can't really use PE in my project!

Attachments

Outcomes