diff options
Diffstat (limited to 'lib/chibios-contrib/ext/mcux-sdk/devices/MKE02Z4/drivers/fsl_clock.h')
-rw-r--r-- | lib/chibios-contrib/ext/mcux-sdk/devices/MKE02Z4/drivers/fsl_clock.h | 796 |
1 files changed, 796 insertions, 0 deletions
diff --git a/lib/chibios-contrib/ext/mcux-sdk/devices/MKE02Z4/drivers/fsl_clock.h b/lib/chibios-contrib/ext/mcux-sdk/devices/MKE02Z4/drivers/fsl_clock.h new file mode 100644 index 000000000..ce3df1d46 --- /dev/null +++ b/lib/chibios-contrib/ext/mcux-sdk/devices/MKE02Z4/drivers/fsl_clock.h | |||
@@ -0,0 +1,796 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2015, Freescale Semiconductor, Inc. | ||
3 | * Copyright 2016 - 2020, NXP | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * | ||
7 | * SPDX-License-Identifier: BSD-3-Clause | ||
8 | */ | ||
9 | |||
10 | #ifndef _FSL_CLOCK_H_ | ||
11 | #define _FSL_CLOCK_H_ | ||
12 | |||
13 | #include "fsl_common.h" | ||
14 | |||
15 | /*! @addtogroup clock */ | ||
16 | /*! @{ */ | ||
17 | |||
18 | /*! @file */ | ||
19 | |||
20 | /******************************************************************************* | ||
21 | * Configurations | ||
22 | ******************************************************************************/ | ||
23 | |||
24 | /*! @brief Configures whether to check a parameter in a function. | ||
25 | * | ||
26 | * Some ICS settings must be changed with conditions, for example: | ||
27 | * 1. ICSIRCLK settings, such as the source, divider, and the trim value should not change when | ||
28 | * ICSIRCLK is used as a system clock source. | ||
29 | * 2. ICS_C7[OSCSEL] should not be changed when the external reference clock is used | ||
30 | * as a system clock source. For example, in FBE/BELP/PBE modes. | ||
31 | * 3. The users should only switch between the supported clock modes. | ||
32 | * | ||
33 | * ICS functions check the parameter and ICS status before setting, if not allowed | ||
34 | * to change, the functions return error. The parameter checking increases code size, | ||
35 | * if code size is a critical requirement, change #ICS_CONFIG_CHECK_PARAM to 0 to | ||
36 | * disable parameter checking. | ||
37 | */ | ||
38 | #ifndef ICS_CONFIG_CHECK_PARAM | ||
39 | #define ICS_CONFIG_CHECK_PARAM 0U | ||
40 | #endif | ||
41 | |||
42 | /*! @brief Configure whether driver controls clock | ||
43 | * | ||
44 | * When set to 0, peripheral drivers will enable clock in initialize function | ||
45 | * and disable clock in de-initialize function. When set to 1, peripheral | ||
46 | * driver will not control the clock, application could control the clock out of | ||
47 | * the driver. | ||
48 | * | ||
49 | * @note All drivers share this feature switcher. If it is set to 1, application | ||
50 | * should handle clock enable and disable for all drivers. | ||
51 | */ | ||
52 | #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)) | ||
53 | #define FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL 0 | ||
54 | #endif | ||
55 | |||
56 | /******************************************************************************* | ||
57 | * Definitions | ||
58 | ******************************************************************************/ | ||
59 | |||
60 | /*! @name Driver version */ | ||
61 | /*@{*/ | ||
62 | /*! @brief CLOCK driver version 2.2.1. */ | ||
63 | #define FSL_CLOCK_DRIVER_VERSION (MAKE_VERSION(2, 2, 1)) | ||
64 | /*@}*/ | ||
65 | |||
66 | /* Definition for delay API in clock driver, users can redefine it to the real application. */ | ||
67 | #ifndef SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY | ||
68 | #define SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY (40000000UL) | ||
69 | #endif | ||
70 | |||
71 | /*! @brief External XTAL0 (OSC0) clock frequency. | ||
72 | * | ||
73 | * The XTAL0/EXTAL0 (OSC0) clock frequency in Hz. When the clock is set up, use the | ||
74 | * function CLOCK_SetXtal0Freq to set the value in the clock driver. For example, | ||
75 | * if XTAL0 is 8 MHz: | ||
76 | * @code | ||
77 | * CLOCK_InitOsc0(...); | ||
78 | * CLOCK_SetXtal0Freq(80000000) | ||
79 | * @endcode | ||
80 | * | ||
81 | * This is important for the multicore platforms where only one core needs to set up the | ||
82 | * OSC0 using the CLOCK_InitOsc0. All other cores need to call the CLOCK_SetXtal0Freq | ||
83 | * to get a valid clock frequency. | ||
84 | */ | ||
85 | extern volatile uint32_t g_xtal0Freq; | ||
86 | |||
87 | #if (defined(OSC) && !(defined(OSC0))) | ||
88 | #define OSC0 OSC | ||
89 | #endif | ||
90 | |||
91 | /*! @brief Clock ip name array for UART. */ | ||
92 | #define UART_CLOCKS \ | ||
93 | { \ | ||
94 | kCLOCK_Uart0, kCLOCK_Uart1, kCLOCK_Uart2 \ | ||
95 | } | ||
96 | |||
97 | /*! @brief Clock ip name array for ADC16. */ | ||
98 | #define ADC_CLOCKS \ | ||
99 | { \ | ||
100 | kCLOCK_Adc0 \ | ||
101 | } | ||
102 | |||
103 | /*! @brief Clock ip name array for IRQ. */ | ||
104 | #define IRQ_CLOCKS \ | ||
105 | { \ | ||
106 | kCLOCK_Irq0 \ | ||
107 | } | ||
108 | |||
109 | /*! @brief Clock ip name array for KBI. */ | ||
110 | #define KBI_CLOCKS \ | ||
111 | { \ | ||
112 | kCLOCK_Kbi0, kCLOCK_Kbi1 \ | ||
113 | } | ||
114 | |||
115 | /*! @brief Clock ip name array for SPI. */ | ||
116 | #define SPI_CLOCKS \ | ||
117 | { \ | ||
118 | kCLOCK_Spi0, kCLOCK_Spi1 \ | ||
119 | } | ||
120 | |||
121 | /*! @brief Clock ip name array for I2C. */ | ||
122 | #define I2C_CLOCKS \ | ||
123 | { \ | ||
124 | kCLOCK_I2c0 \ | ||
125 | } | ||
126 | |||
127 | /*! @brief Clock ip name array for FTM. */ | ||
128 | #define FTM_CLOCKS \ | ||
129 | { \ | ||
130 | kCLOCK_Ftm0, kCLOCK_Ftm1, kCLOCK_Ftm2 \ | ||
131 | } | ||
132 | |||
133 | /*! @brief Clock ip name array for CMP. */ | ||
134 | #define ACMP_CLOCKS \ | ||
135 | { \ | ||
136 | kCLOCK_Acmp0, kCLOCK_Acmp1 \ | ||
137 | } | ||
138 | |||
139 | /*! @brief Clock ip name array for CRC. */ | ||
140 | #define CRC_CLOCKS \ | ||
141 | { \ | ||
142 | kCLOCK_Crc0, \ | ||
143 | } | ||
144 | |||
145 | /*! @brief Clock ip name array for PIT. */ | ||
146 | #define PIT_CLOCKS \ | ||
147 | { \ | ||
148 | kCLOCK_Pit0, \ | ||
149 | } | ||
150 | |||
151 | /*! @brief Clock ip name array for RTC. */ | ||
152 | #define RTC_CLOCKS \ | ||
153 | { \ | ||
154 | kCLOCK_Rtc0, \ | ||
155 | } | ||
156 | |||
157 | /*! | ||
158 | * @brief LPO clock frequency. | ||
159 | */ | ||
160 | #define LPO_CLK_FREQ 1000U | ||
161 | |||
162 | /*! @brief Clock name used to get clock frequency. */ | ||
163 | typedef enum _clock_name | ||
164 | { | ||
165 | |||
166 | /* ----------------------------- System layer clock -------------------------------*/ | ||
167 | kCLOCK_CoreSysClk, /*!< Core/system clock */ | ||
168 | kCLOCK_PlatClk, /*!< Platform clock */ | ||
169 | kCLOCK_BusClk, /*!< Bus clock */ | ||
170 | kCLOCK_FlashClk, /*!< Flash clock */ | ||
171 | |||
172 | /* ---------------------------------- OSC clock -----------------------------------*/ | ||
173 | kCLOCK_Osc0ErClk, /*!< OSC0 external reference clock (OSC0ERCLK) */ | ||
174 | |||
175 | /* ----------------------------- ICS and ICS-Lite clock ---------------------------*/ | ||
176 | kCLOCK_ICSFixedFreqClk, /*!< ICS fixed frequency clock (ICSFFCLK) */ | ||
177 | kCLOCK_ICSInternalRefClk, /*!< ICS internal reference clock (ICSIRCLK) */ | ||
178 | kCLOCK_ICSFllClk, /*!< ICSFLLCLK */ | ||
179 | kCLOCK_ICSOutClk, /*!< ICS Output clock */ | ||
180 | |||
181 | /* --------------------------------- Other clock ----------------------------------*/ | ||
182 | kCLOCK_LpoClk, /*!< LPO clock */ | ||
183 | |||
184 | } clock_name_t; | ||
185 | |||
186 | /*------------------------------------------------------------------------------ | ||
187 | |||
188 | clock_gate_t definition: | ||
189 | |||
190 | 31 16 0 | ||
191 | ----------------------------------------------------------------- | ||
192 | | SIM_SCGC register offset | control bit offset in SCGC | | ||
193 | ----------------------------------------------------------------- | ||
194 | |||
195 | For example, the SDHC clock gate is controlled by SIM_SCGC3[17], the | ||
196 | SIM_SCGC3 offset in SIM is 0x1030, then kCLOCK_GateSdhc0 is defined as | ||
197 | |||
198 | kCLOCK_GateSdhc0 = (0x1030 << 16) | 17; | ||
199 | |||
200 | ------------------------------------------------------------------------------*/ | ||
201 | |||
202 | #define CLK_GATE_REG_OFFSET_SHIFT 16U | ||
203 | #define CLK_GATE_REG_OFFSET_MASK 0xFFFF0000U | ||
204 | #define CLK_GATE_BIT_SHIFT_SHIFT 0U | ||
205 | #define CLK_GATE_BIT_SHIFT_MASK 0x0000FFFFU | ||
206 | |||
207 | #define CLK_GATE_DEFINE(reg_offset, bit_shift) \ | ||
208 | ((((reg_offset) << CLK_GATE_REG_OFFSET_SHIFT) & CLK_GATE_REG_OFFSET_MASK) | \ | ||
209 | (((bit_shift) << CLK_GATE_BIT_SHIFT_SHIFT) & CLK_GATE_BIT_SHIFT_MASK)) | ||
210 | |||
211 | #define CLK_GATE_ABSTRACT_REG_OFFSET(x) (((x)&CLK_GATE_REG_OFFSET_MASK) >> CLK_GATE_REG_OFFSET_SHIFT) | ||
212 | #define CLK_GATE_ABSTRACT_BITS_SHIFT(x) (((x)&CLK_GATE_BIT_SHIFT_MASK) >> CLK_GATE_BIT_SHIFT_SHIFT) | ||
213 | |||
214 | /*! @brief Clock gate name used for CLOCK_EnableClock/CLOCK_DisableClock. */ | ||
215 | typedef enum _clock_ip_name | ||
216 | { | ||
217 | kCLOCK_IpInvalid = 0U, | ||
218 | |||
219 | kCLOCK_I2c0 = CLK_GATE_DEFINE(0xCU, 17U), | ||
220 | |||
221 | kCLOCK_Uart0 = CLK_GATE_DEFINE(0xCU, 20U), | ||
222 | kCLOCK_Uart1 = CLK_GATE_DEFINE(0xCU, 21U), | ||
223 | kCLOCK_Uart2 = CLK_GATE_DEFINE(0xCU, 22U), | ||
224 | |||
225 | kCLOCK_Acmp0 = CLK_GATE_DEFINE(0xCU, 30U), | ||
226 | kCLOCK_Acmp1 = CLK_GATE_DEFINE(0xCU, 31U), | ||
227 | |||
228 | kCLOCK_Spi0 = CLK_GATE_DEFINE(0xCU, 18U), | ||
229 | kCLOCK_Spi1 = CLK_GATE_DEFINE(0xCU, 19U), | ||
230 | |||
231 | kCLOCK_Irq0 = CLK_GATE_DEFINE(0xCU, 27U), | ||
232 | |||
233 | kCLOCK_Kbi0 = CLK_GATE_DEFINE(0xCU, 24U), | ||
234 | kCLOCK_Kbi1 = CLK_GATE_DEFINE(0xCU, 25U), | ||
235 | |||
236 | kCLOCK_Adc0 = CLK_GATE_DEFINE(0xCU, 29U), | ||
237 | |||
238 | kCLOCK_Crc0 = CLK_GATE_DEFINE(0xCU, 10U), | ||
239 | |||
240 | kCLOCK_Ftm0 = CLK_GATE_DEFINE(0xCU, 5U), | ||
241 | kCLOCK_Ftm1 = CLK_GATE_DEFINE(0xCU, 6U), | ||
242 | kCLOCK_Ftm2 = CLK_GATE_DEFINE(0xCU, 7U), | ||
243 | |||
244 | kCLOCK_Pit0 = CLK_GATE_DEFINE(0xCU, 1U), | ||
245 | kCLOCK_Rtc0 = CLK_GATE_DEFINE(0xCU, 0U), | ||
246 | } clock_ip_name_t; | ||
247 | |||
248 | /*!@brief SIM configuration structure for clock setting. */ | ||
249 | typedef struct _sim_clock_config | ||
250 | { | ||
251 | uint32_t busDiv; /*!< SIM_BUSDIV. */ | ||
252 | uint8_t busClkPrescaler; /*!< A option prescaler for bus clock */ | ||
253 | } sim_clock_config_t; | ||
254 | |||
255 | /*! @brief OSC work mode. */ | ||
256 | enum _osc_work_mode | ||
257 | { | ||
258 | kOSC_ModeExt = 0U, /*!< OSC source from external clock. */ | ||
259 | kOSC_ModeOscLowPower = OSC_CR_OSCOS_MASK, /*!< Oscillator low freq low power. */ | ||
260 | kOSC_ModeOscHighGain = OSC_CR_HGO_MASK | OSC_CR_OSCOS_MASK, /*!< Oscillator low freq high gain. */ | ||
261 | }; | ||
262 | |||
263 | /*! @brief OSC enable mode. */ | ||
264 | enum _osc_enable_mode | ||
265 | { | ||
266 | kOSC_Enable = OSC_CR_OSCEN_MASK, /*!< Enable. */ | ||
267 | kOSC_EnableInStop = OSC_CR_OSCSTEN_MASK /*!< Enable in stop mode. */ | ||
268 | }; | ||
269 | |||
270 | /*! | ||
271 | * @brief OSC Initialization Configuration Structure | ||
272 | * | ||
273 | * Defines the configuration data structure to initialize the OSC. | ||
274 | * When porting to a new board, set the following members | ||
275 | * according to the board setting: | ||
276 | * 1. freq: The external frequency. | ||
277 | * 2. workMode: The OSC module mode. | ||
278 | * 3. enableMode: The OSC enable mode. | ||
279 | */ | ||
280 | typedef struct _osc_config | ||
281 | { | ||
282 | uint32_t freq; /*!< External clock frequency. */ | ||
283 | uint8_t workMode; /*!< OSC work mode setting. */ | ||
284 | uint8_t enableMode; /*!< Configuration for OSCERCLK. */ | ||
285 | } osc_config_t; | ||
286 | |||
287 | /*! @brief ICS FLL reference clock source select. */ | ||
288 | typedef enum _ics_fll_src | ||
289 | { | ||
290 | kICS_FllSrcExternal, /*!< External reference clock is selected */ | ||
291 | kICS_FllSrcInternal /*!< The slow internal reference clock is selected */ | ||
292 | } ics_fll_src_t; | ||
293 | |||
294 | /*! @brief ICSOUT clock source. */ | ||
295 | typedef enum _ics_clkout_src | ||
296 | { | ||
297 | kICS_ClkOutSrcFll, /*!< Output of the FLL is selected (reset default) */ | ||
298 | kICS_ClkOutSrcInternal, /*!< Internal reference clock is selected, FLL is bypassed */ | ||
299 | kICS_ClkOutSrcExternal, /*!< External reference clock is selected, FLL is bypassed */ | ||
300 | } ics_clkout_src_t; | ||
301 | |||
302 | /*! @brief ICS status. */ | ||
303 | enum _ics_status | ||
304 | { | ||
305 | kStatus_ICS_ModeUnreachable = MAKE_STATUS(kStatusGroup_ICS, 0), /*!< Can't switch to target mode. */ | ||
306 | kStatus_ICS_SourceUsed = MAKE_STATUS(kStatusGroup_ICS, 1) /*!< Can't change the clock source because | ||
307 | it is in use. */ | ||
308 | }; | ||
309 | |||
310 | /*! @brief ICS internal reference clock (ICSIRCLK) enable mode definition. */ | ||
311 | enum _ics_irclk_enable_mode | ||
312 | { | ||
313 | kICS_IrclkDisable = 0U, /*!< ICSIRCLK disable. */ | ||
314 | kICS_IrclkEnable = ICS_C1_IRCLKEN_MASK, /*!< ICSIRCLK enable. */ | ||
315 | kICS_IrclkEnableInStop = ICS_C1_IREFSTEN_MASK /*!< ICSIRCLK enable in stop mode. */ | ||
316 | }; | ||
317 | |||
318 | /*! @brief ICS mode definitions */ | ||
319 | typedef enum _ics_mode | ||
320 | { | ||
321 | kICS_ModeFEI = 0U, /*!< FEI - FLL Engaged Internal */ | ||
322 | kICS_ModeFBI, /*!< FBI - FLL Bypassed Internal */ | ||
323 | kICS_ModeBILP, /*!< BILP - Bypassed Low Power Internal */ | ||
324 | kICS_ModeFEE, /*!< FEE - FLL Engaged External */ | ||
325 | kICS_ModeFBE, /*!< FBE - FLL Bypassed External */ | ||
326 | kICS_ModeBELP, /*!< BELP - Bypassed Low Power External */ | ||
327 | kICS_ModeError /*!< Unknown mode */ | ||
328 | } ics_mode_t; | ||
329 | |||
330 | /*! @brief ICS configuration structure | ||
331 | * | ||
332 | * When porting to a new board, set the following members | ||
333 | * according to the board setting: | ||
334 | * 1. icsMode: ICS mode | ||
335 | * 2. irClkEnableMode: ICSIRCLK enable mode | ||
336 | * 3. rDiv: If the FLL uses the external reference clock, set this | ||
337 | * value to ensure that the external reference clock divided by rDiv is | ||
338 | * in the 31.25 kHz to 39.0625 kHz range. | ||
339 | * 4. bDiv, this divider determine the ISCOUT clock | ||
340 | */ | ||
341 | typedef struct _ics_config | ||
342 | { | ||
343 | ics_mode_t icsMode; /*!< ICS mode. */ | ||
344 | uint8_t irClkEnableMode; /*!< ICSIRCLK enable mode. */ | ||
345 | uint8_t rDiv; /*!< Divider for external reference clock, ICS_C1[RDIV]. */ | ||
346 | uint8_t bDiv; /*!< Divider for ICS output clock ICS_C2[BDIV]. */ | ||
347 | } ics_config_t; | ||
348 | |||
349 | /******************************************************************************* | ||
350 | * API | ||
351 | ******************************************************************************/ | ||
352 | |||
353 | #if defined(__cplusplus) | ||
354 | extern "C" { | ||
355 | #endif /* __cplusplus */ | ||
356 | |||
357 | /*! | ||
358 | * @brief Enable the clock for specific IP. | ||
359 | * | ||
360 | * @param name Which clock to enable, see \ref clock_ip_name_t. | ||
361 | */ | ||
362 | static inline void CLOCK_EnableClock(clock_ip_name_t name) | ||
363 | { | ||
364 | uint32_t regAddr = SIM_BASE + CLK_GATE_ABSTRACT_REG_OFFSET((uint32_t)name); | ||
365 | (*(volatile uint32_t *)regAddr) |= (1UL << CLK_GATE_ABSTRACT_BITS_SHIFT((uint32_t)name)); | ||
366 | } | ||
367 | |||
368 | /*! | ||
369 | * @brief Disable the clock for specific IP. | ||
370 | * | ||
371 | * @param name Which clock to disable, see \ref clock_ip_name_t. | ||
372 | */ | ||
373 | static inline void CLOCK_DisableClock(clock_ip_name_t name) | ||
374 | { | ||
375 | uint32_t regAddr = SIM_BASE + CLK_GATE_ABSTRACT_REG_OFFSET((uint32_t)name); | ||
376 | (*(volatile uint32_t *)regAddr) &= ~(1UL << CLK_GATE_ABSTRACT_BITS_SHIFT((uint32_t)name)); | ||
377 | } | ||
378 | |||
379 | /*! | ||
380 | * @brief clock divider | ||
381 | * | ||
382 | * Set the SIM_BUSDIV. | ||
383 | * Carefully configure the SIM_BUSDIV to avoid bus/flash clock frequency higher | ||
384 | * than 24MHZ. | ||
385 | * @param busDiv bus clock output divider value. | ||
386 | */ | ||
387 | static inline void CLOCK_SetBusClkDiv(uint32_t busDiv) | ||
388 | { | ||
389 | SIM->BUSDIV = SIM_BUSDIV_BUSDIV(busDiv); | ||
390 | } | ||
391 | |||
392 | /*! | ||
393 | * @brief Gets the clock frequency for a specific clock name. | ||
394 | * | ||
395 | * This function checks the current clock configurations and then calculates | ||
396 | * the clock frequency for a specific clock name defined in clock_name_t. | ||
397 | * The ICS must be properly configured before using this function. | ||
398 | * | ||
399 | * @param clockName Clock names defined in clock_name_t | ||
400 | * @return Clock frequency value in Hertz | ||
401 | */ | ||
402 | uint32_t CLOCK_GetFreq(clock_name_t clockName); | ||
403 | |||
404 | /*! | ||
405 | * @brief Get the core clock or system clock frequency. | ||
406 | * | ||
407 | * @return Clock frequency in Hz. | ||
408 | */ | ||
409 | uint32_t CLOCK_GetCoreSysClkFreq(void); | ||
410 | |||
411 | /*! | ||
412 | * @brief Get the bus clock frequency. | ||
413 | * | ||
414 | * @return Clock frequency in Hz. | ||
415 | */ | ||
416 | uint32_t CLOCK_GetBusClkFreq(void); | ||
417 | |||
418 | /*! | ||
419 | * @brief Get the flash clock frequency. | ||
420 | * | ||
421 | * @return Clock frequency in Hz. | ||
422 | */ | ||
423 | uint32_t CLOCK_GetFlashClkFreq(void); | ||
424 | |||
425 | /*! | ||
426 | * @brief Get the OSC0 external reference clock frequency (OSC0ERCLK). | ||
427 | * | ||
428 | * @return Clock frequency in Hz. | ||
429 | */ | ||
430 | uint32_t CLOCK_GetOsc0ErClkFreq(void); | ||
431 | |||
432 | /*! | ||
433 | * @brief Set the clock configure in SIM module. | ||
434 | * | ||
435 | * This function sets system layer clock settings in SIM module. | ||
436 | * | ||
437 | * @param config Pointer to the configure structure. | ||
438 | */ | ||
439 | void CLOCK_SetSimConfig(sim_clock_config_t const *config); | ||
440 | |||
441 | /*! | ||
442 | * @brief Set the system clock dividers in SIM to safe value. | ||
443 | * | ||
444 | * The system level clocks (core clock, bus clock, and flash clock) | ||
445 | * must be in allowed ranges. During ICS clock mode switch, the ICS output clock | ||
446 | * changes then the system level clocks may be out of range. This function could | ||
447 | * be used before ICS mode change, to make sure system level clocks are in allowed | ||
448 | * range. | ||
449 | * | ||
450 | */ | ||
451 | static inline void CLOCK_SetSimSafeDivs(void) | ||
452 | { | ||
453 | SIM->BUSDIV = 0x1U; | ||
454 | } | ||
455 | |||
456 | /*! @name ICS frequency functions. */ | ||
457 | /*@{*/ | ||
458 | |||
459 | /*! | ||
460 | * @brief Gets the ICS output clock (ICSOUTCLK) frequency. | ||
461 | * | ||
462 | * This function gets the ICS output clock frequency in Hz based on the current ICS | ||
463 | * register value. | ||
464 | * | ||
465 | * @return The frequency of ICSOUTCLK. | ||
466 | */ | ||
467 | uint32_t CLOCK_GetICSOutClkFreq(void); | ||
468 | |||
469 | /*! | ||
470 | * @brief Gets the ICS FLL clock (ICSFLLCLK) frequency. | ||
471 | * | ||
472 | * This function gets the ICS FLL clock frequency in Hz based on the current ICS | ||
473 | * register value. The FLL is enabled in FEI/FBI/FEE/FBE mode and | ||
474 | * disabled in low power state in other modes. | ||
475 | * | ||
476 | * @return The frequency of ICSFLLCLK. | ||
477 | */ | ||
478 | uint32_t CLOCK_GetFllFreq(void); | ||
479 | |||
480 | /*! | ||
481 | * @brief Gets the ICS internal reference clock (ICSIRCLK) frequency. | ||
482 | * | ||
483 | * This function gets the ICS internal reference clock frequency in Hz based | ||
484 | * on the current ICS register value. | ||
485 | * | ||
486 | * @return The frequency of ICSIRCLK. | ||
487 | */ | ||
488 | uint32_t CLOCK_GetInternalRefClkFreq(void); | ||
489 | |||
490 | /*! | ||
491 | * @brief Gets the ICS fixed frequency clock (ICSFFCLK) frequency. | ||
492 | * | ||
493 | * This function gets the ICS fixed frequency clock frequency in Hz based | ||
494 | * on the current ICS register value. | ||
495 | * | ||
496 | * @return The frequency of ICSFFCLK. | ||
497 | */ | ||
498 | uint32_t CLOCK_GetICSFixedFreqClkFreq(void); | ||
499 | |||
500 | /*@}*/ | ||
501 | |||
502 | /*! @name ICS clock configuration. */ | ||
503 | /*@{*/ | ||
504 | |||
505 | /*! | ||
506 | * @brief Enables or disables the ICS low power. | ||
507 | * | ||
508 | * Enabling the ICS low power disables the PLL and FLL in bypass modes. In other words, | ||
509 | * in FBE and PBE modes, enabling low power sets the ICS to BELP mode. In FBI and | ||
510 | * PBI modes, enabling low power sets the ICS to BILP mode. | ||
511 | * When disabling the ICS low power, the PLL or FLL are enabled based on ICS settings. | ||
512 | * | ||
513 | * @param enable True to enable ICS low power, false to disable ICS low power. | ||
514 | */ | ||
515 | static inline void CLOCK_SetLowPowerEnable(bool enable) | ||
516 | { | ||
517 | if (enable) | ||
518 | { | ||
519 | ICS->C2 |= ICS_C2_LP_MASK; | ||
520 | } | ||
521 | else | ||
522 | { | ||
523 | ICS->C2 &= (uint8_t)(~ICS_C2_LP_MASK); | ||
524 | } | ||
525 | } | ||
526 | |||
527 | /*! | ||
528 | * @brief Configures the Internal Reference clock (ICSIRCLK). | ||
529 | * | ||
530 | * This function sets the ICSIRCLK base on parameters. | ||
531 | * This function also sets whether the \c ICSIRCLK is enabled in stop mode. | ||
532 | * | ||
533 | * @param enableMode ICSIRCLK enable mode, OR'ed value of _ICS_irclk_enable_mode. | ||
534 | * @retval kStatus_ICS_SourceUsed Because the internal reference clock is used as a clock source, | ||
535 | * the configuration should not be changed. Otherwise, a glitch occurs. | ||
536 | * @retval kStatus_Success ICSIRCLK configuration finished successfully. | ||
537 | */ | ||
538 | static inline void CLOCK_SetInternalRefClkConfig(uint8_t enableMode) | ||
539 | { | ||
540 | /* Set internal reference clock selection. */ | ||
541 | ICS->C1 = (uint8_t)((ICS->C1 & ~(ICS_C1_IRCLKEN_MASK | ICS_C1_IREFSTEN_MASK)) | (uint8_t)enableMode); | ||
542 | } | ||
543 | |||
544 | /*! | ||
545 | * @brief Set the FLL external reference clock divider value. | ||
546 | * | ||
547 | * Sets the FLL external reference clock divider value, the register ICS_C1[RDIV]. | ||
548 | * Resulting frequency must be in the range 31.25KHZ to 39.0625KHZ. | ||
549 | * | ||
550 | * @param rdiv The FLL external reference clock divider value, ICS_C1[RDIV]. | ||
551 | */ | ||
552 | static inline void CLOCK_SetFllExtRefDiv(uint8_t rdiv) | ||
553 | { | ||
554 | ICS->C1 = (uint8_t)((ICS->C1 & ~ICS_C1_RDIV_MASK) | ICS_C1_RDIV(rdiv)); | ||
555 | } | ||
556 | |||
557 | /*@}*/ | ||
558 | |||
559 | /*! @name ICS clock lock monitor functions. */ | ||
560 | /*@{*/ | ||
561 | |||
562 | /*! | ||
563 | * @brief Sets the OSC0 clock monitor mode. | ||
564 | * | ||
565 | * This function sets the OSC0 clock monitor mode. See ics_monitor_mode_t for details. | ||
566 | * | ||
567 | * @param enable True to enable clock monitor, false to disable clock monitor. | ||
568 | */ | ||
569 | static inline void CLOCK_SetOsc0MonitorMode(bool enable) | ||
570 | { | ||
571 | if (enable) | ||
572 | { | ||
573 | ICS->C4 |= ICS_C4_CME_MASK; | ||
574 | } | ||
575 | else | ||
576 | { | ||
577 | ICS->C4 &= (uint8_t)(~ICS_C4_CME_MASK); | ||
578 | } | ||
579 | } | ||
580 | |||
581 | /*@}*/ | ||
582 | |||
583 | /*! | ||
584 | * @name OSC configuration | ||
585 | * @{ | ||
586 | */ | ||
587 | |||
588 | /*! | ||
589 | * @brief Initializes the OSC0. | ||
590 | * | ||
591 | * This function initializes the OSC0 according to the board configuration. | ||
592 | * | ||
593 | * @param config Pointer to the OSC0 configuration structure. | ||
594 | */ | ||
595 | void CLOCK_InitOsc0(osc_config_t const *config); | ||
596 | |||
597 | /*! | ||
598 | * @brief Deinitializes the OSC0. | ||
599 | * | ||
600 | * This function deinitializes the OSC0. | ||
601 | */ | ||
602 | void CLOCK_DeinitOsc0(void); | ||
603 | |||
604 | /* @} */ | ||
605 | |||
606 | /*! | ||
607 | * @name External clock frequency | ||
608 | * @{ | ||
609 | */ | ||
610 | |||
611 | /*! | ||
612 | * @brief Sets the XTAL0 frequency based on board settings. | ||
613 | * | ||
614 | * @param freq The XTAL0/EXTAL0 input clock frequency in Hz. | ||
615 | */ | ||
616 | static inline void CLOCK_SetXtal0Freq(uint32_t freq) | ||
617 | { | ||
618 | g_xtal0Freq = freq; | ||
619 | } | ||
620 | |||
621 | /*! | ||
622 | * @brief Sets the OSC enable. | ||
623 | * | ||
624 | * @param enable osc enable mode. | ||
625 | */ | ||
626 | static inline void CLOCK_SetOsc0Enable(uint8_t enable) | ||
627 | { | ||
628 | OSC0->CR |= (uint8_t)((OSC0->CR & (~(OSC_CR_OSCSTEN_MASK | OSC_CR_OSCEN_MASK))) | enable); | ||
629 | } | ||
630 | |||
631 | /* @} */ | ||
632 | |||
633 | /*! @name ICS mode functions. */ | ||
634 | /*@{*/ | ||
635 | |||
636 | /*! | ||
637 | * @brief Gets the current ICS mode. | ||
638 | * | ||
639 | * This function checks the ICS registers and determines the current ICS mode. | ||
640 | * | ||
641 | * @return Current ICS mode or error code; See @ref ics_mode_t. | ||
642 | */ | ||
643 | ics_mode_t CLOCK_GetMode(void); | ||
644 | |||
645 | /*! | ||
646 | * @brief Sets the ICS to FEI mode. | ||
647 | * | ||
648 | * This function sets the ICS to FEI mode. If setting to FEI mode fails | ||
649 | * from the current mode, this function returns an error. | ||
650 | * | ||
651 | * @param bDiv bus clock divider | ||
652 | * @retval kStatus_ICS_ModeUnreachable Could not switch to the target mode. | ||
653 | * @retval kStatus_Success Switched to the target mode successfully. | ||
654 | */ | ||
655 | status_t CLOCK_SetFeiMode(uint8_t bDiv); | ||
656 | |||
657 | /*! | ||
658 | * @brief Sets the ICS to FEE mode. | ||
659 | * | ||
660 | * This function sets the ICS to FEE mode. If setting to FEE mode fails | ||
661 | * from the current mode, this function returns an error. | ||
662 | * | ||
663 | * @param bDiv bus clock divider | ||
664 | * @param rDiv FLL reference clock divider setting, RDIV. | ||
665 | * | ||
666 | * @retval kStatus_ICS_ModeUnreachable Could not switch to the target mode. | ||
667 | * @retval kStatus_Success Switched to the target mode successfully. | ||
668 | */ | ||
669 | status_t CLOCK_SetFeeMode(uint8_t bDiv, uint8_t rDiv); | ||
670 | |||
671 | /*! | ||
672 | * @brief Sets the ICS to FBI mode. | ||
673 | * | ||
674 | * This function sets the ICS to FBI mode. If setting to FBI mode fails | ||
675 | * from the current mode, this function returns an error. | ||
676 | * | ||
677 | * @param bDiv bus clock divider | ||
678 | * @retval kStatus_ICS_ModeUnreachable Could not switch to the target mode. | ||
679 | * @retval kStatus_Success Switched to the target mode successfully.s | ||
680 | */ | ||
681 | status_t CLOCK_SetFbiMode(uint8_t bDiv); | ||
682 | |||
683 | /*! | ||
684 | * @brief Sets the ICS to FBE mode. | ||
685 | * | ||
686 | * This function sets the ICS to FBE mode. If setting to FBE mode fails | ||
687 | * from the current mode, this function returns an error. | ||
688 | * | ||
689 | * @param bDiv bus clock divider | ||
690 | * @param rDiv FLL reference clock divider setting, RDIV. | ||
691 | * | ||
692 | * @retval kStatus_ICS_ModeUnreachable Could not switch to the target mode. | ||
693 | * @retval kStatus_Success Switched to the target mode successfully. | ||
694 | */ | ||
695 | status_t CLOCK_SetFbeMode(uint8_t bDiv, uint8_t rDiv); | ||
696 | |||
697 | /*! | ||
698 | * @brief Sets the ICS to BILP mode. | ||
699 | * | ||
700 | * This function sets the ICS to BILP mode. If setting to BILP mode fails | ||
701 | * from the current mode, this function returns an error. | ||
702 | * | ||
703 | * @param bDiv bus clock divider | ||
704 | * @retval kStatus_ICS_ModeUnreachable Could not switch to the target mode. | ||
705 | * @retval kStatus_Success Switched to the target mode successfully. | ||
706 | */ | ||
707 | status_t CLOCK_SetBilpMode(uint8_t bDiv); | ||
708 | |||
709 | /*! | ||
710 | * @brief Sets the ICS to BELP mode. | ||
711 | * | ||
712 | * This function sets the ICS to BELP mode. If setting to BELP mode fails | ||
713 | * from the current mode, this function returns an error. | ||
714 | * | ||
715 | * @param bDiv bus clock divider | ||
716 | * @retval kStatus_ICS_ModeUnreachable Could not switch to the target mode. | ||
717 | * @retval kStatus_Success Switched to the target mode successfully. | ||
718 | */ | ||
719 | status_t CLOCK_SetBelpMode(uint8_t bDiv); | ||
720 | |||
721 | /*! | ||
722 | * @brief Sets the ICS to FEI mode during system boot up. | ||
723 | * | ||
724 | * This function sets the ICS to FEI mode from the reset mode. It can also be used to | ||
725 | * set up ICS during system boot up. | ||
726 | * | ||
727 | * @param bDiv bus clock divider. | ||
728 | * | ||
729 | * @retval kStatus_ICS_ModeUnreachable Could not switch to the target mode. | ||
730 | * @retval kStatus_Success Switched to the target mode successfully. | ||
731 | */ | ||
732 | status_t CLOCK_BootToFeiMode(uint8_t bDiv); | ||
733 | |||
734 | /*! | ||
735 | * @brief Sets the ICS to FEE mode during system bootup. | ||
736 | * | ||
737 | * This function sets ICS to FEE mode from the reset mode. It can also be used to | ||
738 | * set up the ICS during system boot up. | ||
739 | * | ||
740 | * @param bDiv bus clock divider. | ||
741 | * @param rDiv FLL reference clock divider setting, RDIV. | ||
742 | * | ||
743 | * @retval kStatus_ICS_ModeUnreachable Could not switch to the target mode. | ||
744 | * @retval kStatus_Success Switched to the target mode successfully. | ||
745 | */ | ||
746 | status_t CLOCK_BootToFeeMode(uint8_t bDiv, uint8_t rDiv); | ||
747 | |||
748 | /*! | ||
749 | * @brief Sets the ICS to BILP mode during system boot up. | ||
750 | * | ||
751 | * This function sets the ICS to BILP mode from the reset mode. It can also be used to | ||
752 | * set up the ICS during system boot up. | ||
753 | * | ||
754 | * @param bDiv bus clock divider. | ||
755 | * @retval kStatus_ICS_SourceUsed Could not change ICSIRCLK setting. | ||
756 | * @retval kStatus_Success Switched to the target mode successfully. | ||
757 | */ | ||
758 | status_t CLOCK_BootToBilpMode(uint8_t bDiv); | ||
759 | |||
760 | /*! | ||
761 | * @brief Sets the ICS to BELP mode during system boot up. | ||
762 | * | ||
763 | * This function sets the ICS to BELP mode from the reset mode. It can also be used to | ||
764 | * set up the ICS during system boot up. | ||
765 | * | ||
766 | * @param bDiv bus clock divider. | ||
767 | * @retval kStatus_ICS_ModeUnreachable Could not switch to the target mode. | ||
768 | * @retval kStatus_Success Switched to the target mode successfully. | ||
769 | */ | ||
770 | status_t CLOCK_BootToBelpMode(uint8_t bDiv); | ||
771 | |||
772 | /*! | ||
773 | * @brief Sets the ICS to a target mode. | ||
774 | * | ||
775 | * This function sets ICS to a target mode defined by the configuration | ||
776 | * structure. If switching to the target mode fails, this function | ||
777 | * chooses the correct path. | ||
778 | * | ||
779 | * @param config Pointer to the target ICS mode configuration structure. | ||
780 | * @return Return kStatus_Success if switched successfully; Otherwise, it returns an error code _ICS_status. | ||
781 | * | ||
782 | * @note If the external clock is used in the target mode, ensure that it is | ||
783 | * enabled. For example, if the OSC0 is used, set up OSC0 correctly before calling this | ||
784 | * function. | ||
785 | */ | ||
786 | status_t CLOCK_SetIcsConfig(ics_config_t const *config); | ||
787 | |||
788 | /*@}*/ | ||
789 | |||
790 | #if defined(__cplusplus) | ||
791 | } | ||
792 | #endif /* __cplusplus */ | ||
793 | |||
794 | /*! @} */ | ||
795 | |||
796 | #endif /* _FSL_CLOCK_H_ */ | ||