We've got a 16MHz crystal hooked up to a K20FX512, and we're trying to configure it in PEE mode to generate a system clock at 120MHz and a bus clock at 60MHz. The code gets through the MCG initialisation, but then whether we generate a PWM out of one of the timers, or setup UART5, we get speeds twice what we're expecting. The setup code is:
SIM_CLKDIV1=(0<<28)|(1<<24)|(3<<20)|(7<<16); //120 system, 60 bus, 30 flexbus, 15 flash
SIM_SOPT2=(SIM_SOPT2&~(0b11<<16))|(0b01<<16); //PLL0CLK
OSC0_CR=(1<<7);
OSC0_CR|=(1<<1); //8pF
MCG_C2=0b00101100;
MCG_C1=0b10100000; //External Reference as system clock -> 16mhz/512=31.25khz
while(!(MCG_S&(1<<1))); //Wait for OSCINIT0
while((MCG_S&(1<<4))); //Wait for !IREFST
while((MCG_S&(0b11<<2))!=(0b10<<2)); //Wait for CLKST=External Clock
MCG_C5=0b01000001; //PRDIV=2
MCG_C6=0b01001110; //PLL selected, x30=240Mhz
while(!(MCG_S&(1<<5))); //Wait for PLLST
while(!(MCG_S&(1<<6))); //Wait for LOCK0
MCG_C1=0b00100000; //PLL as system clock
while((MCG_S&(0b11<<2))!=(0b11<<2)); //Wait for CLKST=PLL
In case I've missed something obvious here, the PWM and UART setups are:
FTM3_SC=0b00001111; //Bus/128 = 468750Hz
FTM3_MOD=46875; //10Hz
FTM3_C1SC=0b00100100; //Edge Aligned PWM Set on match
FTM3_C1V=46875/2;
-----
tempbd=60000000/16/9600;
UART5_C2=0;
UART5_C1=0;
UART5_BDH=tempbd>>8;
UART5_BDL=tempbd;
UART5_C3=0b00001111;
enable_irq(INT_UART5_RX_TX-16);
UART5_C2=0b00111100;
Regards,
Alex.
Hi NORMAN,
Have you observe the frequency of FlexBus clock with an oscilloscope?
If the frequency of FlexBus clock is 30MHz (SIM_CLKDIV1=0x01370000), then the Bus clock should be correct(60MHz).
PTA6 and PTC3 can be configured as CLKOUT function (use PORTx_PCRn[MUX]). You can select the CLKOUT output FlexBus clock(SIM_SOPT2[CLKOUTSEL]).
If you have Kinetis Design Studio, then you can compared the code which is generated by Processor Expert.
Best Regards,
Robin
Thank you for your reply Robin,
I was able to measure the FlexBUS clock on PTC3, and found that it was running at 60MHz. I'm using Codewarrior, so I started a new project with Processor Expert and compared the generated setup code with my own. Besides confirming the reset states of a lot of registers, the only significant difference I found was that the oscillator was set for low-power, rather than high-gain operation.
MCG_C2=0b00100100;
Changing my code to low-power operation (HGO0, in MCG_C2, above) resolved the problem. Looking at the reference manual, it appears that "High-frequency, high-gain" is only a valid mode on Connections 2 and 3 - are the connections numbered 1 - EXTAL0, 2 - EXTAL1, 3 - EXTAL32?
Hi NORMAN,
The FlexBUS clock on PTC3 should not running at 60MHz, according to the datasheet the maximum frequency is 50MHz .
Please observe the values of SIM_CLKDIV1\ MCG_C5\ MCG_C6 when debugging. The Divide Factor and Multiply Factor should equal the values in below image.
Have you tested the project generated by ProcessorExpert? Will the FlexBUS clock on PTC3 change if you only change the Oscillator operating mode?
I don't have K20FX512, so I test it on TWR-K60F120M board.
Changing the the Oscillator operating mode will not change the FlexBUS clock on PTC3(or period of PWM) on my board. But I am using "System oscillator 1", there is a 12MHz crystal connected on it.
What is the Mask Set of your K20FX512? So that we can check whether it has some errata.
Are the connections numbered 1 - EXTAL0, 2 - EXTAL1, 3 - EXTAL32?
No.
Connection 1: Figure 26-2. Crystal/Ceramic Resonator Connections
Connection 2: Figure 26-3. Crystal/Ceramic Resonator Connections
Connection 3: Figure 26-4. Crystal/Ceramic Resonator Connections
Best Regards,
Robin