Problem configuring K20 to use an external crystal...

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem configuring K20 to use an external crystal...

1,979 Views
Alex_HMBE
Contributor II

We've had trouble nailing down our system clock speed when the board heats up (turns out there's an errata on that) but, fortunately, we've got a 32k crystal hooked up to [E]XTAL32 for a RTC we didn't end up using. So, as far as I can tell, we can route the RTC through to the FLL clock to get a 96Mhz system clock.

 

My original code didn't work, and neither did the code I pulled from processorexpert so I'm left assuming I've stuffed up somewhere.

 

Here's what I've pulled from PE:

Spoiler

SIM_SCGC6 |= (uint32_t)0x20000000UL; 

if ((RTC_CR & RTC_CR_OSCE_MASK) == 0u) { /* Only if the OSCILLATOR is not already enabled */
  /* RTC_CR: SC2P=0,SC4P=1,SC8P=1,SC16P=0 */
  RTC_CR &= (uint32_t)~0x3C00UL; 
  RTC_CR|=(0b0110<<10);
  /* RTC_CR: OSCE=1 */
  RTC_CR |= (uint32_t)0x0100UL; 
  /* RTC_CR: CLKO=1 */
  RTC_CR |= (uint32_t)0x0200UL; 
}


/* Disable the WDOG module */
/* WDOG_UNLOCK: WDOGUNLOCK=0xC520 */
WDOG_UNLOCK = (uint16_t)0xC520U; /* Key 1 */
/* WDOG_UNLOCK : WDOGUNLOCK=0xD928 */
WDOG_UNLOCK = (uint16_t)0xD928U; /* Key 2 */
/* WDOG_STCTRLH: ??=0,DISTESTWDOG=0,BYTESEL=0,TESTSEL=0,TESTWDOG=0,??=0,STNDBYEN=1,WAITEN=1,STOPEN=1,DBGEN=0,ALLOWUPDATE=1,WINEN=0,IRQRSTEN=0,CLKsrc=1,WDOGEN=0 */
WDOG_STCTRLH = (uint16_t)0x01D2U; 

/* System clock initialization */
/* SIM_CLKDIV1: OUTDIV1=0,OUTDIV2=1,OUTDIV3=3,OUTDIV4=3,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
SIM_CLKDIV1 = (uint32_t)0x01330000UL; /* Update system prescalers */
/* SIM_CLKDIV2: USBDIV=1,USBFRAC=0 */
SIM_CLKDIV2 = (uint32_t)((SIM_CLKDIV2 & (uint32_t)~0x0DUL) | (uint32_t)0x02UL); /* Update USB clock prescalers */
/* SIM_SOPT2: PLLFLLSEL=0 */
SIM_SOPT2 &= (uint32_t)~0x00010000UL; /* Select FLL as a clock source for various peripherals */
/* SIM_SOPT1: OSC32KSEL=1 */
SIM_SOPT1 |= (uint32_t)0x00080000UL; /* RTC oscillator drives 32 kHz clock for various peripherals */
/* Switch to FEE Mode */
/* OSC_CR: ERCLKEN=0,??=0,EREFSTEN=0,??=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
OSC_CR = (uint8_t)0x00U; 
/* SIM_SOPT2: MCGCLKSEL=1 */
SIM_SOPT2 |= (uint32_t)0x01UL;
SIM_SOPT2=1;

/* MCG_C2: ??=0,??=0,RANGE=0,HGO=0,EREFS=0,LP=0,IRCS=0 */
MCG_C2 = (uint8_t)0x00U; 
/* MCG_C1: CLKS=0,FRDIV=0,IREFS=0,IRCLKEN=1,IREFSTEN=0 */
MCG_C1 = (uint8_t)0x00U; 
/* MCG_C4: DMX32=1,DRST_DRS=3 */
MCG_C4 |= (uint8_t)0xE0U; 
/* MCG_C5: ??=0,PLLCLKEN=0,PLLSTEN=0,PRDIV=0 */
MCG_C5 = (uint8_t)0x00U; 
/* MCG_C6: LOLIE=0,PLLS=0,CME=0,VDIV=0 */
MCG_C6 = (uint8_t)0x00U;

 

