How can I control the clock speed of Teensy 3.2?

Status
Not open for further replies.

JimmyT

New member
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
 
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.
 
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!
 
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.
 
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
 
Status
Not open for further replies.
Back
Top