I am currently trying to enable/use a 12 MHz external oscillator on a design with a MKL27Z256. The software was previously running with the internal clock. I cannot remember exactly where the original files came from, but they are copyright Freescale, with a build identification b140923 for rev 1.3, so presumably the Kinetis SDK 1.3 (?) Now, we have added an external crystal.
These files include (edited version attached) some defins like CPU_XTAL_CLK_HZ and some predefined settings for the MCGLite registers according to CLOCK_SETUP with some pre-defined example settings.
By adjusting option 5 somewhat, I got the device to boot; however, it did require both misconfiguration of the macros, adjustment of the values (which to me seemed incorrect compared to the data sheet, and definitely wrong when compared to reality.) Currently I'm rather confused as to whether my setting is correct.
#define CPU_XTAL_CLK_HZ 24000000u /* strange? Value of the exter
nal crystal or oscillator clock frequency in Hz is 12 MHz really */
#define CPU_INT_FAST_CLK_HZ 48000000u /* Value of the fast
internal oscillator clock frequency in Hz */
#define CPU_INT_IRC_CLK_HZ 48000000u /* Value of the 48M i
nternal oscillator clock frequency in Hz */
#elif (CLOCK_SETUP == 5)
#define DEFAULT_SYSTEM_CLOCK 48000000u /* Default System clock value */
#define CPU_INT_SLOW_CLK_HZ 8000000u /* Value of the slow internal oscillator clock frequency in Hz */
#define MCG_MODE MCG_MODE_EXT /* Clock generator mode */
/* MCG_C1: CLKS=2,IRCLKEN=0,IREFSTEN=0 */
#define MCG_C1_VALUE 0x82u /* MCG_C1 */
/* MCG_C2: RANGE0=3,HGO0=0,EREFS0=1,IRCS=1 */
#define MCG_C2_VALUE 0x25u /* MCG_C2; should be 0x25, not 0x2d */
/* MCG_SC: FCRDIV=0 */
#define MCG_SC_VALUE 0x00u /* MCG_SC */
/* MCG_MC: HIRCEN=0 LIRC_DIV2=0 */
#define MCG_MC_VALUE 0x00u /* MCG_MC */
/* OSC0_CR: ERCLKEN=1,EREFSTEN=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
#define OSC0_CR_VALUE 0x80u /* OSC0_CR */
/* SMC_PMCTRL: RUNM=0,STOPA=0,STOPM=0 */
#define SMC_PMCTRL_VALUE 0x00u /* SMC_PMCTRL */
/* SIM_CLKDIV1: OUTDIV1=0,OUTDIV4=1 */
#define SYSTEM_SIM_CLKDIV1_VALUE 0x10000u /* SIM_CLKDIV1 */
/* SIM_SOPT1: OSC32KSEL=0,OSC32KOUT=0 */
#define SYSTEM_SIM_SOPT1_VALUE 0x00000000u /* SIM_SOPT1 */
/* SIM_SOPT2: LPUART1SRC=0,LPUART0SRC=0,TPMSRC=3,FLEXIOSRC=0,USBSRC=0,CLKOUTSEL=0,RTCCLKOUTSEL=0 */
#define SYSTEM_SIM_SOPT2_VALUE 0x03000000u /* SIM_SOPT2 */
#error The selected clock setup is not supported.
#endif /* (CLOCK_SETUP == 5) */
Also, the SystemInit() routine seemed to hang forever, if the HCG0 bit is set direcly in MCG_S2, -- only when setting the HCG0 bit after having the previous check on the status check finish, was I able to set the clock (however, if not in high gani mode, it seemed that I at least failed to get correct timings as well.)
The wrong setting in the CPU_XTAL_CLK_HZ was needed, or SystemCoreClockUpdate() returned a wrong reading?
Now, system_MKL17Z644.h/.c hint at a different CPU version as well?
Does anybody have a concise example of how to set the MKL27Z256 to use an external crystal oscillator at 12 MHz and a matching SystemCoreClockUpdate() to subsequently update SystemCoreClock, and get a correct 1 ms tick (i.e. get SysTick_Config() result in correct settings.) Right now I'm confused and slightly unsure if my programming of the clock is correctly, and even what the resulting CPU clock ends up at!
Original Attachment has been moved to: system_MKL17Z644.c.zip
Original Attachment has been moved to: system_MKL17Z644.h.zip