How to safely disable oscillator on K24

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

How to safely disable oscillator on K24

1,103 次查看
joee
Contributor I

My board is running with an external clock at 50MHz.   The MCG and OSC are setup in the bootloader with the following code (from Keil Uvision startup file):

#elif (CLOCK_SETUP == 2)
  /* SIM_CLKDIV1: OUTDIV1=0,OUTDIV2=0,OUTDIV3=1,OUTDIV4=1,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
  SIM->CLKDIV1 = (uint32_t)0x00110000u; /* Update system prescalers */
  /* Switch to FBE Mode */
  /* OSC->CR: ERCLKEN=0,??=0,EREFSTEN=0,??=0,SC2P=0,SC4P=0,SC8P=0,SC16P=0 */
  OSC->CR = (uint8_t)0x00u;
  /* MCG->C7: OSCSEL=0 */
  MCG->C7 = (uint8_t)0x00u;
  /* MCG->C2: ??=0,??=0,RANGE0=2,HGO=0,EREFS=1,LP=0,IRCS=0 */
  MCG->C2 = (uint8_t)0x24u;
  /* MCG->C1: CLKS=2,FRDIV=5,IREFS=0,IRCLKEN=0,IREFSTEN=0 */
  MCG->C1 = (uint8_t)0xA8u;
  /* MCG->C4: DMX32=0,DRST_DRS=0 */
  MCG->C4 &= (uint8_t)~(uint8_t)0xE0u;
  /* MCG->C5: ??=0,PLLCLKEN=0,PLLSTEN=0,PRDIV0=0 */
  MCG->C5 = (uint8_t)0x00u;
  /* MCG->C6: LOLIE=0,PLLS=0,CME=0,VDIV0=0 */
  MCG->C6 = (uint8_t)0x00u;
  while((MCG->S & MCG_S_OSCINIT0_MASK) == 0u) { /* Check that the oscillator is running */
  }
  while((MCG->S & MCG_S_IREFST_MASK) != 0u) { /* Check that the source of the FLL reference clock is the external reference clock. */
  }
  while((MCG->S & 0x0Cu) != 0x08u) {    /* Wait until external reference clock is selected as MCG output */
  }
  /* Switch to BLPE Mode */
  /* MCG->C2: ??=0,??=0,RANGE0=2,HGO=0,EREFS=1,LP=0,IRCS=0 */
  MCG->C2 = (uint8_t)0x24u;
#endif

 

I need to disable the oscillator in the main code so that I can use the XTAL pin as GPIO. I am trying to do this by setting the EREFS bit of MCG_C2 as seen in the following code, but occasionally I get a bus fault afterwards.  

MCG->C2 &= ~MCG_C2_EREFS0_MASK; //set for external clock
while((MCG->S & MCG_S_OSCINIT0_MASK ) != 0x00u);   /* Wait until external reference clock is selected as MCG output */

 

Is there some other sequence I need to perform to do this or some other status to wait for?

 

thanks

0 项奖励
回复
2 回复数

999 次查看
joee
Contributor I

Thanks for your response.  The post does make things a bit clearer, but I don't see that it addresses switching from external crystal to external clock.

For now I am switching back to the FEI state and then switching to FBE, then BLPE with the EREFS bit of MCG_C2 set to 0.  So far I have not had any issues with this method.

0 项奖励
回复

1,047 次查看
vicentegomez
NXP TechSupport
NXP TechSupport
0 项奖励
回复