During last week I receive a question about identify a way to notice when the 32kHz oscillator is ready to be able to use it as clock source. In other words, when the 32kHz oscillator is stable to be used as reference clock for the MCG module. Then, system can switch over it.
Kinetis devices starts up using its internal clock which is called FEI mode, then, the applications change to a different mode which require an external clock source (i.e. FEE mode). Normally, we have two options which are:
- Main reference clock. Talking specifically to KW devices, there is the 32MHz external crystal for the main reference clock.
- The 32kHz external crystal for the 32kHz oscillator which is driven through the RTC registers.
For the first option, there is a register which could be monitored to know when oscillator is ready (RSIM_CONTROL_RF_OSC_READY). So, it can be polled to notice when the oscillator is up and ready.
About the second option, there is no “OSCINIT” bit for the 32 kHz oscillator. However, there is a way to know when the 32kHz Oscillator is running, it is to simply enable the RTC OSC, and configure the RTC to count. After doing that immediately poll (read) the RTC_TPR register and check it until it is greater than 4096. It will roll over once it reaches 32767 so, it is important to the poll this register in a loop doing nothing else or the register could potentially be read when it is less than 4096 but had already rolled over. Once it is greater than 4096, it can be determined that oscillator is running well. If the RTC is not required, the counter can be disabled. Now that the 32kHz clock is available, the application can switch to FEE mode.
So, in order to perform the above description, I modified the “CLOCK_CONFIG_EnableRtcOsc()” as shown next:
static void CLOCK_CONFIG_EnableRtcOsc(uint32_t capLoad)
/* RTC clock gate enable */
if ((RTC->CR & RTC_CR_OSCE_MASK) == 0u)
/* Only if the Rtc oscillator is not already enabled */
/* Set the specified capacitor configuration for the RTC oscillator, "capLoad" parameter shall be set
to the value specific to the customer board requirement*/
/*Init the RTC with default configuration*/
/* Enable the RTC 32KHz oscillator */
RTC->CR |= RTC_CR_OSCE_MASK;
/* Start the RTC time counter */
/* Verify TPR register reaches 4096 counts */
while(u32cTPR_counter < 4096)
/* 32kHz Oscillator is ready. Based on the application requirements, it can let the RTC enabled or disabled.
In this case, we can disable RTC since it is not needed by this application */
/* RTC clock gate disable since RTC is not needed anymore*/
Then, using the above function, it can be noticed when the 32kHz oscillator is ready to be used.
Hope this helps....