MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Multipurpose Clock Generator (MCG)

The MCUXpresso SDK provides a peripheral driver for the module of MCUXpresso SDK devices.

Function description

MCG driver provides these functions:

MCG frequency functions

MCG module provides clocks, such as MCGOUTCLK, MCGIRCLK, MCGFFCLK, MCGFLLCLK and MCGPLLCLK. The MCG driver provides functions to get the frequency of these clocks, such as CLOCK_GetOutClkFreq(), CLOCK_GetInternalRefClkFreq(), CLOCK_GetFixedFreqClkFreq(), CLOCK_GetFllFreq(), CLOCK_GetPll0Freq(), CLOCK_GetPll1Freq(), and CLOCK_GetExtPllFreq(). These functions get the clock frequency based on the current MCG registers.

MCG clock configuration

The MCG driver provides functions to configure the internal reference clock (MCGIRCLK), the external reference clock, and MCGPLLCLK.
The function CLOCK_SetInternalRefClkConfig() configures the MCGIRCLK, including the source and the driver. Do not change MCGIRCLK when the MCG mode is BLPI/FBI/PBI because the MCGIRCLK is used as a system clock in these modes and changing settings makes the system clock unstable.
The function CLOCK_SetExternalRefClkConfig() configures the external reference clock source (MCG_C7[OSCSEL]). Do not call this function when the MCG mode is BLPE/FBE/PBE/FEE/PEE because the external reference clock is used as a clock source in these modes. Changing the external reference clock source requires at least a 50 microseconds wait. The function CLOCK_SetExternalRefClkConfig() implements a for loop delay internally. The for loop delay assumes that the system clock is 96 MHz, which ensures at least 50 micro seconds delay. However, when the system clock is slow, the delay time may significantly increase. This for loop count can be optimized for better performance for specific cases.
The MCGPLLCLK is disabled in FBE/FEE/FBI/FEI modes by default. Applications can enable the MCGPLLCLK in these modes using the functions CLOCK_EnablePll0() and CLOCK_EnablePll1(). To enable the MCGPLLCLK, the PLL reference clock divider(PRDIV) and the PLL VCO divider(VDIV) must be set to a proper value. The function CLOCK_CalcPllDiv() helps to get the PRDIV/VDIV.

MCG clock lock monitor functions

The MCG module monitors the OSC and the PLL clock lock status. The MCG driver provides the functions to set the clock monitor mode, check the clock lost status, and clear the clock lost status.

OSC configuration

The MCG is needed together with the OSC module to enable the OSC clock. The function CLOCK_InitOsc0() CLOCK_InitOsc1 uses the MCG and OSC to initialize the OSC. The OSC should be configured based on the board design.

MCG auto-trim machine

The MCG provides an auto-trim machine to trim the MCG internal reference clock based on the external reference clock (BUS clock). During clock trimming, the MCG must not work in FEI/FBI/BLPI/PBI/PEI modes. The function CLOCK_TrimInternalRefClk() is used for the auto clock trimming.

MCG mode functions

The function CLOCK_GetMcgMode returns the current MCG mode. The MCG can only switch between the neighbouring modes. If the target mode is not current mode's neighbouring mode, the application must choose the proper switch path. For example, to switch to PEE mode from FEI mode, use FEI -> FBE -> PBE -> PEE.
For the MCG modes, the MCG driver provides three kinds of functions:
The first type of functions involve functions CLOCK_SetXxxMode, such as CLOCK_SetFeiMode(). These functions only set the MCG mode from neighbouring modes. If switching to the target mode directly from current mode is not possible, the functions return an error.

The second type of functions are the functions CLOCK_BootToXxxMode, such as CLOCK_BootToFeiMode(). These functions set the MCG to specific modes from reset mode. Because the source mode and target mode are specific, these functions choose the best switch path. The functions are also useful to set up the system clock during boot up.

The third type of functions is the CLOCK_SetMcgConfig(). This function chooses the right path to switch to the target mode. It is easy to use, but introduces a large code size.

Whenever the FLL settings change, there should be a 1 millisecond delay to ensure that the FLL is stable. The function CLOCK_SetMcgConfig() implements a for loop delay internally to ensure that the FLL is stable. The for loop delay assumes that the system clock is 96 MHz, which ensures at least 1 millisecond delay. However, when the system clock is slow, the delay time may increase significantly. The for loop count can be optimized for better performance according to a specific use case.

Typical use case

The function CLOCK_SetMcgConfig is used to switch between any modes. However, this heavy-light function introduces a large code size. This section shows how to use the mode function to implement a quick and light-weight switch between typical specific modes. Note that the step to enable the external clock is not included in the following steps. Enable the corresponding clock before using it as a clock source.

Switch between BLPI and FEI

Use case Steps Functions
BLPI -> FEI BLPI -> FBI CLOCK_InternalModeToFbiModeQuick(...)
FBI -> FEI CLOCK_SetFeiMode(...)
Configure MCGIRCLK if need CLOCK_SetInternalRefClkConfig(...)
FEI -> BLPI Configure MCGIRCLK if need CLOCK_SetInternalRefClkConfig(...)
FEI -> FBI CLOCK_SetFbiMode(...) with fllStableDelay=NULL
FBI -> BLPI CLOCK_SetLowPowerEnable(true)

Switch between BLPI and FEE

Use case Steps Functions
BLPI -> FEE BLPI -> FBI CLOCK_InternalModeToFbiModeQuick(...)
Change external clock source if need CLOCK_SetExternalRefClkConfig(...)
FBI -> FEE CLOCK_SetFeeMode(...)
FEE -> BLPI Configure MCGIRCLK if need CLOCK_SetInternalRefClkConfig(...)
FEE -> FBI CLOCK_SetFbiMode(...) with fllStableDelay=NULL
FBI -> BLPI CLOCK_SetLowPowerEnable(true)

