ISSDK  1.7
IoT Sensing Software Development Kit
clock_config.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * How to setup clock using clock driver functions:
37  *
38  * 1. CLOCK_SetSimSafeDivs, to make sure core clock, bus clock, flexbus clock
39  * and flash clock are in allowed range during clock mode switch.
40  *
41  * 2. Call CLOCK_Osc0Init to setup OSC clock, if it is used in target mode.
42  *
43  * 3. Set MCG configuration, MCG includes three parts: FLL clock, PLL clock and
44  * internal reference clock(MCGIRCLK). Follow the steps to setup:
45  *
46  * 1). Call CLOCK_BootToXxxMode to set MCG to target mode.
47  *
48  * 2). If target mode is FBI/BLPI/PBI mode, the MCGIRCLK has been configured
49  * correctly. For other modes, need to call CLOCK_SetInternalRefClkConfig
50  * explicitly to setup MCGIRCLK.
51  *
52  * 3). Don't need to configure FLL explicitly, because if target mode is FLL
53  * mode, then FLL has been configured by the function CLOCK_BootToXxxMode,
54  * if the target mode is not FLL mode, the FLL is disabled.
55  *
56  * 4). If target mode is PEE/PBE/PEI/PBI mode, then the related PLL has been
57  * setup by CLOCK_BootToXxxMode. In FBE/FBI/FEE/FBE mode, the PLL could
58  * be enabled independently, call CLOCK_EnablePll0 explicitly in this case.
59  *
60  * 4. Call CLOCK_SetSimConfig to set the clock configuration in SIM.
61  */
62 
63 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
64 !!ClocksProfile
65 product: Clocks v1.0
66 processor: MK64FN1M0xxx12
67 package_id: MK64FN1M0VLL12
68 mcu_data: ksdk2_0
69 processor_version: 1.0.1
70 board: FRDM-K64F
71  * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
72 
73 #include "fsl_smc.h"
74 #include "clock_config.h"
75 
76 /*******************************************************************************
77  * Definitions
78  ******************************************************************************/
79 #define MCG_PLL_DISABLE 0U /*!< MCGPLLCLK disabled */
80 #define OSC_CAP0P 0U /*!< Oscillator 0pF capacitor load */
81 #define OSC_ER_CLK_DISABLE 0U /*!< Disable external reference clock */
82 #define SIM_OSC32KSEL_RTC32KCLK_CLK 2U /*!< OSC32KSEL select: RTC32KCLK clock (32.768kHz) */
83 #define SIM_PLLFLLSEL_IRC48MCLK_CLK 3U /*!< PLLFLL select: IRC48MCLK clock */
84 #define SIM_PLLFLLSEL_MCGPLLCLK_CLK 1U /*!< PLLFLL select: MCGPLLCLK clock */
85 
86 /*******************************************************************************
87  * Variables
88  ******************************************************************************/
89 /* System clock frequency. */
90 extern uint32_t SystemCoreClock;
91 
92 /*******************************************************************************
93  * Code
94  ******************************************************************************/
95 /*FUNCTION**********************************************************************
96  *
97  * Function Name : CLOCK_CONFIG_SetFllExtRefDiv
98  * Description : Configure FLL external reference divider (FRDIV).
99  * Param frdiv : The value to set FRDIV.
100  *
101  *END**************************************************************************/
102 static void CLOCK_CONFIG_SetFllExtRefDiv(uint8_t frdiv)
103 {
104  MCG->C1 = ((MCG->C1 & ~MCG_C1_FRDIV_MASK) | MCG_C1_FRDIV(frdiv));
105 }
106 
107 /*******************************************************************************
108  ********************** Configuration BOARD_BootClockRUN ***********************
109  ******************************************************************************/
110 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
111 !!Configuration
112 name: BOARD_BootClockRUN
113 outputs:
114 - {id: Bus_clock.outFreq, value: 60 MHz}
115 - {id: Core_clock.outFreq, value: 120 MHz, locked: true, accuracy: '0.001'}
116 - {id: Flash_clock.outFreq, value: 24 MHz}
117 - {id: FlexBus_clock.outFreq, value: 40 MHz}
118 - {id: LPO_clock.outFreq, value: 1 kHz}
119 - {id: MCGFFCLK.outFreq, value: 1.5625 MHz}
120 - {id: MCGIRCLK.outFreq, value: 32.768 kHz}
121 - {id: OSCERCLK.outFreq, value: 50 MHz}
122 - {id: PLLFLLCLK.outFreq, value: 120 MHz}
123 - {id: System_clock.outFreq, value: 120 MHz}
124 settings:
125 - {id: MCGMode, value: PEE}
126 - {id: MCG.FCRDIV.scale, value: '1', locked: true}
127 - {id: MCG.FRDIV.scale, value: '32'}
128 - {id: MCG.IREFS.sel, value: MCG.FRDIV}
129 - {id: MCG.PLLS.sel, value: MCG.PLL}
130 - {id: MCG.PRDIV.scale, value: '20', locked: true}
131 - {id: MCG.VDIV.scale, value: '48', locked: true}
132 - {id: MCG_C1_IRCLKEN_CFG, value: Enabled}
133 - {id: MCG_C2_RANGE0_CFG, value: Very_high}
134 - {id: MCG_C2_RANGE0_FRDIV_CFG, value: Very_high}
135 - {id: OSC_CR_ERCLKEN_CFG, value: Enabled}
136 - {id: RTCCLKOUTConfig, value: 'yes'}
137 - {id: RTC_CR_OSCE_CFG, value: Enabled}
138 - {id: RTC_CR_OSC_CAP_LOAD_CFG, value: SC10PF}
139 - {id: SIM.OSC32KSEL.sel, value: RTC.RTC32KCLK}
140 - {id: SIM.OUTDIV2.scale, value: '2'}
141 - {id: SIM.OUTDIV3.scale, value: '3'}
142 - {id: SIM.OUTDIV4.scale, value: '5'}
143 - {id: SIM.PLLFLLSEL.sel, value: MCG.MCGPLLCLK}
144 - {id: SIM.RTCCLKOUTSEL.sel, value: RTC.RTC32KCLK}
145 - {id: SIM.SDHCSRCSEL.sel, value: OSC.OSCERCLK}
146 - {id: SIM.TIMESRCSEL.sel, value: OSC.OSCERCLK}
147 - {id: SIM.USBDIV.scale, value: '5'}
148 - {id: SIM.USBFRAC.scale, value: '2'}
149 - {id: SIM.USBSRCSEL.sel, value: SIM.USBDIV}
150 sources:
151 - {id: OSC.OSC.outFreq, value: 50 MHz, enabled: true}
152  * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
153 
154 /*******************************************************************************
155  * Variables for BOARD_BootClockRUN configuration
156  ******************************************************************************/
157 const mcg_config_t mcgConfig_BOARD_BootClockRUN =
158  {
159  .mcgMode = kMCG_ModePEE, /* PEE - PLL Engaged External */
160  .irclkEnableMode = kMCG_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
161  .ircs = kMCG_IrcSlow, /* Slow internal reference clock selected */
162  .fcrdiv = 0x0U, /* Fast IRC divider: divided by 1 */
163  .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
164  .drs = kMCG_DrsLow, /* Low frequency range */
165  .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
166  .oscsel = kMCG_OscselOsc, /* Selects System Oscillator (OSCCLK) */
167  .pll0Config =
168  {
169  .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
170  .prdiv = 0x13U, /* PLL Reference divider: divided by 20 */
171  .vdiv = 0x18U, /* VCO divider: multiplied by 48 */
172  },
173  };
174 const sim_clock_config_t simConfig_BOARD_BootClockRUN =
175  {
176  .pllFllSel = SIM_PLLFLLSEL_MCGPLLCLK_CLK, /* PLLFLL select: MCGPLLCLK clock */
177  .er32kSrc = SIM_OSC32KSEL_RTC32KCLK_CLK, /* OSC32KSEL select: RTC32KCLK clock (32.768kHz) */
178  .clkdiv1 = 0x1240000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV2: /2, OUTDIV3: /3, OUTDIV4: /5 */
179  };
180 const osc_config_t oscConfig_BOARD_BootClockRUN =
181  {
182  .freq = 50000000U, /* Oscillator frequency: 50000000Hz */
183  .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
184  .workMode = kOSC_ModeExt, /* Use external clock */
185  .oscerConfig =
186  {
187  .enableMode = kOSC_ErClkEnable, /* Enable external reference clock, disable external reference clock in STOP mode */
188  }
189  };
190 
191 /*******************************************************************************
192  * Code for BOARD_BootClockRUN configuration
193  ******************************************************************************/
195 {
196  /* Set the system clock dividers in SIM to safe value. */
197  CLOCK_SetSimSafeDivs();
198  /* Initializes OSC0 according to board configuration. */
199  CLOCK_InitOsc0(&oscConfig_BOARD_BootClockRUN);
200  CLOCK_SetXtal0Freq(oscConfig_BOARD_BootClockRUN.freq);
201  /* Configure the Internal Reference clock (MCGIRCLK). */
202  CLOCK_SetInternalRefClkConfig(mcgConfig_BOARD_BootClockRUN.irclkEnableMode,
205  /* Configure FLL external reference divider (FRDIV). */
206  CLOCK_CONFIG_SetFllExtRefDiv(mcgConfig_BOARD_BootClockRUN.frdiv);
207  /* Set MCG to PEE mode. */
208  CLOCK_BootToPeeMode(mcgConfig_BOARD_BootClockRUN.oscsel,
209  kMCG_PllClkSelPll0,
210  &mcgConfig_BOARD_BootClockRUN.pll0Config);
211  /* Set the clock configuration in SIM module. */
212  CLOCK_SetSimConfig(&simConfig_BOARD_BootClockRUN);
213  /* Set SystemCoreClock variable. */
215 }
216 
217 /*******************************************************************************
218  ********************* Configuration BOARD_BootClockVLPR ***********************
219  ******************************************************************************/
220 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
221 !!Configuration
222 name: BOARD_BootClockVLPR
223 outputs:
224 - {id: Bus_clock.outFreq, value: 4 MHz}
225 - {id: Core_clock.outFreq, value: 4 MHz, locked: true, accuracy: '0.001'}
226 - {id: Flash_clock.outFreq, value: 800 kHz}
227 - {id: FlexBus_clock.outFreq, value: 4 MHz}
228 - {id: LPO_clock.outFreq, value: 1 kHz}
229 - {id: MCGIRCLK.outFreq, value: 4 MHz}
230 - {id: System_clock.outFreq, value: 4 MHz}
231 settings:
232 - {id: MCGMode, value: BLPI}
233 - {id: powerMode, value: VLPR}
234 - {id: MCG.CLKS.sel, value: MCG.IRCS}
235 - {id: MCG.FCRDIV.scale, value: '1'}
236 - {id: MCG.FRDIV.scale, value: '32'}
237 - {id: MCG.IRCS.sel, value: MCG.FCRDIV}
238 - {id: MCG_C1_IRCLKEN_CFG, value: Enabled}
239 - {id: MCG_C2_RANGE0_CFG, value: Very_high}
240 - {id: MCG_C2_RANGE0_FRDIV_CFG, value: Very_high}
241 - {id: RTC_CR_OSCE_CFG, value: Enabled}
242 - {id: RTC_CR_OSC_CAP_LOAD_CFG, value: SC10PF}
243 - {id: SIM.OSC32KSEL.sel, value: RTC.RTC32KCLK}
244 - {id: SIM.OUTDIV3.scale, value: '1'}
245 - {id: SIM.OUTDIV4.scale, value: '5'}
246 - {id: SIM.PLLFLLSEL.sel, value: IRC48M.IRC48MCLK}
247 - {id: SIM.RTCCLKOUTSEL.sel, value: RTC.RTC32KCLK}
248 sources:
249 - {id: OSC.OSC.outFreq, value: 50 MHz}
250  * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
251 
252 /*******************************************************************************
253  * Variables for BOARD_BootClockVLPR configuration
254  ******************************************************************************/
255 const mcg_config_t mcgConfig_BOARD_BootClockVLPR =
256  {
257  .mcgMode = kMCG_ModeBLPI, /* BLPI - Bypassed Low Power Internal */
258  .irclkEnableMode = kMCG_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
259  .ircs = kMCG_IrcFast, /* Fast internal reference clock selected */
260  .fcrdiv = 0x0U, /* Fast IRC divider: divided by 1 */
261  .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
262  .drs = kMCG_DrsLow, /* Low frequency range */
263  .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
264  .oscsel = kMCG_OscselOsc, /* Selects System Oscillator (OSCCLK) */
265  .pll0Config =
266  {
267  .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
268  .prdiv = 0x0U, /* PLL Reference divider: divided by 1 */
269  .vdiv = 0x0U, /* VCO divider: multiplied by 24 */
270  },
271  };
272 const sim_clock_config_t simConfig_BOARD_BootClockVLPR =
273  {
274  .pllFllSel = SIM_PLLFLLSEL_IRC48MCLK_CLK, /* PLLFLL select: IRC48MCLK clock */
275  .er32kSrc = SIM_OSC32KSEL_RTC32KCLK_CLK, /* OSC32KSEL select: RTC32KCLK clock (32.768kHz) */
276  .clkdiv1 = 0x40000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV2: /1, OUTDIV3: /1, OUTDIV4: /5 */
277  };
278 const osc_config_t oscConfig_BOARD_BootClockVLPR =
279  {
280  .freq = 0U, /* Oscillator frequency: 0Hz */
281  .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
282  .workMode = kOSC_ModeExt, /* Use external clock */
283  .oscerConfig =
284  {
285  .enableMode = OSC_ER_CLK_DISABLE, /* Disable external reference clock */
286  }
287  };
288 
289 /*******************************************************************************
290  * Code for BOARD_BootClockVLPR configuration
291  ******************************************************************************/
293 {
294  /* Set the system clock dividers in SIM to safe value. */
295  CLOCK_SetSimSafeDivs();
296  /* Set MCG to BLPI mode. */
297  CLOCK_BootToBlpiMode(mcgConfig_BOARD_BootClockVLPR.fcrdiv,
299  mcgConfig_BOARD_BootClockVLPR.irclkEnableMode);
300  /* Set the clock configuration in SIM module. */
301  CLOCK_SetSimConfig(&simConfig_BOARD_BootClockVLPR);
302  /* Set VLPR power mode. */
303  SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeAll);
304 #if (defined(FSL_FEATURE_SMC_HAS_LPWUI) && FSL_FEATURE_SMC_HAS_LPWUI)
305  SMC_SetPowerModeVlpr(SMC, false);
306 #else
307  SMC_SetPowerModeVlpr(SMC);
308 #endif
309  while (SMC_GetPowerModeState(SMC) != kSMC_PowerStateVlpr)
310  {
311  }
312  /* Set SystemCoreClock variable. */
314 }
315 
void BOARD_BootClockRUN(void)
This function executes configuration of clocks.
Definition: clock_config.c:194
const sim_clock_config_t simConfig_BOARD_BootClockVLPR
SIM module set for BOARD_BootClockVLPR configuration.
Definition: clock_config.c:272
const osc_config_t oscConfig_BOARD_BootClockRUN
OSC set for BOARD_BootClockRUN configuration.
Definition: clock_config.c:180
const osc_config_t oscConfig_BOARD_BootClockVLPR
OSC set for BOARD_BootClockVLPR configuration.
Definition: clock_config.c:278
const mcg_config_t mcgConfig_BOARD_BootClockRUN
MCG set for BOARD_BootClockRUN configuration.
Definition: clock_config.c:157
#define MCG_PLL_DISABLE
Definition: clock_config.c:79
void BOARD_BootClockVLPR(void)
This function executes configuration of clocks.
Definition: clock_config.c:292
#define OSC_CAP0P
Definition: clock_config.c:80
#define BOARD_BOOTCLOCKVLPR_CORE_CLOCK
Definition: clock_config.h:86
#define SIM_PLLFLLSEL_IRC48MCLK_CLK
Definition: clock_config.c:83
const mcg_config_t mcgConfig_BOARD_BootClockVLPR
MCG set for BOARD_BootClockVLPR configuration.
Definition: clock_config.c:255
#define BOARD_BOOTCLOCKRUN_CORE_CLOCK
Definition: clock_config.h:51
uint32_t SystemCoreClock
#define OSC_ER_CLK_DISABLE
Definition: clock_config.c:81
#define SIM_OSC32KSEL_RTC32KCLK_CLK
Definition: clock_config.c:82
#define SIM_PLLFLLSEL_MCGPLLCLK_CLK
Definition: clock_config.c:84
#define SMC
Definition: lpc54114.h:144
const sim_clock_config_t simConfig_BOARD_BootClockRUN
SIM module set for BOARD_BootClockRUN configuration.
Definition: clock_config.c:174