Flex timer initialization for pwm generation on k53 is not working

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

Flex timer initialization for pwm generation on k53 is not working

2,864 Views
renganathan_bs
Contributor II

I wish to use the Flex timer for generating PWM signals and porting it to one of the port to control the intensity of the LED. This is the code which I have done till now and tried to generate edge aligned pwm with 50% duty cycle. I am currently using k53 tower board. Problem is that it is TOF flag is not getting set and it is not going to the ISR. Checking step up step with the registers I was able to find out the CNT register is not updating once it leaves this ftm0_init() function and goes into infinite while loop.

CODE :

void ftm0_init()

{

  SIM_SCGC6 |=SIM_SCGC6_FTM0_MASK; //TURN ON FTM0 TIMER

  FTM0_MODE = ( (FTM_MODE_WPDIS_MASK & 0x01) | (FTM_MODE_FTMEN_MASK & 0x01)); /* enabling write protection and file access */

  // disabling ftm0

  FTM0_SC = (FTM_SC_CLKS(0x00) | FTM_SC_PS(0x00));

  FTM0_PWMLOAD |= FTM_PWMLOAD_LDOK_MASK|FTM_PWMLOAD_CH1SEL_MASK;

  //FTM0_MODE = ( (FTM_MODE_WPDIS_MASK & 0x00) | (FTM_MODE_FTMEN_MASK & 0x00)); /* enabling write protection and file access */

  FTM0_CNTIN = FTM_CNTIN_INIT(0x00);

  FTM0_CNT = FTM_CNT_COUNT(0x00);

  FTM0_MOD = FTM_MOD_MOD(0x32);

  FTM0_C1SC = (FTM_CnSC_MSB_MASK | (FTM_CnSC_ELSB_MASK) | (FTM_CnSC_ELSA_MASK & 0x00)); /* Set up channel status and control register */

  FTM0_C1V = FTM_CnV_VAL(0x19);        /* Set up channel value register */

  FTM0_PWMLOAD |= FTM_PWMLOAD_LDOK_MASK|FTM_PWMLOAD_CH1SEL_MASK;

  FTM0_STATUS |= (FTM_STATUS_CH1F_MASK & 0x00);

  FTM0_CONF |= (FTM_CONF_NUMTOF_MASK & 0x00);

  disable_irq(62);

  __VECTOR_RAM[78]=(uint32)ftm0_isr;

enable_irq(62); //FTM2 Vector is 80. IRQ# is 80-16=64

  //enabling ftm0

  FTM0_SC = FTM_SC_TOIE_MASK|(FTM_SC_CLKS(0x01) | FTM_SC_PS(0x00));

  FTM0_MODE = ( (FTM_MODE_WPDIS_MASK & 0x00) | (FTM_MODE_FTMEN_MASK & 0x00)); /* enabling write protection and file access */

  SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK;

  PORTA_PCR11 = PORT_PCR_MUX(3);

  GPIOA_PDDR = 0xFFFFFFFF;

}

void main (void)

{

  ftm0_init();

  while(1);

}

void ftm0_isr()

{

   FTM0_MODE = ( (FTM_MODE_WPDIS_MASK & 0x01) | (FTM_MODE_FTMEN_MASK & 0x01)); /* enabling write protection and file access */

  // disabling ftm0

  FTM0_SC = (FTM_SC_CLKS(0x00) | FTM_SC_PS(0x00));

  FTM0_CNTIN = FTM_CNTIN_INIT(0x00);

  FTM0_CNT = FTM_CNT_COUNT(0x00);

  FTM0_MOD = FTM_MOD_MOD(0x32);

  FTM2_C1V = FTM_CnV_VAL(0x19);        /* Set up channel value register */

  FTM0_PWMLOAD |= FTM_PWMLOAD_LDOK_MASK|FTM_PWMLOAD_CH1SEL_MASK;

  FTM0_STATUS |= (FTM_STATUS_CH1F_MASK & 0x00);

  FTM0_CONF |= (FTM_CONF_NUMTOF_MASK & 0x00);

  FTM0_SC |= (FTM_SC_TOF_MASK & 0x00);

  //enabling ftm0

  FTM0_SC = (FTM_SC_CLKS(0x01) | FTM_SC_PS(0x00));

  FTM0_MODE = ( (FTM_MODE_WPDIS_MASK & 0x00) | (FTM_MODE_FTMEN_MASK & 0x00)); /* enabling write protection and file access */

}

