aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/ext/mcux-sdk/devices/MK63F12/drivers/fsl_clock.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/ext/mcux-sdk/devices/MK63F12/drivers/fsl_clock.h')
-rw-r--r--lib/chibios-contrib/ext/mcux-sdk/devices/MK63F12/drivers/fsl_clock.h1561
1 files changed, 1561 insertions, 0 deletions
diff --git a/lib/chibios-contrib/ext/mcux-sdk/devices/MK63F12/drivers/fsl_clock.h b/lib/chibios-contrib/ext/mcux-sdk/devices/MK63F12/drivers/fsl_clock.h
new file mode 100644
index 000000000..ad0a7d467
--- /dev/null
+++ b/lib/chibios-contrib/ext/mcux-sdk/devices/MK63F12/drivers/fsl_clock.h
@@ -0,0 +1,1561 @@
1/*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016 - 2019, NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#ifndef _FSL_CLOCK_H_
10#define _FSL_CLOCK_H_
11
12#include "fsl_common.h"
13
14/*! @addtogroup clock */
15/*! @{ */
16
17/*! @file */
18
19/*******************************************************************************
20 * Configurations
21 ******************************************************************************/
22
23/*! @brief Configures whether to check a parameter in a function.
24 *
25 * Some MCG settings must be changed with conditions, for example:
26 * 1. MCGIRCLK settings, such as the source, divider, and the trim value should not change when
27 * MCGIRCLK is used as a system clock source.
28 * 2. MCG_C7[OSCSEL] should not be changed when the external reference clock is used
29 * as a system clock source. For example, in FBE/BLPE/PBE modes.
30 * 3. The users should only switch between the supported clock modes.
31 *
32 * MCG functions check the parameter and MCG status before setting, if not allowed
33 * to change, the functions return error. The parameter checking increases code size,
34 * if code size is a critical requirement, change #MCG_CONFIG_CHECK_PARAM to 0 to
35 * disable parameter checking.
36 */
37#ifndef MCG_CONFIG_CHECK_PARAM
38#define MCG_CONFIG_CHECK_PARAM 0U
39#endif
40
41/*! @brief Configure whether driver controls clock
42 *
43 * When set to 0, peripheral drivers will enable clock in initialize function
44 * and disable clock in de-initialize function. When set to 1, peripheral
45 * driver will not control the clock, application could control the clock out of
46 * the driver.
47 *
48 * @note All drivers share this feature switcher. If it is set to 1, application
49 * should handle clock enable and disable for all drivers.
50 */
51#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL))
52#define FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL 0
53#endif
54
55/*******************************************************************************
56 * Definitions
57 ******************************************************************************/
58
59/*! @name Driver version */
60/*@{*/
61/*! @brief CLOCK driver version 2.5.1. */
62#define FSL_CLOCK_DRIVER_VERSION (MAKE_VERSION(2, 5, 1))
63/*@}*/
64
65/*! @brief External XTAL0 (OSC0) clock frequency.
66 *
67 * The XTAL0/EXTAL0 (OSC0) clock frequency in Hz. When the clock is set up, use the
68 * function CLOCK_SetXtal0Freq to set the value in the clock driver. For example,
69 * if XTAL0 is 8 MHz:
70 * @code
71 * Set up the OSC0
72 * CLOCK_InitOsc0(...);
73 * Set the XTAL0 value to the clock driver.
74 * CLOCK_SetXtal0Freq(80000000);
75 * @endcode
76 *
77 * This is important for the multicore platforms where only one core needs to set up the
78 * OSC0 using the CLOCK_InitOsc0. All other cores need to call the CLOCK_SetXtal0Freq
79 * to get a valid clock frequency.
80 */
81extern volatile uint32_t g_xtal0Freq;
82
83/*! @brief External XTAL32/EXTAL32/RTC_CLKIN clock frequency.
84 *
85 * The XTAL32/EXTAL32/RTC_CLKIN clock frequency in Hz. When the clock is set up, use the
86 * function CLOCK_SetXtal32Freq to set the value in the clock driver.
87 *
88 * This is important for the multicore platforms where only one core needs to set up
89 * the clock. All other cores need to call the CLOCK_SetXtal32Freq
90 * to get a valid clock frequency.
91 */
92extern volatile uint32_t g_xtal32Freq;
93
94/*! @brief IRC48M clock frequency in Hz. */
95#define MCG_INTERNAL_IRC_48M 48000000U
96
97#if (defined(OSC) && !(defined(OSC0)))
98#define OSC0 OSC
99#endif
100
101/* Definition for delay API in clock driver, users can redefine it to the real application. */
102#ifndef SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY
103#define SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY (120000000UL)
104#endif
105
106/*! @brief Clock ip name array for DMAMUX. */
107#define DMAMUX_CLOCKS \
108 { \
109 kCLOCK_Dmamux0 \
110 }
111
112/*! @brief Clock ip name array for RTC. */
113#define RTC_CLOCKS \
114 { \
115 kCLOCK_Rtc0 \
116 }
117
118/*! @brief Clock ip name array for ENET. */
119#define ENET_CLOCKS \
120 { \
121 kCLOCK_Enet0 \
122 }
123
124/*! @brief Clock ip name array for PORT. */
125#define PORT_CLOCKS \
126 { \
127 kCLOCK_PortA, kCLOCK_PortB, kCLOCK_PortC, kCLOCK_PortD, kCLOCK_PortE \
128 }
129
130/*! @brief Clock ip name array for SAI. */
131#define SAI_CLOCKS \
132 { \
133 kCLOCK_Sai0 \
134 }
135
136/*! @brief Clock ip name array for FLEXBUS. */
137#define FLEXBUS_CLOCKS \
138 { \
139 kCLOCK_Flexbus0 \
140 }
141
142/*! @brief Clock ip name array for EWM. */
143#define EWM_CLOCKS \
144 { \
145 kCLOCK_Ewm0 \
146 }
147
148/*! @brief Clock ip name array for PIT. */
149#define PIT_CLOCKS \
150 { \
151 kCLOCK_Pit0 \
152 }
153
154/*! @brief Clock ip name array for DSPI. */
155#define DSPI_CLOCKS \
156 { \
157 kCLOCK_Spi0, kCLOCK_Spi1, kCLOCK_Spi2 \
158 }
159
160/*! @brief Clock ip name array for LPTMR. */
161#define LPTMR_CLOCKS \
162 { \
163 kCLOCK_Lptmr0 \
164 }
165
166/*! @brief Clock ip name array for SDHC. */
167#define SDHC_CLOCKS \
168 { \
169 kCLOCK_Sdhc0 \
170 }
171
172/*! @brief Clock ip name array for FTM. */
173#define FTM_CLOCKS \
174 { \
175 kCLOCK_Ftm0, kCLOCK_Ftm1, kCLOCK_Ftm2, kCLOCK_Ftm3 \
176 }
177
178/*! @brief Clock ip name array for EDMA. */
179#define EDMA_CLOCKS \
180 { \
181 kCLOCK_Dma0 \
182 }
183
184/*! @brief Clock ip name array for FLEXCAN. */
185#define FLEXCAN_CLOCKS \
186 { \
187 kCLOCK_Flexcan0 \
188 }
189
190/*! @brief Clock ip name array for DAC. */
191#define DAC_CLOCKS \
192 { \
193 kCLOCK_Dac0, kCLOCK_Dac1 \
194 }
195
196/*! @brief Clock ip name array for ADC16. */
197#define ADC16_CLOCKS \
198 { \
199 kCLOCK_Adc0, kCLOCK_Adc1 \
200 }
201
202/*! @brief Clock ip name array for MPU. */
203#define SYSMPU_CLOCKS \
204 { \
205 kCLOCK_Sysmpu0 \
206 }
207
208/*! @brief Clock ip name array for VREF. */
209#define VREF_CLOCKS \
210 { \
211 kCLOCK_Vref0 \
212 }
213
214/*! @brief Clock ip name array for CMT. */
215#define CMT_CLOCKS \
216 { \
217 kCLOCK_Cmt0 \
218 }
219
220/*! @brief Clock ip name array for UART. */
221#define UART_CLOCKS \
222 { \
223 kCLOCK_Uart0, kCLOCK_Uart1, kCLOCK_Uart2, kCLOCK_Uart3, kCLOCK_Uart4, kCLOCK_Uart5 \
224 }
225
226/*! @brief Clock ip name array for RNGA. */
227#define RNGA_CLOCKS \
228 { \
229 kCLOCK_Rnga0 \
230 }
231
232/*! @brief Clock ip name array for CRC. */
233#define CRC_CLOCKS \
234 { \
235 kCLOCK_Crc0 \
236 }
237
238/*! @brief Clock ip name array for I2C. */
239#define I2C_CLOCKS \
240 { \
241 kCLOCK_I2c0, kCLOCK_I2c1, kCLOCK_I2c2 \
242 }
243
244/*! @brief Clock ip name array for PDB. */
245#define PDB_CLOCKS \
246 { \
247 kCLOCK_Pdb0 \
248 }
249
250/*! @brief Clock ip name array for FTF. */
251#define FTF_CLOCKS \
252 { \
253 kCLOCK_Ftf0 \
254 }
255
256/*! @brief Clock ip name array for CMP. */
257#define CMP_CLOCKS \
258 { \
259 kCLOCK_Cmp0, kCLOCK_Cmp1, kCLOCK_Cmp2 \
260 }
261
262/*!
263 * @brief LPO clock frequency.
264 */
265#define LPO_CLK_FREQ 1000U
266
267/*! @brief Peripherals clock source definition. */
268#define SYS_CLK kCLOCK_CoreSysClk
269#define BUS_CLK kCLOCK_BusClk
270
271#define I2C0_CLK_SRC BUS_CLK
272#define I2C1_CLK_SRC BUS_CLK
273#define I2C2_CLK_SRC BUS_CLK
274#define DSPI0_CLK_SRC BUS_CLK
275#define DSPI1_CLK_SRC BUS_CLK
276#define DSPI2_CLK_SRC BUS_CLK
277#define UART0_CLK_SRC SYS_CLK
278#define UART1_CLK_SRC SYS_CLK
279#define UART2_CLK_SRC BUS_CLK
280#define UART3_CLK_SRC BUS_CLK
281#define UART4_CLK_SRC BUS_CLK
282#define UART5_CLK_SRC BUS_CLK
283
284/*! @brief Clock name used to get clock frequency. */
285typedef enum _clock_name
286{
287
288 /* ----------------------------- System layer clock -------------------------------*/
289 kCLOCK_CoreSysClk, /*!< Core/system clock */
290 kCLOCK_PlatClk, /*!< Platform clock */
291 kCLOCK_BusClk, /*!< Bus clock */
292 kCLOCK_FlexBusClk, /*!< FlexBus clock */
293 kCLOCK_FlashClk, /*!< Flash clock */
294 kCLOCK_FastPeriphClk, /*!< Fast peripheral clock */
295 kCLOCK_PllFllSelClk, /*!< The clock after SIM[PLLFLLSEL]. */
296
297 /* ---------------------------------- OSC clock -----------------------------------*/
298 kCLOCK_Er32kClk, /*!< External reference 32K clock (ERCLK32K) */
299 kCLOCK_Osc0ErClk, /*!< OSC0 external reference clock (OSC0ERCLK) */
300 kCLOCK_Osc1ErClk, /*!< OSC1 external reference clock (OSC1ERCLK) */
301 kCLOCK_Osc0ErClkUndiv, /*!< OSC0 external reference undivided clock(OSC0ERCLK_UNDIV). */
302
303 /* ----------------------------- MCG and MCG-Lite clock ---------------------------*/
304 kCLOCK_McgFixedFreqClk, /*!< MCG fixed frequency clock (MCGFFCLK) */
305 kCLOCK_McgInternalRefClk, /*!< MCG internal reference clock (MCGIRCLK) */
306 kCLOCK_McgFllClk, /*!< MCGFLLCLK */
307 kCLOCK_McgPll0Clk, /*!< MCGPLL0CLK */
308 kCLOCK_McgPll1Clk, /*!< MCGPLL1CLK */
309 kCLOCK_McgExtPllClk, /*!< EXT_PLLCLK */
310 kCLOCK_McgPeriphClk, /*!< MCG peripheral clock (MCGPCLK) */
311 kCLOCK_McgIrc48MClk, /*!< MCG IRC48M clock */
312
313 /* --------------------------------- Other clock ----------------------------------*/
314 kCLOCK_LpoClk, /*!< LPO clock */
315
316} clock_name_t;
317
318/*! @brief USB clock source definition. */
319typedef enum _clock_usb_src
320{
321 kCLOCK_UsbSrcPll0 = SIM_SOPT2_USBSRC(1U) | SIM_SOPT2_PLLFLLSEL(1U), /*!< Use PLL0. */
322 kCLOCK_UsbSrcIrc48M = SIM_SOPT2_USBSRC(1U) | SIM_SOPT2_PLLFLLSEL(3U), /*!< Use IRC48M. */
323 kCLOCK_UsbSrcExt = SIM_SOPT2_USBSRC(0U) /*!< Use USB_CLKIN. */
324} clock_usb_src_t;
325
326/*------------------------------------------------------------------------------
327
328 clock_gate_t definition:
329
330 31 16 0
331 -----------------------------------------------------------------
332 | SIM_SCGC register offset | control bit offset in SCGC |
333 -----------------------------------------------------------------
334
335 For example, the SDHC clock gate is controlled by SIM_SCGC3[17], the
336 SIM_SCGC3 offset in SIM is 0x1030, then kCLOCK_GateSdhc0 is defined as
337
338 kCLOCK_GateSdhc0 = (0x1030 << 16) | 17;
339
340------------------------------------------------------------------------------*/
341
342#define CLK_GATE_REG_OFFSET_SHIFT 16U
343#define CLK_GATE_REG_OFFSET_MASK 0xFFFF0000U
344#define CLK_GATE_BIT_SHIFT_SHIFT 0U
345#define CLK_GATE_BIT_SHIFT_MASK 0x0000FFFFU
346
347#define CLK_GATE_DEFINE(reg_offset, bit_shift) \
348 ((((reg_offset) << CLK_GATE_REG_OFFSET_SHIFT) & CLK_GATE_REG_OFFSET_MASK) | \
349 (((bit_shift) << CLK_GATE_BIT_SHIFT_SHIFT) & CLK_GATE_BIT_SHIFT_MASK))
350
351#define CLK_GATE_ABSTRACT_REG_OFFSET(x) (((x)&CLK_GATE_REG_OFFSET_MASK) >> CLK_GATE_REG_OFFSET_SHIFT)
352#define CLK_GATE_ABSTRACT_BITS_SHIFT(x) (((x)&CLK_GATE_BIT_SHIFT_MASK) >> CLK_GATE_BIT_SHIFT_SHIFT)
353
354/*! @brief Clock gate name used for CLOCK_EnableClock/CLOCK_DisableClock. */
355typedef enum _clock_ip_name
356{
357 kCLOCK_IpInvalid = 0U,
358 kCLOCK_I2c2 = CLK_GATE_DEFINE(0x1028U, 6U),
359 kCLOCK_Uart4 = CLK_GATE_DEFINE(0x1028U, 10U),
360 kCLOCK_Uart5 = CLK_GATE_DEFINE(0x1028U, 11U),
361
362 kCLOCK_Enet0 = CLK_GATE_DEFINE(0x102CU, 0U),
363 kCLOCK_Dac0 = CLK_GATE_DEFINE(0x102CU, 12U),
364 kCLOCK_Dac1 = CLK_GATE_DEFINE(0x102CU, 13U),
365
366 kCLOCK_Spi2 = CLK_GATE_DEFINE(0x1030U, 12U),
367 kCLOCK_Sdhc0 = CLK_GATE_DEFINE(0x1030U, 17U),
368 kCLOCK_Ftm3 = CLK_GATE_DEFINE(0x1030U, 25U),
369 kCLOCK_Adc1 = CLK_GATE_DEFINE(0x1030U, 27U),
370
371 kCLOCK_Ewm0 = CLK_GATE_DEFINE(0x1034U, 1U),
372 kCLOCK_Cmt0 = CLK_GATE_DEFINE(0x1034U, 2U),
373 kCLOCK_I2c0 = CLK_GATE_DEFINE(0x1034U, 6U),
374 kCLOCK_I2c1 = CLK_GATE_DEFINE(0x1034U, 7U),
375 kCLOCK_Uart0 = CLK_GATE_DEFINE(0x1034U, 10U),
376 kCLOCK_Uart1 = CLK_GATE_DEFINE(0x1034U, 11U),
377 kCLOCK_Uart2 = CLK_GATE_DEFINE(0x1034U, 12U),
378 kCLOCK_Uart3 = CLK_GATE_DEFINE(0x1034U, 13U),
379 kCLOCK_Usbfs0 = CLK_GATE_DEFINE(0x1034U, 18U),
380 kCLOCK_Cmp0 = CLK_GATE_DEFINE(0x1034U, 19U),
381 kCLOCK_Cmp1 = CLK_GATE_DEFINE(0x1034U, 19U),
382 kCLOCK_Cmp2 = CLK_GATE_DEFINE(0x1034U, 19U),
383 kCLOCK_Vref0 = CLK_GATE_DEFINE(0x1034U, 20U),
384
385 kCLOCK_Lptmr0 = CLK_GATE_DEFINE(0x1038U, 0U),
386 kCLOCK_PortA = CLK_GATE_DEFINE(0x1038U, 9U),
387 kCLOCK_PortB = CLK_GATE_DEFINE(0x1038U, 10U),
388 kCLOCK_PortC = CLK_GATE_DEFINE(0x1038U, 11U),
389 kCLOCK_PortD = CLK_GATE_DEFINE(0x1038U, 12U),
390 kCLOCK_PortE = CLK_GATE_DEFINE(0x1038U, 13U),
391
392 kCLOCK_Ftf0 = CLK_GATE_DEFINE(0x103CU, 0U),
393 kCLOCK_Dmamux0 = CLK_GATE_DEFINE(0x103CU, 1U),
394 kCLOCK_Flexcan0 = CLK_GATE_DEFINE(0x103CU, 4U),
395 kCLOCK_Rnga0 = CLK_GATE_DEFINE(0x103CU, 9U),
396 kCLOCK_Spi0 = CLK_GATE_DEFINE(0x103CU, 12U),
397 kCLOCK_Spi1 = CLK_GATE_DEFINE(0x103CU, 13U),
398 kCLOCK_Sai0 = CLK_GATE_DEFINE(0x103CU, 15U),
399 kCLOCK_Crc0 = CLK_GATE_DEFINE(0x103CU, 18U),
400 kCLOCK_Usbdcd0 = CLK_GATE_DEFINE(0x103CU, 21U),
401 kCLOCK_Pdb0 = CLK_GATE_DEFINE(0x103CU, 22U),
402 kCLOCK_Pit0 = CLK_GATE_DEFINE(0x103CU, 23U),
403 kCLOCK_Ftm0 = CLK_GATE_DEFINE(0x103CU, 24U),
404 kCLOCK_Ftm1 = CLK_GATE_DEFINE(0x103CU, 25U),
405 kCLOCK_Ftm2 = CLK_GATE_DEFINE(0x103CU, 26U),
406 kCLOCK_Adc0 = CLK_GATE_DEFINE(0x103CU, 27U),
407 kCLOCK_Rtc0 = CLK_GATE_DEFINE(0x103CU, 29U),
408
409 kCLOCK_Flexbus0 = CLK_GATE_DEFINE(0x1040U, 0U),
410 kCLOCK_Dma0 = CLK_GATE_DEFINE(0x1040U, 1U),
411 kCLOCK_Sysmpu0 = CLK_GATE_DEFINE(0x1040U, 2U),
412} clock_ip_name_t;
413
414/*!@brief SIM configuration structure for clock setting. */
415typedef struct _sim_clock_config
416{
417 uint8_t pllFllSel; /*!< PLL/FLL/IRC48M selection. */
418 uint8_t er32kSrc; /*!< ERCLK32K source selection. */
419 uint32_t clkdiv1; /*!< SIM_CLKDIV1. */
420} sim_clock_config_t;
421
422/*! @brief OSC work mode. */
423typedef enum _osc_mode
424{
425 kOSC_ModeExt = 0U, /*!< Use an external clock. */
426#if (defined(MCG_C2_EREFS_MASK) && !(defined(MCG_C2_EREFS0_MASK)))
427 kOSC_ModeOscLowPower = MCG_C2_EREFS_MASK, /*!< Oscillator low power. */
428#else
429 kOSC_ModeOscLowPower = MCG_C2_EREFS0_MASK, /*!< Oscillator low power. */
430#endif
431 kOSC_ModeOscHighGain = 0U
432#if (defined(MCG_C2_EREFS_MASK) && !(defined(MCG_C2_EREFS0_MASK)))
433 | MCG_C2_EREFS_MASK
434#else
435 | MCG_C2_EREFS0_MASK
436#endif
437#if (defined(MCG_C2_HGO_MASK) && !(defined(MCG_C2_HGO0_MASK)))
438 | MCG_C2_HGO_MASK, /*!< Oscillator high gain. */
439#else
440 | MCG_C2_HGO0_MASK, /*!< Oscillator high gain. */
441#endif
442} osc_mode_t;
443
444/*! @brief Oscillator capacitor load setting.*/
445enum _osc_cap_load
446{
447 kOSC_Cap2P = OSC_CR_SC2P_MASK, /*!< 2 pF capacitor load */
448 kOSC_Cap4P = OSC_CR_SC4P_MASK, /*!< 4 pF capacitor load */
449 kOSC_Cap8P = OSC_CR_SC8P_MASK, /*!< 8 pF capacitor load */
450 kOSC_Cap16P = OSC_CR_SC16P_MASK /*!< 16 pF capacitor load */
451};
452
453/*! @brief OSCERCLK enable mode. */
454enum _oscer_enable_mode
455{
456 kOSC_ErClkEnable = OSC_CR_ERCLKEN_MASK, /*!< Enable. */
457 kOSC_ErClkEnableInStop = OSC_CR_EREFSTEN_MASK /*!< Enable in stop mode. */
458};
459
460/*! @brief OSC configuration for OSCERCLK. */
461typedef struct _oscer_config
462{
463 uint8_t enableMode; /*!< OSCERCLK enable mode. OR'ed value of @ref _oscer_enable_mode. */
464
465} oscer_config_t;
466
467/*!
468 * @brief OSC Initialization Configuration Structure
469 *
470 * Defines the configuration data structure to initialize the OSC.
471 * When porting to a new board, set the following members
472 * according to the board setting:
473 * 1. freq: The external frequency.
474 * 2. workMode: The OSC module mode.
475 */
476typedef struct _osc_config
477{
478 uint32_t freq; /*!< External clock frequency. */
479 uint8_t capLoad; /*!< Capacitor load setting. */
480 osc_mode_t workMode; /*!< OSC work mode setting. */
481 oscer_config_t oscerConfig; /*!< Configuration for OSCERCLK. */
482} osc_config_t;
483
484/*! @brief MCG FLL reference clock source select. */
485typedef enum _mcg_fll_src
486{
487 kMCG_FllSrcExternal, /*!< External reference clock is selected */
488 kMCG_FllSrcInternal /*!< The slow internal reference clock is selected */
489} mcg_fll_src_t;
490
491/*! @brief MCG internal reference clock select */
492typedef enum _mcg_irc_mode
493{
494 kMCG_IrcSlow, /*!< Slow internal reference clock selected */
495 kMCG_IrcFast /*!< Fast internal reference clock selected */
496} mcg_irc_mode_t;
497
498/*! @brief MCG DCO Maximum Frequency with 32.768 kHz Reference */
499typedef enum _mcg_dmx32
500{
501 kMCG_Dmx32Default, /*!< DCO has a default range of 25% */
502 kMCG_Dmx32Fine /*!< DCO is fine-tuned for maximum frequency with 32.768 kHz reference */
503} mcg_dmx32_t;
504
505/*! @brief MCG DCO range select */
506typedef enum _mcg_drs
507{
508 kMCG_DrsLow, /*!< Low frequency range */
509 kMCG_DrsMid, /*!< Mid frequency range */
510 kMCG_DrsMidHigh, /*!< Mid-High frequency range */
511 kMCG_DrsHigh /*!< High frequency range */
512} mcg_drs_t;
513
514/*! @brief MCG PLL reference clock select */
515typedef enum _mcg_pll_ref_src
516{
517 kMCG_PllRefOsc0, /*!< Selects OSC0 as PLL reference clock */
518 kMCG_PllRefOsc1 /*!< Selects OSC1 as PLL reference clock */
519} mcg_pll_ref_src_t;
520
521/*! @brief MCGOUT clock source. */
522typedef enum _mcg_clkout_src
523{
524 kMCG_ClkOutSrcOut, /*!< Output of the FLL is selected (reset default) */
525 kMCG_ClkOutSrcInternal, /*!< Internal reference clock is selected */
526 kMCG_ClkOutSrcExternal, /*!< External reference clock is selected */
527} mcg_clkout_src_t;
528
529/*! @brief MCG Automatic Trim Machine Select */
530typedef enum _mcg_atm_select
531{
532 kMCG_AtmSel32k, /*!< 32 kHz Internal Reference Clock selected */
533 kMCG_AtmSel4m /*!< 4 MHz Internal Reference Clock selected */
534} mcg_atm_select_t;
535
536/*! @brief MCG OSC Clock Select */
537typedef enum _mcg_oscsel
538{
539 kMCG_OscselOsc, /*!< Selects System Oscillator (OSCCLK) */
540 kMCG_OscselRtc, /*!< Selects 32 kHz RTC Oscillator */
541 kMCG_OscselIrc /*!< Selects 48 MHz IRC Oscillator */
542} mcg_oscsel_t;
543
544/*! @brief MCG PLLCS select */
545typedef enum _mcg_pll_clk_select
546{
547 kMCG_PllClkSelPll0, /*!< PLL0 output clock is selected */
548 kMCG_PllClkSelPll1 /* PLL1 output clock is selected */
549} mcg_pll_clk_select_t;
550
551/*! @brief MCG clock monitor mode. */
552typedef enum _mcg_monitor_mode
553{
554 kMCG_MonitorNone, /*!< Clock monitor is disabled. */
555 kMCG_MonitorInt, /*!< Trigger interrupt when clock lost. */
556 kMCG_MonitorReset /*!< System reset when clock lost. */
557} mcg_monitor_mode_t;
558
559/*! @brief MCG status. */
560enum
561{
562 kStatus_MCG_ModeUnreachable = MAKE_STATUS(kStatusGroup_MCG, 0U), /*!< Can't switch to target mode. */
563 kStatus_MCG_ModeInvalid = MAKE_STATUS(kStatusGroup_MCG, 1U), /*!< Current mode invalid for the specific
564 function. */
565 kStatus_MCG_AtmBusClockInvalid = MAKE_STATUS(kStatusGroup_MCG, 2U), /*!< Invalid bus clock for ATM. */
566 kStatus_MCG_AtmDesiredFreqInvalid = MAKE_STATUS(kStatusGroup_MCG, 3U), /*!< Invalid desired frequency for ATM. */
567 kStatus_MCG_AtmIrcUsed = MAKE_STATUS(kStatusGroup_MCG, 4U), /*!< IRC is used when using ATM. */
568 kStatus_MCG_AtmHardwareFail = MAKE_STATUS(kStatusGroup_MCG, 5U), /*!< Hardware fail occurs during ATM. */
569 kStatus_MCG_SourceUsed = MAKE_STATUS(kStatusGroup_MCG, 6U) /*!< Can't change the clock source because
570 it is in use. */
571};
572
573/*! @brief MCG status flags. */
574enum
575{
576 kMCG_Osc0LostFlag = (1U << 0U), /*!< OSC0 lost. */
577 kMCG_Osc0InitFlag = (1U << 1U), /*!< OSC0 crystal initialized. */
578 kMCG_RtcOscLostFlag = (1U << 4U), /*!< RTC OSC lost. */
579 kMCG_Pll0LostFlag = (1U << 5U), /*!< PLL0 lost. */
580 kMCG_Pll0LockFlag = (1U << 6U), /*!< PLL0 locked. */
581};
582
583/*! @brief MCG internal reference clock (MCGIRCLK) enable mode definition. */
584enum
585{
586 kMCG_IrclkEnable = MCG_C1_IRCLKEN_MASK, /*!< MCGIRCLK enable. */
587 kMCG_IrclkEnableInStop = MCG_C1_IREFSTEN_MASK /*!< MCGIRCLK enable in stop mode. */
588};
589
590/*! @brief MCG PLL clock enable mode definition. */
591enum
592{
593 kMCG_PllEnableIndependent = MCG_C5_PLLCLKEN0_MASK, /*!< MCGPLLCLK enable independent of the
594 MCG clock mode. Generally, the PLL
595 is disabled in FLL modes
596 (FEI/FBI/FEE/FBE). Setting the PLL clock
597 enable independent, enables the
598 PLL in the FLL modes. */
599 kMCG_PllEnableInStop = MCG_C5_PLLSTEN0_MASK /*!< MCGPLLCLK enable in STOP mode. */
600};
601
602/*! @brief MCG mode definitions */
603typedef enum _mcg_mode
604{
605 kMCG_ModeFEI = 0U, /*!< FEI - FLL Engaged Internal */
606 kMCG_ModeFBI, /*!< FBI - FLL Bypassed Internal */
607 kMCG_ModeBLPI, /*!< BLPI - Bypassed Low Power Internal */
608 kMCG_ModeFEE, /*!< FEE - FLL Engaged External */
609 kMCG_ModeFBE, /*!< FBE - FLL Bypassed External */
610 kMCG_ModeBLPE, /*!< BLPE - Bypassed Low Power External */
611 kMCG_ModePBE, /*!< PBE - PLL Bypassed External */
612 kMCG_ModePEE, /*!< PEE - PLL Engaged External */
613 kMCG_ModeError /*!< Unknown mode */
614} mcg_mode_t;
615
616/*! @brief MCG PLL configuration. */
617typedef struct _mcg_pll_config
618{
619 uint8_t enableMode; /*!< Enable mode. OR'ed value of @ref _mcg_pll_enable_mode. */
620 uint8_t prdiv; /*!< Reference divider PRDIV. */
621 uint8_t vdiv; /*!< VCO divider VDIV. */
622} mcg_pll_config_t;
623
624/*! @brief MCG mode change configuration structure
625 *
626 * When porting to a new board, set the following members
627 * according to the board setting:
628 * 1. frdiv: If the FLL uses the external reference clock, set this
629 * value to ensure that the external reference clock divided by frdiv is
630 * in the 31.25 kHz to 39.0625 kHz range.
631 * 2. The PLL reference clock divider PRDIV: PLL reference clock frequency after
632 * PRDIV should be in the FSL_FEATURE_MCG_PLL_REF_MIN to
633 * FSL_FEATURE_MCG_PLL_REF_MAX range.
634 */
635typedef struct _mcg_config
636{
637 mcg_mode_t mcgMode; /*!< MCG mode. */
638
639 /* ----------------------- MCGIRCCLK settings ------------------------ */
640 uint8_t irclkEnableMode; /*!< MCGIRCLK enable mode. */
641 mcg_irc_mode_t ircs; /*!< Source, MCG_C2[IRCS]. */
642 uint8_t fcrdiv; /*!< Divider, MCG_SC[FCRDIV]. */
643
644 /* ------------------------ MCG FLL settings ------------------------- */
645 uint8_t frdiv; /*!< Divider MCG_C1[FRDIV]. */
646 mcg_drs_t drs; /*!< DCO range MCG_C4[DRST_DRS]. */
647 mcg_dmx32_t dmx32; /*!< MCG_C4[DMX32]. */
648 mcg_oscsel_t oscsel; /*!< OSC select MCG_C7[OSCSEL]. */
649
650 /* ------------------------ MCG PLL settings ------------------------- */
651 mcg_pll_config_t pll0Config; /*!< MCGPLL0CLK configuration. */
652
653} mcg_config_t;
654
655/*******************************************************************************
656 * API
657 ******************************************************************************/
658
659#if defined(__cplusplus)
660extern "C" {
661#endif /* __cplusplus */
662
663/*!
664 * @brief Enable the clock for specific IP.
665 *
666 * @param name Which clock to enable, see \ref clock_ip_name_t.
667 */
668static inline void CLOCK_EnableClock(clock_ip_name_t name)
669{
670 uint32_t regAddr = SIM_BASE + CLK_GATE_ABSTRACT_REG_OFFSET((uint32_t)name);
671 (*(volatile uint32_t *)regAddr) |= (1UL << CLK_GATE_ABSTRACT_BITS_SHIFT((uint32_t)name));
672}
673
674/*!
675 * @brief Disable the clock for specific IP.
676 *
677 * @param name Which clock to disable, see \ref clock_ip_name_t.
678 */
679static inline void CLOCK_DisableClock(clock_ip_name_t name)
680{
681 uint32_t regAddr = SIM_BASE + CLK_GATE_ABSTRACT_REG_OFFSET((uint32_t)name);
682 (*(volatile uint32_t *)regAddr) &= ~(1UL << CLK_GATE_ABSTRACT_BITS_SHIFT((uint32_t)name));
683}
684
685/*!
686 * @brief Set ERCLK32K source.
687 *
688 * @param src The value to set ERCLK32K clock source.
689 */
690static inline void CLOCK_SetEr32kClock(uint32_t src)
691{
692 SIM->SOPT1 = ((SIM->SOPT1 & ~SIM_SOPT1_OSC32KSEL_MASK) | SIM_SOPT1_OSC32KSEL(src));
693}
694
695/*!
696 * @brief Set SDHC0 clock source.
697 *
698 * @param src The value to set SDHC0 clock source.
699 */
700static inline void CLOCK_SetSdhc0Clock(uint32_t src)
701{
702 SIM->SOPT2 = ((SIM->SOPT2 & ~SIM_SOPT2_SDHCSRC_MASK) | SIM_SOPT2_SDHCSRC(src));
703}
704
705/*!
706 * @brief Set enet timestamp clock source.
707 *
708 * @param src The value to set enet timestamp clock source.
709 */
710static inline void CLOCK_SetEnetTime0Clock(uint32_t src)
711{
712 SIM->SOPT2 = ((SIM->SOPT2 & ~SIM_SOPT2_TIMESRC_MASK) | SIM_SOPT2_TIMESRC(src));
713}
714
715/*!
716 * @brief Set RMII clock source.
717 *
718 * @param src The value to set RMII clock source.
719 */
720static inline void CLOCK_SetRmii0Clock(uint32_t src)
721{
722 SIM->SOPT2 = ((SIM->SOPT2 & ~SIM_SOPT2_RMIISRC_MASK) | SIM_SOPT2_RMIISRC(src));
723}
724
725/*!
726 * @brief Set debug trace clock source.
727 *
728 * @param src The value to set debug trace clock source.
729 */
730static inline void CLOCK_SetTraceClock(uint32_t src)
731{
732 SIM->SOPT2 = ((SIM->SOPT2 & ~SIM_SOPT2_TRACECLKSEL_MASK) | SIM_SOPT2_TRACECLKSEL(src));
733}
734
735/*!
736 * @brief Set PLLFLLSEL clock source.
737 *
738 * @param src The value to set PLLFLLSEL clock source.
739 */
740static inline void CLOCK_SetPllFllSelClock(uint32_t src)
741{
742 SIM->SOPT2 = ((SIM->SOPT2 & ~SIM_SOPT2_PLLFLLSEL_MASK) | SIM_SOPT2_PLLFLLSEL(src));
743}
744
745/*!
746 * @brief Set CLKOUT source.
747 *
748 * @param src The value to set CLKOUT source.
749 */
750static inline void CLOCK_SetClkOutClock(uint32_t src)
751{
752 SIM->SOPT2 = ((SIM->SOPT2 & ~SIM_SOPT2_CLKOUTSEL_MASK) | SIM_SOPT2_CLKOUTSEL(src));
753}
754
755/*!
756 * @brief Set RTC_CLKOUT source.
757 *
758 * @param src The value to set RTC_CLKOUT source.
759 */
760static inline void CLOCK_SetRtcClkOutClock(uint32_t src)
761{
762 SIM->SOPT2 = ((SIM->SOPT2 & ~SIM_SOPT2_RTCCLKOUTSEL_MASK) | SIM_SOPT2_RTCCLKOUTSEL(src));
763}
764
765/*! @brief Enable USB FS clock.
766 *
767 * @param src USB FS clock source.
768 * @param freq The frequency specified by src.
769 * @retval true The clock is set successfully.
770 * @retval false The clock source is invalid to get proper USB FS clock.
771 */
772bool CLOCK_EnableUsbfs0Clock(clock_usb_src_t src, uint32_t freq);
773
774/*! @brief Disable USB FS clock.
775 *
776 * Disable USB FS clock.
777 */
778static inline void CLOCK_DisableUsbfs0Clock(void)
779{
780 CLOCK_DisableClock(kCLOCK_Usbfs0);
781}
782
783/*!
784 * @brief System clock divider
785 *
786 * Set the SIM_CLKDIV1[OUTDIV1], SIM_CLKDIV1[OUTDIV2], SIM_CLKDIV1[OUTDIV3], SIM_CLKDIV1[OUTDIV4].
787 *
788 * @param outdiv1 Clock 1 output divider value.
789 *
790 * @param outdiv2 Clock 2 output divider value.
791 *
792 * @param outdiv3 Clock 3 output divider value.
793 *
794 * @param outdiv4 Clock 4 output divider value.
795 */
796static inline void CLOCK_SetOutDiv(uint32_t outdiv1, uint32_t outdiv2, uint32_t outdiv3, uint32_t outdiv4)
797{
798 SIM->CLKDIV1 = SIM_CLKDIV1_OUTDIV1(outdiv1) | SIM_CLKDIV1_OUTDIV2(outdiv2) | SIM_CLKDIV1_OUTDIV3(outdiv3) |
799 SIM_CLKDIV1_OUTDIV4(outdiv4);
800}
801
802/*!
803 * @brief Gets the clock frequency for a specific clock name.
804 *
805 * This function checks the current clock configurations and then calculates
806 * the clock frequency for a specific clock name defined in clock_name_t.
807 * The MCG must be properly configured before using this function.
808 *
809 * @param clockName Clock names defined in clock_name_t
810 * @return Clock frequency value in Hertz
811 */
812uint32_t CLOCK_GetFreq(clock_name_t clockName);
813
814/*!
815 * @brief Get the core clock or system clock frequency.
816 *
817 * @return Clock frequency in Hz.
818 */
819uint32_t CLOCK_GetCoreSysClkFreq(void);
820
821/*!
822 * @brief Get the bus clock frequency.
823 *
824 * @return Clock frequency in Hz.
825 */
826uint32_t CLOCK_GetBusClkFreq(void);
827
828/*!
829 * @brief Get the flexbus clock frequency.
830 *
831 * @return Clock frequency in Hz.
832 */
833uint32_t CLOCK_GetFlexBusClkFreq(void);
834
835/*!
836 * @brief Get the flash clock frequency.
837 *
838 * @return Clock frequency in Hz.
839 */
840uint32_t CLOCK_GetFlashClkFreq(void);
841
842/*!
843 * @brief Get the output clock frequency selected by SIM[PLLFLLSEL].
844 *
845 * @return Clock frequency in Hz.
846 */
847uint32_t CLOCK_GetPllFllSelClkFreq(void);
848
849/*!
850 * @brief Get the external reference 32K clock frequency (ERCLK32K).
851 *
852 * @return Clock frequency in Hz.
853 */
854uint32_t CLOCK_GetEr32kClkFreq(void);
855
856/*!
857 * @brief Get the OSC0 external reference clock frequency (OSC0ERCLK).
858 *
859 * @return Clock frequency in Hz.
860 */
861uint32_t CLOCK_GetOsc0ErClkFreq(void);
862
863/*!
864 * @brief Set the clock configure in SIM module.
865 *
866 * This function sets system layer clock settings in SIM module.
867 *
868 * @param config Pointer to the configure structure.
869 */
870void CLOCK_SetSimConfig(sim_clock_config_t const *config);
871
872/*!
873 * @brief Set the system clock dividers in SIM to safe value.
874 *
875 * The system level clocks (core clock, bus clock, flexbus clock and flash clock)
876 * must be in allowed ranges. During MCG clock mode switch, the MCG output clock
877 * changes then the system level clocks may be out of range. This function could
878 * be used before MCG mode change, to make sure system level clocks are in allowed
879 * range.
880 *
881 * @param config Pointer to the configure structure.
882 */
883static inline void CLOCK_SetSimSafeDivs(void)
884{
885 SIM->CLKDIV1 = 0x01240000U;
886}
887
888/*! @name MCG frequency functions. */
889/*@{*/
890
891/*!
892 * @brief Gets the MCG output clock (MCGOUTCLK) frequency.
893 *
894 * This function gets the MCG output clock frequency in Hz based on the current MCG
895 * register value.
896 *
897 * @return The frequency of MCGOUTCLK.
898 */
899uint32_t CLOCK_GetOutClkFreq(void);
900
901/*!
902 * @brief Gets the MCG FLL clock (MCGFLLCLK) frequency.
903 *
904 * This function gets the MCG FLL clock frequency in Hz based on the current MCG
905 * register value. The FLL is enabled in FEI/FBI/FEE/FBE mode and
906 * disabled in low power state in other modes.
907 *
908 * @return The frequency of MCGFLLCLK.
909 */
910uint32_t CLOCK_GetFllFreq(void);
911
912/*!
913 * @brief Gets the MCG internal reference clock (MCGIRCLK) frequency.
914 *
915 * This function gets the MCG internal reference clock frequency in Hz based
916 * on the current MCG register value.
917 *
918 * @return The frequency of MCGIRCLK.
919 */
920uint32_t CLOCK_GetInternalRefClkFreq(void);
921
922/*!
923 * @brief Gets the MCG fixed frequency clock (MCGFFCLK) frequency.
924 *
925 * This function gets the MCG fixed frequency clock frequency in Hz based
926 * on the current MCG register value.
927 *
928 * @return The frequency of MCGFFCLK.
929 */
930uint32_t CLOCK_GetFixedFreqClkFreq(void);
931
932/*!
933 * @brief Gets the MCG PLL0 clock (MCGPLL0CLK) frequency.
934 *
935 * This function gets the MCG PLL0 clock frequency in Hz based on the current MCG
936 * register value.
937 *
938 * @return The frequency of MCGPLL0CLK.
939 */
940uint32_t CLOCK_GetPll0Freq(void);
941
942/*@}*/
943
944/*! @name MCG clock configuration. */
945/*@{*/
946
947/*!
948 * @brief Enables or disables the MCG low power.
949 *
950 * Enabling the MCG low power disables the PLL and FLL in bypass modes. In other words,
951 * in FBE and PBE modes, enabling low power sets the MCG to BLPE mode. In FBI and
952 * PBI modes, enabling low power sets the MCG to BLPI mode.
953 * When disabling the MCG low power, the PLL or FLL are enabled based on MCG settings.
954 *
955 * @param enable True to enable MCG low power, false to disable MCG low power.
956 */
957static inline void CLOCK_SetLowPowerEnable(bool enable)
958{
959 if (enable)
960 {
961 MCG->C2 |= MCG_C2_LP_MASK;
962 }
963 else
964 {
965 MCG->C2 &= ~(uint8_t)MCG_C2_LP_MASK;
966 }
967}
968
969/*!
970 * @brief Configures the Internal Reference clock (MCGIRCLK).
971 *
972 * This function sets the \c MCGIRCLK base on parameters. It also selects the IRC
973 * source. If the fast IRC is used, this function sets the fast IRC divider.
974 * This function also sets whether the \c MCGIRCLK is enabled in stop mode.
975 * Calling this function in FBI/PBI/BLPI modes may change the system clock. As a result,
976 * using the function in these modes it is not allowed.
977 *
978 * @param enableMode MCGIRCLK enable mode, OR'ed value of @ref _mcg_irclk_enable_mode.
979 * @param ircs MCGIRCLK clock source, choose fast or slow.
980 * @param fcrdiv Fast IRC divider setting (\c FCRDIV).
981 * @retval kStatus_MCG_SourceUsed Because the internal reference clock is used as a clock source,
982 * the configuration should not be changed. Otherwise, a glitch occurs.
983 * @retval kStatus_Success MCGIRCLK configuration finished successfully.
984 */
985status_t CLOCK_SetInternalRefClkConfig(uint8_t enableMode, mcg_irc_mode_t ircs, uint8_t fcrdiv);
986
987/*!
988 * @brief Selects the MCG external reference clock.
989 *
990 * Selects the MCG external reference clock source, changes the MCG_C7[OSCSEL],
991 * and waits for the clock source to be stable. Because the external reference
992 * clock should not be changed in FEE/FBE/BLPE/PBE/PEE modes, do not call this function in these modes.
993 *
994 * @param oscsel MCG external reference clock source, MCG_C7[OSCSEL].
995 * @retval kStatus_MCG_SourceUsed Because the external reference clock is used as a clock source,
996 * the configuration should not be changed. Otherwise, a glitch occurs.
997 * @retval kStatus_Success External reference clock set successfully.
998 */
999status_t CLOCK_SetExternalRefClkConfig(mcg_oscsel_t oscsel);
1000
1001/*!
1002 * @brief Set the FLL external reference clock divider value.
1003 *
1004 * Sets the FLL external reference clock divider value, the register MCG_C1[FRDIV].
1005 *
1006 * @param frdiv The FLL external reference clock divider value, MCG_C1[FRDIV].
1007 */
1008static inline void CLOCK_SetFllExtRefDiv(uint8_t frdiv)
1009{
1010 MCG->C1 = (uint8_t)((MCG->C1 & ~MCG_C1_FRDIV_MASK) | MCG_C1_FRDIV(frdiv));
1011}
1012
1013/*!
1014 * @brief Enables the PLL0 in FLL mode.
1015 *
1016 * This function sets us the PLL0 in FLL mode and reconfigures
1017 * the PLL0. Ensure that the PLL reference
1018 * clock is enabled before calling this function and that the PLL0 is not used as a clock source.
1019 * The function CLOCK_CalcPllDiv gets the correct PLL
1020 * divider values.
1021 *
1022 * @param config Pointer to the configuration structure.
1023 */
1024void CLOCK_EnablePll0(mcg_pll_config_t const *config);
1025
1026/*!
1027 * @brief Disables the PLL0 in FLL mode.
1028 *
1029 * This function disables the PLL0 in FLL mode. It should be used together with the
1030 * @ref CLOCK_EnablePll0.
1031 */
1032static inline void CLOCK_DisablePll0(void)
1033{
1034 MCG->C5 &= (uint8_t)(~(MCG_C5_PLLCLKEN0_MASK | MCG_C5_PLLSTEN0_MASK));
1035}
1036
1037/*!
1038 * @brief Calculates the PLL divider setting for a desired output frequency.
1039 *
1040 * This function calculates the correct reference clock divider (\c PRDIV) and
1041 * VCO divider (\c VDIV) to generate a desired PLL output frequency. It returns the
1042 * closest frequency match with the corresponding \c PRDIV/VDIV
1043 * returned from parameters. If a desired frequency is not valid, this function
1044 * returns 0.
1045 *
1046 * @param refFreq PLL reference clock frequency.
1047 * @param desireFreq Desired PLL output frequency.
1048 * @param prdiv PRDIV value to generate desired PLL frequency.
1049 * @param vdiv VDIV value to generate desired PLL frequency.
1050 * @return Closest frequency match that the PLL was able generate.
1051 */
1052uint32_t CLOCK_CalcPllDiv(uint32_t refFreq, uint32_t desireFreq, uint8_t *prdiv, uint8_t *vdiv);
1053
1054/*@}*/
1055
1056/*! @name MCG clock lock monitor functions. */
1057/*@{*/
1058
1059/*!
1060 * @brief Sets the OSC0 clock monitor mode.
1061 *
1062 * This function sets the OSC0 clock monitor mode. See @ref mcg_monitor_mode_t for details.
1063 *
1064 * @param mode Monitor mode to set.
1065 */
1066void CLOCK_SetOsc0MonitorMode(mcg_monitor_mode_t mode);
1067
1068/*!
1069 * @brief Sets the RTC OSC clock monitor mode.
1070 *
1071 * This function sets the RTC OSC clock monitor mode. See @ref mcg_monitor_mode_t for details.
1072 *
1073 * @param mode Monitor mode to set.
1074 */
1075void CLOCK_SetRtcOscMonitorMode(mcg_monitor_mode_t mode);
1076
1077/*!
1078 * @brief Sets the PLL0 clock monitor mode.
1079 *
1080 * This function sets the PLL0 clock monitor mode. See @ref mcg_monitor_mode_t for details.
1081 *
1082 * @param mode Monitor mode to set.
1083 */
1084void CLOCK_SetPll0MonitorMode(mcg_monitor_mode_t mode);
1085
1086/*!
1087 * @brief Gets the MCG status flags.
1088 *
1089 * This function gets the MCG clock status flags. All status flags are
1090 * returned as a logical OR of the enumeration @ref _mcg_status_flags_t. To
1091 * check a specific flag, compare the return value with the flag.
1092 *
1093 * Example:
1094 * @code
1095 * To check the clock lost lock status of OSC0 and PLL0.
1096 * uint32_t mcgFlags;
1097 *
1098 * mcgFlags = CLOCK_GetStatusFlags();
1099 *
1100 * if (mcgFlags & kMCG_Osc0LostFlag)
1101 * {
1102 * OSC0 clock lock lost. Do something.
1103 * }
1104 * if (mcgFlags & kMCG_Pll0LostFlag)
1105 * {
1106 * PLL0 clock lock lost. Do something.
1107 * }
1108 * @endcode
1109 *
1110 * @return Logical OR value of the @ref _mcg_status_flags_t.
1111 */
1112uint32_t CLOCK_GetStatusFlags(void);
1113
1114/*!
1115 * @brief Clears the MCG status flags.
1116 *
1117 * This function clears the MCG clock lock lost status. The parameter is a logical
1118 * OR value of the flags to clear. See @ref _mcg_status_flags_t.
1119 *
1120 * Example:
1121 * @code
1122 * To clear the clock lost lock status flags of OSC0 and PLL0.
1123 *
1124 * CLOCK_ClearStatusFlags(kMCG_Osc0LostFlag | kMCG_Pll0LostFlag);
1125 * @endcode
1126 *
1127 * @param mask The status flags to clear. This is a logical OR of members of the
1128 * enumeration @ref _mcg_status_flags_t.
1129 */
1130void CLOCK_ClearStatusFlags(uint32_t mask);
1131
1132/*@}*/
1133
1134/*!
1135 * @name OSC configuration
1136 * @{
1137 */
1138
1139/*!
1140 * @brief Configures the OSC external reference clock (OSCERCLK).
1141 *
1142 * This function configures the OSC external reference clock (OSCERCLK).
1143 * This is an example to enable the OSCERCLK in normal and stop modes and also set
1144 * the output divider to 1:
1145 *
1146 @code
1147 oscer_config_t config =
1148 {
1149 .enableMode = kOSC_ErClkEnable | kOSC_ErClkEnableInStop,
1150 .erclkDiv = 1U,
1151 };
1152
1153 OSC_SetExtRefClkConfig(OSC, &config);
1154 @endcode
1155 *
1156 * @param base OSC peripheral address.
1157 * @param config Pointer to the configuration structure.
1158 */
1159static inline void OSC_SetExtRefClkConfig(OSC_Type *base, oscer_config_t const *config)
1160{
1161 uint8_t reg = base->CR;
1162
1163 reg &= (uint8_t)(~(OSC_CR_ERCLKEN_MASK | OSC_CR_EREFSTEN_MASK));
1164 reg |= config->enableMode;
1165
1166 base->CR = reg;
1167}
1168
1169/*!
1170 * @brief Sets the capacitor load configuration for the oscillator.
1171 *
1172 * This function sets the specified capacitors configuration for the oscillator.
1173 * This should be done in the early system level initialization function call
1174 * based on the system configuration.
1175 *
1176 * @param base OSC peripheral address.
1177 * @param capLoad OR'ed value for the capacitor load option, see \ref _osc_cap_load.
1178 *
1179 * Example:
1180 @code
1181 To enable only 2 pF and 8 pF capacitor load, please use like this.
1182 OSC_SetCapLoad(OSC, kOSC_Cap2P | kOSC_Cap8P);
1183 @endcode
1184 */
1185static inline void OSC_SetCapLoad(OSC_Type *base, uint8_t capLoad)
1186{
1187 uint8_t reg = base->CR;
1188
1189 reg &= (uint8_t)(~(OSC_CR_SC2P_MASK | OSC_CR_SC4P_MASK | OSC_CR_SC8P_MASK | OSC_CR_SC16P_MASK));
1190 reg |= capLoad;
1191
1192 base->CR = reg;
1193}
1194
1195/*!
1196 * @brief Initializes the OSC0.
1197 *
1198 * This function initializes the OSC0 according to the board configuration.
1199 *
1200 * @param config Pointer to the OSC0 configuration structure.
1201 */
1202void CLOCK_InitOsc0(osc_config_t const *config);
1203
1204/*!
1205 * @brief Deinitializes the OSC0.
1206 *
1207 * This function deinitializes the OSC0.
1208 */
1209void CLOCK_DeinitOsc0(void);
1210
1211/* @} */
1212
1213/*!
1214 * @name External clock frequency
1215 * @{
1216 */
1217
1218/*!
1219 * @brief Sets the XTAL0 frequency based on board settings.
1220 *
1221 * @param freq The XTAL0/EXTAL0 input clock frequency in Hz.
1222 */
1223static inline void CLOCK_SetXtal0Freq(uint32_t freq)
1224{
1225 g_xtal0Freq = freq;
1226}
1227
1228/*!
1229 * @brief Sets the XTAL32/RTC_CLKIN frequency based on board settings.
1230 *
1231 * @param freq The XTAL32/EXTAL32/RTC_CLKIN input clock frequency in Hz.
1232 */
1233static inline void CLOCK_SetXtal32Freq(uint32_t freq)
1234{
1235 g_xtal32Freq = freq;
1236}
1237/* @} */
1238
1239/*!
1240 * @name IRCs frequency
1241 * @{
1242 */
1243
1244/*!
1245 * @brief Set the Slow IRC frequency based on the trimmed value
1246 *
1247 * @param freq The Slow IRC frequency input clock frequency in Hz.
1248 */
1249void CLOCK_SetSlowIrcFreq(uint32_t freq);
1250
1251/*!
1252 * @brief Set the Fast IRC frequency based on the trimmed value
1253 *
1254 * @param freq The Fast IRC frequency input clock frequency in Hz.
1255 */
1256void CLOCK_SetFastIrcFreq(uint32_t freq);
1257/* @} */
1258
1259/*!
1260 * @name MCG auto-trim machine.
1261 * @{
1262 */
1263
1264/*!
1265 * @brief Auto trims the internal reference clock.
1266 *
1267 * This function trims the internal reference clock by using the external clock. If
1268 * successful, it returns the kStatus_Success and the frequency after
1269 * trimming is received in the parameter @p actualFreq. If an error occurs,
1270 * the error code is returned.
1271 *
1272 * @param extFreq External clock frequency, which should be a bus clock.
1273 * @param desireFreq Frequency to trim to.
1274 * @param actualFreq Actual frequency after trimming.
1275 * @param atms Trim fast or slow internal reference clock.
1276 * @retval kStatus_Success ATM success.
1277 * @retval kStatus_MCG_AtmBusClockInvalid The bus clock is not in allowed range for the ATM.
1278 * @retval kStatus_MCG_AtmDesiredFreqInvalid MCGIRCLK could not be trimmed to the desired frequency.
1279 * @retval kStatus_MCG_AtmIrcUsed Could not trim because MCGIRCLK is used as a bus clock source.
1280 * @retval kStatus_MCG_AtmHardwareFail Hardware fails while trimming.
1281 */
1282status_t CLOCK_TrimInternalRefClk(uint32_t extFreq, uint32_t desireFreq, uint32_t *actualFreq, mcg_atm_select_t atms);
1283/* @} */
1284
1285/*! @name MCG mode functions. */
1286/*@{*/
1287
1288/*!
1289 * @brief Gets the current MCG mode.
1290 *
1291 * This function checks the MCG registers and determines the current MCG mode.
1292 *
1293 * @return Current MCG mode or error code; See @ref mcg_mode_t.
1294 */
1295mcg_mode_t CLOCK_GetMode(void);
1296
1297/*!
1298 * @brief Sets the MCG to FEI mode.
1299 *
1300 * This function sets the MCG to FEI mode. If setting to FEI mode fails
1301 * from the current mode, this function returns an error.
1302 *
1303 * @param dmx32 DMX32 in FEI mode.
1304 * @param drs The DCO range selection.
1305 * @param fllStableDelay Delay function to ensure that the FLL is stable. Passing
1306 * NULL does not cause a delay.
1307 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1308 * @retval kStatus_Success Switched to the target mode successfully.
1309 * @note If @p dmx32 is set to kMCG_Dmx32Fine, the slow IRC must not be trimmed
1310 * to a frequency above 32768 Hz.
1311 */
1312status_t CLOCK_SetFeiMode(mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void));
1313
1314/*!
1315 * @brief Sets the MCG to FEE mode.
1316 *
1317 * This function sets the MCG to FEE mode. If setting to FEE mode fails
1318 * from the current mode, this function returns an error.
1319 *
1320 * @param frdiv FLL reference clock divider setting, FRDIV.
1321 * @param dmx32 DMX32 in FEE mode.
1322 * @param drs The DCO range selection.
1323 * @param fllStableDelay Delay function to make sure FLL is stable. Passing
1324 * NULL does not cause a delay.
1325 *
1326 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1327 * @retval kStatus_Success Switched to the target mode successfully.
1328 */
1329status_t CLOCK_SetFeeMode(uint8_t frdiv, mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void));
1330
1331/*!
1332 * @brief Sets the MCG to FBI mode.
1333 *
1334 * This function sets the MCG to FBI mode. If setting to FBI mode fails
1335 * from the current mode, this function returns an error.
1336 *
1337 * @param dmx32 DMX32 in FBI mode.
1338 * @param drs The DCO range selection.
1339 * @param fllStableDelay Delay function to make sure FLL is stable. If the FLL
1340 * is not used in FBI mode, this parameter can be NULL. Passing
1341 * NULL does not cause a delay.
1342 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1343 * @retval kStatus_Success Switched to the target mode successfully.
1344 * @note If @p dmx32 is set to kMCG_Dmx32Fine, the slow IRC must not be trimmed
1345 * to frequency above 32768 Hz.
1346 */
1347status_t CLOCK_SetFbiMode(mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void));
1348
1349/*!
1350 * @brief Sets the MCG to FBE mode.
1351 *
1352 * This function sets the MCG to FBE mode. If setting to FBE mode fails
1353 * from the current mode, this function returns an error.
1354 *
1355 * @param frdiv FLL reference clock divider setting, FRDIV.
1356 * @param dmx32 DMX32 in FBE mode.
1357 * @param drs The DCO range selection.
1358 * @param fllStableDelay Delay function to make sure FLL is stable. If the FLL
1359 * is not used in FBE mode, this parameter can be NULL. Passing NULL
1360 * does not cause a delay.
1361 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1362 * @retval kStatus_Success Switched to the target mode successfully.
1363 */
1364status_t CLOCK_SetFbeMode(uint8_t frdiv, mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void));
1365
1366/*!
1367 * @brief Sets the MCG to BLPI mode.
1368 *
1369 * This function sets the MCG to BLPI mode. If setting to BLPI mode fails
1370 * from the current mode, this function returns an error.
1371 *
1372 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1373 * @retval kStatus_Success Switched to the target mode successfully.
1374 */
1375status_t CLOCK_SetBlpiMode(void);
1376
1377/*!
1378 * @brief Sets the MCG to BLPE mode.
1379 *
1380 * This function sets the MCG to BLPE mode. If setting to BLPE mode fails
1381 * from the current mode, this function returns an error.
1382 *
1383 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1384 * @retval kStatus_Success Switched to the target mode successfully.
1385 */
1386status_t CLOCK_SetBlpeMode(void);
1387
1388/*!
1389 * @brief Sets the MCG to PBE mode.
1390 *
1391 * This function sets the MCG to PBE mode. If setting to PBE mode fails
1392 * from the current mode, this function returns an error.
1393 *
1394 * @param pllcs The PLL selection, PLLCS.
1395 * @param config Pointer to the PLL configuration.
1396 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1397 * @retval kStatus_Success Switched to the target mode successfully.
1398 *
1399 * @note
1400 * 1. The parameter \c pllcs selects the PLL. For platforms with
1401 * only one PLL, the parameter pllcs is kept for interface compatibility.
1402 * 2. The parameter \c config is the PLL configuration structure. On some
1403 * platforms, it is possible to choose the external PLL directly, which renders the
1404 * configuration structure not necessary. In this case, pass in NULL.
1405 * For example: CLOCK_SetPbeMode(kMCG_OscselOsc, kMCG_PllClkSelExtPll, NULL);
1406 */
1407status_t CLOCK_SetPbeMode(mcg_pll_clk_select_t pllcs, mcg_pll_config_t const *config);
1408
1409/*!
1410 * @brief Sets the MCG to PEE mode.
1411 *
1412 * This function sets the MCG to PEE mode.
1413 *
1414 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1415 * @retval kStatus_Success Switched to the target mode successfully.
1416 *
1417 * @note This function only changes the CLKS to use the PLL/FLL output. If the
1418 * PRDIV/VDIV are different than in the PBE mode, set them up
1419 * in PBE mode and wait. When the clock is stable, switch to PEE mode.
1420 */
1421status_t CLOCK_SetPeeMode(void);
1422
1423/*!
1424 * @brief Switches the MCG to FBE mode from the external mode.
1425 *
1426 * This function switches the MCG from external modes (PEE/PBE/BLPE/FEE) to the FBE mode quickly.
1427 * The external clock is used as the system clock source and PLL is disabled. However,
1428 * the FLL settings are not configured. This is a lite function with a small code size, which is useful
1429 * during the mode switch. For example, to switch from PEE mode to FEI mode:
1430 *
1431 * @code
1432 * CLOCK_ExternalModeToFbeModeQuick();
1433 * CLOCK_SetFeiMode(...);
1434 * @endcode
1435 *
1436 * @retval kStatus_Success Switched successfully.
1437 * @retval kStatus_MCG_ModeInvalid If the current mode is not an external mode, do not call this function.
1438 */
1439status_t CLOCK_ExternalModeToFbeModeQuick(void);
1440
1441/*!
1442 * @brief Switches the MCG to FBI mode from internal modes.
1443 *
1444 * This function switches the MCG from internal modes (PEI/PBI/BLPI/FEI) to the FBI mode quickly.
1445 * The MCGIRCLK is used as the system clock source and PLL is disabled. However,
1446 * FLL settings are not configured. This is a lite function with a small code size, which is useful
1447 * during the mode switch. For example, to switch from PEI mode to FEE mode:
1448 *
1449 * @code
1450 * CLOCK_InternalModeToFbiModeQuick();
1451 * CLOCK_SetFeeMode(...);
1452 * @endcode
1453 *
1454 * @retval kStatus_Success Switched successfully.
1455 * @retval kStatus_MCG_ModeInvalid If the current mode is not an internal mode, do not call this function.
1456 */
1457status_t CLOCK_InternalModeToFbiModeQuick(void);
1458
1459/*!
1460 * @brief Sets the MCG to FEI mode during system boot up.
1461 *
1462 * This function sets the MCG to FEI mode from the reset mode. It can also be used to
1463 * set up MCG during system boot up.
1464 *
1465 * @param dmx32 DMX32 in FEI mode.
1466 * @param drs The DCO range selection.
1467 * @param fllStableDelay Delay function to ensure that the FLL is stable.
1468 *
1469 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1470 * @retval kStatus_Success Switched to the target mode successfully.
1471 * @note If @p dmx32 is set to kMCG_Dmx32Fine, the slow IRC must not be trimmed
1472 * to frequency above 32768 Hz.
1473 */
1474status_t CLOCK_BootToFeiMode(mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void));
1475
1476/*!
1477 * @brief Sets the MCG to FEE mode during system bootup.
1478 *
1479 * This function sets MCG to FEE mode from the reset mode. It can also be used to
1480 * set up the MCG during system boot up.
1481 *
1482 * @param oscsel OSC clock select, OSCSEL.
1483 * @param frdiv FLL reference clock divider setting, FRDIV.
1484 * @param dmx32 DMX32 in FEE mode.
1485 * @param drs The DCO range selection.
1486 * @param fllStableDelay Delay function to ensure that the FLL is stable.
1487 *
1488 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1489 * @retval kStatus_Success Switched to the target mode successfully.
1490 */
1491status_t CLOCK_BootToFeeMode(
1492 mcg_oscsel_t oscsel, uint8_t frdiv, mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void));
1493
1494/*!
1495 * @brief Sets the MCG to BLPI mode during system boot up.
1496 *
1497 * This function sets the MCG to BLPI mode from the reset mode. It can also be used to
1498 * set up the MCG during system boot up.
1499 *
1500 * @param fcrdiv Fast IRC divider, FCRDIV.
1501 * @param ircs The internal reference clock to select, IRCS.
1502 * @param ircEnableMode The MCGIRCLK enable mode, OR'ed value of @ref _mcg_irclk_enable_mode.
1503 *
1504 * @retval kStatus_MCG_SourceUsed Could not change MCGIRCLK setting.
1505 * @retval kStatus_Success Switched to the target mode successfully.
1506 */
1507status_t CLOCK_BootToBlpiMode(uint8_t fcrdiv, mcg_irc_mode_t ircs, uint8_t ircEnableMode);
1508
1509/*!
1510 * @brief Sets the MCG to BLPE mode during system boot up.
1511 *
1512 * This function sets the MCG to BLPE mode from the reset mode. It can also be used to
1513 * set up the MCG during system boot up.
1514 *
1515 * @param oscsel OSC clock select, MCG_C7[OSCSEL].
1516 *
1517 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1518 * @retval kStatus_Success Switched to the target mode successfully.
1519 */
1520status_t CLOCK_BootToBlpeMode(mcg_oscsel_t oscsel);
1521
1522/*!
1523 * @brief Sets the MCG to PEE mode during system boot up.
1524 *
1525 * This function sets the MCG to PEE mode from reset mode. It can also be used to
1526 * set up the MCG during system boot up.
1527 *
1528 * @param oscsel OSC clock select, MCG_C7[OSCSEL].
1529 * @param pllcs The PLL selection, PLLCS.
1530 * @param config Pointer to the PLL configuration.
1531 *
1532 * @retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
1533 * @retval kStatus_Success Switched to the target mode successfully.
1534 */
1535status_t CLOCK_BootToPeeMode(mcg_oscsel_t oscsel, mcg_pll_clk_select_t pllcs, mcg_pll_config_t const *config);
1536
1537/*!
1538 * @brief Sets the MCG to a target mode.
1539 *
1540 * This function sets MCG to a target mode defined by the configuration
1541 * structure. If switching to the target mode fails, this function
1542 * chooses the correct path.
1543 *
1544 * @param config Pointer to the target MCG mode configuration structure.
1545 * @return Return kStatus_Success if switched successfully; Otherwise, it returns an error code #_mcg_status.
1546 *
1547 * @note If the external clock is used in the target mode, ensure that it is
1548 * enabled. For example, if the OSC0 is used, set up OSC0 correctly before calling this
1549 * function.
1550 */
1551status_t CLOCK_SetMcgConfig(mcg_config_t const *config);
1552
1553/*@}*/
1554
1555#if defined(__cplusplus)
1556}
1557#endif /* __cplusplus */
1558
1559/*! @} */
1560
1561#endif /* _FSL_CLOCK_H_ */