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