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. Call CLOCK_SetMcgliteConfig to set MCG_Lite configuration.
44  *
45  * 4. Call CLOCK_SetSimConfig to set the clock configuration in SIM.
46  */
47 
48 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
49 !!ClocksProfile
50 product: Clocks v1.0
51 processor: MKL27Z64xxx4
52 package_id: MKL27Z64VLH4
53 mcu_data: ksdk2_0
54 processor_version: 1.0.1
55 board: FRDM-KL27Z
56  * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
57 
58 #include "fsl_smc.h"
59 #include "clock_config.h"
60 
61 /*******************************************************************************
62  * Definitions
63  ******************************************************************************/
64 #define SIM_OSC32KSEL_OSC32KCLK_CLK 0U /*!< OSC32KSEL select: OSC32KCLK clock */
65 
66 /*******************************************************************************
67  * Variables
68  ******************************************************************************/
69 /* System clock frequency. */
70 extern uint32_t SystemCoreClock;
71 
72 /*******************************************************************************
73  ********************** Configuration BOARD_BootClockRUN ***********************
74  ******************************************************************************/
75 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
76 !!Configuration
77 name: BOARD_BootClockRUN
78 outputs:
79 - {id: Bus_clock.outFreq, value: 24 MHz}
80 - {id: Core_clock.outFreq, value: 48 MHz}
81 - {id: Flash_clock.outFreq, value: 24 MHz}
82 - {id: LPO_clock.outFreq, value: 1 kHz}
83 - {id: MCGIRCLK.outFreq, value: 8 MHz}
84 - {id: MCGPCLK.outFreq, value: 48 MHz}
85 - {id: System_clock.outFreq, value: 48 MHz}
86 settings:
87 - {id: MCGMode, value: HIRC}
88 - {id: MCG.CLKS.sel, value: MCG.HIRC}
89 - {id: MCG_C2_OSC_MODE_CFG, value: ModeOscLowPower}
90 - {id: MCG_MC_HIRCEN_CFG, value: Enabled}
91 - {id: OSC0_CR_ERCLKEN_CFG, value: Enabled}
92 - {id: OSC_CR_ERCLKEN_CFG, value: Enabled}
93 - {id: OSC_CR_SYS_OSC_CAP_LOAD_CFG, value: SC12PF}
94 - {id: SIM.CLKOUTSEL.sel, value: MCG.MCGPCLK}
95 - {id: SIM.COPCLKSEL.sel, value: OSC.OSCERCLK}
96 - {id: SIM.FLEXIOSRCSEL.sel, value: MCG.MCGPCLK}
97 - {id: SIM.LPUART0SRCSEL.sel, value: MCG.MCGPCLK}
98 - {id: SIM.LPUART1SRCSEL.sel, value: MCG.MCGPCLK}
99 - {id: SIM.RTCCLKOUTSEL.sel, value: OSC.OSCERCLK}
100 - {id: SIM.TPMSRCSEL.sel, value: MCG.MCGPCLK}
101 - {id: SIM.USBSRCSEL.sel, value: MCG.MCGPCLK}
102 sources:
103 - {id: MCG.HIRC.outFreq, value: 48 MHz}
104 - {id: OSC.OSC.outFreq, value: 32.768 kHz}
105  * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
106 
107 /*******************************************************************************
108  * Variables for BOARD_BootClockRUN configuration
109  ******************************************************************************/
110 const mcglite_config_t mcgliteConfig_BOARD_BootClockRUN =
111  {
112  .outSrc = kMCGLITE_ClkSrcHirc, /* MCGOUTCLK source is HIRC */
113  .irclkEnableMode = kMCGLITE_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
114  .ircs = kMCGLITE_Lirc8M, /* Slow internal reference (LIRC) 8 MHz clock selected */
115  .fcrdiv = kMCGLITE_LircDivBy1, /* Low-frequency Internal Reference Clock Divider: divided by 1 */
116  .lircDiv2 = kMCGLITE_LircDivBy1, /* Second Low-frequency Internal Reference Clock Divider: divided by 1 */
117  .hircEnableInNotHircMode = true, /* HIRC source is enabled */
118  };
119 const sim_clock_config_t simConfig_BOARD_BootClockRUN =
120  {
121  .er32kSrc = SIM_OSC32KSEL_OSC32KCLK_CLK, /* OSC32KSEL select: OSC32KCLK clock */
122  .clkdiv1 = 0x10000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV4: /2 */
123  };
124 const osc_config_t oscConfig_BOARD_BootClockRUN =
125  {
126  .freq = 0U, /* Oscillator frequency: 0Hz */
127  .capLoad = (kOSC_Cap4P | kOSC_Cap8P), /* Oscillator capacity load: 12pF */
128  .workMode = kOSC_ModeOscLowPower, /* Oscillator low power */
129  .oscerConfig =
130  {
131  .enableMode = kOSC_ErClkEnable, /* Enable external reference clock, disable external reference clock in STOP mode */
132  }
133  };
134 
135 /*******************************************************************************
136  * Code for BOARD_BootClockRUN configuration
137  ******************************************************************************/
139 {
140  /* Set the system clock dividers in SIM to safe value. */
141  CLOCK_SetSimSafeDivs();
142  /* Set MCG to HIRC mode. */
143  CLOCK_SetMcgliteConfig(&mcgliteConfig_BOARD_BootClockRUN);
144  /* Set the clock configuration in SIM module. */
145  CLOCK_SetSimConfig(&simConfig_BOARD_BootClockRUN);
146  /* Set SystemCoreClock variable. */
148 }
149 
150 /*******************************************************************************
151  ********************* Configuration BOARD_BootClockVLPR ***********************
152  ******************************************************************************/
153 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
154 !!Configuration
155 name: BOARD_BootClockVLPR
156 outputs:
157 - {id: Bus_clock.outFreq, value: 1 MHz}
158 - {id: Core_clock.outFreq, value: 2 MHz}
159 - {id: Flash_clock.outFreq, value: 1 MHz}
160 - {id: LPO_clock.outFreq, value: 1 kHz}
161 - {id: MCGIRCLK.outFreq, value: 2 MHz}
162 - {id: System_clock.outFreq, value: 2 MHz}
163 settings:
164 - {id: MCGMode, value: LIRC2M}
165 - {id: powerMode, value: VLPR}
166 - {id: MCG.LIRCDIV1.scale, value: '1', locked: true}
167 - {id: OSC0_CR_ERCLKEN_CFG, value: Enabled}
168 - {id: OSC_CR_ERCLKEN_CFG, value: Enabled}
169 - {id: OSC_CR_SYS_OSC_CAP_LOAD_CFG, value: SC12PF}
170 - {id: RTCCLKOUTConfig, value: 'yes'}
171 - {id: SIM.OUTDIV1.scale, value: '1', locked: true}
172 - {id: SIM.OUTDIV4.scale, value: '2', locked: true}
173 - {id: SIM.RTCCLKOUTSEL.sel, value: OSC.OSCERCLK}
174 sources:
175 - {id: MCG.LIRC.outFreq, value: 2 MHz}
176 - {id: OSC.OSC.outFreq, value: 32.768 kHz}
177  * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
178 
179 /*******************************************************************************
180  * Variables for BOARD_BootClockVLPR configuration
181  ******************************************************************************/
182 const mcglite_config_t mcgliteConfig_BOARD_BootClockVLPR =
183  {
184  .outSrc = kMCGLITE_ClkSrcLirc, /* MCGOUTCLK source is LIRC */
185  .irclkEnableMode = kMCGLITE_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
186  .ircs = kMCGLITE_Lirc2M, /* Slow internal reference (LIRC) 2 MHz clock selected */
187  .fcrdiv = kMCGLITE_LircDivBy1, /* Low-frequency Internal Reference Clock Divider: divided by 1 */
188  .lircDiv2 = kMCGLITE_LircDivBy1, /* Second Low-frequency Internal Reference Clock Divider: divided by 1 */
189  .hircEnableInNotHircMode = false, /* HIRC source is not enabled */
190  };
191 const sim_clock_config_t simConfig_BOARD_BootClockVLPR =
192  {
193  .er32kSrc = SIM_OSC32KSEL_OSC32KCLK_CLK, /* OSC32KSEL select: OSC32KCLK clock */
194  .clkdiv1 = 0x10000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV4: /2 */
195  };
196 const osc_config_t oscConfig_BOARD_BootClockVLPR =
197  {
198  .freq = 0U, /* Oscillator frequency: 0Hz */
199  .capLoad = (kOSC_Cap4P | kOSC_Cap8P), /* Oscillator capacity load: 12pF */
200  .workMode = kOSC_ModeExt, /* Use external clock */
201  .oscerConfig =
202  {
203  .enableMode = kOSC_ErClkEnable, /* Enable external reference clock, disable external reference clock in STOP mode */
204  }
205  };
206 
207 /*******************************************************************************
208  * Code for BOARD_BootClockVLPR configuration
209  ******************************************************************************/
211 {
212  /* Set the system clock dividers in SIM to safe value. */
213  CLOCK_SetSimSafeDivs();
214  /* Set MCG to LIRC2M mode. */
215  CLOCK_SetMcgliteConfig(&mcgliteConfig_BOARD_BootClockVLPR);
216  /* Set the clock configuration in SIM module. */
217  CLOCK_SetSimConfig(&simConfig_BOARD_BootClockVLPR);
218  /* Set VLPR power mode. */
219  SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeAll);
220 #if (defined(FSL_FEATURE_SMC_HAS_LPWUI) && FSL_FEATURE_SMC_HAS_LPWUI)
221  SMC_SetPowerModeVlpr(SMC, false);
222 #else
223  SMC_SetPowerModeVlpr(SMC);
224 #endif
225  while (SMC_GetPowerModeState(SMC) != kSMC_PowerStateVlpr)
226  {
227  }
228  /* Set SystemCoreClock variable. */
230 }
231 
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 mcglite_config_t mcgliteConfig_BOARD_BootClockRUN
MCG lite set for BOARD_BootClockRUN configuration.
Definition: clock_config.c:110
const osc_config_t oscConfig_BOARD_BootClockVLPR
OSC set for BOARD_BootClockVLPR configuration.
Definition: clock_config.c:278
void BOARD_BootClockVLPR(void)
This function executes configuration of clocks.
Definition: clock_config.c:292
#define BOARD_BOOTCLOCKVLPR_CORE_CLOCK
Definition: clock_config.h:86
#define BOARD_BOOTCLOCKRUN_CORE_CLOCK
Definition: clock_config.h:51
uint32_t SystemCoreClock
#define SMC
Definition: lpc54114.h:144
const mcglite_config_t mcgliteConfig_BOARD_BootClockVLPR
MCG lite set for BOARD_BootClockVLPR configuration.
Definition: clock_config.c:182
const sim_clock_config_t simConfig_BOARD_BootClockRUN
SIM module set for BOARD_BootClockRUN configuration.
Definition: clock_config.c:174
#define SIM_OSC32KSEL_OSC32KCLK_CLK
Definition: clock_config.c:64