The KSDK provides a peripheral driver for the MCG_Lite module of Kinetis devices.
Function description
The MCG_Lite driver provides three kinds of APIs:
- APIs to get the MCG_Lite frequency.
- APIs for MCG_Lite mode.
- APIs for OSC setup.
MCG_Lite clock frequency
The CLOCK_GetOutClkFreq(), CLOCK_GetInternalRefClkFreq() and CLOCK_GetPeriphClkFreq() functions are used to get the frequency of MCGOUTCLK, MCGIRCLK, and MCGPCLK based on the current hardware setting.
MCG_Lite mode
The function CLOCK_GetMode() gets the current MCG_Lite mode.
The function CLOCK_SetMcgliteConfig() sets the MCG_Lite to a desired configuration. The MCG_Lite can't switch between the LIRC2M and LIRC8M. Instead, the function switches to the HIRC mode first and then switches to the target mode.
OSC configuration
To enable the OSC clock, the MCG_Lite is needed together with the OSC module. The function CLOCK_InitOsc0() uses the MCG_Lite and the OSC to initialize the OSC. The OSC should be configured based on the board design.
|
enum | clock_name_t {
kCLOCK_CoreSysClk,
kCLOCK_PlatClk,
kCLOCK_BusClk,
kCLOCK_FlexBusClk,
kCLOCK_FlashClk,
kCLOCK_FastPeriphClk,
kCLOCK_PllFllSelClk,
kCLOCK_Er32kClk,
kCLOCK_Osc0ErClk,
kCLOCK_Osc1ErClk,
kCLOCK_Osc0ErClkUndiv,
kCLOCK_McgFixedFreqClk,
kCLOCK_McgInternalRefClk,
kCLOCK_McgFllClk,
kCLOCK_McgPll0Clk,
kCLOCK_McgPll1Clk,
kCLOCK_McgExtPllClk,
kCLOCK_McgPeriphClk,
kCLOCK_McgIrc48MClk,
kCLOCK_LpoClk
} |
| Clock name used to get clock frequency. More...
|
|
enum | clock_usb_src_t {
kCLOCK_UsbSrcIrc48M = SIM_SOPT2_USBSRC(1U),
kCLOCK_UsbSrcExt = SIM_SOPT2_USBSRC(0U)
} |
| USB clock source definition. More...
|
|
enum | clock_ip_name_t |
| Clock gate name used for CLOCK_EnableClock/CLOCK_DisableClock. More...
|
|
enum | _osc_cap_load {
kOSC_Cap2P = OSC_CR_SC2P_MASK,
kOSC_Cap4P = OSC_CR_SC4P_MASK,
kOSC_Cap8P = OSC_CR_SC8P_MASK,
kOSC_Cap16P = OSC_CR_SC16P_MASK
} |
| Oscillator capacitor load setting. More...
|
|
enum | _oscer_enable_mode {
kOSC_ErClkEnable = OSC_CR_ERCLKEN_MASK,
kOSC_ErClkEnableInStop = OSC_CR_EREFSTEN_MASK
} |
| OSCERCLK enable mode. More...
|
|
enum | osc_mode_t {
kOSC_ModeExt = 0U,
kOSC_ModeOscLowPower = MCG_C2_EREFS0_MASK,
kOSC_ModeOscHighGain = MCG_C2_EREFS0_MASK | MCG_C2_HGO0_MASK
} |
| OSC work mode. More...
|
|
enum | mcglite_clkout_src_t {
kMCGLITE_ClkSrcHirc,
kMCGLITE_ClkSrcLirc,
kMCGLITE_ClkSrcExt
} |
| MCG_Lite clock source selection. More...
|
|
enum | mcglite_lirc_mode_t {
kMCGLITE_Lirc2M,
kMCGLITE_Lirc8M
} |
| MCG_Lite LIRC select. More...
|
|
enum | mcglite_lirc_div_t {
kMCGLITE_LircDivBy1 = 0U,
kMCGLITE_LircDivBy2,
kMCGLITE_LircDivBy4,
kMCGLITE_LircDivBy8,
kMCGLITE_LircDivBy16,
kMCGLITE_LircDivBy32,
kMCGLITE_LircDivBy64,
kMCGLITE_LircDivBy128
} |
| MCG_Lite divider factor selection for clock source. More...
|
|
enum | mcglite_mode_t {
kMCGLITE_ModeHirc48M,
kMCGLITE_ModeLirc8M,
kMCGLITE_ModeLirc2M,
kMCGLITE_ModeExt,
kMCGLITE_ModeError
} |
| MCG_Lite clock mode definitions. More...
|
|
enum | _mcglite_irclk_enable_mode {
kMCGLITE_IrclkEnable = MCG_C1_IRCLKEN_MASK,
kMCGLITE_IrclkEnableInStop = MCG_C1_IREFSTEN_MASK
} |
| MCG internal reference clock (MCGIRCLK) enable mode definition. More...
|
|
struct sim_clock_config_t |
uint8_t sim_clock_config_t::er32kSrc |
uint32_t sim_clock_config_t::clkdiv1 |
uint8_t oscer_config_t::enableMode |
Defines the configuration data structure to initialize the OSC. When porting to a new board, set the following members according to board settings:
- freq: The external frequency.
- workMode: The OSC module mode.
uint32_t osc_config_t::freq |
uint8_t osc_config_t::capLoad |
uint8_t mcglite_config_t::irclkEnableMode |
bool mcglite_config_t::hircEnableInNotHircMode |
Value:{ \
kCLOCK_Spi0, kCLOCK_Spi1 \
}
Value:{ \
kCLOCK_PortA, kCLOCK_PortB, kCLOCK_PortC, kCLOCK_PortD, kCLOCK_PortE \
}
Value:{ \
kCLOCK_Lpuart0, kCLOCK_Lpuart1 \
}
Value:{ \
kCLOCK_IpInvalid, kCLOCK_IpInvalid, kCLOCK_Uart2 \
}
Value:{ \
kCLOCK_Tpm0, kCLOCK_Tpm1, kCLOCK_Tpm2 \
}
Value:{ \
kCLOCK_I2c0, kCLOCK_I2c1 \
}
Value:{ \
kCLOCK_Cmp0, kCLOCK_Cmp1, kCLOCK_Cmp2 \
}
Enumerator |
---|
kCLOCK_CoreSysClk |
Core/system clock.
|
kCLOCK_PlatClk |
Platform clock.
|
kCLOCK_BusClk |
Bus clock.
|
kCLOCK_FlexBusClk |
FlexBus clock.
|
kCLOCK_FlashClk |
Flash clock.
|
kCLOCK_FastPeriphClk |
Fast peripheral clock.
|
kCLOCK_PllFllSelClk |
The clock after SIM[PLLFLLSEL].
|
kCLOCK_Er32kClk |
External reference 32K clock (ERCLK32K)
|
kCLOCK_Osc0ErClk |
OSC0 external reference clock (OSC0ERCLK)
|
kCLOCK_Osc1ErClk |
OSC1 external reference clock (OSC1ERCLK)
|
kCLOCK_Osc0ErClkUndiv |
OSC0 external reference undivided clock(OSC0ERCLK_UNDIV).
|
kCLOCK_McgFixedFreqClk |
MCG fixed frequency clock (MCGFFCLK)
|
kCLOCK_McgInternalRefClk |
MCG internal reference clock (MCGIRCLK)
|
kCLOCK_McgFllClk |
MCGFLLCLK.
|
kCLOCK_McgPll0Clk |
MCGPLL0CLK.
|
kCLOCK_McgPll1Clk |
MCGPLL1CLK.
|
kCLOCK_McgExtPllClk |
EXT_PLLCLK.
|
kCLOCK_McgPeriphClk |
MCG peripheral clock (MCGPCLK)
|
kCLOCK_McgIrc48MClk |
MCG IRC48M clock.
|
kCLOCK_LpoClk |
LPO clock.
|
Enumerator |
---|
kCLOCK_UsbSrcIrc48M |
Use IRC48M.
|
kCLOCK_UsbSrcExt |
Use USB_CLKIN.
|
Enumerator |
---|
kOSC_Cap2P |
2 pF capacitor load
|
kOSC_Cap4P |
4 pF capacitor load
|
kOSC_Cap8P |
8 pF capacitor load
|
kOSC_Cap16P |
16 pF capacitor load
|
Enumerator |
---|
kOSC_ErClkEnable |
Enable.
|
kOSC_ErClkEnableInStop |
Enable in stop mode.
|
Enumerator |
---|
kOSC_ModeExt |
Use external clock.
|
kOSC_ModeOscLowPower |
Oscillator low power.
|
kOSC_ModeOscHighGain |
Oscillator high gain.
|
Enumerator |
---|
kMCGLITE_ClkSrcHirc |
MCGOUTCLK source is HIRC.
|
kMCGLITE_ClkSrcLirc |
MCGOUTCLK source is LIRC.
|
kMCGLITE_ClkSrcExt |
MCGOUTCLK source is external clock source.
|
Enumerator |
---|
kMCGLITE_Lirc2M |
Slow internal reference(LIRC) 2MHz clock selected.
|
kMCGLITE_Lirc8M |
Slow internal reference(LIRC) 8MHz clock selected.
|
Enumerator |
---|
kMCGLITE_LircDivBy1 |
Divider is 1.
|
kMCGLITE_LircDivBy2 |
Divider is 2.
|
kMCGLITE_LircDivBy4 |
Divider is 4.
|
kMCGLITE_LircDivBy8 |
Divider is 8.
|
kMCGLITE_LircDivBy16 |
Divider is 16.
|
kMCGLITE_LircDivBy32 |
Divider is 32.
|
kMCGLITE_LircDivBy64 |
Divider is 64.
|
kMCGLITE_LircDivBy128 |
Divider is 128.
|
Enumerator |
---|
kMCGLITE_ModeHirc48M |
Clock mode is HIRC 48 M.
|
kMCGLITE_ModeLirc8M |
Clock mode is LIRC 8 M.
|
kMCGLITE_ModeLirc2M |
Clock mode is LIRC 2 M.
|
kMCGLITE_ModeExt |
Clock mode is EXT.
|
kMCGLITE_ModeError |
Unknown mode.
|
Enumerator |
---|
kMCGLITE_IrclkEnable |
MCGIRCLK enable.
|
kMCGLITE_IrclkEnableInStop |
MCGIRCLK enable in stop mode.
|
static void CLOCK_SetXtal0Freq |
( |
uint32_t |
freq | ) |
|
|
inlinestatic |
- Parameters
-
freq | The XTAL0/EXTAL0 input clock frequency in Hz. |
static void CLOCK_SetXtal32Freq |
( |
uint32_t |
freq | ) |
|
|
inlinestatic |
- Parameters
-
freq | The XTAL32/EXTAL32/RTC_CLKIN input clock frequency in Hz. |
static void CLOCK_SetEr32kClock |
( |
uint32_t |
src | ) |
|
|
inlinestatic |
- Parameters
-
src | The value to set ERCLK32K clock source. |
static void CLOCK_SetLpuart0Clock |
( |
uint32_t |
src | ) |
|
|
inlinestatic |
- Parameters
-
src | The value to set LPUART0 clock source. |
static void CLOCK_SetLpuart1Clock |
( |
uint32_t |
src | ) |
|
|
inlinestatic |
- Parameters
-
src | The value to set LPUART1 clock source. |
static void CLOCK_SetTpmClock |
( |
uint32_t |
src | ) |
|
|
inlinestatic |
- Parameters
-
src | The value to set TPM clock source. |
static void CLOCK_SetFlexio0Clock |
( |
uint32_t |
src | ) |
|
|
inlinestatic |
- Parameters
-
src | The value to set FLEXIO clock source. |
- Parameters
-
src | USB FS clock source. |
freq | The frequency specified by src. |
- Return values
-
true | The clock is set successfully. |
false | The clock source is invalid to get proper USB FS clock. |
static void CLOCK_DisableUsbfs0Clock |
( |
void |
| ) |
|
|
inlinestatic |
static void CLOCK_SetClkOutClock |
( |
uint32_t |
src | ) |
|
|
inlinestatic |
- Parameters
-
src | The value to set CLKOUT source. |
static void CLOCK_SetRtcClkOutClock |
( |
uint32_t |
src | ) |
|
|
inlinestatic |
- Parameters
-
src | The value to set RTC_CLKOUT source. |
static void CLOCK_SetOutDiv |
( |
uint32_t |
outdiv1, |
|
|
uint32_t |
outdiv4 |
|
) |
| |
|
inlinestatic |
Set the SIM_CLKDIV1[OUTDIV1], SIM_CLKDIV1[OUTDIV4].
- Parameters
-
outdiv1 | Clock 1 output divider value. |
outdiv4 | Clock 4 output divider value. |
This function checks the current clock configurations and then calculates the clock frequency for a specific clock name defined in clock_name_t. The MCG must be properly configured before using this function.
- Parameters
-
clockName | Clock names defined in clock_name_t |
- Returns
- Clock frequency value in Hertz
uint32_t CLOCK_GetCoreSysClkFreq |
( |
void |
| ) |
|
- Returns
- Clock frequency in Hz.
uint32_t CLOCK_GetPlatClkFreq |
( |
void |
| ) |
|
- Returns
- Clock frequency in Hz.
uint32_t CLOCK_GetBusClkFreq |
( |
void |
| ) |
|
- Returns
- Clock frequency in Hz.
uint32_t CLOCK_GetFlashClkFreq |
( |
void |
| ) |
|
- Returns
- Clock frequency in Hz.
uint32_t CLOCK_GetEr32kClkFreq |
( |
void |
| ) |
|
- Returns
- Clock frequency in Hz.
uint32_t CLOCK_GetOsc0ErClkFreq |
( |
void |
| ) |
|
- Returns
- Clock frequency in Hz.
This function sets system layer clock settings in SIM module.
- Parameters
-
config | Pointer to the configure structure. |
static void CLOCK_SetSimSafeDivs |
( |
void |
| ) |
|
|
inlinestatic |
The system level clocks (core clock, bus clock, flexbus clock and flash clock) must be in allowed ranges. During MCG clock mode switch, the MCG output clock changes then the system level clocks may be out of range. This function could be used before MCG mode change, to make sure system level clocks are in allowed range.
- Parameters
-
config | Pointer to the configure structure. |
uint32_t CLOCK_GetOutClkFreq |
( |
void |
| ) |
|
This function gets the MCG_Lite output clock frequency (Hz) based on the current MCG_Lite register value.
- Returns
- The frequency of MCGOUTCLK.
uint32_t CLOCK_GetInternalRefClkFreq |
( |
void |
| ) |
|
This function gets the MCG_Lite internal reference clock frequency (Hz) based on the current MCG register value.
- Returns
- The frequency of MCGIRCLK.
uint32_t CLOCK_GetPeriphClkFreq |
( |
void |
| ) |
|
This function gets the MCGPCLK frequency (Hertz) based on the current MCG_Lite register settings.
- Returns
- The frequency of MCGPCLK.
This function checks the MCG_Lite registers and determines the current MCG_Lite mode.
- Returns
- Current MCG_Lite mode or error code.
This function configures the MCG_Lite, include output clock source, MCGIRCLK setting, HIRC setting and so on, see mcglite_config_t for details.
- Parameters
-
targetConfig | Pointer to the target MCG_Lite mode configuration structure. |
- Returns
- Error code.
static void OSC_SetExtRefClkConfig |
( |
OSC_Type * |
base, |
|
|
oscer_config_t const * |
config |
|
) |
| |
|
inlinestatic |
This function configures the OSC external reference clock (OSCERCLK). For example, to enable the OSCERCLK in normal mode and stop mode, and also set the output divider to 1, as follows:
- Parameters
-
base | OSC peripheral address. |
config | Pointer to the configuration structure. |
static void OSC_SetCapLoad |
( |
OSC_Type * |
base, |
|
|
uint8_t |
capLoad |
|
) |
| |
|
inlinestatic |
This function sets the specified capacitors configuration for the oscillator. This should be done in the early system level initialization function call based on the system configuration.
- Parameters
-
base | OSC peripheral address. |
capLoad | OR'ed value for the capacitor load option, see _osc_cap_load. |
Example:
This function initializes the OSC0 according to the board configuration.
- Parameters
-
config | Pointer to the OSC0 configuration structure. |
void CLOCK_DeinitOsc0 |
( |
void |
| ) |
|
This function deinitializes the OSC0.
The XTAL0/EXTAL0 (OSC0) clock frequency in Hz, when the clock is setup, use the function CLOCK_SetXtal0Freq to set the value in to clock driver. For example, if XTAL0 is 8MHz,
This is important for the multicore platforms, only one core needs to setup OSC0 using CLOCK_InitOsc0, all other cores need to call CLOCK_SetXtal0Freq to get valid clock frequency.
The XTAL32/EXTAL32/RTC_CLKIN clock frequency in Hz, when the clock is setup, use the function CLOCK_SetXtal32Freq to set the value in to clock driver.
This is important for the multicore platforms, only one core needs to setup the clock, all other cores need to call CLOCK_SetXtal32Freq to get valid clock frequency.