I am trying to get a KL26Z256VMC2 0N40H to use the RTC crystal as the clock source.
As far as I can tell the following process should do it, but it does not work for some reason.
The first step that goes wrong is when I attempt to set MCG_C7.OSCSEL to 1 (Selects 32 kHz RTC Oscillator - see KL26P121M48SF4RM KL26 Sub-Family Reference Manual, Rev. 3.2, October 2013).
When I attempt to view the register in a debugger, it is clear that the assignment has not worked. Using the debugger to manually set the bit does not work either; it always reads as zero.
This has been ported from the K10 where it works successfully
1) Set the SIM_CLKDIV1 to configure the clock dividers: SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV4(1);
2) Disable RTC interrupts: RTC_IER = 0;
3) Set an appropriate RTC capacitive load
4) Enable the RTC and enable clock usage: RTC_CR |= RTC_CR_OSCE_MASK; RTC_CR &= ~RTC_CR_CLKO_MASK;
5) Initialise the time counters to zero while counting is temporarily disabled)
6) Wait for 4096 RTC oscillation counts to be counted to ensure the RTC is stable
7) Select the external RTC oscillator as the external clock source: MCG_C7 = 0x1; *** This does not work on the KL26; reading back this register reveals that MCG_C7 remains 0 ***
8) Make sure FLL is selected: MCG_C6 = 0;
9) Configuring the clock source to be the external reference: MCG_C1 = 0;
10) Waiting to make sure that the external reference has been used (waiting for MCG_S.IREFST to become zero) *** The FLL never actually switches to use the external reference ***
11) Configure the FLL to use an appropriate multiplier from the default x640 and wait for the FLL lock time before continuing
I can not get the KL26 past the check in step 10.
The RTC crystal is correctly oscillating at ~32.8kHz according to my oscilloscope.
Step 7 does not work on the KL26
I also noticed that MCG_S is almost always 0x12 on the KL26: IREFST = 1 and OSCINIT0 = 1 (I have occasionally seen it as 0x10 before 6). (NB: on the K10, MCG_S starts out as 0x10 at step 1 and changes to 0x00 during step 10)
I do not know why my KL26 is almost always reporting that OSC module's crystal is initialised, even at the start of the crt0 assembler unless it is referring to the RTC oscillator on the KL26 (but not on the K10)
The errata for mask 0N40H does not suggest that there is a problem with the RTC or FLL that could be causing this.
Please can anyone tell me:
1) That it definitely is possible to use the RTC reference to derive the main clocks on the KL26
2) What am I doing wrong to select the RTC as the external clock source in step 7
3) Why MGC_S.OSCINIT0 is almost always reporting that the OSC module initialisation period has elapsed on the KL26 when only the RTC has ever been enabled (OSC0_CR always reads 0 from entry into the reset handler onwards)
4) If I am missing something fundamental in the process above
Thank you all for any help you can provide,
Solved! Go to Solution.
For anyone interested:
The K10 and KL26 differ slightly in the working methods of configuring the RTC 32kHz oscillator.
The K10 allows you to use MCG_C7 to select the 32kHz RTC reference as a clock source (i.e. MCG_C2.EREFS0 == 0).
The KL26 doesn't seem to let you do this (despite the documentation for MCG_C7 being no different to the K10); you must configure it as an external crystal by setting MCG_C2.EREFS0 = 1