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: MKL25Z128xxx4
67 package_id: MKL25Z128VLK4
68 mcu_data: ksdk2_0
69 processor_version: 1.1.0
70 board: FRDM-KL25Z
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_LPO_CLK 3U /*!< OSC32KSEL select: LPO clock */
83 #define SIM_PLLFLLSEL_MCGFLLCLK_CLK 0U /*!< PLLFLL select: MCGFLLCLK 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: 24 MHz}
115 - {id: Core_clock.outFreq, value: 48 MHz, locked: true, accuracy: '0.001'}
116 - {id: ERCLK32K.outFreq, value: 1 kHz}
117 - {id: Flash_clock.outFreq, value: 24 MHz}
118 - {id: LPO_clock.outFreq, value: 1 kHz}
119 - {id: MCGIRCLK.outFreq, value: 32.768 kHz}
120 - {id: OSCERCLK.outFreq, value: 8 MHz}
121 - {id: PLLFLLCLK.outFreq, value: 48 MHz}
122 - {id: System_clock.outFreq, value: 48 MHz}
123 settings:
124 - {id: MCGMode, value: PEE}
125 - {id: MCG.FCRDIV.scale, value: '1', locked: true}
126 - {id: MCG.FRDIV.scale, value: '32'}
127 - {id: MCG.IREFS.sel, value: MCG.FRDIV}
128 - {id: MCG.PLLS.sel, value: MCG.PLL}
129 - {id: MCG.PRDIV.scale, value: '2', locked: true}
130 - {id: MCG.VDIV.scale, value: '24', locked: true}
131 - {id: MCG_C1_IRCLKEN_CFG, value: Enabled}
132 - {id: MCG_C2_OSC_MODE_CFG, value: ModeOscLowPower}
133 - {id: MCG_C2_RANGE0_CFG, value: High}
134 - {id: MCG_C2_RANGE0_FRDIV_CFG, value: High}
135 - {id: OSC0_CR_ERCLKEN_CFG, value: Enabled}
136 - {id: OSC_CR_ERCLKEN_CFG, value: Enabled}
137 - {id: SIM.CLKOUTSEL.sel, value: SIM.OUTDIV4}
138 - {id: SIM.OSC32KSEL.sel, value: PMC.LPOCLK}
139 - {id: SIM.OUTDIV1.scale, value: '2'}
140 - {id: SIM.PLLFLLSEL.sel, value: SIM.MCGPLLCLK_DIV2}
141 - {id: SIM.TPMSRCSEL.sel, value: SIM.PLLFLLSEL}
142 - {id: SIM.UART0SRCSEL.sel, value: SIM.PLLFLLSEL}
143 - {id: SIM.USBSRCSEL.sel, value: SIM.PLLFLLSEL}
144 sources:
145 - {id: OSC.OSC.outFreq, value: 8 MHz, enabled: true}
146  * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
147 
148 /*******************************************************************************
149  * Variables for BOARD_BootClockRUN configuration
150  ******************************************************************************/
151 const mcg_config_t mcgConfig_BOARD_BootClockRUN =
152  {
153  .mcgMode = kMCG_ModePEE, /* PEE - PLL Engaged External */
154  .irclkEnableMode = kMCG_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
155  .ircs = kMCG_IrcSlow, /* Slow internal reference clock selected */
156  .fcrdiv = 0x0U, /* Fast IRC divider: divided by 1 */
157  .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
158  .drs = kMCG_DrsLow, /* Low frequency range */
159  .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
160  .pll0Config =
161  {
162  .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
163  .prdiv = 0x1U, /* PLL Reference divider: divided by 2 */
164  .vdiv = 0x0U, /* VCO divider: multiplied by 24 */
165  },
166  };
167 const sim_clock_config_t simConfig_BOARD_BootClockRUN =
168  {
169  .pllFllSel = SIM_PLLFLLSEL_MCGPLLCLK_CLK, /* PLLFLL select: MCGPLLCLK clock */
170  .er32kSrc = SIM_OSC32KSEL_LPO_CLK, /* OSC32KSEL select: LPO clock */
171  .clkdiv1 = 0x10010000U, /* SIM_CLKDIV1 - OUTDIV1: /2, OUTDIV4: /2 */
172  };
173 const osc_config_t oscConfig_BOARD_BootClockRUN =
174  {
175  .freq = 8000000U, /* Oscillator frequency: 8000000Hz */
176  .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
177  .workMode = kOSC_ModeOscLowPower, /* Oscillator low power */
178  .oscerConfig =
179  {
180  .enableMode = kOSC_ErClkEnable, /* Enable external reference clock, disable external reference clock in STOP mode */
181  }
182  };
183 
184 /*******************************************************************************
185  * Code for BOARD_BootClockRUN configuration
186  ******************************************************************************/
188 {
189  /* Set the system clock dividers in SIM to safe value. */
190  CLOCK_SetSimSafeDivs();
191  /* Initializes OSC0 according to board configuration. */
192  CLOCK_InitOsc0(&oscConfig_BOARD_BootClockRUN);
193  CLOCK_SetXtal0Freq(oscConfig_BOARD_BootClockRUN.freq);
194  /* Configure FLL external reference divider (FRDIV). */
195  CLOCK_CONFIG_SetFllExtRefDiv(mcgConfig_BOARD_BootClockRUN.frdiv);
196  /* Set MCG to PEE mode. */
197  CLOCK_BootToPeeMode(kMCG_OscselOsc,
198  kMCG_PllClkSelPll0,
199  &mcgConfig_BOARD_BootClockRUN.pll0Config);
200  /* Configure the Internal Reference clock (MCGIRCLK). */
201  CLOCK_SetInternalRefClkConfig(mcgConfig_BOARD_BootClockRUN.irclkEnableMode,
204  /* Set the clock configuration in SIM module. */
205  CLOCK_SetSimConfig(&simConfig_BOARD_BootClockRUN);
206  /* Set SystemCoreClock variable. */
208 }
209 
210 /*******************************************************************************
211  ********************* Configuration BOARD_BootClockVLPR ***********************
212  ******************************************************************************/
213 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
214 !!Configuration
215 name: BOARD_BootClockVLPR
216 outputs:
217 - {id: Bus_clock.outFreq, value: 800 kHz}
218 - {id: Core_clock.outFreq, value: 4 MHz}
219 - {id: ERCLK32K.outFreq, value: 1 kHz}
220 - {id: Flash_clock.outFreq, value: 800 kHz}
221 - {id: LPO_clock.outFreq, value: 1 kHz}
222 - {id: MCGIRCLK.outFreq, value: 4 MHz}
223 - {id: System_clock.outFreq, value: 4 MHz}
224 settings:
225 - {id: MCGMode, value: BLPI}
226 - {id: powerMode, value: VLPR}
227 - {id: MCG.CLKS.sel, value: MCG.IRCS}
228 - {id: MCG.FCRDIV.scale, value: '1', locked: true}
229 - {id: MCG.FRDIV.scale, value: '32'}
230 - {id: MCG.IRCS.sel, value: MCG.FCRDIV}
231 - {id: MCG_C1_IRCLKEN_CFG, value: Enabled}
232 - {id: MCG_C2_OSC_MODE_CFG, value: ModeOscLowPower}
233 - {id: MCG_C2_RANGE0_CFG, value: High}
234 - {id: MCG_C2_RANGE0_FRDIV_CFG, value: High}
235 - {id: SIM.OSC32KSEL.sel, value: PMC.LPOCLK}
236 - {id: SIM.OUTDIV4.scale, value: '5'}
237 sources:
238 - {id: OSC.OSC.outFreq, value: 8 MHz}
239  * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
240 
241 /*******************************************************************************
242  * Variables for BOARD_BootClockVLPR configuration
243  ******************************************************************************/
244 const mcg_config_t mcgConfig_BOARD_BootClockVLPR =
245  {
246  .mcgMode = kMCG_ModeBLPI, /* BLPI - Bypassed Low Power Internal */
247  .irclkEnableMode = kMCG_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
248  .ircs = kMCG_IrcFast, /* Fast internal reference clock selected */
249  .fcrdiv = 0x0U, /* Fast IRC divider: divided by 1 */
250  .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
251  .drs = kMCG_DrsLow, /* Low frequency range */
252  .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
253  .pll0Config =
254  {
255  .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
256  .prdiv = 0x0U, /* PLL Reference divider: divided by 1 */
257  .vdiv = 0x0U, /* VCO divider: multiplied by 24 */
258  },
259  };
260 const sim_clock_config_t simConfig_BOARD_BootClockVLPR =
261  {
262  .pllFllSel = SIM_PLLFLLSEL_MCGFLLCLK_CLK, /* PLLFLL select: MCGFLLCLK clock */
263  .er32kSrc = SIM_OSC32KSEL_LPO_CLK, /* OSC32KSEL select: LPO clock */
264  .clkdiv1 = 0x40000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV4: /5 */
265  };
266 const osc_config_t oscConfig_BOARD_BootClockVLPR =
267  {
268  .freq = 0U, /* Oscillator frequency: 0Hz */
269  .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
270  .workMode = kOSC_ModeOscLowPower, /* Oscillator low power */
271  .oscerConfig =
272  {
273  .enableMode = OSC_ER_CLK_DISABLE, /* Disable external reference clock */
274  }
275  };
276 
277 /*******************************************************************************
278  * Code for BOARD_BootClockVLPR configuration
279  ******************************************************************************/
281 {
282  /* Set the system clock dividers in SIM to safe value. */
283  CLOCK_SetSimSafeDivs();
284  /* Set MCG to BLPI mode. */
285  CLOCK_BootToBlpiMode(mcgConfig_BOARD_BootClockVLPR.fcrdiv,
287  mcgConfig_BOARD_BootClockVLPR.irclkEnableMode);
288  /* Set the clock configuration in SIM module. */
289  CLOCK_SetSimConfig(&simConfig_BOARD_BootClockVLPR);
290  /* Set VLPR power mode. */
291  SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeAll);
292 #if (defined(FSL_FEATURE_SMC_HAS_LPWUI) && FSL_FEATURE_SMC_HAS_LPWUI)
293  SMC_SetPowerModeVlpr(SMC, false);
294 #else
295  SMC_SetPowerModeVlpr(SMC);
296 #endif
297  while (SMC_GetPowerModeState(SMC) != kSMC_PowerStateVlpr)
298  {
299  }
300  /* Set SystemCoreClock variable. */
302 }
303 
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 SIM_PLLFLLSEL_MCGPLLCLK_CLK
Definition: clock_config.c:84
void BOARD_BootClockVLPR(void)
This function executes configuration of clocks.
Definition: clock_config.c:292
#define OSC_CAP0P
Definition: clock_config.c:80
#define OSC_ER_CLK_DISABLE
Definition: clock_config.c:81
#define SIM_PLLFLLSEL_MCGFLLCLK_CLK
Definition: clock_config.c:83
#define BOARD_BOOTCLOCKVLPR_CORE_CLOCK
Definition: clock_config.h:86
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
#define SIM_OSC32KSEL_LPO_CLK
Definition: clock_config.c:82
uint32_t SystemCoreClock
#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
#define MCG_PLL_DISABLE
Definition: clock_config.c:79