Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Direct Access to FTM/PWM control

  1. #1

    Direct Access to FTM/PWM control

    Hello,

    I've just got a Teensy 3.2 and am trying to port a project from a Due where I do PWM that changes from an interrupt routine. I'd like this to be as quick as possible as it updates frequently so I'd like to access the registers direct.

    So far I'm trying to get pin 6 to do PWM but I can't get it to work. The pin just goes high after a reset. I'm probably missing something obvious...

    Code:
    void setup() {
      // put your setup code here, to run once:
    
      SIM_SCGC6 |= SIM_SCGC6_FTM0;
    
      PORTD_PCR4 = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
    
      //WRITE ENABLE
      FTM0_MODE |= FTM_MODE_WPDIS;
      FTM0_MODE |= FTM_MODE_FTMEN;
    
      FTM0_MOD = 300;
      FTM0_CNTIN = 0;
      FTM0_C4V = 100;
      //Edge aligned PWM neg
      FTM0_C4SC = FTM_CSC_MSB | FTM_CSC_ELSA;  
      //System clock, prescaler 128
      FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(7);
      //NOT SURE IF NEEDED?
      FTM0_OUTINIT = 0;
    
      //allow all outputs
      FTM0_OUTMASK = 0;
    
      // no combine
      FTM0_COMBINE = 0;
      
      FTM0_SYNC |= FTM_SYNC_SWSYNC;
      FTM0_CNT = 0;
      FTM0_PWMLOAD = 0x2F;
    
    }

  2. #2
    Senior Member Ben's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    401
    Have you tried the "normal" approach using an ISR with Teensyduino's analogWrite(6, something); ? Don't optimize if you don't have to. Test if you have to. Teensyduino comes with pretty smart and fast built in functions, maybe they suffice for your speed needs.
    Ben

  3. #3
    Quote Originally Posted by Ben View Post
    Have you tried the "normal" approach using an ISR with Teensyduino's analogWrite(6, something); ? Don't optimize if you don't have to. Test if you have to. Teensyduino comes with pretty smart and fast built in functions, maybe they suffice for your speed needs.
    Ben
    Yes I tried a quick test program, and no it's not fast enough even at 120Mhz.

    I want to understand how to program the chip at a low level as this will be very difficult to get going at the speed I need, lots of DSP and lots of bit banging to drive a load of RGB panels.

    D

  4. #4
    Thanks Ben,

    Yes I have tried and it is too slow. Just the call overhead is a bit more than I'd like. I have to reconfigure the PWM on the fly and leave as much main thread time available as possible.

    I suspect it is something simple (but not so easy to find in the datasheet) that I have forgotten to do.

    Dom

  5. #5
    For anyone else who is stuck I finally got it working. The thing to NOT do is set the FTMEN bit in the _SC register!


    Code:
      //WRITE ENABLE
      FTM0_MODE |= FTM_MODE_WPDIS; // | FTM_MODE_FTMEN;
      //Disable everything before setting mod
      FTM0_SC = 0;
      //Timer modulus
      FTM0_MOD = ticks_main;
      //System clock, prescaler 0, enable modulus overflow interrupt
      FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0) | FTM_SC_TOIE;
      //Set pin 6 as timer output with high current and slew rate
      PORTD_PCR4 = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
      //reset counter
      FTM0_CNT = 0;
      //reset counter start
      FTM0_CNTIN = 0;
      //set channel options for PWM inverted
      FTM0_C4SC = FTM_CSC_MSB | FTM_CSC_ELSB;
      //initial pwm value
      FTM0_C4V = 3;
       // Enable FTM0 interrupt inside NVIC
      NVIC_ENABLE_IRQ(IRQ_FTM0);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •