Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

1533 Zeilen
52 KiB

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