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

Thread: How can I control the clock speed of Teensy 3.2?

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    4

    How can I control the clock speed of Teensy 3.2?

    I have a few questions about the clock speed of the teensy 3.2

    1. What is the default speed?
    2. Can the clock speed be increased?
    3. Is there a code example on how to do this?

    Thanks,
    JimmyT

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,442
    The clock speed is set during compile time, using the corresponding clock speed option in the tools menu of the Arduino IDE. There you select a clock speed between 4MHz and 96MHz. Editing the corresponding boards.txt file which comes with the Teensyduino installation, you’ll find a few over locking options to uncomment.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,174
    Quote Originally Posted by JimmyT View Post
    3. Is there a code example on how to do this?
    It's so easy. In Arduino, click Tools > CPU Speed.

  4. #4
    Junior Member
    Join Date
    Aug 2018
    Posts
    5
    To continue this thread, is it possible to force the CPU Speed in the code itself?

    I have a situation where a Teensy 3.6 occasionally uses EEPROM and timing is very important, so the CPU speed needs to be 120 mHz all the time. Somebody recently flashed it and didn't set the CPU Speed properly, and I would like to prevent that in the future.

    Thanks!

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,174
    Quote Originally Posted by td0g View Post
    Somebody recently flashed it and didn't set the CPU Speed properly, and I would like to prevent that in the future.
    Simplest way might be something like this:

    Code:
    #if F_CPU != 120000000
    #error "This code requires 120 MHz CPU speed, use Tools > CPU Speed menu"
    #endif
    If you *really* want to control the CPU speed at runtime, you certainly can, but it's complicated. Look at mk20dx128.c for the startup code which sets the PLL and configures the clocks.

    Also consider things like millis(), delay(), and baud rates are all compiled with F_CPU, so if you change clock speed without recompiling, lots of timing specific stuff will run at the wrong speed.

    If you will change the PLL after startup, you'll probably need to craft new code which gets the chip back to running from the internal RC oscillator, as it does at startup. Check the MCG chapter of the reference manual for details. Did I mention changing clocking modes is complicated? Editing mk20dx128.c is probably simpler.

    But if you only want to prevent anyone (including yourself) from accidentally reprogramming the board with F_CPU configured wrong, that #ifdef and #error way will do the job nicely.

  6. #6
    Junior Member
    Join Date
    Aug 2018
    Posts
    5
    This is perfect. The #ifdef and #error are exactly what I was looking for!

    Good to know about the other stuff you mention, but I'm hoping to keep this project as simple as possible.

    Cheers, Tyler

Posting Permissions

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