Kinetis SDK v.2.0 API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Clock Driver

Overview

The KSDK provides APIs for Kinetis devices clock operation.

Get frequency

A centralized function CLOCK_GetFreq gets different clock type frequencies by passing a clock name. For example, pass a kCLOCK_CoreSysClk to get the core clock and pass a kCLOCK_BusClk to get the bus clock. Additionally, there are separate functions to get frequency, for example, use CLOCK_GetCoreSysClkFreq to get the core clock frequency and CLOCK_GetBusClkFreq to get the bus clock frequency. Using these functions reduces the image size.

External clock frequency

The external clocks EXTAL0/EXTAL1/EXTAL32 are decided by the board level design. The Clock driver uses variables g_xtal0Freq/g_xtal1Freq/g_xtal32Freq to save clock frequencies. Likewise, the APIs CLOCK_SetXtal0Freq, CLOCK_SetXtal1Freq and CLOCK_SetXtal32Freq are used to set these variables.
The upper layer must set these values correctly, for example, after OSC0(SYSOSC) is initialized using CLOCK_InitOsc0 or CLOCK_InitSysOsc, the upper layer should call the CLOCK_SetXtal0Freq. Otherwise, the clock frequency get functions may not get valid values. This is useful for multicore platforms where only one core calls CLOCK_InitOsc0 to initialize OSC0 and other cores call CLOCK_SetXtal0Freq.

Modules

 Multipurpose Clock Generator Lite (MCGLITE)
 

Files

file  fsl_clock.h
 

Data Structures

struct  sim_clock_config_t
 SIM configuration structure for clock setting. More...
 
struct  oscer_config_t
 The OSC configuration for OSCERCLK. More...
 
struct  osc_config_t
 OSC Initialization Configuration Structure. More...
 
struct  mcglite_config_t
 MCG_Lite configure structure for mode change. More...
 

Macros

#define RTC_CLOCKS
 Clock ip name array for RTC. More...
 
#define LPUART_CLOCKS
 Clock ip name array for LPUART. More...
 
#define SPI_CLOCKS
 Clock ip name array for SPI. More...
 
#define LPTMR_CLOCKS
 Clock ip name array for LPTMR. More...
 
#define ADC16_CLOCKS
 Clock ip name array for ADC16. More...
 
#define TPM_CLOCKS
 Clock ip name array for TPM. More...
 
#define VREF_CLOCKS
 Clock ip name array for VREF. More...
 
#define I2C_CLOCKS
 Clock ip name array for I2C. More...
 
#define PORT_CLOCKS
 Clock ip name array for PORT. More...
 
#define FTF_CLOCKS
 Clock ip name array for FTF. More...
 
#define CMP_CLOCKS
 Clock ip name array for CMP. More...
 
#define LPO_CLK_FREQ   1000U
 LPO clock frequency.
 
#define SYS_CLK   kCLOCK_CoreSysClk
 Peripherals clock source definition. More...
 

Enumerations

enum  clock_name_t {
  kCLOCK_CoreSysClk,
  kCLOCK_PlatClk,
  kCLOCK_BusClk,
  kCLOCK_FlashClk,
  kCLOCK_Er32kClk,
  kCLOCK_Osc0ErClk,
  kCLOCK_McgFixedFreqClk,
  kCLOCK_McgInternalRefClk,
  kCLOCK_McgFllClk,
  kCLOCK_McgPeriphClk,
  kCLOCK_McgIrc48MClk,
  kCLOCK_LpoClk
}
 Clock name used to get clock frequency. 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
}
 The 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...
 

Functions

static void CLOCK_EnableClock (clock_ip_name_t name)
 Enable the clock for specific IP. More...
 
static void CLOCK_DisableClock (clock_ip_name_t name)
 Disable the clock for specific IP. More...
 
static void CLOCK_SetEr32kClock (uint32_t src)
 Set ERCLK32K source. More...
 
static void CLOCK_SetLpuart0Clock (uint32_t src)
 Set LPUART clock source. More...
 
static void CLOCK_SetTpmClock (uint32_t src)
 Set TPM clock source. More...
 
static void CLOCK_SetClkOutClock (uint32_t src)
 Set CLKOUT source. More...
 
