When the processor boots, it is using the internal clock. After a while an external oscillator is turned on and the processor switches to using that. When the app is finished for the day, it switches back to the internal clock. When trying to switch the command "MCG->SC = 0x02;" When this is performed, the processor goes off into the weeds (starts executing in an invalid memory space (0x4004XXXX). I use the same code when booting up and when switching back to the internal clock.
Can the processor switch from the internal clock to the external clock and back? If so, how?
Thank you for any assistance,
George Weatherford
Hi George,
For instance, the following code will illustrate the procedure of switching to the HIRC mode.
void CLOCK_HAL_SetHircMode(void)
{
MCG->MC |= MCG_C2_HIRCEN_MASK;
MCG->C1 =MCG_C1_CLKS(0x0);
while((MCG->S & MCG_S_CLKST_MASK) != 0x00U) {
}
/* Enable the IRC source*/
MCG_C1 |= MCG_C1_IRCLKEN_MASK;
}
Have a great day,
Ping
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
OK, I figured the problem out.
In the code from Keil the “when loop” is looking for which clock is
selected. When I change the when loop to look to at the “OSX clock is
ready” bit, every thing works.
From: jeremyzhou
Sent: Wednesday, May 25, 2016 9:52 PM
To: George Weatherford
Subject: Re: - Using the MKL17Z256VLH4
processor. Can you switch from the internal clock to an external clock and
then back?
NXP Community
<https://community.freescale.com/resources/statics/1000/35400-NXP-Community-Email-banner-600x75.jpg>
Using the MKL17Z256VLH4 processor. Can you switch from the internal clock
to an external clock and then back?
reply from jeremyzhou
<https://community.nxp.com/people/jeremyzhou?et=watches.email.thread>
in *Kinetis
Microcontrollers* - View the full discussion
<https://community.nxp.com/message/654472?et=watches.email.thread#comment-654472>
Hi George,
I've adjusted the code again, then it works on the FRDM-KL27 board.
void CLOCK_SetExtMode(void)
{
// enable OSC
SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK;
PORTA_PCR18 &= (uint32_t)~(uint32_t)((PORT_PCR_ISF_MASK | PORT_PCR_MUX(0x07)));
PORTA_PCR19 &= (uint32_t)~(uint32_t)((PORT_PCR_ISF_MASK | PORT_PCR_MUX(0x07)));
MCG->C2 |= MCG_C2_EREFS0_MASK; /* Enable LIRC 8MHz */
MCG->C1 |=MCG_C1_IRCLKEN_MASK;
MCG->SC |=MCG_SC_FCRDIV(0x00);
if (((MCG_C2) & MCG_C2_EREFS0_MASK) != 0U) {
while((MCG->S & MCG_S_OSCINIT0_MASK) == 0x00U) { /* Check that the oscillator is running */
}
}
MCG->C1 |=MCG_C1_CLKS(0x02);
while((MCG->S & MCG_S_CLKST_MASK) != 0x08U) { /* Wait until external reference clock is selected as MCG_Lite output */
}
}
Have a great day,
Ping
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Thank you for your code Jeremy,
This code appears to be to switch to the external clock. The code I have
seems to be doing that ok now. We are trying to switch back from external
clock to internal clock.
Do you have anything on that?
Thank you again,
George
From: jeremyzhou
Sent: Tuesday, May 24, 2016 3:44 AM
To: George Weatherford
Subject: Re: - Using the MKL17Z256VLH4
processor. Can you switch from the internal clock to an external clock and
then back?
NXP Community
<https://community.freescale.com/resources/statics/1000/35400-NXP-Community-Email-banner-600x75.jpg>
Using the MKL17Z256VLH4 processor. Can you switch from the internal clock
to an external clock and then back?
reply from jeremyzhou
<https://community.freescale.com/people/jeremyzhou?et=watches.email.thread>
in Kinetis Microcontrollers - View the full discussion
<https://community.freescale.com/message/653488?et=watches.email.thread#comment-653488>
Hi George,
Before answer your question, I was wondering if you can share the code that transits the MCG_Lite to the LIRC mode or HIRC mode from the EXT mode.
Then I can investigate the code to find out the root cause.
I'm looking forward to your reply.
Have a great day,
Ping
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Here is the code use to go from internal clock source to external clock
source (set_ext_clk) and from external clock source to internal clock
source (set_int_clk).
Thank you for you assistance,
George
From: jeremyzhou
Sent: Thursday, May 19, 2016 2:10 AM
To: George Weatherford
Subject: Re: - Using the MKL17Z256VLH4
processor. Can you switch from the internal clock to an external clock and
then back?
NXP Community
<https://community.freescale.com/resources/statics/1000/35400-NXP-Community-Email-banner-600x75.jpg>
Using the MKL17Z256VLH4 processor. Can you switch from the internal clock
to an external clock and then back?
reply from jeremyzhou
<https://community.freescale.com/people/jeremyzhou?et=watches.email.thread>
in Kinetis Microcontrollers - View the full discussion
<https://community.freescale.com/message/652058?et=watches.email.thread#comment-652058>
Hi George,
Please give a shot again with the modified code.
//Sets internal clock source
void set_int_clk(void)
{
//SIM->COPC = (uint32_t)0x00u;
//SIM->SOPT2 &= ~SIM_SOPT2_USBSRC_MASK; /* USB_CLKIN is clock source for USB FS (applicable only for derivatived with USB)*/
//SIM->CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0x00) |
// SIM_CLKDIV1_OUTDIV4(0x01); /* Update system prescalers */
// OSC0->CR = (uint32_t)0x00u; /* Disable External reference */
MCG->C2 |= MCG_C2_IRCS_MASK; /* Enable LIRC 8MHz */
MCG->C1 |=MCG_C1_IRCLKEN_MASK;
MCG->SC |=MCG_SC_FCRDIV(0x00);
MCG->C1 |=MCG_C1_IRCLKEN_MASK;
while((MCG->S & MCG_S_CLKST_MASK) != 0x04u) {} /* Check that the clock source is the LIRC clock. */
}
Have a great day,
Ping
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Sorry, I didn’t get back to you sooner. I was out of the office Friday.
Anyway, I put your code in and it does not work.
The code hangs at:
while((MCG->S & MCG_S_CLKST_MASK) != 0x04u) {} /* Check that the clock
source is the LIRC clock. */
Thanks for your help.
George
From: jeremyzhou
Sent: Friday, May 20, 2016 2:00 AM
To: George Weatherford
Subject: Re: - Using the MKL17Z256VLH4
processor. Can you switch from the internal clock to an external clock and
then back?
NXP Community
<https://community.freescale.com/resources/statics/1000/35400-NXP-Community-Email-banner-600x75.jpg>
Using the MKL17Z256VLH4 processor. Can you switch from the internal clock
to an external clock and then back?
reply from jeremyzhou
<https://community.freescale.com/people/jeremyzhou?et=watches.email.thread>
in Kinetis Microcontrollers - View the full discussion
<https://community.freescale.com/message/652506?et=watches.email.thread#comment-652506>