From time to time I forget how to set the UC3B CPU to run at full speed. The EVK1101 comes with a 12MHz crystal, and I use the same crystal for my board because it is a nice number that let me easily reach the 48Mhz needed for driving the USB port correctly. So I will not change it, but I will try to get the most of the CPU.
The UC3B comes with the following oscilators:
- 2 PLL [80MHz – 240MHz]
- Crystal Osc. [0.4MHz – 20MHz] (but I will let it fixed at 12MHz)
- Crystal Osc. [32KHz] (The RTC clock)
- RC Osc. [115KHz] (May be useful for low power consumption apps, but not this case.)
The Power Management unit handles all clock generators. A close look at the part of the unit responsible for driving CPU, HSB, PBA and PBB clocks is shown here:
So if we want to run our UC3B as fast as cars do in “The Fast & The Furious” (but without Nitro), we have to use PLL. The rules for setting PLL propertly are:
- Crystal must be between 0.4MHz and 16MHz
- PLL multiplication factor goes from 3 to 15
- PLL frequency must go from 80MHz to 240MHz
PLL output frecuency is the result of ((Crystal frequency/ OSC_DIVIDE) * PLL_MUL_FACTOR). As the datasheet states the maximum frequency for the UC3B is 60MHz. So the following should configuration can be applied in order to get 60MHz for CPU and 30MHz for PBA (USB will use PBB and PLL1 in order to get 48Mhz from the 12Mhz source)
Crystal Osc: 12MHz
PLL multiplication factor: 9
PLL divisor: 1
PLL0 = (12MHZ / 1) * (9+1) = 120MHz (and we are safe at this frequency)
PLL0 output divided by two = 1 (yes)
CPU frequency = PLL0 output /2 = 60MHz
PBA frequency = 30MHz (Pheriperial Bus A clock)
In order to get this configuration we have to choices. We can do it manually, or we can tell ASF (AVR Software Framework) to set the registers based on our needs.
The old way:
And the simplest way of configuring the frequency we want is by doing the following.
Of course, both ways do the same thing. But this lastest one, lets the ASF do the maths and the configuration for you.
It is important not to forget to set the CPU frequency for NewLib and to initialize the USB clock after setting the main clock source.