static void CLOCK_SetRtcClkOutClock (uint32_t src)
 Set RTC_CLKOUT source. More...
 
static void CLOCK_SetOutDiv (uint32_t outdiv1, uint32_t outdiv4)
 System clock divider. More...
 
uint32_t CLOCK_GetFreq (clock_name_t clockName)
 Gets the clock frequency for a specific clock name. More...
 
uint32_t CLOCK_GetCoreSysClkFreq (void)
 Get the core clock or system clock frequency. More...
 
uint32_t CLOCK_GetPlatClkFreq (void)
 Get the platform clock frequency. More...
 
uint32_t CLOCK_GetBusClkFreq (void)
 Get the bus clock frequency. More...
 
uint32_t CLOCK_GetFlashClkFreq (void)
 Get the flash clock frequency. More...
 
uint32_t CLOCK_GetEr32kClkFreq (void)
 Get the external reference 32K clock frequency (ERCLK32K). More...
 
uint32_t CLOCK_GetOsc0ErClkFreq (void)
 Get the OSC0 external reference clock frequency (OSC0ERCLK). More...
 
void CLOCK_SetSimConfig (sim_clock_config_t const *config)
 Set the clock configure in SIM module. More...
 
static void CLOCK_SetSimSafeDivs (void)
 Set the system clock dividers in SIM to safe value. More...
 

Variables

uint32_t g_xtal0Freq
 External XTAL0 (OSC0) clock frequency. More...
 
uint32_t g_xtal32Freq
 The external XTAL32/EXTAL32/RTC_CLKIN clock frequency. More...
 

Driver version

#define FSL_CLOCK_DRIVER_VERSION   (MAKE_VERSION(2, 1, 1))
 CLOCK driver version 2.1.1. More...
 

MCG_Lite clock frequency

uint32_t CLOCK_GetOutClkFreq (void)
 Gets the MCG_Lite output clock (MCGOUTCLK) frequency. More...
 
uint32_t CLOCK_GetInternalRefClkFreq (void)
 Gets the MCG internal reference clock (MCGIRCLK) frequency. More...
 
uint32_t CLOCK_GetPeriphClkFreq (void)
 Gets the current MCGPCLK frequency. More...
 

MCG_Lite mode.

mcglite_mode_t CLOCK_GetMode (void)
 Gets the current MCG_Lite mode. More...
 
status_t CLOCK_SetMcgliteConfig (mcglite_config_t const *targetConfig)
 Sets the MCG_Lite configuration. More...
 

OSC configuration

static void OSC_SetExtRefClkConfig (OSC_Type *base, oscer_config_t const *config)
 Configures the OSC external reference clock (OSCERCLK). More...
 
static void OSC_SetCapLoad (OSC_Type *base, uint8_t capLoad)
 Sets the capacitor load configuration for the oscillator. More...
 
void CLOCK_InitOsc0 (osc_config_t const *config)
 Initializes the OSC0. More...
 
void CLOCK_DeinitOsc0 (void)
 Deinitializes the OSC0. More...
 

External clock frequency

static void CLOCK_SetXtal0Freq (uint32_t freq)
 Sets the XTAL0 frequency based on board settings. More...
 
static void CLOCK_SetXtal32Freq (uint32_t freq)
 Sets the XTAL32/RTC_CLKIN frequency based on board settings. More...
 

Data Structure Documentation

struct sim_clock_config_t

Data Fields

uint8_t er32kSrc
 ERCLK32K source selection. More...
 
uint32_t clkdiv1
 SIM_CLKDIV1. More...
 

Field Documentation

uint8_t sim_clock_config_t::er32kSrc
uint32_t sim_clock_config_t::clkdiv1
struct oscer_config_t

Data Fields

uint8_t enableMode
 OSCERCLK enable mode. More...
 

Field Documentation

uint8_t oscer_config_t::enableMode

OR'ed value of _oscer_enable_mode.

struct osc_config_t

Defines the configuration data structure to initialize the OSC. When porting to a new board, set the following members according to the board settings:

  1. freq: The external frequency.
  2. workMode: The OSC module mode.

Data Fields

uint32_t freq
 External clock frequency. More...
 
uint8_t capLoad
 Capacitor load setting. More...
 
osc_mode_t workMode
 OSC work mode setting. More...
 