7 Replies

1,270 Views
renganathan_bs
Contributor II

I was seeing the output in the debugger mode and count was not incremented, so was going to through the forum  and I found out these.

https://community.freescale.com/message/370705#370705

The counter doesn't increment when in debugger mode and it freezes. But I have a question Mark. When the cnt value meets with the c1v value the TOF flag will be set right. Wont it create an interrupt ? Do you mean just initializing the PWM will make it start to work?

Thanks in advance

0 Kudos

1,270 Views
mjbcswitzerland
Specialist V

Hi

You can control the behaviour of the FTM when a breakpoint it hit with the setting in the FTMn_CONF register (either allow it to run or stop it).

The timer will set the overflow/match flag but an interrupt is only generate when the interrupt is also enabled. If no interrup enabled it will simply freerun as PWM.

Regards

Mark

0 Kudos

1,270 Views
renganathan_bs
Contributor II

Thanks Mark for your help. I got it working without the interrupts. I thought the interrupts are required to get the working.

I have posted the working code if someone comes across the problem.

/*

* This programs intialises the PWM module of the FTM2 timer for ch1 output running @ 1mhz with 50% duty cycle

*/

#include "common.h"

void ftm2_init(void);

void main (void)

{

ftm2_init();

while(1);

}

void ftm2_init()

{

SIM_SCGC3 |=SIM_SCGC3_FTM2_MASK; //TURN ON FTM0 TIMER

FTM2_MODE = ( (FTM_MODE_WPDIS_MASK & 0x01) | (FTM_MODE_FTMEN_MASK & 0x01)); /* enabling write protection and file access */

// disabling ftm2

FTM2_SC = (FTM_SC_CLKS(0x00) | FTM_SC_PS(0x00));

FTM2_CNTIN = FTM_CNTIN_INIT(0x00);

FTM2_MOD = FTM_MOD_MOD(0x32);

FTM2_C1SC = (FTM_CnSC_MSB_MASK | (FTM_CnSC_ELSB_MASK) | (FTM_CnSC_ELSA_MASK & 0x00) ); /* Set up channel status and control register */

FTM2_C1V = FTM_CnV_VAL(0x19); /* Set up channel value register */

}

1,270 Views
nitinharish
Contributor V

Thanks renganathan_bs that resolved my issue and helped me a lot in generating PWM out of K64

0 Kudos

1,270 Views
renganathan_bs
Contributor II

I am using IAR embedded workbench Mark. It would be a easier learning curve for me if it is explained in terms of using IAR embedded workbench and using function from its device file and reference manual that I am using to set the registers.

0 Kudos

1,270 Views
mjbcswitzerland
Specialist V

Hi

The fact that you are using IAR to develop code shouldn't have any impact on coding - it is standary C code as you use.

I have posted simple code for PWM operation in your other thread so that you can test an output signal and then verify with your own settings.

Presently your posts are a rather confusing because you are using PWM and interupts which is not a usual combination. PWM and FTM/TPM interrupts are very straightforward on the K and KL parts so I think that you need to be a bit more specific as to what question you are asking because the answer should be also quite simple (eg. in one thread you can't get detect an interrupt flag but in the other you get it but can't clear it, and then later don't seem to get it again...).

Regards

Mark

1,270 Views
mjbcswitzerland
Specialist V

Hi

The FTM module can be used to generate a number of PWM outputs (up to 8 independent PWM outputs using a shared time base) but the K53 tower board doesn't have one of these outputs connected to an on-board LED; however if you are connecting to an off-board LED you can use this mode.

It looks like you may however be using the FTM as a timer with interrupt (this can be done only once per module) which is of course possible but with much greater processor overload (in comparison to zero).

You can get complete code for PWM and timer interrupt use with the FTM from the uTasker project [KINETIS Project Code] (code is compatible with TPM module use when moving between K and KL parts) - there are details of the timers and their interfaces here: http://www.utasker.com/docs/uTasker/uTaskerHWTimers.PDF

Regards

Mark

http://www.utasker.com/kinetis.html