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

Thread: Teensy 4.1 available PWM frequencies

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Location
    Berkeley
    Posts
    2

    Teensy 4.1 available PWM frequencies

    Hello,

    I am wondering what the possible PWM frequencies are on the Teensy 4.1. I am hoping to use 50% duty-cycled PWM or some other hardware timer based method (ideally no load on the CPU) to generate a 1.0MHz clock for an external ADC. On the teensy website https://www.pjrc.com/teensy/td_pulse.html there is a table of frequencies and the corresponding resolution, but are those *all* the available frequencies? Internally, is there some clock divider / PLL doing the work to generate them, and what are the available divider ratios?

    If others have alternative suggestions for how to generate a 1.0MHz clock on any output pin (I have flexibility), I'm certainly open to those.

  2. #2
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    728
    I have a related question - what is the fastest possible clock rate? Ideally for the GPT.

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,915
    In most cases like this you need to look at the sources... and manual:

    PWM: Depends on which pin... Some are FlexPWM and others are Quad timers.

    I believe the default F_BUS_ACTUAL is 150000000

    And the computation that FlexPWM does:
    Code:
    void flexpwmFrequency(IMXRT_FLEXPWM_t *p, unsigned int submodule, uint8_t channel, float frequency)
    {
    	uint16_t mask = 1 << submodule;
    	uint32_t olddiv = p->SM[submodule].VAL1;
    	uint32_t newdiv = (uint32_t)((float)F_BUS_ACTUAL / frequency + 0.5f);
    	uint32_t prescale = 0;
    	//printf(" div=%lu\n", newdiv);
    	while (newdiv > 65535 && prescale < 7) {
    		newdiv = newdiv >> 1;
    		prescale = prescale + 1;
    	}
    So if you pass in 1000000 I think the code will compute 150 which should work...

    Would need to look at quad...

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,041
    Quote Originally Posted by edmundsj View Post
    I am hoping to use 50% duty-cycled PWM or some other hardware timer based method (ideally no load on the CPU) to generate a 1.0MHz clock for an external ADC.
    Use this:

    Code:
    void setup() {
      analogWriteFrequency(2, 1000000);
      analogWrite(2, 128);
    }
    
    void loop() {
    }
    Click image for larger version. 

Name:	file.png 
Views:	10 
Size:	25.4 KB 
ID:	21768

    Quote Originally Posted by jonr View Post
    I have a related question - what is the fastest possible clock rate? Ideally for the GPT.
    The FlexPWM runs from the 150 MHz peripheral clock (when the CPU is 600 MHz or any multiple of 150 MHz), so 75 MHz is the fastest PWM output.

    Code:
    void setup() {
      analogWriteFrequency(2, 75000000);
      analogWrite(2, 128);
    }
    
    void loop() {
    }
    Click image for larger version. 

Name:	file2.png 
Views:	11 
Size:	31.5 KB 
ID:	21769

    My oscilloscope has only 200 MHz bandwidth, so this is the best image I can capture with my limited equipment...

  5. #5
    Junior Member
    Join Date
    Sep 2020
    Location
    Berkeley
    Posts
    2
    Ah, beautiful! Thanks.

Posting Permissions

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