while((MCG_S & MCG_S_IREFST_MASK) != 0x00U); /* Check that the source of the FLL reference clock is the external reference clock. */
while((MCG_S & 0x0CU) != 0x00U); /* Wait until output of the FLL is selected */

 

The code hangs on the second-last line there, waiting for the status bit to set to show the external reference has been selected. Debugger shows the MCG_C1->IREFS=0.

 

Cheers.

11 Replies

889 Views
BlackNight
NXP Employee
NXP Employee

I had a problem with the clock circuits on two boards: one had a 1 MOhm Resistor populated, one not. Because of this, I had to configure to 'low power' mode if the resistor was not present, and in 'high gain' mode if the resistor was present (see the R25 resistor in this post: Tutorial: USB CDC with the KL25Z Freedom Board | MCU on Eclipse). Otherwise the code was hanging in exact that location.

So if this happens, it is worth a try to configure the clock either as low power or as high gain.

Hope this helps,

Erich

889 Views
pengliang
Contributor III

Hi,

I have the board with R25 populated on. I enabled the external crystal 8MHz. I used scope to see the output from external crystal. Because R25 is populated, I can see the output from external crystal only set to high gain mode. If I set to low power mode. the output from external crystal is just noise. This is what I observed. Now, I unpopulated R25 resistor. regarding to what you said, it has to be set to low power mode. but whatever I set to low power or high gain, the output from external crystal is just noise.

Do you have any idea why it behaves like this?

Thanks in advance

0 Kudos

889 Views
BlackNight
NXP Employee
NXP Employee

Hello,

Are you talking about the FRDM-KL25Z Freedom board?

What you say about the R25 installed makes sense: it works only in high gain mode. And this confirms what you see. But for me without R25, it only works in low power mode. I have not measured it, but I would see only noise too. But with low power mode my Cpu works. Maybe your code did not set the low power bit (have you recompiled the project?).

Hope this helps,

Erich

0 Kudos

889 Views
pengliang
Contributor III

Yes, I am working on the FRDM-KL25Z EV. board

firstly thank you reply to me.

do you mean if i take off the R25 resister, I have to set to low power mode, right? I am pretty sure it is set to low power mode in VLLS3 mode. and enabled OSC0ERCLK IN STOP. after I compiled it, and run. I put the probe on the pin41(XTAL) the output of external crystall. There is just noise shown on the scope. I expect the 8MHz signal tho. Did you try to see on the scope?

Thank you

0 Kudos

889 Views
BlackNight
NXP Employee
NXP Employee

No, I have no scope right now where I am, sorry.

I mean if you take off the R25 resistor, the clock settings *have* to use low power mode.

What I mean is that the HGO0 bit below must be set to zero:

  /* MCG_C2: LOCRE0=0,??=0,RANGE0=2,HGO0=0,EREFS0=1,LP=0,IRCS=1 */

  MCG_C2 = (MCG_C2_RANGE0(0x02) | MCG_C2_EREFS0_MASK | MCG_C2_IRCS_MASK);                                                  


See Tutorial: USB CDC with the KL25Z Freedom Board | MCU on Eclipse


Hope this helps,

Erich

0 Kudos

889 Views
pengliang
Contributor III

figured it out~~

Thank you for reply

0 Kudos

889 Views
pengliang
Contributor III

I checked, HGO0 = 0;

but it is not oscillate in low power operating mode for some reason.

0 Kudos

889 Views
luisgallegos
Contributor III

Hello Erich.

Yep it's me, luismgc, the one who was bothering you with all those questions in the blog post :smileyblush:. It did help.

Thanks again.

0 Kudos

889 Views
luisgallegos
Contributor III

Hello Alex.

I am working with the FRDM-KL25Z board and I'm having the same problem trying to make it work with an 8 MHz external crystal. The code stops at the same line.

Were you able to solve this? I'd appreciate any helpful tips.

Thanks.

0 Kudos

889 Views
JimDon
Senior Contributor III

I have posted the code to set up the Freedom Board clock for 48 Mhz from the 8Mhz rock.

Post with code.

889 Views
luisgallegos
Contributor III

Hello Jim

Thanks. The problem was a hardware issue. Apparently my board came with one resistor missing so the configurations were supposed to be different than the ones on the tutorial I was using to set it up. You can see Erich Styger's response in this thread for the details.

Thanks anyway for the code. Might come in handy later on.

Luis.

0 Kudos