Dynamic CPU Frequency in Rockbox
Why adjust the frequency?
To save battery power. We want to run the CPU on the lowest frequency possible as much as possible. When there isn't much to do, the CPU should draw as little power as possible.
The "boost" concept
The general idea is that tasks that need raw CPU power should run at the maximum speed until the task is done, and then switch back to the idle frequency as soon as possible. The typical case is when the CODEC wants to decode a chunk of data to fill the PCM buffer. We conserve battery by running at the idle frequency as much as possible.
The penalty of boosting the CPU frequency
First of all, it draws more power. Secondly, the frequency switch itself takes time, and the tick timer will lose accuracy. On the Coldfire, the PLL takes up to 10ms to lock on the new frequency. Because of this, we should not switch frequency too often.
The idle mode
There are two modes of operation, normal
. The idle mode uses the idle frequency when not boosted instead if the normal frequency to save power, for example when in USB mode.
Whenever a thread wants some CPU power, it calls cpu_boost with a true argument:
cpu_boost(true); /* Gimme da raw powah! */
and when it is done, it calls it again with a false argument:
cpu_boost(false); /* Done for now, let's relax */
The cpu_boost() function keeps track of the number of calls, and will not step down the CPU frequency until all callers have reported that they are done. Make sure you call cpu_boost(false) when you're done, otherwise the CPU will continue to run at the maximum frequency, draining the battery.
When you want to save power, like when in USB mode, you call cpu_idle_mode():
cpu_idle_mode(true); /* Let's be calm for a while */
and we call it again to go back to normal:
cpu_idle_mode(false); /* Back to work */
Copyright © by the contributing authors.