oscer_config_t oscerConfig
 Configuration for OSCERCLK. More...
 

Field Documentation

uint32_t osc_config_t::freq
uint8_t osc_config_t::capLoad
osc_mode_t osc_config_t::workMode
oscer_config_t osc_config_t::oscerConfig
struct mcglite_config_t

Data Fields

mcglite_clkout_src_t outSrc
 MCGOUT clock select. More...
 
uint8_t irclkEnableMode
 MCGIRCLK enable mode, OR'ed value of _mcglite_irclk_enable_mode. More...
 
mcglite_lirc_mode_t ircs
 MCG_C2[IRCS]. More...
 
mcglite_lirc_div_t fcrdiv
 MCG_SC[FCRDIV]. More...
 
mcglite_lirc_div_t lircDiv2
 MCG_MC[LIRC_DIV2]. More...
 
bool hircEnableInNotHircMode
 HIRC enable when not in HIRC mode. More...
 

Field Documentation

mcglite_clkout_src_t mcglite_config_t::outSrc
uint8_t mcglite_config_t::irclkEnableMode
mcglite_lirc_mode_t mcglite_config_t::ircs
mcglite_lirc_div_t mcglite_config_t::fcrdiv
mcglite_lirc_div_t mcglite_config_t::lircDiv2
bool mcglite_config_t::hircEnableInNotHircMode

Macro Definition Documentation

#define FSL_CLOCK_DRIVER_VERSION   (MAKE_VERSION(2, 1, 1))
#define RTC_CLOCKS
Value:
{ \
kCLOCK_Rtc0 \
}
#define LPUART_CLOCKS
Value:
{ \
kCLOCK_Lpuart0 \
}
#define SPI_CLOCKS
Value:
{ \
kCLOCK_Spi0 \
}
#define LPTMR_CLOCKS
Value:
{ \
kCLOCK_Lptmr0 \
}
#define ADC16_CLOCKS
Value:
{ \
kCLOCK_Adc0 \
}
#define TPM_CLOCKS
Value:
{ \
kCLOCK_Tpm0, kCLOCK_Tpm1 \
}
#define VREF_CLOCKS
Value:
{ \
kCLOCK_Vref0 \
}
#define I2C_CLOCKS
Value:
{ \
kCLOCK_I2c0 \
}
#define PORT_CLOCKS
Value:
{ \
kCLOCK_PortA, kCLOCK_PortB \
}
#define FTF_CLOCKS
Value:
{ \
kCLOCK_Ftf0 \
}
#define CMP_CLOCKS
Value:
{ \
kCLOCK_Cmp0 \
}
#define SYS_CLK   kCLOCK_CoreSysClk

Enumeration Type Documentation

Enumerator
kCLOCK_CoreSysClk 

Core/system clock.

kCLOCK_PlatClk 

Platform clock.

kCLOCK_BusClk 

Bus clock.

kCLOCK_FlashClk 

Flash clock.

kCLOCK_Er32kClk 

External reference 32K clock (ERCLK32K)

kCLOCK_Osc0ErClk 

OSC0 external reference clock (OSC0ERCLK)

kCLOCK_McgFixedFreqClk 

MCG fixed frequency clock (MCGFFCLK)

kCLOCK_McgInternalRefClk 

MCG internal reference clock (MCGIRCLK)

kCLOCK_McgFllClk 

MCGFLLCLK.

kCLOCK_McgPeriphClk 

MCG peripheral clock (MCGPCLK)

kCLOCK_McgIrc48MClk 

MCG IRC48M clock.

kCLOCK_LpoClk 

LPO clock.

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.

enum osc_mode_t
Enumerator
kOSC_ModeExt 

Use external clock.

kOSC_ModeOscLowPower 

Oscillator low power.

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) 2 MHz clock selected.

kMCGLITE_Lirc8M 

Slow internal reference(LIRC) 8 MHz 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.

Function Documentation

