fsl_clock.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  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. #include "fsl_clock.h"
  36. /*******************************************************************************
  37. * Definitions
  38. ******************************************************************************/
  39. /* Component ID definition, used by tools. */
  40. #ifndef FSL_COMPONENT_ID
  41. #define FSL_COMPONENT_ID "platform.drivers.clock"
  42. #endif
  43. /* Macro definition remap workaround. */
  44. #if (defined(MCG_C2_EREFS_MASK) && !(defined(MCG_C2_EREFS0_MASK)))
  45. #define MCG_C2_EREFS0_MASK MCG_C2_EREFS_MASK
  46. #endif
  47. #if (defined(MCG_C2_HGO_MASK) && !(defined(MCG_C2_HGO0_MASK)))
  48. #define MCG_C2_HGO0_MASK MCG_C2_HGO_MASK
  49. #endif
  50. #if (defined(MCG_C2_RANGE_MASK) && !(defined(MCG_C2_RANGE0_MASK)))
  51. #define MCG_C2_RANGE0_MASK MCG_C2_RANGE_MASK
  52. #endif
  53. #if (defined(MCG_C6_CME_MASK) && !(defined(MCG_C6_CME0_MASK)))
  54. #define MCG_C6_CME0_MASK MCG_C6_CME_MASK
  55. #endif
  56. /* PLL fixed multiplier when there is not PRDIV and VDIV. */
  57. #define PLL_FIXED_MULT (375U)
  58. /* Max frequency of the reference clock used for internal clock trim. */
  59. #define TRIM_REF_CLK_MIN (8000000U)
  60. /* Min frequency of the reference clock used for internal clock trim. */
  61. #define TRIM_REF_CLK_MAX (16000000U)
  62. /* Max trim value of fast internal reference clock. */
  63. #define TRIM_FIRC_MAX (5000000U)
  64. /* Min trim value of fast internal reference clock. */
  65. #define TRIM_FIRC_MIN (3000000U)
  66. /* Max trim value of fast internal reference clock. */
  67. #define TRIM_SIRC_MAX (39063U)
  68. /* Min trim value of fast internal reference clock. */
  69. #define TRIM_SIRC_MIN (31250U)
  70. #define MCG_S_IRCST_VAL ((MCG->S & MCG_S_IRCST_MASK) >> MCG_S_IRCST_SHIFT)
  71. #define MCG_S_CLKST_VAL ((MCG->S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT)
  72. #define MCG_S_IREFST_VAL ((MCG->S & MCG_S_IREFST_MASK) >> MCG_S_IREFST_SHIFT)
  73. #define MCG_S_PLLST_VAL ((MCG->S & MCG_S_PLLST_MASK) >> MCG_S_PLLST_SHIFT)
  74. #define MCG_C1_FRDIV_VAL ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT)
  75. #define MCG_C2_LP_VAL ((MCG->C2 & MCG_C2_LP_MASK) >> MCG_C2_LP_SHIFT)
  76. #define MCG_C2_RANGE_VAL ((MCG->C2 & MCG_C2_RANGE_MASK) >> MCG_C2_RANGE_SHIFT)
  77. #define MCG_SC_FCRDIV_VAL ((MCG->SC & MCG_SC_FCRDIV_MASK) >> MCG_SC_FCRDIV_SHIFT)
  78. #define MCG_S2_PLLCST_VAL ((MCG->S2 & MCG_S2_PLLCST_MASK) >> MCG_S2_PLLCST_SHIFT)
  79. #define MCG_C7_OSCSEL_VAL ((MCG->C7 & MCG_C7_OSCSEL_MASK) >> MCG_C7_OSCSEL_SHIFT)
  80. #define MCG_C4_DMX32_VAL ((MCG->C4 & MCG_C4_DMX32_MASK) >> MCG_C4_DMX32_SHIFT)
  81. #define MCG_C4_DRST_DRS_VAL ((MCG->C4 & MCG_C4_DRST_DRS_MASK) >> MCG_C4_DRST_DRS_SHIFT)
  82. #define MCG_C7_PLL32KREFSEL_VAL ((MCG->C7 & MCG_C7_PLL32KREFSEL_MASK) >> MCG_C7_PLL32KREFSEL_SHIFT)
  83. #define MCG_C5_PLLREFSEL0_VAL ((MCG->C5 & MCG_C5_PLLREFSEL0_MASK) >> MCG_C5_PLLREFSEL0_SHIFT)
  84. #define MCG_C11_PLLREFSEL1_VAL ((MCG->C11 & MCG_C11_PLLREFSEL1_MASK) >> MCG_C11_PLLREFSEL1_SHIFT)
  85. #define MCG_C11_PRDIV1_VAL ((MCG->C11 & MCG_C11_PRDIV1_MASK) >> MCG_C11_PRDIV1_SHIFT)
  86. #define MCG_C12_VDIV1_VAL ((MCG->C12 & MCG_C12_VDIV1_MASK) >> MCG_C12_VDIV1_SHIFT)
  87. #define MCG_C5_PRDIV0_VAL ((MCG->C5 & MCG_C5_PRDIV0_MASK) >> MCG_C5_PRDIV0_SHIFT)
  88. #define MCG_C6_VDIV0_VAL ((MCG->C6 & MCG_C6_VDIV0_MASK) >> MCG_C6_VDIV0_SHIFT)
  89. #define OSC_MODE_MASK (MCG_C2_EREFS0_MASK | MCG_C2_HGO0_MASK | MCG_C2_RANGE0_MASK)
  90. #define SIM_CLKDIV1_OUTDIV1_VAL ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV1_MASK) >> SIM_CLKDIV1_OUTDIV1_SHIFT)
  91. #define SIM_CLKDIV1_OUTDIV2_VAL ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV2_MASK) >> SIM_CLKDIV1_OUTDIV2_SHIFT)
  92. #define SIM_CLKDIV1_OUTDIV3_VAL ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV3_MASK) >> SIM_CLKDIV1_OUTDIV3_SHIFT)
  93. #define SIM_CLKDIV1_OUTDIV4_VAL ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV4_MASK) >> SIM_CLKDIV1_OUTDIV4_SHIFT)
  94. #define SIM_SOPT1_OSC32KSEL_VAL ((SIM->SOPT1 & SIM_SOPT1_OSC32KSEL_MASK) >> SIM_SOPT1_OSC32KSEL_SHIFT)
  95. #define SIM_SOPT2_PLLFLLSEL_VAL ((SIM->SOPT2 & SIM_SOPT2_PLLFLLSEL_MASK) >> SIM_SOPT2_PLLFLLSEL_SHIFT)
  96. #define SIM_CLKDIV3_PLLFLLDIV_VAL ((SIM->CLKDIV3 & SIM_CLKDIV3_PLLFLLDIV_MASK) >> SIM_CLKDIV3_PLLFLLDIV_SHIFT)
  97. #define SIM_CLKDIV3_PLLFLLFRAC_VAL ((SIM->CLKDIV3 & SIM_CLKDIV3_PLLFLLFRAC_MASK) >> SIM_CLKDIV3_PLLFLLFRAC_SHIFT)
  98. /* MCG_S_CLKST definition. */
  99. enum _mcg_clkout_stat
  100. {
  101. kMCG_ClkOutStatFll, /* FLL. */
  102. kMCG_ClkOutStatInt, /* Internal clock. */
  103. kMCG_ClkOutStatExt, /* External clock. */
  104. kMCG_ClkOutStatPll /* PLL. */
  105. };
  106. /* MCG_S_PLLST definition. */
  107. enum _mcg_pllst
  108. {
  109. kMCG_PllstFll, /* FLL is used. */
  110. kMCG_PllstPll /* PLL is used. */
  111. };
  112. /*******************************************************************************
  113. * Variables
  114. ******************************************************************************/
  115. /* Slow internal reference clock frequency. */
  116. static uint32_t s_slowIrcFreq = 32768U;
  117. /* Fast internal reference clock frequency. */
  118. static uint32_t s_fastIrcFreq = 4000000U;
  119. /* External XTAL0 (OSC0) clock frequency. */
  120. uint32_t g_xtal0Freq;
  121. /* External XTAL32K clock frequency. */
  122. uint32_t g_xtal32Freq;
  123. /*******************************************************************************
  124. * Prototypes
  125. ******************************************************************************/
  126. /*!
  127. * @brief Get the MCG external reference clock frequency.
  128. *
  129. * Get the current MCG external reference clock frequency in Hz. It is
  130. * the frequency select by MCG_C7[OSCSEL]. This is an internal function.
  131. *
  132. * @return MCG external reference clock frequency in Hz.
  133. */
  134. static uint32_t CLOCK_GetMcgExtClkFreq(void);
  135. /*!
  136. * @brief Get the MCG FLL external reference clock frequency.
  137. *
  138. * Get the current MCG FLL external reference clock frequency in Hz. It is
  139. * the frequency after by MCG_C1[FRDIV]. This is an internal function.
  140. *
  141. * @return MCG FLL external reference clock frequency in Hz.
  142. */
  143. static uint32_t CLOCK_GetFllExtRefClkFreq(void);
  144. /*!
  145. * @brief Get the MCG FLL reference clock frequency.
  146. *
  147. * Get the current MCG FLL reference clock frequency in Hz. It is
  148. * the frequency select by MCG_C1[IREFS]. This is an internal function.
  149. *
  150. * @return MCG FLL reference clock frequency in Hz.
  151. */
  152. static uint32_t CLOCK_GetFllRefClkFreq(void);
  153. /*!
  154. * @brief Get the frequency of clock selected by MCG_C2[IRCS].
  155. *
  156. * This clock's two output:
  157. * 1. MCGOUTCLK when MCG_S[CLKST]=0.
  158. * 2. MCGIRCLK when MCG_C1[IRCLKEN]=1.
  159. *
  160. * @return The frequency in Hz.
  161. */
  162. static uint32_t CLOCK_GetInternalRefClkSelectFreq(void);
  163. /*!
  164. * @brief Get the MCG PLL/PLL0 reference clock frequency.
  165. *
  166. * Get the current MCG PLL/PLL0 reference clock frequency in Hz.
  167. * This is an internal function.
  168. *
  169. * @return MCG PLL/PLL0 reference clock frequency in Hz.
  170. */
  171. static uint32_t CLOCK_GetPll0RefFreq(void);
  172. /*!
  173. * @brief Calculate the RANGE value base on crystal frequency.
  174. *
  175. * To setup external crystal oscillator, must set the register bits RANGE
  176. * base on the crystal frequency. This function returns the RANGE base on the
  177. * input frequency. This is an internal function.
  178. *
  179. * @param freq Crystal frequency in Hz.
  180. * @return The RANGE value.
  181. */
  182. static uint8_t CLOCK_GetOscRangeFromFreq(uint32_t freq);
  183. #ifndef MCG_USER_CONFIG_FLL_STABLE_DELAY_EN
  184. /*!
  185. * @brief Delay function to wait FLL stable.
  186. *
  187. * Delay function to wait FLL stable in FEI mode or FEE mode, should wait at least
  188. * 1ms. Every time changes FLL setting, should wait this time for FLL stable.
  189. */
  190. static void CLOCK_FllStableDelay(void);
  191. #endif
  192. /*******************************************************************************
  193. * Code
  194. ******************************************************************************/
  195. #ifndef MCG_USER_CONFIG_FLL_STABLE_DELAY_EN
  196. static void CLOCK_FllStableDelay(void)
  197. {
  198. /*
  199. Should wait at least 1ms. Because in these modes, the core clock is 100MHz
  200. at most, so this function could obtain the 1ms delay.
  201. */
  202. volatile uint32_t i = 30000U;
  203. while (i--)
  204. {
  205. __NOP();
  206. }
  207. }
  208. #else /* With MCG_USER_CONFIG_FLL_STABLE_DELAY_EN defined. */
  209. /* Once user defines the MCG_USER_CONFIG_FLL_STABLE_DELAY_EN to use their own delay function, he has to
  210. * create his own CLOCK_FllStableDelay() function in application code. Since the clock functions in this
  211. * file would call the CLOCK_FllStableDelay() regardness how it is defined.
  212. */
  213. extern void CLOCK_FllStableDelay(void);
  214. #endif /* MCG_USER_CONFIG_FLL_STABLE_DELAY_EN */
  215. static uint32_t CLOCK_GetMcgExtClkFreq(void)
  216. {
  217. uint32_t freq;
  218. switch (MCG_C7_OSCSEL_VAL)
  219. {
  220. case 0U:
  221. /* Please call CLOCK_SetXtal0Freq base on board setting before using OSC0 clock. */
  222. assert(g_xtal0Freq);
  223. freq = g_xtal0Freq;
  224. break;
  225. case 1U:
  226. /* Please call CLOCK_SetXtal32Freq base on board setting before using XTAL32K/RTC_CLKIN clock. */
  227. assert(g_xtal32Freq);
  228. freq = g_xtal32Freq;
  229. break;
  230. case 2U:
  231. freq = MCG_INTERNAL_IRC_48M;
  232. break;
  233. default:
  234. freq = 0U;
  235. break;
  236. }
  237. return freq;
  238. }
  239. static uint32_t CLOCK_GetFllExtRefClkFreq(void)
  240. {
  241. /* FllExtRef = McgExtRef / FllExtRefDiv */
  242. uint8_t frdiv;
  243. uint8_t range;
  244. uint8_t oscsel;
  245. uint32_t freq = CLOCK_GetMcgExtClkFreq();
  246. if (!freq)
  247. {
  248. return freq;
  249. }
  250. frdiv = MCG_C1_FRDIV_VAL;
  251. freq >>= frdiv;
  252. range = MCG_C2_RANGE_VAL;
  253. oscsel = MCG_C7_OSCSEL_VAL;
  254. /*
  255. When should use divider 32, 64, 128, 256, 512, 1024, 1280, 1536.
  256. 1. MCG_C7[OSCSEL] selects IRC48M.
  257. 2. MCG_C7[OSCSEL] selects OSC0 and MCG_C2[RANGE] is not 0.
  258. */
  259. if (((0U != range) && (kMCG_OscselOsc == oscsel)) || (kMCG_OscselIrc == oscsel))
  260. {
  261. switch (frdiv)
  262. {
  263. case 0:
  264. case 1:
  265. case 2:
  266. case 3:
  267. case 4:
  268. case 5:
  269. freq >>= 5u;
  270. break;
  271. case 6:
  272. /* 64*20=1280 */
  273. freq /= 20u;
  274. break;
  275. case 7:
  276. /* 128*12=1536 */
  277. freq /= 12u;
  278. break;
  279. default:
  280. freq = 0u;
  281. break;
  282. }
  283. }
  284. return freq;
  285. }
  286. static uint32_t CLOCK_GetInternalRefClkSelectFreq(void)
  287. {
  288. if (kMCG_IrcSlow == MCG_S_IRCST_VAL)
  289. {
  290. /* Slow internal reference clock selected*/
  291. return s_slowIrcFreq;
  292. }
  293. else
  294. {
  295. /* Fast internal reference clock selected*/
  296. return s_fastIrcFreq >> MCG_SC_FCRDIV_VAL;
  297. }
  298. }
  299. static uint32_t CLOCK_GetFllRefClkFreq(void)
  300. {
  301. /* If use external reference clock. */
  302. if (kMCG_FllSrcExternal == MCG_S_IREFST_VAL)
  303. {
  304. return CLOCK_GetFllExtRefClkFreq();
  305. }
  306. /* If use internal reference clock. */
  307. else
  308. {
  309. return s_slowIrcFreq;
  310. }
  311. }
  312. static uint32_t CLOCK_GetPll0RefFreq(void)
  313. {
  314. /* MCG external reference clock. */
  315. return CLOCK_GetMcgExtClkFreq();
  316. }
  317. static uint8_t CLOCK_GetOscRangeFromFreq(uint32_t freq)
  318. {
  319. uint8_t range;
  320. if (freq <= 39063U)
  321. {
  322. range = 0U;
  323. }
  324. else if (freq <= 8000000U)
  325. {
  326. range = 1U;
  327. }
  328. else
  329. {
  330. range = 2U;
  331. }
  332. return range;
  333. }
  334. uint32_t CLOCK_GetOsc0ErClkUndivFreq(void)
  335. {
  336. if (OSC0->CR & OSC_CR_ERCLKEN_MASK)
  337. {
  338. /* Please call CLOCK_SetXtal0Freq base on board setting before using OSC0 clock. */
  339. assert(g_xtal0Freq);
  340. return g_xtal0Freq;
  341. }
  342. else
  343. {
  344. return 0U;
  345. }
  346. }
  347. uint32_t CLOCK_GetOsc0ErClkDivFreq(void)
  348. {
  349. if (OSC0->CR & OSC_CR_ERCLKEN_MASK)
  350. {
  351. /* Please call CLOCK_SetXtal0Freq base on board setting before using OSC0 clock. */
  352. assert(g_xtal0Freq);
  353. return g_xtal0Freq >> ((OSC0->DIV & OSC_DIV_ERPS_MASK) >> OSC_DIV_ERPS_SHIFT);
  354. }
  355. else
  356. {
  357. return 0U;
  358. }
  359. }
  360. uint32_t CLOCK_GetEr32kClkFreq(void)
  361. {
  362. uint32_t freq;
  363. switch (SIM_SOPT1_OSC32KSEL_VAL)
  364. {
  365. case 0U: /* OSC 32k clock */
  366. freq = (CLOCK_GetOsc0ErClkUndivFreq() == 32768U) ? 32768U : 0U;
  367. break;
  368. case 2U: /* RTC 32k clock */
  369. /* Please call CLOCK_SetXtal32Freq base on board setting before using XTAL32K/RTC_CLKIN clock. */
  370. assert(g_xtal32Freq);
  371. freq = g_xtal32Freq;
  372. break;
  373. case 3U: /* LPO clock */
  374. freq = LPO_CLK_FREQ;
  375. break;
  376. default:
  377. freq = 0U;
  378. break;
  379. }
  380. return freq;
  381. }
  382. uint32_t CLOCK_GetPllFllSelClkFreq(void)
  383. {
  384. uint32_t freq;
  385. switch (SIM_SOPT2_PLLFLLSEL_VAL)
  386. {
  387. case 0U: /* FLL. */
  388. freq = CLOCK_GetFllFreq();
  389. break;
  390. case 1U: /* PLL. */
  391. freq = CLOCK_GetPll0Freq();
  392. break;
  393. case 3U: /* MCG IRC48M. */
  394. freq = MCG_INTERNAL_IRC_48M;
  395. break;
  396. default:
  397. freq = 0U;
  398. break;
  399. }
  400. freq *= (SIM_CLKDIV3_PLLFLLFRAC_VAL + 1U);
  401. freq /= (SIM_CLKDIV3_PLLFLLDIV_VAL + 1U);
  402. return freq;
  403. }
  404. uint32_t CLOCK_GetOsc0ErClkFreq(void)
  405. {
  406. return CLOCK_GetOsc0ErClkUndivFreq();
  407. }
  408. uint32_t CLOCK_GetPlatClkFreq(void)
  409. {
  410. return CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV1_VAL + 1);
  411. }
  412. uint32_t CLOCK_GetFlashClkFreq(void)
  413. {
  414. return CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV4_VAL + 1);
  415. }
  416. uint32_t CLOCK_GetFlexBusClkFreq(void)
  417. {
  418. return CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV3_VAL + 1);
  419. }
  420. uint32_t CLOCK_GetBusClkFreq(void)
  421. {
  422. return CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV2_VAL + 1);
  423. }
  424. uint32_t CLOCK_GetCoreSysClkFreq(void)
  425. {
  426. return CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV1_VAL + 1);
  427. }
  428. uint32_t CLOCK_GetFreq(clock_name_t clockName)
  429. {
  430. uint32_t freq;
  431. switch (clockName)
  432. {
  433. case kCLOCK_CoreSysClk:
  434. case kCLOCK_PlatClk:
  435. freq = CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV1_VAL + 1);
  436. break;
  437. case kCLOCK_BusClk:
  438. freq = CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV2_VAL + 1);
  439. break;
  440. case kCLOCK_FlexBusClk:
  441. freq = CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV3_VAL + 1);
  442. break;
  443. case kCLOCK_FlashClk:
  444. freq = CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV4_VAL + 1);
  445. break;
  446. case kCLOCK_PllFllSelClk:
  447. freq = CLOCK_GetPllFllSelClkFreq();
  448. break;
  449. case kCLOCK_Er32kClk:
  450. freq = CLOCK_GetEr32kClkFreq();
  451. break;
  452. case kCLOCK_McgFixedFreqClk:
  453. freq = CLOCK_GetFixedFreqClkFreq();
  454. break;
  455. case kCLOCK_McgInternalRefClk:
  456. freq = CLOCK_GetInternalRefClkFreq();
  457. break;
  458. case kCLOCK_McgFllClk:
  459. freq = CLOCK_GetFllFreq();
  460. break;
  461. case kCLOCK_McgPll0Clk:
  462. freq = CLOCK_GetPll0Freq();
  463. break;
  464. case kCLOCK_McgIrc48MClk:
  465. freq = MCG_INTERNAL_IRC_48M;
  466. break;
  467. case kCLOCK_LpoClk:
  468. freq = LPO_CLK_FREQ;
  469. break;
  470. case kCLOCK_Osc0ErClkUndiv:
  471. freq = CLOCK_GetOsc0ErClkDivFreq();
  472. break;
  473. case kCLOCK_Osc0ErClk:
  474. freq = CLOCK_GetOsc0ErClkUndivFreq();
  475. break;
  476. default:
  477. freq = 0U;
  478. break;
  479. }
  480. return freq;
  481. }
  482. void CLOCK_SetSimConfig(sim_clock_config_t const *config)
  483. {
  484. SIM->CLKDIV1 = config->clkdiv1;
  485. CLOCK_SetPllFllSelClock(config->pllFllSel, config->pllFllDiv, config->pllFllFrac);
  486. CLOCK_SetEr32kClock(config->er32kSrc);
  487. }
  488. bool CLOCK_EnableUsbfs0Clock(clock_usb_src_t src, uint32_t freq)
  489. {
  490. bool ret = true;
  491. CLOCK_DisableClock(kCLOCK_Usbfs0);
  492. if (kCLOCK_UsbSrcExt == src)
  493. {
  494. SIM->SOPT2 &= ~SIM_SOPT2_USBSRC_MASK;
  495. }
  496. else
  497. {
  498. switch (freq)
  499. {
  500. case 120000000U:
  501. SIM->CLKDIV2 = SIM_CLKDIV2_USBDIV(4) | SIM_CLKDIV2_USBFRAC(1);
  502. break;
  503. case 96000000U:
  504. SIM->CLKDIV2 = SIM_CLKDIV2_USBDIV(1) | SIM_CLKDIV2_USBFRAC(0);
  505. break;
  506. case 72000000U:
  507. SIM->CLKDIV2 = SIM_CLKDIV2_USBDIV(2) | SIM_CLKDIV2_USBFRAC(1);
  508. break;
  509. case 48000000U:
  510. SIM->CLKDIV2 = SIM_CLKDIV2_USBDIV(0) | SIM_CLKDIV2_USBFRAC(0);
  511. break;
  512. default:
  513. ret = false;
  514. break;
  515. }
  516. SIM->SOPT2 = ((SIM->SOPT2 & ~(SIM_SOPT2_PLLFLLSEL_MASK | SIM_SOPT2_USBSRC_MASK)) | (uint32_t)src);
  517. }
  518. CLOCK_EnableClock(kCLOCK_Usbfs0);
  519. if (kCLOCK_UsbSrcIrc48M == src)
  520. {
  521. USB0->CLK_RECOVER_IRC_EN = 0x03U;
  522. USB0->CLK_RECOVER_CTRL |= USB_CLK_RECOVER_CTRL_CLOCK_RECOVER_EN_MASK;
  523. }
  524. return ret;
  525. }
  526. uint32_t CLOCK_GetOutClkFreq(void)
  527. {
  528. uint32_t mcgoutclk;
  529. uint32_t clkst = MCG_S_CLKST_VAL;
  530. switch (clkst)
  531. {
  532. case kMCG_ClkOutStatPll:
  533. mcgoutclk = CLOCK_GetPll0Freq();
  534. break;
  535. case kMCG_ClkOutStatFll:
  536. mcgoutclk = CLOCK_GetFllFreq();
  537. break;
  538. case kMCG_ClkOutStatInt:
  539. mcgoutclk = CLOCK_GetInternalRefClkSelectFreq();
  540. break;
  541. case kMCG_ClkOutStatExt:
  542. mcgoutclk = CLOCK_GetMcgExtClkFreq();
  543. break;
  544. default:
  545. mcgoutclk = 0U;
  546. break;
  547. }
  548. return mcgoutclk;
  549. }
  550. uint32_t CLOCK_GetFllFreq(void)
  551. {
  552. static const uint16_t fllFactorTable[4][2] = {{640, 732}, {1280, 1464}, {1920, 2197}, {2560, 2929}};
  553. uint8_t drs, dmx32;
  554. uint32_t freq;
  555. /* If FLL is not enabled currently, then return 0U. */
  556. if ((MCG->C2 & MCG_C2_LP_MASK) || (MCG->S & MCG_S_PLLST_MASK))
  557. {
  558. return 0U;
  559. }
  560. /* Get FLL reference clock frequency. */
  561. freq = CLOCK_GetFllRefClkFreq();
  562. if (!freq)
  563. {
  564. return freq;
  565. }
  566. drs = MCG_C4_DRST_DRS_VAL;
  567. dmx32 = MCG_C4_DMX32_VAL;
  568. return freq * fllFactorTable[drs][dmx32];
  569. }
  570. uint32_t CLOCK_GetInternalRefClkFreq(void)
  571. {
  572. /* If MCGIRCLK is gated. */
  573. if (!(MCG->C1 & MCG_C1_IRCLKEN_MASK))
  574. {
  575. return 0U;
  576. }
  577. return CLOCK_GetInternalRefClkSelectFreq();
  578. }
  579. uint32_t CLOCK_GetFixedFreqClkFreq(void)
  580. {
  581. uint32_t freq = CLOCK_GetFllRefClkFreq();
  582. /* MCGFFCLK must be no more than MCGOUTCLK/8. */
  583. if ((freq) && (freq <= (CLOCK_GetOutClkFreq() / 8U)))
  584. {
  585. return freq;
  586. }
  587. else
  588. {
  589. return 0U;
  590. }
  591. }
  592. uint32_t CLOCK_GetPll0Freq(void)
  593. {
  594. uint32_t mcgpll0clk;
  595. /* If PLL0 is not enabled, return 0. */
  596. if (!(MCG->S & MCG_S_LOCK0_MASK))
  597. {
  598. return 0U;
  599. }
  600. mcgpll0clk = CLOCK_GetPll0RefFreq();
  601. /*
  602. * Please call CLOCK_SetXtal0Freq base on board setting before using OSC0 clock.
  603. * Please call CLOCK_SetXtal1Freq base on board setting before using OSC1 clock.
  604. */
  605. assert(mcgpll0clk);
  606. mcgpll0clk /= (FSL_FEATURE_MCG_PLL_PRDIV_BASE + MCG_C5_PRDIV0_VAL);
  607. mcgpll0clk *= (FSL_FEATURE_MCG_PLL_VDIV_BASE + MCG_C6_VDIV0_VAL);
  608. mcgpll0clk >>= 1U;
  609. return mcgpll0clk;
  610. }
  611. status_t CLOCK_SetExternalRefClkConfig(mcg_oscsel_t oscsel)
  612. {
  613. bool needDelay;
  614. uint32_t i;
  615. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  616. /* If change MCG_C7[OSCSEL] and external reference clock is system clock source, return error. */
  617. if ((MCG_C7_OSCSEL_VAL != oscsel) && (!(MCG->S & MCG_S_IREFST_MASK)))
  618. {
  619. return kStatus_MCG_SourceUsed;
  620. }
  621. #endif /* MCG_CONFIG_CHECK_PARAM */
  622. if (MCG_C7_OSCSEL_VAL != oscsel)
  623. {
  624. /* If change OSCSEL, need to delay, ERR009878. */
  625. needDelay = true;
  626. }
  627. else
  628. {
  629. needDelay = false;
  630. }
  631. MCG->C7 = (MCG->C7 & ~MCG_C7_OSCSEL_MASK) | MCG_C7_OSCSEL(oscsel);
  632. if (needDelay)
  633. {
  634. /* ERR009878 Delay at least 50 micro-seconds for external clock change valid. */
  635. i = 1500U;
  636. while (i--)
  637. {
  638. __NOP();
  639. }
  640. }
  641. return kStatus_Success;
  642. }
  643. status_t CLOCK_SetInternalRefClkConfig(uint8_t enableMode, mcg_irc_mode_t ircs, uint8_t fcrdiv)
  644. {
  645. uint32_t mcgOutClkState = MCG_S_CLKST_VAL;
  646. mcg_irc_mode_t curIrcs = (mcg_irc_mode_t)MCG_S_IRCST_VAL;
  647. uint8_t curFcrdiv = MCG_SC_FCRDIV_VAL;
  648. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  649. /* If MCGIRCLK is used as system clock source. */
  650. if (kMCG_ClkOutStatInt == mcgOutClkState)
  651. {
  652. /* If need to change MCGIRCLK source or driver, return error. */
  653. if (((kMCG_IrcFast == curIrcs) && (fcrdiv != curFcrdiv)) || (ircs != curIrcs))
  654. {
  655. return kStatus_MCG_SourceUsed;
  656. }
  657. }
  658. #endif
  659. /* If need to update the FCRDIV. */
  660. if (fcrdiv != curFcrdiv)
  661. {
  662. /* If fast IRC is in use currently, change to slow IRC. */
  663. if ((kMCG_IrcFast == curIrcs) && ((mcgOutClkState == kMCG_ClkOutStatInt) || (MCG->C1 & MCG_C1_IRCLKEN_MASK)))
  664. {
  665. MCG->C2 = ((MCG->C2 & ~MCG_C2_IRCS_MASK) | (MCG_C2_IRCS(kMCG_IrcSlow)));
  666. while (MCG_S_IRCST_VAL != kMCG_IrcSlow)
  667. {
  668. }
  669. }
  670. /* Update FCRDIV. */
  671. MCG->SC = (MCG->SC & ~(MCG_SC_FCRDIV_MASK | MCG_SC_ATMF_MASK | MCG_SC_LOCS0_MASK)) | MCG_SC_FCRDIV(fcrdiv);
  672. }
  673. /* Set internal reference clock selection. */
  674. MCG->C2 = (MCG->C2 & ~MCG_C2_IRCS_MASK) | (MCG_C2_IRCS(ircs));
  675. MCG->C1 = (MCG->C1 & ~(MCG_C1_IRCLKEN_MASK | MCG_C1_IREFSTEN_MASK)) | (uint8_t)enableMode;
  676. /* If MCGIRCLK is used, need to wait for MCG_S_IRCST. */
  677. if ((mcgOutClkState == kMCG_ClkOutStatInt) || (enableMode & kMCG_IrclkEnable))
  678. {
  679. while (MCG_S_IRCST_VAL != ircs)
  680. {
  681. }
  682. }
  683. return kStatus_Success;
  684. }
  685. uint32_t CLOCK_CalcPllDiv(uint32_t refFreq, uint32_t desireFreq, uint8_t *prdiv, uint8_t *vdiv)
  686. {
  687. uint8_t ret_prdiv; /* PRDIV to return. */
  688. uint8_t ret_vdiv; /* VDIV to return. */
  689. uint8_t prdiv_min; /* Min PRDIV value to make reference clock in allowed range. */
  690. uint8_t prdiv_max; /* Max PRDIV value to make reference clock in allowed range. */
  691. uint8_t prdiv_cur; /* PRDIV value for iteration. */
  692. uint8_t vdiv_cur; /* VDIV value for iteration. */
  693. uint32_t ret_freq = 0U; /* PLL output fequency to return. */
  694. uint32_t diff = 0xFFFFFFFFU; /* Difference between desireFreq and return frequency. */
  695. uint32_t ref_div; /* Reference frequency after PRDIV. */
  696. /*
  697. Steps:
  698. 1. Get allowed prdiv with such rules:
  699. 1). refFreq / prdiv >= FSL_FEATURE_MCG_PLL_REF_MIN.
  700. 2). refFreq / prdiv <= FSL_FEATURE_MCG_PLL_REF_MAX.
  701. 2. For each allowed prdiv, there are two candidate vdiv values:
  702. 1). (desireFreq / (refFreq / prdiv)).
  703. 2). (desireFreq / (refFreq / prdiv)) + 1.
  704. If could get the precise desired frequency, return current prdiv and
  705. vdiv directly. Otherwise choose the one which is closer to desired
  706. frequency.
  707. */
  708. /* Reference frequency is out of range. */
  709. if ((refFreq < FSL_FEATURE_MCG_PLL_REF_MIN) ||
  710. (refFreq > (FSL_FEATURE_MCG_PLL_REF_MAX * (FSL_FEATURE_MCG_PLL_PRDIV_MAX + FSL_FEATURE_MCG_PLL_PRDIV_BASE))))
  711. {
  712. return 0U;
  713. }
  714. /* refFreq/PRDIV must in a range. First get the allowed PRDIV range. */
  715. prdiv_max = refFreq / FSL_FEATURE_MCG_PLL_REF_MIN;
  716. prdiv_min = (refFreq + FSL_FEATURE_MCG_PLL_REF_MAX - 1U) / FSL_FEATURE_MCG_PLL_REF_MAX;
  717. desireFreq *= 2U;
  718. /* PRDIV traversal. */
  719. for (prdiv_cur = prdiv_max; prdiv_cur >= prdiv_min; prdiv_cur--)
  720. {
  721. /* Reference frequency after PRDIV. */
  722. ref_div = refFreq / prdiv_cur;
  723. vdiv_cur = desireFreq / ref_div;
  724. if ((vdiv_cur < FSL_FEATURE_MCG_PLL_VDIV_BASE - 1U) || (vdiv_cur > FSL_FEATURE_MCG_PLL_VDIV_BASE + 31U))
  725. {
  726. /* No VDIV is available with this PRDIV. */
  727. continue;
  728. }
  729. ret_freq = vdiv_cur * ref_div;
  730. if (vdiv_cur >= FSL_FEATURE_MCG_PLL_VDIV_BASE)
  731. {
  732. if (ret_freq == desireFreq) /* If desire frequency is got. */
  733. {
  734. *prdiv = prdiv_cur - FSL_FEATURE_MCG_PLL_PRDIV_BASE;
  735. *vdiv = vdiv_cur - FSL_FEATURE_MCG_PLL_VDIV_BASE;
  736. return ret_freq / 2U;
  737. }
  738. /* New PRDIV/VDIV is closer. */
  739. if (diff > desireFreq - ret_freq)
  740. {
  741. diff = desireFreq - ret_freq;
  742. ret_prdiv = prdiv_cur;
  743. ret_vdiv = vdiv_cur;
  744. }
  745. }
  746. vdiv_cur++;
  747. if (vdiv_cur <= (FSL_FEATURE_MCG_PLL_VDIV_BASE + 31U))
  748. {
  749. ret_freq += ref_div;
  750. /* New PRDIV/VDIV is closer. */
  751. if (diff > ret_freq - desireFreq)
  752. {
  753. diff = ret_freq - desireFreq;
  754. ret_prdiv = prdiv_cur;
  755. ret_vdiv = vdiv_cur;
  756. }
  757. }
  758. }
  759. if (0xFFFFFFFFU != diff)
  760. {
  761. /* PRDIV/VDIV found. */
  762. *prdiv = ret_prdiv - FSL_FEATURE_MCG_PLL_PRDIV_BASE;
  763. *vdiv = ret_vdiv - FSL_FEATURE_MCG_PLL_VDIV_BASE;
  764. ret_freq = (refFreq / ret_prdiv) * ret_vdiv;
  765. return ret_freq / 2U;
  766. }
  767. else
  768. {
  769. /* No proper PRDIV/VDIV found. */
  770. return 0U;
  771. }
  772. }
  773. void CLOCK_EnablePll0(mcg_pll_config_t const *config)
  774. {
  775. assert(config);
  776. uint8_t mcg_c5 = 0U;
  777. mcg_c5 |= MCG_C5_PRDIV0(config->prdiv);
  778. MCG->C5 = mcg_c5; /* Disable the PLL first. */
  779. MCG->C6 = (MCG->C6 & ~MCG_C6_VDIV0_MASK) | MCG_C6_VDIV0(config->vdiv);
  780. /* Set enable mode. */
  781. MCG->C5 |= ((uint32_t)kMCG_PllEnableIndependent | (uint32_t)config->enableMode);
  782. /* Wait for PLL lock. */
  783. while (!(MCG->S & MCG_S_LOCK0_MASK))
  784. {
  785. }
  786. }
  787. void CLOCK_SetOsc0MonitorMode(mcg_monitor_mode_t mode)
  788. {
  789. /* Clear the previous flag, MCG_SC[LOCS0]. */
  790. MCG->SC &= ~MCG_SC_ATMF_MASK;
  791. if (kMCG_MonitorNone == mode)
  792. {
  793. MCG->C6 &= ~MCG_C6_CME0_MASK;
  794. }
  795. else
  796. {
  797. if (kMCG_MonitorInt == mode)
  798. {
  799. MCG->C2 &= ~MCG_C2_LOCRE0_MASK;
  800. }
  801. else
  802. {
  803. MCG->C2 |= MCG_C2_LOCRE0_MASK;
  804. }
  805. MCG->C6 |= MCG_C6_CME0_MASK;
  806. }
  807. }
  808. void CLOCK_SetRtcOscMonitorMode(mcg_monitor_mode_t mode)
  809. {
  810. uint8_t mcg_c8 = MCG->C8;
  811. mcg_c8 &= ~(MCG_C8_CME1_MASK | MCG_C8_LOCRE1_MASK);
  812. if (kMCG_MonitorNone != mode)
  813. {
  814. if (kMCG_MonitorReset == mode)
  815. {
  816. mcg_c8 |= MCG_C8_LOCRE1_MASK;
  817. }
  818. mcg_c8 |= MCG_C8_CME1_MASK;
  819. }
  820. MCG->C8 = mcg_c8;
  821. }
  822. void CLOCK_SetPll0MonitorMode(mcg_monitor_mode_t mode)
  823. {
  824. uint8_t mcg_c8;
  825. /* Clear previous flag. */
  826. MCG->S = MCG_S_LOLS0_MASK;
  827. if (kMCG_MonitorNone == mode)
  828. {
  829. MCG->C6 &= ~MCG_C6_LOLIE0_MASK;
  830. }
  831. else
  832. {
  833. mcg_c8 = MCG->C8;
  834. mcg_c8 &= ~MCG_C8_LOCS1_MASK;
  835. if (kMCG_MonitorInt == mode)
  836. {
  837. mcg_c8 &= ~MCG_C8_LOLRE_MASK;
  838. }
  839. else
  840. {
  841. mcg_c8 |= MCG_C8_LOLRE_MASK;
  842. }
  843. MCG->C8 = mcg_c8;
  844. MCG->C6 |= MCG_C6_LOLIE0_MASK;
  845. }
  846. }
  847. uint32_t CLOCK_GetStatusFlags(void)
  848. {
  849. uint32_t ret = 0U;
  850. uint8_t mcg_s = MCG->S;
  851. if (MCG->SC & MCG_SC_LOCS0_MASK)
  852. {
  853. ret |= kMCG_Osc0LostFlag;
  854. }
  855. if (mcg_s & MCG_S_OSCINIT0_MASK)
  856. {
  857. ret |= kMCG_Osc0InitFlag;
  858. }
  859. if (MCG->C8 & MCG_C8_LOCS1_MASK)
  860. {
  861. ret |= kMCG_RtcOscLostFlag;
  862. }
  863. if (mcg_s & MCG_S_LOLS0_MASK)
  864. {
  865. ret |= kMCG_Pll0LostFlag;
  866. }
  867. if (mcg_s & MCG_S_LOCK0_MASK)
  868. {
  869. ret |= kMCG_Pll0LockFlag;
  870. }
  871. return ret;
  872. }
  873. void CLOCK_ClearStatusFlags(uint32_t mask)
  874. {
  875. uint8_t reg;
  876. if (mask & kMCG_Osc0LostFlag)
  877. {
  878. MCG->SC &= ~MCG_SC_ATMF_MASK;
  879. }
  880. if (mask & kMCG_RtcOscLostFlag)
  881. {
  882. reg = MCG->C8;
  883. MCG->C8 = reg;
  884. }
  885. if (mask & kMCG_Pll0LostFlag)
  886. {
  887. MCG->S = MCG_S_LOLS0_MASK;
  888. }
  889. }
  890. void CLOCK_InitOsc0(osc_config_t const *config)
  891. {
  892. uint8_t range = CLOCK_GetOscRangeFromFreq(config->freq);
  893. OSC_SetCapLoad(OSC0, config->capLoad);
  894. OSC_SetExtRefClkConfig(OSC0, &config->oscerConfig);
  895. MCG->C2 = ((MCG->C2 & ~OSC_MODE_MASK) | MCG_C2_RANGE(range) | (uint8_t)config->workMode);
  896. if ((kOSC_ModeExt != config->workMode) && (OSC0->CR & OSC_CR_ERCLKEN_MASK))
  897. {
  898. /* Wait for stable. */
  899. while (!(MCG->S & MCG_S_OSCINIT0_MASK))
  900. {
  901. }
  902. }
  903. }
  904. void CLOCK_DeinitOsc0(void)
  905. {
  906. OSC0->CR = 0U;
  907. MCG->C2 &= ~OSC_MODE_MASK;
  908. }
  909. status_t CLOCK_TrimInternalRefClk(uint32_t extFreq, uint32_t desireFreq, uint32_t *actualFreq, mcg_atm_select_t atms)
  910. {
  911. uint32_t multi; /* extFreq / desireFreq */
  912. uint32_t actv; /* Auto trim value. */
  913. uint8_t mcg_sc;
  914. static const uint32_t trimRange[2][2] = {
  915. /* Min Max */
  916. {TRIM_SIRC_MIN, TRIM_SIRC_MAX}, /* Slow IRC. */
  917. {TRIM_FIRC_MIN, TRIM_FIRC_MAX} /* Fast IRC. */
  918. };
  919. if ((extFreq > TRIM_REF_CLK_MAX) || (extFreq < TRIM_REF_CLK_MIN))
  920. {
  921. return kStatus_MCG_AtmBusClockInvalid;
  922. }
  923. /* Check desired frequency range. */
  924. if ((desireFreq < trimRange[atms][0]) || (desireFreq > trimRange[atms][1]))
  925. {
  926. return kStatus_MCG_AtmDesiredFreqInvalid;
  927. }
  928. /*
  929. Make sure internal reference clock is not used to generate bus clock.
  930. Here only need to check (MCG_S_IREFST == 1).
  931. */
  932. if (MCG_S_IREFST(kMCG_FllSrcInternal) == (MCG->S & MCG_S_IREFST_MASK))
  933. {
  934. return kStatus_MCG_AtmIrcUsed;
  935. }
  936. multi = extFreq / desireFreq;
  937. actv = multi * 21U;
  938. if (kMCG_AtmSel4m == atms)
  939. {
  940. actv *= 128U;
  941. }
  942. /* Now begin to start trim. */
  943. MCG->ATCVL = (uint8_t)actv;
  944. MCG->ATCVH = (uint8_t)(actv >> 8U);
  945. mcg_sc = MCG->SC;
  946. mcg_sc &= ~(MCG_SC_ATMS_MASK | MCG_SC_LOCS0_MASK);
  947. mcg_sc |= (MCG_SC_ATMF_MASK | MCG_SC_ATMS(atms));
  948. MCG->SC = (mcg_sc | MCG_SC_ATME_MASK);
  949. /* Wait for finished. */
  950. while (MCG->SC & MCG_SC_ATME_MASK)
  951. {
  952. }
  953. /* Error occurs? */
  954. if (MCG->SC & MCG_SC_ATMF_MASK)
  955. {
  956. /* Clear the failed flag. */
  957. MCG->SC = mcg_sc;
  958. return kStatus_MCG_AtmHardwareFail;
  959. }
  960. *actualFreq = extFreq / multi;
  961. if (kMCG_AtmSel4m == atms)
  962. {
  963. s_fastIrcFreq = *actualFreq;
  964. }
  965. else
  966. {
  967. s_slowIrcFreq = *actualFreq;
  968. }
  969. return kStatus_Success;
  970. }
  971. mcg_mode_t CLOCK_GetMode(void)
  972. {
  973. mcg_mode_t mode = kMCG_ModeError;
  974. uint32_t clkst = MCG_S_CLKST_VAL;
  975. uint32_t irefst = MCG_S_IREFST_VAL;
  976. uint32_t lp = MCG_C2_LP_VAL;
  977. uint32_t pllst = MCG_S_PLLST_VAL;
  978. /*------------------------------------------------------------------
  979. Mode and Registers
  980. ____________________________________________________________________
  981. Mode | CLKST | IREFST | PLLST | LP
  982. ____________________________________________________________________
  983. FEI | 00(FLL) | 1(INT) | 0(FLL) | X
  984. ____________________________________________________________________
  985. FEE | 00(FLL) | 0(EXT) | 0(FLL) | X
  986. ____________________________________________________________________
  987. FBE | 10(EXT) | 0(EXT) | 0(FLL) | 0(NORMAL)
  988. ____________________________________________________________________
  989. FBI | 01(INT) | 1(INT) | 0(FLL) | 0(NORMAL)
  990. ____________________________________________________________________
  991. BLPI | 01(INT) | 1(INT) | 0(FLL) | 1(LOW POWER)
  992. ____________________________________________________________________
  993. BLPE | 10(EXT) | 0(EXT) | X | 1(LOW POWER)
  994. ____________________________________________________________________
  995. PEE | 11(PLL) | 0(EXT) | 1(PLL) | X
  996. ____________________________________________________________________
  997. PBE | 10(EXT) | 0(EXT) | 1(PLL) | O(NORMAL)
  998. ____________________________________________________________________
  999. PBI | 01(INT) | 1(INT) | 1(PLL) | 0(NORMAL)
  1000. ____________________________________________________________________
  1001. PEI | 11(PLL) | 1(INT) | 1(PLL) | X
  1002. ____________________________________________________________________
  1003. ----------------------------------------------------------------------*/
  1004. switch (clkst)
  1005. {
  1006. case kMCG_ClkOutStatFll:
  1007. if (kMCG_FllSrcExternal == irefst)
  1008. {
  1009. mode = kMCG_ModeFEE;
  1010. }
  1011. else
  1012. {
  1013. mode = kMCG_ModeFEI;
  1014. }
  1015. break;
  1016. case kMCG_ClkOutStatInt:
  1017. if (lp)
  1018. {
  1019. mode = kMCG_ModeBLPI;
  1020. }
  1021. else
  1022. {
  1023. {
  1024. mode = kMCG_ModeFBI;
  1025. }
  1026. }
  1027. break;
  1028. case kMCG_ClkOutStatExt:
  1029. if (lp)
  1030. {
  1031. mode = kMCG_ModeBLPE;
  1032. }
  1033. else
  1034. {
  1035. if (kMCG_PllstPll == pllst)
  1036. {
  1037. mode = kMCG_ModePBE;
  1038. }
  1039. else
  1040. {
  1041. mode = kMCG_ModeFBE;
  1042. }
  1043. }
  1044. break;
  1045. case kMCG_ClkOutStatPll:
  1046. {
  1047. mode = kMCG_ModePEE;
  1048. }
  1049. break;
  1050. default:
  1051. break;
  1052. }
  1053. return mode;
  1054. }
  1055. status_t CLOCK_SetFeiMode(mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1056. {
  1057. uint8_t mcg_c4;
  1058. bool change_drs = false;
  1059. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1060. mcg_mode_t mode = CLOCK_GetMode();
  1061. if (!((kMCG_ModeFEI == mode) || (kMCG_ModeFBI == mode) || (kMCG_ModeFBE == mode) || (kMCG_ModeFEE == mode)))
  1062. {
  1063. return kStatus_MCG_ModeUnreachable;
  1064. }
  1065. #endif
  1066. mcg_c4 = MCG->C4;
  1067. /*
  1068. Errata: ERR007993
  1069. Workaround: Invert MCG_C4[DMX32] or change MCG_C4[DRST_DRS] before
  1070. reference clock source changes, then reset to previous value after
  1071. reference clock changes.
  1072. */
  1073. if (kMCG_FllSrcExternal == MCG_S_IREFST_VAL)
  1074. {
  1075. change_drs = true;
  1076. /* Change the LSB of DRST_DRS. */
  1077. MCG->C4 ^= (1U << MCG_C4_DRST_DRS_SHIFT);
  1078. }
  1079. /* Set CLKS and IREFS. */
  1080. MCG->C1 =
  1081. ((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_IREFS_MASK))) | (MCG_C1_CLKS(kMCG_ClkOutSrcOut) /* CLKS = 0 */
  1082. | MCG_C1_IREFS(kMCG_FllSrcInternal)); /* IREFS = 1 */
  1083. /* Wait and check status. */
  1084. while (kMCG_FllSrcInternal != MCG_S_IREFST_VAL)
  1085. {
  1086. }
  1087. /* Errata: ERR007993 */
  1088. if (change_drs)
  1089. {
  1090. MCG->C4 = mcg_c4;
  1091. }
  1092. /* In FEI mode, the MCG_C4[DMX32] is set to 0U. */
  1093. MCG->C4 = (mcg_c4 & ~(MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) | (MCG_C4_DMX32(dmx32) | MCG_C4_DRST_DRS(drs));
  1094. /* Check MCG_S[CLKST] */
  1095. while (kMCG_ClkOutStatFll != MCG_S_CLKST_VAL)
  1096. {
  1097. }
  1098. /* Wait for FLL stable time. */
  1099. if (fllStableDelay)
  1100. {
  1101. fllStableDelay();
  1102. }
  1103. return kStatus_Success;
  1104. }
  1105. status_t CLOCK_SetFeeMode(uint8_t frdiv, mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1106. {
  1107. uint8_t mcg_c4;
  1108. bool change_drs = false;
  1109. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1110. mcg_mode_t mode = CLOCK_GetMode();
  1111. if (!((kMCG_ModeFEE == mode) || (kMCG_ModeFBI == mode) || (kMCG_ModeFBE == mode) || (kMCG_ModeFEI == mode)))
  1112. {
  1113. return kStatus_MCG_ModeUnreachable;
  1114. }
  1115. #endif
  1116. mcg_c4 = MCG->C4;
  1117. /*
  1118. Errata: ERR007993
  1119. Workaround: Invert MCG_C4[DMX32] or change MCG_C4[DRST_DRS] before
  1120. reference clock source changes, then reset to previous value after
  1121. reference clock changes.
  1122. */
  1123. if (kMCG_FllSrcInternal == MCG_S_IREFST_VAL)
  1124. {
  1125. change_drs = true;
  1126. /* Change the LSB of DRST_DRS. */
  1127. MCG->C4 ^= (1U << MCG_C4_DRST_DRS_SHIFT);
  1128. }
  1129. /* Set CLKS and IREFS. */
  1130. MCG->C1 = ((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_FRDIV_MASK | MCG_C1_IREFS_MASK)) |
  1131. (MCG_C1_CLKS(kMCG_ClkOutSrcOut) /* CLKS = 0 */
  1132. | MCG_C1_FRDIV(frdiv) /* FRDIV */
  1133. | MCG_C1_IREFS(kMCG_FllSrcExternal))); /* IREFS = 0 */
  1134. /* If use external crystal as clock source, wait for it stable. */
  1135. if (MCG_C7_OSCSEL(kMCG_OscselOsc) == (MCG->C7 & MCG_C7_OSCSEL_MASK))
  1136. {
  1137. if (MCG->C2 & MCG_C2_EREFS_MASK)
  1138. {
  1139. while (!(MCG->S & MCG_S_OSCINIT0_MASK))
  1140. {
  1141. }
  1142. }
  1143. }
  1144. /* Wait and check status. */
  1145. while (kMCG_FllSrcExternal != MCG_S_IREFST_VAL)
  1146. {
  1147. }
  1148. /* Errata: ERR007993 */
  1149. if (change_drs)
  1150. {
  1151. MCG->C4 = mcg_c4;
  1152. }
  1153. /* Set DRS and DMX32. */
  1154. mcg_c4 = ((mcg_c4 & ~(MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) | (MCG_C4_DMX32(dmx32) | MCG_C4_DRST_DRS(drs)));
  1155. MCG->C4 = mcg_c4;
  1156. /* Wait for DRST_DRS update. */
  1157. while (MCG->C4 != mcg_c4)
  1158. {
  1159. }
  1160. /* Check MCG_S[CLKST] */
  1161. while (kMCG_ClkOutStatFll != MCG_S_CLKST_VAL)
  1162. {
  1163. }
  1164. /* Wait for FLL stable time. */
  1165. if (fllStableDelay)
  1166. {
  1167. fllStableDelay();
  1168. }
  1169. return kStatus_Success;
  1170. }
  1171. status_t CLOCK_SetFbiMode(mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1172. {
  1173. uint8_t mcg_c4;
  1174. bool change_drs = false;
  1175. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1176. mcg_mode_t mode = CLOCK_GetMode();
  1177. if (!((kMCG_ModeFEE == mode) || (kMCG_ModeFBI == mode) || (kMCG_ModeFBE == mode) || (kMCG_ModeFEI == mode) ||
  1178. (kMCG_ModeBLPI == mode)))
  1179. {
  1180. return kStatus_MCG_ModeUnreachable;
  1181. }
  1182. #endif
  1183. mcg_c4 = MCG->C4;
  1184. MCG->C2 &= ~MCG_C2_LP_MASK; /* Disable lowpower. */
  1185. /*
  1186. Errata: ERR007993
  1187. Workaround: Invert MCG_C4[DMX32] or change MCG_C4[DRST_DRS] before
  1188. reference clock source changes, then reset to previous value after
  1189. reference clock changes.
  1190. */
  1191. if (kMCG_FllSrcExternal == MCG_S_IREFST_VAL)
  1192. {
  1193. change_drs = true;
  1194. /* Change the LSB of DRST_DRS. */
  1195. MCG->C4 ^= (1U << MCG_C4_DRST_DRS_SHIFT);
  1196. }
  1197. /* Set CLKS and IREFS. */
  1198. MCG->C1 =
  1199. ((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_IREFS_MASK)) | (MCG_C1_CLKS(kMCG_ClkOutSrcInternal) /* CLKS = 1 */
  1200. | MCG_C1_IREFS(kMCG_FllSrcInternal))); /* IREFS = 1 */
  1201. /* Wait and check status. */
  1202. while (kMCG_FllSrcInternal != MCG_S_IREFST_VAL)
  1203. {
  1204. }
  1205. /* Errata: ERR007993 */
  1206. if (change_drs)
  1207. {
  1208. MCG->C4 = mcg_c4;
  1209. }
  1210. while (kMCG_ClkOutStatInt != MCG_S_CLKST_VAL)
  1211. {
  1212. }
  1213. MCG->C4 = (mcg_c4 & ~(MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) | (MCG_C4_DMX32(dmx32) | MCG_C4_DRST_DRS(drs));
  1214. /* Wait for FLL stable time. */
  1215. if (fllStableDelay)
  1216. {
  1217. fllStableDelay();
  1218. }
  1219. return kStatus_Success;
  1220. }
  1221. status_t CLOCK_SetFbeMode(uint8_t frdiv, mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1222. {
  1223. uint8_t mcg_c4;
  1224. bool change_drs = false;
  1225. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1226. mcg_mode_t mode = CLOCK_GetMode();
  1227. if (!((kMCG_ModeFEE == mode) || (kMCG_ModeFBI == mode) || (kMCG_ModeFBE == mode) || (kMCG_ModeFEI == mode) ||
  1228. (kMCG_ModePBE == mode) || (kMCG_ModeBLPE == mode)))
  1229. {
  1230. return kStatus_MCG_ModeUnreachable;
  1231. }
  1232. #endif
  1233. /* Change to FLL mode. */
  1234. MCG->C6 &= ~MCG_C6_PLLS_MASK;
  1235. while (MCG->S & MCG_S_PLLST_MASK)
  1236. {
  1237. }
  1238. /* Set LP bit to enable the FLL */
  1239. MCG->C2 &= ~MCG_C2_LP_MASK;
  1240. mcg_c4 = MCG->C4;
  1241. /*
  1242. Errata: ERR007993
  1243. Workaround: Invert MCG_C4[DMX32] or change MCG_C4[DRST_DRS] before
  1244. reference clock source changes, then reset to previous value after
  1245. reference clock changes.
  1246. */
  1247. if (kMCG_FllSrcInternal == MCG_S_IREFST_VAL)
  1248. {
  1249. change_drs = true;
  1250. /* Change the LSB of DRST_DRS. */
  1251. MCG->C4 ^= (1U << MCG_C4_DRST_DRS_SHIFT);
  1252. }
  1253. /* Set CLKS and IREFS. */
  1254. MCG->C1 = ((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_FRDIV_MASK | MCG_C1_IREFS_MASK)) |
  1255. (MCG_C1_CLKS(kMCG_ClkOutSrcExternal) /* CLKS = 2 */
  1256. | MCG_C1_FRDIV(frdiv) /* FRDIV = frdiv */
  1257. | MCG_C1_IREFS(kMCG_FllSrcExternal))); /* IREFS = 0 */
  1258. /* If use external crystal as clock source, wait for it stable. */
  1259. if (MCG_C7_OSCSEL(kMCG_OscselOsc) == (MCG->C7 & MCG_C7_OSCSEL_MASK))
  1260. {
  1261. if (MCG->C2 & MCG_C2_EREFS_MASK)
  1262. {
  1263. while (!(MCG->S & MCG_S_OSCINIT0_MASK))
  1264. {
  1265. }
  1266. }
  1267. }
  1268. /* Wait for Reference clock Status bit to clear */
  1269. while (kMCG_FllSrcExternal != MCG_S_IREFST_VAL)
  1270. {
  1271. }
  1272. /* Errata: ERR007993 */
  1273. if (change_drs)
  1274. {
  1275. MCG->C4 = mcg_c4;
  1276. }
  1277. /* Set DRST_DRS and DMX32. */
  1278. mcg_c4 = ((mcg_c4 & ~(MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) | (MCG_C4_DMX32(dmx32) | MCG_C4_DRST_DRS(drs)));
  1279. /* Wait for clock status bits to show clock source is ext ref clk */
  1280. while (kMCG_ClkOutStatExt != MCG_S_CLKST_VAL)
  1281. {
  1282. }
  1283. /* Wait for fll stable time. */
  1284. if (fllStableDelay)
  1285. {
  1286. fllStableDelay();
  1287. }
  1288. return kStatus_Success;
  1289. }
  1290. status_t CLOCK_SetBlpiMode(void)
  1291. {
  1292. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1293. if (MCG_S_CLKST_VAL != kMCG_ClkOutStatInt)
  1294. {
  1295. return kStatus_MCG_ModeUnreachable;
  1296. }
  1297. #endif /* MCG_CONFIG_CHECK_PARAM */
  1298. /* Set LP. */
  1299. MCG->C2 |= MCG_C2_LP_MASK;
  1300. return kStatus_Success;
  1301. }
  1302. status_t CLOCK_SetBlpeMode(void)
  1303. {
  1304. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1305. if (MCG_S_CLKST_VAL != kMCG_ClkOutStatExt)
  1306. {
  1307. return kStatus_MCG_ModeUnreachable;
  1308. }
  1309. #endif
  1310. /* Set LP bit to enter BLPE mode. */
  1311. MCG->C2 |= MCG_C2_LP_MASK;
  1312. return kStatus_Success;
  1313. }
  1314. status_t CLOCK_SetPbeMode(mcg_pll_clk_select_t pllcs, mcg_pll_config_t const *config)
  1315. {
  1316. assert(config);
  1317. /*
  1318. This function is designed to change MCG to PBE mode from PEE/BLPE/FBE,
  1319. but with this workflow, the source mode could be all modes except PEI/PBI.
  1320. */
  1321. MCG->C2 &= ~MCG_C2_LP_MASK; /* Disable lowpower. */
  1322. /* Change to use external clock first. */
  1323. MCG->C1 = ((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_IREFS_MASK)) | MCG_C1_CLKS(kMCG_ClkOutSrcExternal));
  1324. /* Wait for CLKST clock status bits to show clock source is ext ref clk */
  1325. while ((MCG->S & (MCG_S_IREFST_MASK | MCG_S_CLKST_MASK)) !=
  1326. (MCG_S_IREFST(kMCG_FllSrcExternal) | MCG_S_CLKST(kMCG_ClkOutStatExt)))
  1327. {
  1328. }
  1329. /* Disable PLL first, then configure PLL. */
  1330. MCG->C6 &= ~MCG_C6_PLLS_MASK;
  1331. while (MCG->S & MCG_S_PLLST_MASK)
  1332. {
  1333. }
  1334. /* Configure the PLL. */
  1335. {
  1336. CLOCK_EnablePll0(config);
  1337. }
  1338. /* Change to PLL mode. */
  1339. MCG->C6 |= MCG_C6_PLLS_MASK;
  1340. /* Wait for PLL mode changed. */
  1341. while (!(MCG->S & MCG_S_PLLST_MASK))
  1342. {
  1343. }
  1344. return kStatus_Success;
  1345. }
  1346. status_t CLOCK_SetPeeMode(void)
  1347. {
  1348. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1349. mcg_mode_t mode = CLOCK_GetMode();
  1350. if (kMCG_ModePBE != mode)
  1351. {
  1352. return kStatus_MCG_ModeUnreachable;
  1353. }
  1354. #endif
  1355. /* Change to use PLL/FLL output clock first. */
  1356. MCG->C1 = (MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcOut);
  1357. /* Wait for clock status bits to update */
  1358. while (MCG_S_CLKST_VAL != kMCG_ClkOutStatPll)
  1359. {
  1360. }
  1361. return kStatus_Success;
  1362. }
  1363. status_t CLOCK_ExternalModeToFbeModeQuick(void)
  1364. {
  1365. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1366. if (MCG->S & MCG_S_IREFST_MASK)
  1367. {
  1368. return kStatus_MCG_ModeInvalid;
  1369. }
  1370. #endif /* MCG_CONFIG_CHECK_PARAM */
  1371. /* Disable low power */
  1372. MCG->C2 &= ~MCG_C2_LP_MASK;
  1373. MCG->C1 = ((MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcExternal));
  1374. while (MCG_S_CLKST_VAL != kMCG_ClkOutStatExt)
  1375. {
  1376. }
  1377. /* Disable PLL. */
  1378. MCG->C6 &= ~MCG_C6_PLLS_MASK;
  1379. while (MCG->S & MCG_S_PLLST_MASK)
  1380. {
  1381. }
  1382. return kStatus_Success;
  1383. }
  1384. status_t CLOCK_InternalModeToFbiModeQuick(void)
  1385. {
  1386. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1387. if (!(MCG->S & MCG_S_IREFST_MASK))
  1388. {
  1389. return kStatus_MCG_ModeInvalid;
  1390. }
  1391. #endif
  1392. /* Disable low power */
  1393. MCG->C2 &= ~MCG_C2_LP_MASK;
  1394. MCG->C1 = ((MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcInternal));
  1395. while (MCG_S_CLKST_VAL != kMCG_ClkOutStatInt)
  1396. {
  1397. }
  1398. return kStatus_Success;
  1399. }
  1400. status_t CLOCK_BootToFeiMode(mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1401. {
  1402. return CLOCK_SetFeiMode(dmx32, drs, fllStableDelay);
  1403. }
  1404. status_t CLOCK_BootToFeeMode(
  1405. mcg_oscsel_t oscsel, uint8_t frdiv, mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1406. {
  1407. CLOCK_SetExternalRefClkConfig(oscsel);
  1408. return CLOCK_SetFeeMode(frdiv, dmx32, drs, fllStableDelay);
  1409. }
  1410. status_t CLOCK_BootToBlpiMode(uint8_t fcrdiv, mcg_irc_mode_t ircs, uint8_t ircEnableMode)
  1411. {
  1412. /* If reset mode is FEI mode, set MCGIRCLK and always success. */
  1413. CLOCK_SetInternalRefClkConfig(ircEnableMode, ircs, fcrdiv);
  1414. /* If reset mode is not BLPI, first enter FBI mode. */
  1415. MCG->C1 = (MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcInternal);
  1416. while (MCG_S_CLKST_VAL != kMCG_ClkOutStatInt)
  1417. {
  1418. }
  1419. /* Enter BLPI mode. */
  1420. MCG->C2 |= MCG_C2_LP_MASK;
  1421. return kStatus_Success;
  1422. }
  1423. status_t CLOCK_BootToBlpeMode(mcg_oscsel_t oscsel)
  1424. {
  1425. CLOCK_SetExternalRefClkConfig(oscsel);
  1426. /* Set to FBE mode. */
  1427. MCG->C1 =
  1428. ((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_IREFS_MASK)) | (MCG_C1_CLKS(kMCG_ClkOutSrcExternal) /* CLKS = 2 */
  1429. | MCG_C1_IREFS(kMCG_FllSrcExternal))); /* IREFS = 0 */
  1430. /* If use external crystal as clock source, wait for it stable. */
  1431. if (MCG_C7_OSCSEL(kMCG_OscselOsc) == (MCG->C7 & MCG_C7_OSCSEL_MASK))
  1432. {
  1433. if (MCG->C2 & MCG_C2_EREFS_MASK)
  1434. {
  1435. while (!(MCG->S & MCG_S_OSCINIT0_MASK))
  1436. {
  1437. }
  1438. }
  1439. }
  1440. /* Wait for MCG_S[CLKST] and MCG_S[IREFST]. */
  1441. while ((MCG->S & (MCG_S_IREFST_MASK | MCG_S_CLKST_MASK)) !=
  1442. (MCG_S_IREFST(kMCG_FllSrcExternal) | MCG_S_CLKST(kMCG_ClkOutStatExt)))
  1443. {
  1444. }
  1445. /* In FBE now, start to enter BLPE. */
  1446. MCG->C2 |= MCG_C2_LP_MASK;
  1447. return kStatus_Success;
  1448. }
  1449. status_t CLOCK_BootToPeeMode(mcg_oscsel_t oscsel, mcg_pll_clk_select_t pllcs, mcg_pll_config_t const *config)
  1450. {
  1451. assert(config);
  1452. CLOCK_SetExternalRefClkConfig(oscsel);
  1453. CLOCK_SetPbeMode(pllcs, config);
  1454. /* Change to use PLL output clock. */
  1455. MCG->C1 = (MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcOut);
  1456. while (MCG_S_CLKST_VAL != kMCG_ClkOutStatPll)
  1457. {
  1458. }
  1459. return kStatus_Success;
  1460. }
  1461. /*
  1462. The transaction matrix. It defines the path for mode switch, the row is for
  1463. current mode and the column is target mode.
  1464. For example, switch from FEI to PEE:
  1465. 1. Current mode FEI, next mode is mcgModeMatrix[FEI][PEE] = FBE, so swith to FBE.
  1466. 2. Current mode FBE, next mode is mcgModeMatrix[FBE][PEE] = PBE, so swith to PBE.
  1467. 3. Current mode PBE, next mode is mcgModeMatrix[PBE][PEE] = PEE, so swith to PEE.
  1468. Thus the MCG mode has changed from FEI to PEE.
  1469. */
  1470. static const mcg_mode_t mcgModeMatrix[8][8] = {
  1471. {kMCG_ModeFEI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFEE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE,
  1472. kMCG_ModeFBE}, /* FEI */
  1473. {kMCG_ModeFEI, kMCG_ModeFBI, kMCG_ModeBLPI, kMCG_ModeFEE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE,
  1474. kMCG_ModeFBE}, /* FBI */
  1475. {kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeBLPI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFBI,
  1476. kMCG_ModeFBI}, /* BLPI */
  1477. {kMCG_ModeFEI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFEE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE,
  1478. kMCG_ModeFBE}, /* FEE */
  1479. {kMCG_ModeFEI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFEE, kMCG_ModeFBE, kMCG_ModeBLPE, kMCG_ModePBE,
  1480. kMCG_ModePBE}, /* FBE */
  1481. {kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeBLPE, kMCG_ModePBE,
  1482. kMCG_ModePBE}, /* BLPE */
  1483. {kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeBLPE, kMCG_ModePBE,
  1484. kMCG_ModePEE}, /* PBE */
  1485. {kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE,
  1486. kMCG_ModePBE} /* PEE */
  1487. /* FEI FBI BLPI FEE FBE BLPE PBE PEE */
  1488. };
  1489. status_t CLOCK_SetMcgConfig(const mcg_config_t *config)
  1490. {
  1491. mcg_mode_t next_mode;
  1492. status_t status = kStatus_Success;
  1493. mcg_pll_clk_select_t pllcs = kMCG_PllClkSelPll0;
  1494. /* If need to change external clock, MCG_C7[OSCSEL]. */
  1495. if (MCG_C7_OSCSEL_VAL != config->oscsel)
  1496. {
  1497. /* If external clock is in use, change to FEI first. */
  1498. if (kMCG_FllSrcExternal == MCG_S_IREFST_VAL)
  1499. {
  1500. CLOCK_ExternalModeToFbeModeQuick();
  1501. CLOCK_SetFeiMode(config->dmx32, config->drs, NULL);
  1502. }
  1503. CLOCK_SetExternalRefClkConfig(config->oscsel);
  1504. }
  1505. /* Re-configure MCGIRCLK, if MCGIRCLK is used as system clock source, then change to FEI/PEI first. */
  1506. if (MCG_S_CLKST_VAL == kMCG_ClkOutStatInt)
  1507. {
  1508. MCG->C2 &= ~MCG_C2_LP_MASK; /* Disable lowpower. */
  1509. {
  1510. CLOCK_SetFeiMode(config->dmx32, config->drs, CLOCK_FllStableDelay);
  1511. }
  1512. }
  1513. /* Configure MCGIRCLK. */
  1514. CLOCK_SetInternalRefClkConfig(config->irclkEnableMode, config->ircs, config->fcrdiv);
  1515. next_mode = CLOCK_GetMode();
  1516. do
  1517. {
  1518. next_mode = mcgModeMatrix[next_mode][config->mcgMode];
  1519. switch (next_mode)
  1520. {
  1521. case kMCG_ModeFEI:
  1522. status = CLOCK_SetFeiMode(config->dmx32, config->drs, CLOCK_FllStableDelay);
  1523. break;
  1524. case kMCG_ModeFEE:
  1525. status = CLOCK_SetFeeMode(config->frdiv, config->dmx32, config->drs, CLOCK_FllStableDelay);
  1526. break;
  1527. case kMCG_ModeFBI:
  1528. status = CLOCK_SetFbiMode(config->dmx32, config->drs, NULL);
  1529. break;
  1530. case kMCG_ModeFBE:
  1531. status = CLOCK_SetFbeMode(config->frdiv, config->dmx32, config->drs, NULL);
  1532. break;
  1533. case kMCG_ModeBLPI:
  1534. status = CLOCK_SetBlpiMode();
  1535. break;
  1536. case kMCG_ModeBLPE:
  1537. status = CLOCK_SetBlpeMode();
  1538. break;
  1539. case kMCG_ModePBE:
  1540. /* If target mode is not PBE or PEE, then only need to set CLKS = EXT here. */
  1541. if ((kMCG_ModePEE == config->mcgMode) || (kMCG_ModePBE == config->mcgMode))
  1542. {
  1543. {
  1544. status = CLOCK_SetPbeMode(pllcs, &config->pll0Config);
  1545. }
  1546. }
  1547. else
  1548. {
  1549. MCG->C1 = ((MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcExternal));
  1550. while (MCG_S_CLKST_VAL != kMCG_ClkOutStatExt)
  1551. {
  1552. }
  1553. }
  1554. break;
  1555. case kMCG_ModePEE:
  1556. status = CLOCK_SetPeeMode();
  1557. break;
  1558. default:
  1559. break;
  1560. }
  1561. if (kStatus_Success != status)
  1562. {
  1563. return status;
  1564. }
  1565. } while (next_mode != config->mcgMode);
  1566. if (config->pll0Config.enableMode & kMCG_PllEnableIndependent)
  1567. {
  1568. CLOCK_EnablePll0(&config->pll0Config);
  1569. }
  1570. else
  1571. {
  1572. MCG->C5 &= ~(uint32_t)kMCG_PllEnableIndependent;
  1573. }
  1574. return kStatus_Success;
  1575. }