rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
clock_config.c
Go to the documentation of this file.
1/*
2 * How to setup clock using clock driver functions:
3 *
4 * 1. Call CLOCK_InitXXX() to configure corresponding SCG clock source.
5 * Note: The clock could not be set when it is being used as system clock.
6 * In default out of reset, the CPU is clocked from FIRC(IRC48M),
7 * so before setting FIRC, change to use another avaliable clock source.
8 *
9 * 2. Call CLOCK_SetXtal0Freq() to set XTAL0 frequency based on board settings.
10 *
11 * 3. Call CLOCK_SetXxxModeSysClkConfig() to set SCG mode for Xxx run mode.
12 * Wait until the system clock source is changed to target source.
13 *
14 * 4. If power mode change is needed, call SMC_SetPowerModeProtection() to allow
15 * corresponding power mode and SMC_SetPowerModeXxx() to change to Xxx mode.
16 * Supported run mode and clock restrictions could be found in Reference Manual.
17 */
18
19#include "hal.h"
20#include "fsl_smc.h"
21#include "clock_config.h"
22
23/* System clock frequency. */
24//extern uint32_t SystemCoreClock;
25
27
28/* Variables for BOARD_BootClockRUN High-speed configuration */
30 .divSlow = kSCG_SysClkDivBy7, /* Slow Clock Divider: divided by 7 */
31 .divBus = kSCG_SysClkDivBy8, /* Bus Clock Divider: divided by 8 */
32 .divCore = kSCG_SysClkDivBy1, /* Core Clock Divider: divided by 1 */
33 .src = kSCG_SysClkSrcSysPll, /* System PLL is selected as System Clock Source */
34};
35
36// has half-frequency Core Clock
38 .divSlow = kSCG_SysClkDivBy7, /* Slow Clock Divider: divided by 7 */
39 .divBus = kSCG_SysClkDivBy8, /* Bus Clock Divider: divided by 8 */
40 .divCore = kSCG_SysClkDivBy2, /* Core Clock Divider: divided by 2 */
41 .src = kSCG_SysClkSrcSysPll, /* System PLL is selected as System Clock Source */
42 };
43
45 .freq = KINETIS_CLK, /* System Oscillator frequency: 8000000Hz */
46 .enableMode = kSCG_SysOscEnable, /* Enable System OSC clock */
47 .monitorMode = kSCG_SysOscMonitorDisable, /* Monitor disabled */
48 .div1 = kSCG_AsyncClkDisable, /* System OSC Clock Divider 1: Clock output is disabled */
49 .div2 = kSCG_AsyncClkDisable, /* System OSC Clock Divider 2: Clock output is disabled */
50 .workMode = kSCG_SysOscModeExt, /* Use external clock */
51};
53 .enableMode = kSCG_SircEnableInLowPower,/* Enable SIRC clock, Enable SIRC in low power mode */
54 .div1 = kSCG_AsyncClkDisable, /* Slow IRC Clock Divider 1: Clock output is disabled */
55 .div2 = kSCG_AsyncClkDisable, /* Slow IRC Clock Divider 2: Clock output is disabled */
56 .range = kSCG_SircRangeHigh, /* Slow IRC high range clock (8 MHz) */
57};
59 .enableMode = kSCG_FircDisableRegulator, /* Enable FIRC clock */
60 .div1 = kSCG_AsyncClkDisable, /* Fast IRC Clock Divider 1: Clock output is disabled */
61 .div2 = kSCG_AsyncClkDisable, /* Fast IRC Clock Divider 2: Clock output is disabled */
62 .range = kSCG_FircRange48M, /* Fast IRC is trimmed to 48MHz */
63 .trimConfig = NULL, /* Fast IRC Trim disabled */
64};
66 .enableMode = kSCG_SysPllEnable, /* Enable SPLL clock */
67 .monitorMode = kSCG_SysPllMonitorDisable, /* Monitor disabled */
68 .div1 = kSCG_AsyncClkDivBy64, /* System PLL Clock Divider 1: divided by 64 */
69 .div2 = kSCG_AsyncClkDivBy8, /* System PLL Clock Divider 2: divided by 8 */
70 .src = kSCG_SysPllSrcSysOsc, /* System PLL clock source is System OSC */
71 .prediv = KINETIS_EXT_PLL_DIV, /* 0=Divided by 1 */
72 .mult = KINETIS_EXT_PLL_MUL, /* Multiply Factor */
73};
74
76 .enableMode = kSCG_FircEnable, /* Enable FIRC clock */
77 .div1 = kSCG_AsyncClkDisable, /* Fast IRC Clock Divider 1: Clock output is disabled */
78 .div2 = kSCG_AsyncClkDisable, /* Fast IRC Clock Divider 2: Clock output is disabled */
79 .range = kSCG_FircRange48M, /* Fast IRC is trimmed to 48MHz */
80 .trimConfig = NULL, /* Fast IRC Trim disabled */
81};
83 .enableMode = kSCG_SysPllEnable, /* Enable SPLL clock */
84 .monitorMode = kSCG_SysPllMonitorDisable, /* Monitor disabled */
85 .div1 = kSCG_AsyncClkDivBy64, /* System PLL Clock Divider 1: divided by 64 */
86 .div2 = kSCG_AsyncClkDivBy8, /* System PLL Clock Divider 2: divided by 8 */
87 .src = kSCG_SysPllSrcFirc, /* System PLL clock source is Fast IRC */
88 .prediv = KINETIS_INT_PLL_DIV, /* Divided by Factor */
89 .mult = KINETIS_INT_PLL_MUL, /* Multiply Factor */
90};
91
92/*FUNCTION**********************************************************************
93 *
94 * Function Name : CLOCK_CONFIG_FircSafeConfig
95 * Description : This function is used to safely configure FIRC clock.
96 * In default out of reset, the CPU is clocked from FIRC(IRC48M).
97 * Before setting FIRC, change to use SIRC as system clock,
98 * then configure FIRC. After FIRC is set, change back to use FIRC
99 * in case SIRC need to be configured.
100 * Param fircConfig : FIRC configuration.
101 *
102 *END**************************************************************************/
103static void CLOCK_CONFIG_FircSafeConfig(const scg_firc_config_t *fircConfig) {
104 scg_sys_clk_config_t curConfig;
105 const scg_sirc_config_t scgSircConfig = {
107 .div1 = kSCG_AsyncClkDisable,
108 .div2 = kSCG_AsyncClkDivBy2,
109 .range = kSCG_SircRangeHigh
110 };
111 scg_sys_clk_config_t sysClkSafeConfigSource = {
112 .divSlow = kSCG_SysClkDivBy4, /* Slow clock divider */
113 .divBus = kSCG_SysClkDivBy1, /* Bus clock divider */
114 .divCore = kSCG_SysClkDivBy1, /* Core clock divider */
115 .src = kSCG_SysClkSrcSirc /* System clock source */
116 };
117 /* Init Sirc. */
118 CLOCK_InitSirc(&scgSircConfig);
119 /* Change to use SIRC as system clock source to prepare to change FIRCCFG register. */
120 CLOCK_SetRunModeSysClkConfig(&sysClkSafeConfigSource);
121 /* Wait for clock source switch finished. */
122 do {
123 CLOCK_GetCurSysClkConfig(&curConfig);
124 } while (curConfig.src != sysClkSafeConfigSource.src);
125
126 /* Init Firc. */
127 CLOCK_InitFirc(fircConfig);
128 /* Change back to use FIRC as system clock source in order to configure SIRC if needed. */
129 sysClkSafeConfigSource.src = kSCG_SysClkSrcFirc;
130 CLOCK_SetRunModeSysClkConfig(&sysClkSafeConfigSource);
131 /* Wait for clock source switch finished. */
132 do {
133 CLOCK_GetCurSysClkConfig(&curConfig);
134 } while (curConfig.src != sysClkSafeConfigSource.src);
135}
136
140
144
146 scg_sys_clk_config_t curConfig;
147
148 // set a new config
149 const scg_sys_clk_config_t *sysClkConfig = ke1xf_clock_is_high_speed(ct) ?
151
152 // Update config depending on the current speed mode
154 CLOCK_SetHsrunModeSysClkConfig(sysClkConfig);
155 } else {
156 CLOCK_SetRunModeSysClkConfig(sysClkConfig);
157 }
158
159 // Wait for clock source switch finished.
160 do {
161 CLOCK_GetCurSysClkConfig(&curConfig);
162 } while (curConfig.src != sysClkConfig->src);
163}
164
165/*******************************************************************************
166 * Code for BOARD_BootClockRUN configuration
167 ******************************************************************************/
169 // Init External Osc.
170 if (ke1xf_clock_is_external(ct)) {
171 // Init SOSC according to board configuration.
173 // Set the XTAL0 frequency based on board settings.
175 }
176 // Init FIRC.
179 }
180
182 // Set HSRUN power mode.
186 }
187 } else {
188 // If we've already started and want to change the mode, then we have to lower the frequency first:
189 // [RTFM: "Before exiting HSRUN mode, clock frequencies should be reduced back down to those acceptable in RUN mode."]
191 setClockConfig(ct);
192 }
193
196 }
197 }
198
199 // Now the mode is "officially" changed!
200 clockType = ct;
201
202 // Init SIRC.
203 //CLOCK_InitSirc(&g_scgSircConfig_BOARD_BootClockRUN);
204
205 // Init SysPll.
207
208 // Set SCG to SPLL mode.
209 setClockConfig(ct);
210
211 /* Set SystemCoreClock variable. */
212 //SystemCoreClock = KINETIS_PLL_FREQ;
213
214 /* Set PCC ADC0 selection */
216 /* Set PCC ADC1 selection */
218 /* Set PCC ADC2 selection */
220 /* Set PCC LPSPI0 selection */
222 /* Set PCC LPSPI1 selection */
224 /* Set PCC LPUART0 selection */
226 /* Set PCC LPUART1 selection */
228 /* Set PCC LPTMR0 selection */
230 /* Set PCC LPIT0 selection */
232 /* Set PCC FTM0 selection */
234 /* Set PCC FTM1 selection */
236 /* Set PCC FTM2 selection */
238 /* Set PCC FTM3 selection */
240
241#if 0
243 // try to restore the UART if the frequency has been changed
244 LPUART_SetBaudRate(LPUART0, CLOCK_SYS_GetUartFreq(UART0_IDX), KINETIS_UART_FREQUENCY);
245 }
246#endif
247}
248
const scg_spll_config_t g_scgSysPllConfig_EXT_BOARD_BootClockRUN
static kinetis_clock_type_e clockType
void ke1xf_clock_init(kinetis_clock_type_e ct)
This function executes the configuration of clocks.
const scg_firc_config_t g_scgFircConfig_EXT_BOARD_BootClockRUN
static void setClockConfig(kinetis_clock_type_e ct)
const scg_sosc_config_t g_scgSysOscConfig_BOARD_BootClockRUN
const scg_firc_config_t g_scgFircConfig_INT_BOARD_BootClockRUN
static void CLOCK_CONFIG_FircSafeConfig(const scg_firc_config_t *fircConfig)
const scg_sys_clk_config_t g_sysClkConfig_BOARD_BootClockRUN
const scg_sys_clk_config_t g_sysClkConfig_BOARD_BootClockRUN_HS
kinetis_clock_type_e ke1xf_clock_get_current_type(void)
static bool ke1xf_clock_is_high_speed(kinetis_clock_type_e ct)
const scg_spll_config_t g_scgSysPllConfig_INT_BOARD_BootClockRUN
static bool ke1xf_clock_is_external(kinetis_clock_type_e ct)
const scg_sirc_config_t g_scgSircConfig_BOARD_BootClockRUN
kinetis_clock_type_e
Kinetis Clock Type.
Definition clock_config.h:9
@ kinetis_clock_default_firc
@ kinetis_clock_ext_osc_hs
@ kinetis_clock_int_osc_hs
uint8_t enableMode
Definition fsl_clock.h:617
static void CLOCK_SetRunModeSysClkConfig(const scg_sys_clk_config_t *config)
Sets the system clock configuration for RUN mode.
Definition fsl_clock.h:795
static void CLOCK_SetXtal0Freq(uint32_t freq)
Sets the XTAL0 frequency based on board settings.
Definition fsl_clock.h:1347
uint32_t enableMode
Definition fsl_clock.h:575
static void CLOCK_SetIpSrc(clock_ip_name_t name, clock_ip_src_t src)
Set the clock source for specific IP module.
Definition fsl_clock.h:684
static void CLOCK_GetCurSysClkConfig(scg_sys_clk_config_t *config)
Gets the system clock configuration in the current power mode.
Definition fsl_clock.h:823
static void CLOCK_SetHsrunModeSysClkConfig(const scg_sys_clk_config_t *config)
Sets the system clock configuration for HSRUN mode.
Definition fsl_clock.h:809
status_t CLOCK_InitSirc(const scg_sirc_config_t *config)
Initializes the SCG slow IRC clock.
Definition fsl_clock.c:558
status_t CLOCK_InitFirc(const scg_firc_config_t *config)
Initializes the SCG fast IRC clock.
Definition fsl_clock.c:690
uint32_t enableMode
Definition fsl_clock.h:493
status_t CLOCK_InitSysPll(const scg_spll_config_t *config)
Initializes the SCG system PLL.
Definition fsl_clock.c:984
status_t CLOCK_InitSysOsc(const scg_sosc_config_t *config)
Initializes the SCG system OSC.
Definition fsl_clock.c:397
@ kSCG_FircRange48M
Definition fsl_clock.h:555
@ kCLOCK_Lptmr0
Definition fsl_clock.h:293
@ kCLOCK_Adc1
Definition fsl_clock.h:278
@ kCLOCK_Ftm3
Definition fsl_clock.h:277
@ kCLOCK_Adc2
Definition fsl_clock.h:290
@ kCLOCK_Lpuart1
Definition fsl_clock.h:306
@ kCLOCK_Adc0
Definition fsl_clock.h:289
@ kCLOCK_Lpspi1
Definition fsl_clock.h:280
@ kCLOCK_Lpuart0
Definition fsl_clock.h:305
@ kCLOCK_Lpit0
Definition fsl_clock.h:285
@ kCLOCK_Ftm1
Definition fsl_clock.h:287
@ kCLOCK_Lpspi0
Definition fsl_clock.h:279
@ kCLOCK_Ftm2
Definition fsl_clock.h:288
@ kCLOCK_Ftm0
Definition fsl_clock.h:286
@ kSCG_AsyncClkDivBy2
Definition fsl_clock.h:420
@ kSCG_AsyncClkDivBy8
Definition fsl_clock.h:422
@ kSCG_AsyncClkDivBy64
Definition fsl_clock.h:425
@ kSCG_AsyncClkDisable
Definition fsl_clock.h:418
@ kSCG_SircRangeHigh
Definition fsl_clock.h:477
@ kSCG_SysPllSrcSysOsc
Definition fsl_clock.h:590
@ kSCG_SysPllSrcFirc
Definition fsl_clock.h:591
@ kSCG_SircEnable
Definition fsl_clock.h:483
@ kSCG_SircEnableInLowPower
Definition fsl_clock.h:485
@ kSCG_SysPllEnable
Definition fsl_clock.h:608
@ kSCG_SysOscEnable
Definition fsl_clock.h:450
@ kSCG_SysOscMonitorDisable
Definition fsl_clock.h:433
@ kSCG_FircEnable
Definition fsl_clock.h:564
@ kSCG_FircDisableRegulator
Definition fsl_clock.h:567
@ kSCG_SysClkDivBy4
Definition fsl_clock.h:362
@ kSCG_SysClkDivBy1
Definition fsl_clock.h:359
@ kSCG_SysClkDivBy2
Definition fsl_clock.h:360
@ kSCG_SysClkDivBy8
Definition fsl_clock.h:366
@ kSCG_SysClkDivBy7
Definition fsl_clock.h:365
@ kSCG_SysPllMonitorDisable
Definition fsl_clock.h:599
@ kCLOCK_IpSrcSysPllAsync
Definition fsl_clock.h:259
@ kSCG_SysOscModeExt
Definition fsl_clock.h:442
@ kSCG_SysClkSrcSysPll
Definition fsl_clock.h:351
@ kSCG_SysClkSrcFirc
Definition fsl_clock.h:350
@ kSCG_SysClkSrcSirc
Definition fsl_clock.h:349
status_t LPUART_SetBaudRate(LPUART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
Sets the LPUART instance baudrate.
Definition fsl_lpuart.c:564
static void SMC_SetPowerModeProtection(SMC_Type *base, uint8_t allowedModes)
Configures all power mode protection settings.
Definition fsl_smc.h:260
static smc_power_state_t SMC_GetPowerModeState(SMC_Type *base)
Gets the current power mode status.
Definition fsl_smc.h:279
status_t SMC_SetPowerModeRun(SMC_Type *base)
Configures the system to RUN power mode.
Definition fsl_smc.c:147
status_t SMC_SetPowerModeHsrun(SMC_Type *base)
Configures the system to HSRUN power mode.
Definition fsl_smc.c:167
@ kSMC_AllowPowerModeAll
Definition fsl_smc.h:42
@ kSMC_PowerStateRun
Definition fsl_smc.h:65
@ kSMC_PowerStateHsrun
Definition fsl_smc.h:77
SCG fast IRC clock configuration.
Definition fsl_clock.h:574
SCG slow IRC clock configuration.
Definition fsl_clock.h:492
SCG system OSC configuration.
Definition fsl_clock.h:460
SCG system PLL configuration.
Definition fsl_clock.h:616
SCG system clock configuration.
Definition fsl_clock.h:381