I'm desperately trying to use a 32 MHz external crystal oscillator on the MKW22 Kinetis MCU (so using PEE mode).
When I do that, I sometimes get stuck when waiting for MCG->S OSCINIT0 value to be set. The wierdest part is that I always get stuck when using a certain processor, and it always works perfectly when using another (with the exact same reference !). Some random behaviour is observed but it's impossible for me to undersand from where it comes.
The external crystal is placed between XTAL_32M and EXTAL_32M pins.
So fare, I tried it on 5 different processors, it worked for 3 of them and I didn't for the other 2). It is not useful to accuse the hardware, the boards I'm using where soldered by a professional company and checked after production.
A last important detail: We use 802.15.4 protocol on these processors, based on the same external 32 MHz crystal, and this protocol works fine with all processors tested. The problem really seems to come from processor configuration.
Here is the code I use to setup my clock:
MCG->C2 = 0x24; for(i = 0; i<100; i++) __asm__ volatile ("NOP"); MCG->C1 = 0xA8; while(!(MCG->S & MCG_S_OSCINIT0_MASK)) ; while(MCG->S & MCG_S_IRCST_MASK) ; while(((MCG->S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0b10) ; __asm("NOP"); MCG->C5 = 0b00011; for(i = 0; i<100; i++) __asm__ volatile ("NOP"); MCG->C6 = 0x50; while((MCG->S & MCG_S_PLLST_MASK) == 0) ; while((MCG->S & MCG_S_LOCK0_MASK) == 0) ; __asm("NOP"); MCG->C1 = 0x28; while(((MCG->S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0b11) ;
When this code is executed, on some precessors I never have the condition
(MCG->S & MCG_S_OSCINIT0_MASK) == 1
but on other processors, this condition is true after some milliseconds.
The code above is written based on the following example found in the reference manual (Example 1: Moving from FEI to PEE mode: External Crystal = 4 MHz, MCGOUTCLK frequency = 48 MHz):
Is it a problem with the configuration of the oscillator (a external capacitance issue, or the RANGE0 register that does not have the expected value) ? I'm really tired of configuring this clock.
Why does NXP creates such hard-to-configure and bug-full features as this MCG module ? It makes me regret the processor choice at the beginning of this project...
Thank you for your help,
Thank you for your help. Thanks to your message I finally understood my problem.
In order to use the external crystal oscillator, I only had to enable the presence of the modem 32MHz clock on the external reference clock of the MCU for it to work with it.
Configuring the clock in the KW2xD is a little more difficult than for other Kinetis MCUs because this a system-in-package (SiP) which contains an RF transceiver and an MCU. The transceiver has an oscillator required for the 802.15.4 standard and optionally can provide a clock output for the MCU to use. This is the clock configuration used by the demo applications and is described in chapter 220.127.116.11 Single Crystal with CLK_OUT used by MCU of the RM. For additional reference, I wrote a year ago a document with more information on the clock configurations in KW2xD https://community.nxp.com/docs/DOC-333573 .
The example that you are referencing (Example 1: Moving from FEI to PEE mode: External Crystal = 4 MHz, MCGOUTCLK frequency = 48 MHz) is assuming the MCU oscillator has an external crystal in the EXTAL0 and XTAL0 pins and you have the crystal connected to the XTAL_32M and EXTAL_32M pins which is the transceiver oscillator. The code you are using is waiting for the MCU oscillator to be ready but it gets stuck because there is no crystal.
I highly suggest you to use the initialization code from one of our demo applications.
Let me know any question you may have.