Hi
See the following for a practical guide to working with the MCG:
https://www.utasker.com/kinetis/MCG.htmlThis is not the same as the MCG_Lite in the KL03 but the basic principles still apply.
I assume that your basic mode of operation is from the IRC48M and therefore the configuration (app_hw_kinetis.c) should be left for this, which will mean that the project is build for this mode and the processor will start in this mode.
To move from this mode to IRC8M the logical steps are:
1. Select 8MHz source using IRCS and prepare the divider value to be used (FCRDIV)
2. If you plan to use MCGIRCLK for some peripherals also adjust LICR_DIV2 to suit
At this point in time the system will still be operating from IRC48M but will be pre-prepared for the following mode
3. To perform the switch over CLKS is set to select the IRC8M output instead of IRC48M output (usually one spins on the status register checking that the switch-over has completed before continuing)
Now MCGOUTCLK is equal to 8Mz rather than 48MHz and all derived clocks (core, bus/flash) will be (at lest) 6x slower than before. In some cases OUTDIV1 and OUTDIV4 will now be changed to compensate (a little).
If you no longer need IRC48M it can be disabled now.
4. IRCS can be switched between 2MHz and 8MHz at any point whereby the resulting clocks (MCGIRCLK, Core, Bus/Flash) will switch 1/4 of the 8MHz speed and again some compensation may be performed.
When compensations are performed they can be performed 'after' the swith-over when the resulting speed is reduced.
When the resulting speed is increased compensations are usually performed 'before' the switch-over to ensure that the maximum output speed limites are respected during the process.
Whenever there is a status register showing the setting of a switch it should be polled to be the new value before continuing to ensure that the switch-overs have really completed before continuing.
Once the control registers and their control flags are known in the process the code is quite simple, as you have shown in your examples.
The most complicated thing is that when a new clock speed affects operating peripherals (such as UART) these will need to be set up with a new Baud rate value according to the new clock rate. This can lead to loss of data when switches are performed during operation and so such effects need to be carefully considered in the overall design. Specifially to the MCG_Lite that leaves IRC48M operating as clock source for peripherals in all modes, such peripheral speed are not effected by switch-overs to slower core, bus, flash speeds, which can simplify overall system design.
Regards
Mark