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

Thread: Change CPU clock on the fly.

  1. #1

    Change CPU clock on the fly.

    Gentlemen s.
    How to programmatically change the frequency of CPU speed.
    I reviewed the entire forum, I did not find the answer.
    Although it is likely that the answers were, but for me they were not understood.
    Dear system programmers, please tell me how to programmatically change the frequency of the CPU.
    CPU speed in my device (Teensy 3.6) operates at 72 MHz. It is necessary to reduce speed up to 24 MHz to reduce power consumption, and return to the original speed when the battery is fully charged.
    Sorry, I do not understand how to do this.
    Thank.

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,154
    At the moment, a lot of library functions access F_CPU /F_BUS compile time symbols, and cannot adapt to changing CPU speeds.
    If you really want to change CPU speed best is to study Paul's startup code (it is in cores)

    BTW, A significant permanent power reduction is achieved by adding asm("wfi") at the end of loop().
    this 'halts' the CPU until next interrupt (if you do nothing, the CPU does nothing, otherwise CPU wakes up 1000 times per second (systick interrupt)))

  3. #3
    I understood you.
    Thank you very much for your reply.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,929
    The easiest way is to click the Tools > CPU Speed menu in Arduino.

    To change speed on-the-fly while Teensy 3.6 is running, you can write to SIM_CLKDIV1_OUTDIVS. However, this isn't as easy as you might hope. Only certain ratios are legal. The hardware will ignore writes of illegal values, which is a lot more user friendly than the CPU locking up, but you get no feedback or indication why it ignored your write. You can find info in the reference manual about which ratios of clocks are allowed. Or if you're methodical, you can just try things until you find combinations which work.

    There aren't many examples. Look in mk20dx128.c for kinetis_hsrun_disable() and kinetis_hsrun_enable(). The code is used by the EEPROM writing, to temporarily drop to a slower speed so HSRUN mode can be disabled. The EEROM memory will reject all attempts to write while the chip is in HSRUN mode.

    When you do get slower speeds by writing to SIM_CLKDIV1_OUTDIVS, remember all the code like delay(), elapsedMicros, etc are written with F_CPU and F_BUS compiled into the code. Changing F_BUS tends to affect the speed of most peripherals, so you might need to reconfigure hardware you're using if you mess with SIM_CLKDIV1_OUTDIVS in ways that alter F_BUS.

  5. #5
    Many thanks to the detailed and understandable answer.
    But apparently this idea will have to be abandoned periphery on the I2C and SPI bus, so this switch will be very problematic and costly.
    We will manage what we have.

    Thank.

Posting Permissions

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