fsl_clock.h 55 KB

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