Switch between BLPI and PEE

Use case Steps Functions
BLPI -> PEE BLPI -> FBI CLOCK_InternalModeToFbiModeQuick(...)
Change external clock source if need CLOCK_SetExternalRefClkConfig(...)
FBI -> FBE CLOCK_SetFbeMode(...) // fllStableDelay=NULL
FBE -> PBE CLOCK_SetPbeMode(...)
PBE -> PEE CLOCK_SetPeeMode(...)
PEE -> BLPI PEE -> FBE CLOCK_ExternalModeToFbeModeQuick(...)
Configure MCGIRCLK if need CLOCK_SetInternalRefClkConfig(...)
FBE -> FBI CLOCK_SetFbiMode(...) with fllStableDelay=NULL
FBI -> BLPI CLOCK_SetLowPowerEnable(true)

Switch between BLPE and PEE

This table applies when using the same external clock source (MCG_C7[OSCSEL]) in BLPE mode and PEE mode.

Use case Steps Functions
BLPE -> PEE BLPE -> PBE CLOCK_SetPbeMode(...)
PBE -> PEE CLOCK_SetPeeMode(...)
PEE -> BLPE PEE -> FBE CLOCK_ExternalModeToFbeModeQuick(...)
FBE -> BLPE CLOCK_SetLowPowerEnable(true)

If using different external clock sources (MCG_C7[OSCSEL]) in BLPE mode and PEE mode, call the CLOCK_SetExternalRefClkConfig() in FBI or FEI mode to change the external reference clock.

Use case Steps Functions
BLPE -> PEE BLPE -> FBE CLOCK_ExternalModeToFbeModeQuick(...)
FBE -> FBI CLOCK_SetFbiMode(...) with fllStableDelay=NULL
Change source CLOCK_SetExternalRefClkConfig(...)
FBI -> FBE CLOCK_SetFbeMode(...) with fllStableDelay=NULL
FBE -> PBE CLOCK_SetPbeMode(...)
PBE -> PEE CLOCK_SetPeeMode(...)
PEE -> BLPE PEE -> FBE CLOCK_ExternalModeToFbeModeQuick(...)
FBE -> FBI CLOCK_SetFbiMode(...) with fllStableDelay=NULL
Change source CLOCK_SetExternalRefClkConfig(...)
PBI -> FBE CLOCK_SetFbeMode(...) with fllStableDelay=NULL
FBE -> BLPE CLOCK_SetLowPowerEnable(true)

Switch between BLPE and FEE

This table applies when using the same external clock source (MCG_C7[OSCSEL]) in BLPE mode and FEE mode.

Use case Steps Functions
BLPE -> FEE BLPE -> FBE CLOCK_ExternalModeToFbeModeQuick(...)
FBE -> FEE CLOCK_SetFeeMode(...)
FEE -> BLPE PEE -> FBE CLOCK_SetPbeMode(...)
FBE -> BLPE CLOCK_SetLowPowerEnable(true)

If using different external clock sources (MCG_C7[OSCSEL]) in BLPE mode and FEE mode, call the CLOCK_SetExternalRefClkConfig() in FBI or FEI mode to change the external reference clock.

Use case Steps Functions
BLPE -> FEE BLPE -> FBE CLOCK_ExternalModeToFbeModeQuick(...)
FBE -> FBI CLOCK_SetFbiMode(...) with fllStableDelay=NULL
Change source CLOCK_SetExternalRefClkConfig(...)
FBI -> FEE CLOCK_SetFeeMode(...)
FEE -> BLPE FEE -> FBI CLOCK_SetFbiMode(...) with fllStableDelay=NULL
Change source CLOCK_SetExternalRefClkConfig(...)
PBI -> FBE CLOCK_SetFbeMode(...) with fllStableDelay=NULL
FBE -> BLPE CLOCK_SetLowPowerEnable(true)

Switch between BLPI and PEI

Use case Steps Functions
BLPI -> PEI BLPI -> PBI CLOCK_SetPbiMode(...)
PBI -> PEI CLOCK_SetPeiMode(...)
Configure MCGIRCLK if need CLOCK_SetInternalRefClkConfig(...)
PEI -> BLPI Configure MCGIRCLK if need CLOCK_SetInternalRefClkConfig
PEI -> FBI CLOCK_InternalModeToFbiModeQuick(...)
FBI -> BLPI CLOCK_SetLowPowerEnable(true)

Code Configuration Option

MCG_USER_CONFIG_FLL_STABLE_DELAY_EN

When switching to use FLL with function CLOCK_SetFeiMode() and CLOCK_SetFeeMode(), there is an internal function CLOCK_FllStableDelay(). It is used to delay a few ms so that to wait the FLL to be stable enough. By default, it is implemented in driver code like:

#ifndef MCG_USER_CONFIG_FLL_STABLE_DELAY_EN
void CLOCK_FllStableDelay(void)
{
/*
Should wait at least 1ms. Because in these modes, the core clock is 100MHz
at most, so this function could obtain the 1ms delay.
*/
volatile uint32_t i = 30000U;
while (i--)
{
__NOP();
}
}
#endif /* MCG_USER_CONFIG_FLL_STABLE_DELAY_EN */

Once user is willing to create his own delay funcion, just assert the macro MCG_USER_CONFIG_FLL_STABLE_DELAY_EN, and then define function CLOCK_FllStableDelay in the application code.