static void CLOCK_EnableClock ( clock_ip_name_t  name)
inlinestatic
Parameters
nameWhich clock to enable, see clock_ip_name_t.
static void CLOCK_DisableClock ( clock_ip_name_t  name)
inlinestatic
Parameters
nameWhich clock to disable, see clock_ip_name_t.
static void CLOCK_SetEr32kClock ( uint32_t  src)
inlinestatic
Parameters
srcThe value to set ERCLK32K clock source.
static void CLOCK_SetLpuart0Clock ( uint32_t  src)
inlinestatic
Parameters
srcThe value to set LPUART clock source.
static void CLOCK_SetTpmClock ( uint32_t  src)
inlinestatic
Parameters
srcThe value to set TPM clock source.
static void CLOCK_SetClkOutClock ( uint32_t  src)
inlinestatic
Parameters
srcThe value to set CLKOUT source.
static void CLOCK_SetRtcClkOutClock ( uint32_t  src)
inlinestatic
Parameters
srcThe 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
outdiv1Clock 1 output divider value.
outdiv4Clock 4 output divider value.
uint32_t CLOCK_GetFreq ( clock_name_t  clockName)

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
clockNameClock 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.
void CLOCK_SetSimConfig ( sim_clock_config_t const *  config)

This function sets system layer clock settings in SIM module.

Parameters
configPointer 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
configPointer to the configure structure.
uint32_t CLOCK_GetOutClkFreq ( void  )

This function gets the MCG_Lite output clock frequency in 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 in Hz based on the current MCG register value.

Returns
The frequency of MCGIRCLK.
uint32_t CLOCK_GetPeriphClkFreq ( void  )

This function gets the MCGPCLK frequency in Hz based on the current MCG_Lite register settings.

Returns
The frequency of MCGPCLK.
mcglite_mode_t CLOCK_GetMode ( void  )

This function checks the MCG_Lite registers and determines the current MCG_Lite mode.

Returns
The current MCG_Lite mode or error code.
status_t CLOCK_SetMcgliteConfig ( mcglite_config_t const *  targetConfig)

This function configures the MCG_Lite, includes the output clock source, MCGIRCLK settings, HIRC settings, and so on. See mcglite_config_t for details.

Parameters
targetConfigPointer 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). This is an example to enable the OSCERCLK in normal mode and stop mode, and set the output divider to 1.

{
.erclkDiv = 1U,
};
OSC_SetExtRefClkConfig(OSC, &config);
Parameters
baseOSC peripheral address.
configPointer to the configuration structure.
static void OSC_SetCapLoad ( OSC_Type *  base,
uint8_t  capLoad 
)
inlinestatic

This function sets the specified capacitor configuration for the oscillator. This should be done in the early system level initialization function call based on the system configuration.

Parameters
baseOSC peripheral address.
capLoadOR'ed value for the capacitor load option.See _osc_cap_load.

Example:

// To enable only 2 pF and 8 pF capacitor load, please use like this.
void CLOCK_InitOsc0 ( osc_config_t const *  config)

This function initializes the OSC0 according to the board configuration.

Parameters
configPointer to the OSC0 configuration structure.
void CLOCK_DeinitOsc0 ( void  )

This function deinitializes the OSC0.

static void CLOCK_SetXtal0Freq ( uint32_t  freq)
inlinestatic
Parameters
freqThe XTAL0/EXTAL0 input clock frequency in Hz.
static void CLOCK_SetXtal32Freq ( uint32_t  freq)
inlinestatic
Parameters
freqThe XTAL32/EXTAL32/RTC_CLKIN input clock frequency in Hz.

Variable Documentation

uint32_t g_xtal0Freq

The XTAL0/EXTAL0 (OSC0) clock frequency in Hz. When the clock is set up, use the function CLOCK_SetXtal0Freq to set the value in the clock driver. For example, if XTAL0 is 8 MHz:

* CLOCK_InitOsc0(...); // Set up the OSC0
* CLOCK_SetXtal0Freq(80000000); // Set the XTAL0 value to clock driver.
*

This is important for the multicore platforms where one core needs to set up the OSC0 using the CLOCK_InitOsc0. All other cores need to call the CLOCK_SetXtal0Freq to get a valid clock frequency.

uint32_t g_xtal32Freq

The XTAL32/EXTAL32/RTC_CLKIN clock frequency in Hz. When the clock is set up, use the function CLOCK_SetXtal32Freq to set the value in the clock driver.

This is important for the multicore platforms where one core needs to set up the clock. All other cores need to call the CLOCK_SetXtal32Freq to get a valid clock frequency.