FTM code from documentation doesn't work

Status
Not open for further replies.
I found this nice document: https://cache.nxp.com/docs/en/application-note/AN5142.pdf

which shows many example codes for different FTM configurations. However, I'm not succeeding in getting any of them to work on my Teensy 3.6.

For example, on page 5, there's an example on how to make an edge-aligned signal. I expect to copy that code to my sketch, assign FTM0 pins with the following:

PORTC_PCR1 |= 0x400;
PORTC_PCR2 |= 0x400;
PORTC_PCR3 |= 0x400;

which I got for the reference manual (K66) saying that FTM0_CH0,1,2 connected to PTC1,2,3 at ALT4; and after this, to get signals coming out of pins 22/23/9. But, none of this is happening. The pins don't show any signal. Why? What else am I missing?

The following is my code (copied from the pdf + pins assignment):

Code:
#include "Arduino.h"

void PWMOutput_EdgeAlignment(void) {
  SIM_SCGC6 |= 0x03000000; // enable FTM0 and FTM0 module clock
  FTM0_CONF = 0xC0;        // set up BDM in 11
  FTM0_FMS = 0x00; // clear the WPEN so that WPDIS is set in FTM0_MODE register
  FTM0_MODE |= 0x05; // enable write the FTM CnV register
  FTM0_MOD = 1000;
  FTM0_C0SC = 0x28; // edge-alignment, PWM initial state is High, becomes low
                    // //after match
  FTM0_C1SC = 0x28;
  FTM0_COMBINE = 0x02;  // complementary mode for CH0&CH1 of FTM0
  FTM0_COMBINE |= 0x10; // dead timer insertion enabled in complementary mode
                        // for //CH0&CH1 of FTM0
  FTM0_C1V = 500;
  FTM0_C0V = 500;
  FTM0_C2SC = 0x28;
  FTM0_C3SC = 0x28;
  FTM0_COMBINE |= 0x0200;
  FTM0_COMBINE |= 0x1000;
  FTM0_DEADTIME = 0x00;
  FTM0_C3V = 250;
  FTM0_C2V = 250;
  FTM0_CNTIN = 0x00;
  FTM0_SC = 0x08; // PWM edge_alignment, system clock driving, dividing by 1

  PORTC_PCR1 |= 0x400;
  PORTC_PCR2 |= 0x400;
  PORTC_PCR3 |= 0x400;
}

void setup() { PWMOutput_EdgeAlignment(); }

void loop() {}

Thanks in advance! :)
 
Take a look at how the core sets up FTM and pins for PWM/analogWrite(), hardware/teensy/avr/cores/teensy3/pins_teensy.c
maybe print out the PORTx_PCRx values after an analogWrite() to see what bits are set. You may need to set SRE and DSE bits ....
 
Last edited:
Maybe try this:

Code:
CORE_PIN9_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
CORE_PIN22_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
CORE_PIN23_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;

Instead of the bitwise logical OR, just write all 32 bits to the correct setting. Why leave the other 31 bits to chance?
 
Thanks for the precious information, Paul. Actually this is helpful as it made me understand how to properly make generic pin assignment that is device independent. However, this doesn't solve the problem I asked about.

The code from that PDF on section 3.4, page 9, works fine with my hard-coded format:

PORTC_PCR1 |= 0x400;
PORTC_PCR2 |= 0x400;
PORTC_PCR3 |= 0x400;

and with your code nice method (which is what I'm using from now on):

CORE_PIN9_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
CORE_PIN22_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
CORE_PIN23_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;

But not on the first example on page 5. Would you know why this is the case?

Also I would really, really appreciate it if you would know how to get Single Edge Capture Mode to work (section 3.8, page 14). I'm facing the same issue there. I'm expecting to input a signal there at FTM0_CH0, and be able to write the variable "diff" to serial port, which is supposed to be updated from the interrupt. When I input a signal there, the interrupt doesn't seem to be called at all, and the value of "diff" never changes. Would you know why?

There's something fundamental I'm missing here and there, but I'm not sure what. Something should be enabled maybe?

Thanks for any efforts.
 
Sorry, I really can't put time into digging into that app note to figure out exactly why it doesn't work.

But I can point you to the FreqMeasure and FreqMeasureMulti libraries which use edge capture. Maybe looking at their code will help? I can tell you with certainty those libraries do indeed work!
 
Status
Not open for further replies.
Back
Top