You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2213 regels
67 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016 - 2019, NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_clock.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.clock"
  15. #endif
  16. /* Macro definition remap workaround. */
  17. #if (defined(MCG_C2_EREFS_MASK) && !(defined(MCG_C2_EREFS0_MASK)))
  18. #define MCG_C2_EREFS0_MASK MCG_C2_EREFS_MASK
  19. #endif
  20. #if (defined(MCG_C2_HGO_MASK) && !(defined(MCG_C2_HGO0_MASK)))
  21. #define MCG_C2_HGO0_MASK MCG_C2_HGO_MASK
  22. #endif
  23. #if (defined(MCG_C2_RANGE_MASK) && !(defined(MCG_C2_RANGE0_MASK)))
  24. #define MCG_C2_RANGE0_MASK MCG_C2_RANGE_MASK
  25. #endif
  26. #if (defined(MCG_C6_CME_MASK) && !(defined(MCG_C6_CME0_MASK)))
  27. #define MCG_C6_CME0_MASK MCG_C6_CME_MASK
  28. #endif
  29. /* PLL fixed multiplier when there is not PRDIV and VDIV. */
  30. #define PLL_FIXED_MULT (375U)
  31. /* Max frequency of the reference clock used for internal clock trim. */
  32. #define TRIM_REF_CLK_MIN (8000000U)
  33. /* Min frequency of the reference clock used for internal clock trim. */
  34. #define TRIM_REF_CLK_MAX (16000000U)
  35. /* Max trim value of fast internal reference clock. */
  36. #define TRIM_FIRC_MAX (5000000U)
  37. /* Min trim value of fast internal reference clock. */
  38. #define TRIM_FIRC_MIN (3000000U)
  39. /* Max trim value of fast internal reference clock. */
  40. #define TRIM_SIRC_MAX (39063U)
  41. /* Min trim value of fast internal reference clock. */
  42. #define TRIM_SIRC_MIN (31250U)
  43. #define MCG_S_IRCST_VAL (((uint32_t)MCG->S & (uint32_t)MCG_S_IRCST_MASK) >> (uint32_t)MCG_S_IRCST_SHIFT)
  44. #define MCG_S_CLKST_VAL (((uint32_t)MCG->S & (uint32_t)MCG_S_CLKST_MASK) >> (uint32_t)MCG_S_CLKST_SHIFT)
  45. #define MCG_S_IREFST_VAL (((uint32_t)MCG->S & (uint32_t)MCG_S_IREFST_MASK) >> (uint32_t)MCG_S_IREFST_SHIFT)
  46. #define MCG_S_PLLST_VAL (((uint32_t)MCG->S & (uint32_t)MCG_S_PLLST_MASK) >> (uint32_t)MCG_S_PLLST_SHIFT)
  47. #define MCG_C1_FRDIV_VAL ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT)
  48. #define MCG_C2_LP_VAL (((uint32_t)MCG->C2 & (uint32_t)MCG_C2_LP_MASK) >> (uint32_t)MCG_C2_LP_SHIFT)
  49. #define MCG_C2_RANGE_VAL ((MCG->C2 & MCG_C2_RANGE_MASK) >> MCG_C2_RANGE_SHIFT)
  50. #define MCG_SC_FCRDIV_VAL ((MCG->SC & MCG_SC_FCRDIV_MASK) >> MCG_SC_FCRDIV_SHIFT)
  51. #define MCG_S2_PLLCST_VAL (((uint32_t)MCG->S2 & (uint32_t)MCG_S2_PLLCST_MASK) >> (uint32_t)MCG_S2_PLLCST_SHIFT)
  52. #define MCG_C7_OSCSEL_VAL ((MCG->C7 & MCG_C7_OSCSEL_MASK) >> MCG_C7_OSCSEL_SHIFT)
  53. #define MCG_C4_DMX32_VAL ((MCG->C4 & MCG_C4_DMX32_MASK) >> MCG_C4_DMX32_SHIFT)
  54. #define MCG_C4_DRST_DRS_VAL ((MCG->C4 & MCG_C4_DRST_DRS_MASK) >> MCG_C4_DRST_DRS_SHIFT)
  55. #define MCG_C7_PLL32KREFSEL_VAL ((MCG->C7 & MCG_C7_PLL32KREFSEL_MASK) >> MCG_C7_PLL32KREFSEL_SHIFT)
  56. #define MCG_C5_PLLREFSEL0_VAL ((MCG->C5 & MCG_C5_PLLREFSEL0_MASK) >> MCG_C5_PLLREFSEL0_SHIFT)
  57. #define MCG_C11_PLLREFSEL1_VAL ((MCG->C11 & MCG_C11_PLLREFSEL1_MASK) >> MCG_C11_PLLREFSEL1_SHIFT)
  58. #define MCG_C11_PRDIV1_VAL ((MCG->C11 & MCG_C11_PRDIV1_MASK) >> MCG_C11_PRDIV1_SHIFT)
  59. #define MCG_C12_VDIV1_VAL ((MCG->C12 & MCG_C12_VDIV1_MASK) >> MCG_C12_VDIV1_SHIFT)
  60. #define MCG_C5_PRDIV0_VAL ((uint8_t)(MCG->C5 & MCG_C5_PRDIV0_MASK) >> MCG_C5_PRDIV0_SHIFT)
  61. #define MCG_C6_VDIV0_VAL ((uint8_t)(MCG->C6 & MCG_C6_VDIV0_MASK) >> MCG_C6_VDIV0_SHIFT)
  62. #define OSC_MODE_MASK (MCG_C2_EREFS0_MASK | MCG_C2_HGO0_MASK | MCG_C2_RANGE0_MASK)
  63. #define SIM_CLKDIV1_OUTDIV1_VAL ((uint32_t)(SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV1_MASK) >> SIM_CLKDIV1_OUTDIV1_SHIFT)
  64. #define SIM_CLKDIV1_OUTDIV2_VAL ((uint32_t)(SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV2_MASK) >> SIM_CLKDIV1_OUTDIV2_SHIFT)
  65. #define SIM_CLKDIV1_OUTDIV3_VAL ((uint32_t)(SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV3_MASK) >> SIM_CLKDIV1_OUTDIV3_SHIFT)
  66. #define SIM_CLKDIV1_OUTDIV4_VAL ((uint32_t)(SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV4_MASK) >> SIM_CLKDIV1_OUTDIV4_SHIFT)
  67. #define SIM_SOPT1_OSC32KSEL_VAL ((SIM->SOPT1 & SIM_SOPT1_OSC32KSEL_MASK) >> SIM_SOPT1_OSC32KSEL_SHIFT)
  68. #define SIM_SOPT2_PLLFLLSEL_VAL ((SIM->SOPT2 & SIM_SOPT2_PLLFLLSEL_MASK) >> SIM_SOPT2_PLLFLLSEL_SHIFT)
  69. #define SIM_CLKDIV3_PLLFLLDIV_VAL ((SIM->CLKDIV3 & SIM_CLKDIV3_PLLFLLDIV_MASK) >> SIM_CLKDIV3_PLLFLLDIV_SHIFT)
  70. #define SIM_CLKDIV3_PLLFLLFRAC_VAL ((SIM->CLKDIV3 & SIM_CLKDIV3_PLLFLLFRAC_MASK) >> SIM_CLKDIV3_PLLFLLFRAC_SHIFT)
  71. /* MCG_S_CLKST definition. */
  72. enum _mcg_clkout_stat
  73. {
  74. kMCG_ClkOutStatFll, /* FLL. */
  75. kMCG_ClkOutStatInt, /* Internal clock. */
  76. kMCG_ClkOutStatExt, /* External clock. */
  77. kMCG_ClkOutStatPll /* PLL. */
  78. };
  79. /* MCG_S_PLLST definition. */
  80. enum _mcg_pllst
  81. {
  82. kMCG_PllstFll, /* FLL is used. */
  83. kMCG_PllstPll /* PLL is used. */
  84. };
  85. /*******************************************************************************
  86. * Variables
  87. ******************************************************************************/
  88. /* Slow internal reference clock frequency. */
  89. static uint32_t s_slowIrcFreq = 32768U;
  90. /* Fast internal reference clock frequency. */
  91. static uint32_t s_fastIrcFreq = 4000000U;
  92. /* External XTAL0 (OSC0) clock frequency. */
  93. volatile uint32_t g_xtal0Freq;
  94. /* External XTAL32K clock frequency. */
  95. volatile uint32_t g_xtal32Freq;
  96. /*******************************************************************************
  97. * Prototypes
  98. ******************************************************************************/
  99. /*!
  100. * @brief Get the MCG external reference clock frequency.
  101. *
  102. * Get the current MCG external reference clock frequency in Hz. It is
  103. * the frequency select by MCG_C7[OSCSEL]. This is an internal function.
  104. *
  105. * @return MCG external reference clock frequency in Hz.
  106. */
  107. static uint32_t CLOCK_GetMcgExtClkFreq(void);
  108. /*!
  109. * @brief Get the MCG FLL external reference clock frequency.
  110. *
  111. * Get the current MCG FLL external reference clock frequency in Hz. It is
  112. * the frequency after by MCG_C1[FRDIV]. This is an internal function.
  113. *
  114. * @return MCG FLL external reference clock frequency in Hz.
  115. */
  116. static uint32_t CLOCK_GetFllExtRefClkFreq(void);
  117. /*!
  118. * @brief Get the MCG FLL reference clock frequency.
  119. *
  120. * Get the current MCG FLL reference clock frequency in Hz. It is
  121. * the frequency select by MCG_C1[IREFS]. This is an internal function.
  122. *
  123. * @return MCG FLL reference clock frequency in Hz.
  124. */
  125. static uint32_t CLOCK_GetFllRefClkFreq(void);
  126. /*!
  127. * @brief Get the frequency of clock selected by MCG_C2[IRCS].
  128. *
  129. * This clock's two output:
  130. * 1. MCGOUTCLK when MCG_S[CLKST]=0.
  131. * 2. MCGIRCLK when MCG_C1[IRCLKEN]=1.
  132. *
  133. * @return The frequency in Hz.
  134. */
  135. static uint32_t CLOCK_GetInternalRefClkSelectFreq(void);
  136. /*!
  137. * @brief Get the MCG PLL/PLL0 reference clock frequency.
  138. *
  139. * Get the current MCG PLL/PLL0 reference clock frequency in Hz.
  140. * This is an internal function.
  141. *
  142. * @return MCG PLL/PLL0 reference clock frequency in Hz.
  143. */
  144. static uint32_t CLOCK_GetPll0RefFreq(void);
  145. /*!
  146. * @brief Calculate the RANGE value base on crystal frequency.
  147. *
  148. * To setup external crystal oscillator, must set the register bits RANGE
  149. * base on the crystal frequency. This function returns the RANGE base on the
  150. * input frequency. This is an internal function.
  151. *
  152. * @param freq Crystal frequency in Hz.
  153. * @return The RANGE value.
  154. */
  155. static uint8_t CLOCK_GetOscRangeFromFreq(uint32_t freq);
  156. #ifndef MCG_USER_CONFIG_FLL_STABLE_DELAY_EN
  157. /*!
  158. * @brief Delay function to wait FLL stable.
  159. *
  160. * Delay function to wait FLL stable in FEI mode or FEE mode, should wait at least
  161. * 1ms. Every time changes FLL setting, should wait this time for FLL stable.
  162. */
  163. static void CLOCK_FllStableDelay(void);
  164. #endif
  165. /*******************************************************************************
  166. * Code
  167. ******************************************************************************/
  168. #ifndef MCG_USER_CONFIG_FLL_STABLE_DELAY_EN
  169. static void CLOCK_FllStableDelay(void)
  170. {
  171. /*
  172. Should wait at least 1ms. Because in these modes, the core clock is 100MHz
  173. at most, so this function could obtain the 1ms delay.
  174. */
  175. volatile uint32_t i = 30000U;
  176. while (0U != (i--))
  177. {
  178. __NOP();
  179. }
  180. }
  181. #else /* With MCG_USER_CONFIG_FLL_STABLE_DELAY_EN defined. */
  182. /* Once user defines the MCG_USER_CONFIG_FLL_STABLE_DELAY_EN to use their own delay function, he has to
  183. * create his own CLOCK_FllStableDelay() function in application code. Since the clock functions in this
  184. * file would call the CLOCK_FllStableDelay() regardless how it is defined.
  185. */
  186. extern void CLOCK_FllStableDelay(void);
  187. #endif /* MCG_USER_CONFIG_FLL_STABLE_DELAY_EN */
  188. static uint32_t CLOCK_GetMcgExtClkFreq(void)
  189. {
  190. /* Please call CLOCK_SetXtal0Freq base on board setting before using OSC0 clock. */
  191. assert(0U != g_xtal0Freq);
  192. return g_xtal0Freq;
  193. }
  194. static uint32_t CLOCK_GetFllExtRefClkFreq(void)
  195. {
  196. /* FllExtRef = McgExtRef / FllExtRefDiv */
  197. uint8_t frdiv;
  198. uint8_t range;
  199. uint32_t freq = CLOCK_GetMcgExtClkFreq();
  200. frdiv = MCG_C1_FRDIV_VAL;
  201. freq >>= frdiv;
  202. range = MCG_C2_RANGE_VAL;
  203. /*
  204. When should use divider 32, 64, 128, 256, 512, 1024, 1280, 1536.
  205. 1. MCG_C7[OSCSEL] selects IRC48M.
  206. 2. MCG_C7[OSCSEL] selects OSC0 and MCG_C2[RANGE] is not 0.
  207. */
  208. if (((0U != range)))
  209. {
  210. switch (frdiv)
  211. {
  212. case 0:
  213. case 1:
  214. case 2:
  215. case 3:
  216. case 4:
  217. case 5:
  218. freq >>= 5u;
  219. break;
  220. case 6:
  221. /* 64*20=1280 */
  222. freq /= 20u;
  223. break;
  224. case 7:
  225. /* 128*12=1536 */
  226. freq /= 12u;
  227. break;
  228. default:
  229. freq = 0u;
  230. break;
  231. }
  232. }
  233. return freq;
  234. }
  235. static uint32_t CLOCK_GetInternalRefClkSelectFreq(void)
  236. {
  237. uint32_t freq;
  238. if ((uint8_t)kMCG_IrcSlow == MCG_S_IRCST_VAL)
  239. {
  240. /* Slow internal reference clock selected*/
  241. freq = s_slowIrcFreq;
  242. }
  243. else
  244. {
  245. /* Fast internal reference clock selected*/
  246. freq = s_fastIrcFreq >> MCG_SC_FCRDIV_VAL;
  247. }
  248. return freq;
  249. }
  250. static uint32_t CLOCK_GetFllRefClkFreq(void)
  251. {
  252. uint32_t freq;
  253. /* If use external reference clock. */
  254. if ((uint8_t)kMCG_FllSrcExternal == MCG_S_IREFST_VAL)
  255. {
  256. freq = CLOCK_GetFllExtRefClkFreq();
  257. }
  258. /* If use internal reference clock. */
  259. else
  260. {
  261. freq = s_slowIrcFreq;
  262. }
  263. return freq;
  264. }
  265. static uint32_t CLOCK_GetPll0RefFreq(void)
  266. {
  267. /* MCG external reference clock. */
  268. return CLOCK_GetMcgExtClkFreq();
  269. }
  270. static uint8_t CLOCK_GetOscRangeFromFreq(uint32_t freq)
  271. {
  272. uint8_t range;
  273. if (freq <= 39063U)
  274. {
  275. range = 0U;
  276. }
  277. else if (freq <= 8000000U)
  278. {
  279. range = 1U;
  280. }
  281. else
  282. {
  283. range = 2U;
  284. }
  285. return range;
  286. }
  287. /*!
  288. * brief Get the OSC0 external reference undivided clock frequency (OSC0ERCLK_UNDIV).
  289. *
  290. * return Clock frequency in Hz.
  291. */
  292. uint32_t CLOCK_GetOsc0ErClkUndivFreq(void)
  293. {
  294. uint32_t freq;
  295. if ((OSC0->CR & OSC_CR_ERCLKEN_MASK) != 0U)
  296. {
  297. /* Please call CLOCK_SetXtal0Freq base on board setting before using OSC0 clock. */
  298. assert(g_xtal0Freq);
  299. freq = g_xtal0Freq;
  300. }
  301. else
  302. {
  303. freq = 0U;
  304. }
  305. return freq;
  306. }
  307. /*!
  308. * brief Get the OSC0 external reference divided clock frequency.
  309. *
  310. * return Clock frequency in Hz.
  311. */
  312. uint32_t CLOCK_GetOsc0ErClkDivFreq(void)
  313. {
  314. uint32_t freq;
  315. uint8_t temp;
  316. if ((OSC0->CR & OSC_CR_ERCLKEN_MASK) != 0U)
  317. {
  318. /* Please call CLOCK_SetXtal0Freq base on board setting before using OSC0 clock. */
  319. assert(g_xtal0Freq);
  320. temp = OSC0->DIV & OSC_DIV_ERPS_MASK;
  321. freq = g_xtal0Freq >> ((temp) >> OSC_DIV_ERPS_SHIFT);
  322. }
  323. else
  324. {
  325. freq = 0U;
  326. }
  327. return freq;
  328. }
  329. /*!
  330. * brief Get the external reference 32K clock frequency (ERCLK32K).
  331. *
  332. * return Clock frequency in Hz.
  333. */
  334. uint32_t CLOCK_GetEr32kClkFreq(void)
  335. {
  336. uint32_t freq;
  337. switch (SIM_SOPT1_OSC32KSEL_VAL)
  338. {
  339. case 0U: /* OSC 32k clock */
  340. freq = (CLOCK_GetOsc0ErClkUndivFreq() == 32768U) ? 32768U : 0U;
  341. break;
  342. case 3U: /* LPO clock */
  343. freq = LPO_CLK_FREQ;
  344. break;
  345. default:
  346. freq = 0U;
  347. break;
  348. }
  349. return freq;
  350. }
  351. /*!
  352. * brief Get the output clock frequency selected by SIM[PLLFLLSEL].
  353. *
  354. * return Clock frequency in Hz.
  355. */
  356. uint32_t CLOCK_GetPllFllSelClkFreq(void)
  357. {
  358. uint32_t freq;
  359. switch (SIM_SOPT2_PLLFLLSEL_VAL)
  360. {
  361. case 0U: /* FLL. */
  362. freq = CLOCK_GetFllFreq();
  363. break;
  364. case 1U: /* PLL. */
  365. freq = CLOCK_GetPll0Freq();
  366. break;
  367. default:
  368. freq = 0U;
  369. break;
  370. }
  371. return freq;
  372. }
  373. /*!
  374. * brief Get the OSC0 external reference clock frequency (OSC0ERCLK).
  375. *
  376. * return Clock frequency in Hz.
  377. */
  378. uint32_t CLOCK_GetOsc0ErClkFreq(void)
  379. {
  380. return CLOCK_GetOsc0ErClkDivFreq();
  381. }
  382. /*!
  383. * brief Get the fast peripheral clock frequency.
  384. *
  385. * return Clock frequency in Hz.
  386. */
  387. uint32_t CLOCK_GetFastPeriphClkFreq(void)
  388. {
  389. return CLOCK_GetOutClkFreq() / ((uint32_t)(SIM_CLKDIV1_OUTDIV2_VAL + 1UL));
  390. }
  391. /*!
  392. * brief Get the flash clock frequency.
  393. *
  394. * return Clock frequency in Hz.
  395. */
  396. uint32_t CLOCK_GetFlashClkFreq(void)
  397. {
  398. return CLOCK_GetOutClkFreq() / ((uint32_t)(SIM_CLKDIV1_OUTDIV4_VAL + 1UL));
  399. }
  400. /*!
  401. * brief Get the flexbus clock frequency.
  402. *
  403. * return Clock frequency in Hz.
  404. */
  405. uint32_t CLOCK_GetFlexBusClkFreq(void)
  406. {
  407. return CLOCK_GetOutClkFreq() / ((uint32_t)(SIM_CLKDIV1_OUTDIV3_VAL + 1UL));
  408. }
  409. /*!
  410. * brief Get the bus clock frequency.
  411. *
  412. * return Clock frequency in Hz.
  413. */
  414. uint32_t CLOCK_GetBusClkFreq(void)
  415. {
  416. return CLOCK_GetOutClkFreq() / ((uint32_t)(SIM_CLKDIV1_OUTDIV4_VAL + 1UL));
  417. }
  418. /*!
  419. * brief Get the core clock or system clock frequency.
  420. *
  421. * return Clock frequency in Hz.
  422. */
  423. uint32_t CLOCK_GetCoreSysClkFreq(void)
  424. {
  425. return CLOCK_GetOutClkFreq() / ((uint32_t)(SIM_CLKDIV1_OUTDIV1_VAL + 1UL));
  426. }
  427. /*!
  428. * brief Gets the clock frequency for a specific clock name.
  429. *
  430. * This function checks the current clock configurations and then calculates
  431. * the clock frequency for a specific clock name defined in clock_name_t.
  432. * The MCG must be properly configured before using this function.
  433. *
  434. * param clockName Clock names defined in clock_name_t
  435. * return Clock frequency value in Hertz
  436. */
  437. uint32_t CLOCK_GetFreq(clock_name_t clockName)
  438. {
  439. uint32_t freq;
  440. switch (clockName)
  441. {
  442. case kCLOCK_CoreSysClk:
  443. freq = CLOCK_GetOutClkFreq() / ((uint32_t)(SIM_CLKDIV1_OUTDIV1_VAL + 1UL));
  444. break;
  445. case kCLOCK_FastPeriphClk:
  446. freq = CLOCK_GetOutClkFreq() / ((uint32_t)(SIM_CLKDIV1_OUTDIV2_VAL + 1UL));
  447. break;
  448. case kCLOCK_FlexBusClk:
  449. freq = CLOCK_GetOutClkFreq() / ((uint32_t)(SIM_CLKDIV1_OUTDIV3_VAL + 1UL));
  450. break;
  451. case kCLOCK_BusClk:
  452. case kCLOCK_FlashClk:
  453. freq = CLOCK_GetOutClkFreq() / ((uint32_t)(SIM_CLKDIV1_OUTDIV4_VAL + 1UL));
  454. break;
  455. case kCLOCK_PllFllSelClk:
  456. freq = CLOCK_GetPllFllSelClkFreq();
  457. break;
  458. case kCLOCK_Er32kClk:
  459. freq = CLOCK_GetEr32kClkFreq();
  460. break;
  461. case kCLOCK_McgFixedFreqClk:
  462. freq = CLOCK_GetFixedFreqClkFreq();
  463. break;
  464. case kCLOCK_McgInternalRefClk:
  465. freq = CLOCK_GetInternalRefClkFreq();
  466. break;
  467. case kCLOCK_McgFllClk:
  468. freq = CLOCK_GetFllFreq();
  469. break;
  470. case kCLOCK_McgPll0Clk:
  471. freq = CLOCK_GetPll0Freq();
  472. break;
  473. case kCLOCK_LpoClk:
  474. freq = LPO_CLK_FREQ;
  475. break;
  476. case kCLOCK_Osc0ErClkUndiv:
  477. freq = CLOCK_GetOsc0ErClkUndivFreq();
  478. break;
  479. case kCLOCK_Osc0ErClk:
  480. freq = CLOCK_GetOsc0ErClkDivFreq();
  481. break;
  482. default:
  483. freq = 0U;
  484. break;
  485. }
  486. return freq;
  487. }
  488. /*!
  489. * brief Set the clock configure in SIM module.
  490. *
  491. * This function sets system layer clock settings in SIM module.
  492. *
  493. * param config Pointer to the configure structure.
  494. */
  495. void CLOCK_SetSimConfig(sim_clock_config_t const *config)
  496. {
  497. SIM->CLKDIV1 = config->clkdiv1;
  498. CLOCK_SetPllFllSelClock(config->pllFllSel);
  499. CLOCK_SetEr32kClock(config->er32kSrc);
  500. }
  501. /*!
  502. * brief Gets the MCG output clock (MCGOUTCLK) frequency.
  503. *
  504. * This function gets the MCG output clock frequency in Hz based on the current MCG
  505. * register value.
  506. *
  507. * return The frequency of MCGOUTCLK.
  508. */
  509. uint32_t CLOCK_GetOutClkFreq(void)
  510. {
  511. uint32_t mcgoutclk;
  512. uint32_t clkst = (uint32_t)MCG_S_CLKST_VAL;
  513. switch (clkst)
  514. {
  515. case (uint32_t)kMCG_ClkOutStatPll:
  516. mcgoutclk = CLOCK_GetPll0Freq();
  517. break;
  518. case (uint32_t)kMCG_ClkOutStatFll:
  519. mcgoutclk = CLOCK_GetFllFreq();
  520. break;
  521. case (uint32_t)kMCG_ClkOutStatInt:
  522. mcgoutclk = CLOCK_GetInternalRefClkSelectFreq();
  523. break;
  524. case (uint32_t)kMCG_ClkOutStatExt:
  525. mcgoutclk = CLOCK_GetMcgExtClkFreq();
  526. break;
  527. default:
  528. mcgoutclk = 0U;
  529. break;
  530. }
  531. return mcgoutclk;
  532. }
  533. /*!
  534. * brief Gets the MCG FLL clock (MCGFLLCLK) frequency.
  535. *
  536. * This function gets the MCG FLL clock frequency in Hz based on the current MCG
  537. * register value. The FLL is enabled in FEI/FBI/FEE/FBE mode and
  538. * disabled in low power state in other modes.
  539. *
  540. * return The frequency of MCGFLLCLK.
  541. */
  542. uint32_t CLOCK_GetFllFreq(void)
  543. {
  544. static const uint16_t fllFactorTable[4][2] = {{640, 732}, {1280, 1464}, {1920, 2197}, {2560, 2929}};
  545. uint8_t drs, dmx32;
  546. uint32_t freq;
  547. uint32_t ret;
  548. /* If FLL is not enabled currently, then return 0U. */
  549. if ((((MCG->C2 & MCG_C2_LP_MASK) != 0U) || ((MCG->S & MCG_S_PLLST_MASK) != 0U)))
  550. {
  551. ret = 0U;
  552. }
  553. else
  554. {
  555. /* Get FLL reference clock frequency. */
  556. freq = CLOCK_GetFllRefClkFreq();
  557. if (0U == freq)
  558. {
  559. ret = freq;
  560. }
  561. else
  562. {
  563. drs = MCG_C4_DRST_DRS_VAL;
  564. dmx32 = MCG_C4_DMX32_VAL;
  565. ret = freq * fllFactorTable[drs][dmx32];
  566. }
  567. }
  568. return ret;
  569. }
  570. /*!
  571. * brief Gets the MCG internal reference clock (MCGIRCLK) frequency.
  572. *
  573. * This function gets the MCG internal reference clock frequency in Hz based
  574. * on the current MCG register value.
  575. *
  576. * return The frequency of MCGIRCLK.
  577. */
  578. uint32_t CLOCK_GetInternalRefClkFreq(void)
  579. {
  580. uint32_t freq;
  581. /* If MCGIRCLK is gated. */
  582. if (0U == (MCG->C1 & MCG_C1_IRCLKEN_MASK))
  583. {
  584. freq = 0U;
  585. }
  586. else
  587. {
  588. freq = CLOCK_GetInternalRefClkSelectFreq();
  589. }
  590. return freq;
  591. }
  592. /*!
  593. * brief Gets the MCG fixed frequency clock (MCGFFCLK) frequency.
  594. *
  595. * This function gets the MCG fixed frequency clock frequency in Hz based
  596. * on the current MCG register value.
  597. *
  598. * return The frequency of MCGFFCLK.
  599. */
  600. uint32_t CLOCK_GetFixedFreqClkFreq(void)
  601. {
  602. uint32_t freq = CLOCK_GetFllRefClkFreq();
  603. uint32_t ret;
  604. /* MCGFFCLK must be no more than MCGOUTCLK/8. */
  605. if ((freq <= (CLOCK_GetOutClkFreq() / 8U)) && (0U != freq))
  606. {
  607. ret = freq;
  608. }
  609. else
  610. {
  611. ret = 0U;
  612. }
  613. return ret;
  614. }
  615. /*!
  616. * brief Gets the MCG PLL0 clock (MCGPLL0CLK) frequency.
  617. *
  618. * This function gets the MCG PLL0 clock frequency in Hz based on the current MCG
  619. * register value.
  620. *
  621. * return The frequency of MCGPLL0CLK.
  622. */
  623. uint32_t CLOCK_GetPll0Freq(void)
  624. {
  625. uint32_t mcgpll0clk;
  626. uint32_t freq;
  627. uint8_t mcgpll0prdiv;
  628. uint8_t mcgpll0vdiv;
  629. /* If PLL0 is not enabled, return 0. */
  630. if (((MCG->S & MCG_S_LOCK0_MASK)) == 0U)
  631. {
  632. freq = 0U;
  633. }
  634. else
  635. {
  636. mcgpll0clk = CLOCK_GetPll0RefFreq();
  637. /*
  638. * Please call CLOCK_SetXtal0Freq base on board setting before using OSC0 clock.
  639. * Please call CLOCK_SetXtal1Freq base on board setting before using OSC1 clock.
  640. */
  641. assert(mcgpll0clk);
  642. mcgpll0prdiv = ((uint8_t)FSL_FEATURE_MCG_PLL_PRDIV_BASE + MCG_C5_PRDIV0_VAL);
  643. mcgpll0clk /= (uint32_t)mcgpll0prdiv;
  644. mcgpll0vdiv = ((uint8_t)FSL_FEATURE_MCG_PLL_VDIV_BASE + MCG_C6_VDIV0_VAL);
  645. mcgpll0clk *= (uint32_t)mcgpll0vdiv;
  646. mcgpll0clk >>= 1UL;
  647. freq = mcgpll0clk;
  648. }
  649. return freq;
  650. }
  651. /*!
  652. * brief Selects the MCG external reference clock.
  653. *
  654. * Selects the MCG external reference clock source, changes the MCG_C7[OSCSEL],
  655. * and waits for the clock source to be stable. Because the external reference
  656. * clock should not be changed in FEE/FBE/BLPE/PBE/PEE modes, do not call this function in these modes.
  657. *
  658. * param oscsel MCG external reference clock source, MCG_C7[OSCSEL].
  659. * retval kStatus_MCG_SourceUsed Because the external reference clock is used as a clock source,
  660. * the configuration should not be changed. Otherwise, a glitch occurs.
  661. * retval kStatus_Success External reference clock set successfully.
  662. */
  663. status_t CLOCK_SetExternalRefClkConfig(mcg_oscsel_t oscsel)
  664. {
  665. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  666. #endif /* MCG_CONFIG_CHECK_PARAM */
  667. return kStatus_Success;
  668. }
  669. /*!
  670. * brief Configures the Internal Reference clock (MCGIRCLK).
  671. *
  672. * This function sets the \c MCGIRCLK base on parameters. It also selects the IRC
  673. * source. If the fast IRC is used, this function sets the fast IRC divider.
  674. * This function also sets whether the \c MCGIRCLK is enabled in stop mode.
  675. * Calling this function in FBI/PBI/BLPI modes may change the system clock. As a result,
  676. * using the function in these modes it is not allowed.
  677. *
  678. * param enableMode MCGIRCLK enable mode, OR'ed value of ref _mcg_irclk_enable_mode.
  679. * param ircs MCGIRCLK clock source, choose fast or slow.
  680. * param fcrdiv Fast IRC divider setting (\c FCRDIV).
  681. * retval kStatus_MCG_SourceUsed Because the internal reference clock is used as a clock source,
  682. * the configuration should not be changed. Otherwise, a glitch occurs.
  683. * retval kStatus_Success MCGIRCLK configuration finished successfully.
  684. */
  685. status_t CLOCK_SetInternalRefClkConfig(uint8_t enableMode, mcg_irc_mode_t ircs, uint8_t fcrdiv)
  686. {
  687. uint32_t mcgOutClkState = (uint32_t)MCG_S_CLKST_VAL;
  688. mcg_irc_mode_t curIrcs = (mcg_irc_mode_t)((uint32_t)MCG_S_IRCST_VAL);
  689. uint8_t curFcrdiv = MCG_SC_FCRDIV_VAL;
  690. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  691. /* If MCGIRCLK is used as system clock source. */
  692. if ((uint32_t)kMCG_ClkOutStatInt == mcgOutClkState)
  693. {
  694. /* If need to change MCGIRCLK source or driver, return error. */
  695. if (((kMCG_IrcFast == curIrcs) && (fcrdiv != curFcrdiv)) || (ircs != curIrcs))
  696. {
  697. return kStatus_MCG_SourceUsed;
  698. }
  699. }
  700. #endif
  701. /* If need to update the FCRDIV. */
  702. if (fcrdiv != curFcrdiv)
  703. {
  704. /* If fast IRC is in use currently, change to slow IRC. */
  705. if (((0U != (MCG->C1 & MCG_C1_IRCLKEN_MASK)) || (mcgOutClkState == (uint32_t)kMCG_ClkOutStatInt)) &&
  706. (kMCG_IrcFast == curIrcs))
  707. {
  708. MCG->C2 = (uint8_t)((MCG->C2 & ~MCG_C2_IRCS_MASK) | (MCG_C2_IRCS(kMCG_IrcSlow)));
  709. while (MCG_S_IRCST_VAL != (uint8_t)kMCG_IrcSlow)
  710. {
  711. }
  712. }
  713. /* Update FCRDIV. */
  714. MCG->SC =
  715. (uint8_t)(MCG->SC & ~(MCG_SC_FCRDIV_MASK | MCG_SC_ATMF_MASK | MCG_SC_LOCS0_MASK)) | MCG_SC_FCRDIV(fcrdiv);
  716. }
  717. /* Set internal reference clock selection. */
  718. MCG->C2 = (uint8_t)((MCG->C2 & ~MCG_C2_IRCS_MASK) | (MCG_C2_IRCS(ircs)));
  719. MCG->C1 = (uint8_t)((MCG->C1 & ~(MCG_C1_IRCLKEN_MASK | MCG_C1_IREFSTEN_MASK)) | (uint8_t)enableMode);
  720. /* If MCGIRCLK is used, need to wait for MCG_S_IRCST. */
  721. if ((mcgOutClkState == (uint32_t)kMCG_ClkOutStatInt) || (0U != (enableMode & (uint32_t)kMCG_IrclkEnable)))
  722. {
  723. while (MCG_S_IRCST_VAL != (uint8_t)ircs)
  724. {
  725. }
  726. }
  727. return kStatus_Success;
  728. }
  729. /*!
  730. * brief Calculates the PLL divider setting for a desired output frequency.
  731. *
  732. * This function calculates the correct reference clock divider (\c PRDIV) and
  733. * VCO divider (\c VDIV) to generate a desired PLL output frequency. It returns the
  734. * closest frequency match with the corresponding \c PRDIV/VDIV
  735. * returned from parameters. If a desired frequency is not valid, this function
  736. * returns 0.
  737. *
  738. * param refFreq PLL reference clock frequency.
  739. * param desireFreq Desired PLL output frequency.
  740. * param prdiv PRDIV value to generate desired PLL frequency.
  741. * param vdiv VDIV value to generate desired PLL frequency.
  742. * return Closest frequency match that the PLL was able generate.
  743. */
  744. uint32_t CLOCK_CalcPllDiv(uint32_t refFreq, uint32_t desireFreq, uint8_t *prdiv, uint8_t *vdiv)
  745. {
  746. uint8_t ret_prdiv; /* PRDIV to return. */
  747. uint8_t ret_vdiv; /* VDIV to return. */
  748. uint8_t prdiv_min; /* Min PRDIV value to make reference clock in allowed range. */
  749. uint8_t prdiv_max; /* Max PRDIV value to make reference clock in allowed range. */
  750. uint8_t prdiv_cur; /* PRDIV value for iteration. */
  751. uint8_t vdiv_cur; /* VDIV value for iteration. */
  752. uint32_t ret_freq = 0U; /* PLL output frequency to return. */
  753. uint32_t diff = 0xFFFFFFFFU; /* Difference between desireFreq and return frequency. */
  754. uint32_t ref_div; /* Reference frequency after PRDIV. */
  755. /*
  756. Steps:
  757. 1. Get allowed prdiv with such rules:
  758. 1). refFreq / prdiv >= FSL_FEATURE_MCG_PLL_REF_MIN.
  759. 2). refFreq / prdiv <= FSL_FEATURE_MCG_PLL_REF_MAX.
  760. 2. For each allowed prdiv, there are two candidate vdiv values:
  761. 1). (desireFreq / (refFreq / prdiv)).
  762. 2). (desireFreq / (refFreq / prdiv)) + 1.
  763. If could get the precise desired frequency, return current prdiv and
  764. vdiv directly. Otherwise choose the one which is closer to desired
  765. frequency.
  766. */
  767. /* Reference frequency is out of range. */
  768. if ((refFreq < (uint32_t)FSL_FEATURE_MCG_PLL_REF_MIN) ||
  769. (refFreq > ((uint32_t)FSL_FEATURE_MCG_PLL_REF_MAX *
  770. ((uint32_t)FSL_FEATURE_MCG_PLL_PRDIV_MAX + (uint32_t)FSL_FEATURE_MCG_PLL_PRDIV_BASE))))
  771. {
  772. return 0U;
  773. }
  774. /* refFreq/PRDIV must in a range. First get the allowed PRDIV range. */
  775. prdiv_max = (uint8_t)(refFreq / (uint32_t)FSL_FEATURE_MCG_PLL_REF_MIN);
  776. prdiv_min =
  777. (uint8_t)((refFreq + (uint32_t)FSL_FEATURE_MCG_PLL_REF_MAX - 1U) / (uint32_t)FSL_FEATURE_MCG_PLL_REF_MAX);
  778. desireFreq *= 2U;
  779. /* PRDIV traversal. */
  780. for (prdiv_cur = prdiv_max; prdiv_cur >= prdiv_min; prdiv_cur--)
  781. {
  782. /* Reference frequency after PRDIV. */
  783. ref_div = refFreq / prdiv_cur;
  784. vdiv_cur = (uint8_t)(desireFreq / ref_div);
  785. if ((vdiv_cur < ((uint8_t)FSL_FEATURE_MCG_PLL_VDIV_BASE - 1U)) ||
  786. (vdiv_cur > (uint8_t)FSL_FEATURE_MCG_PLL_VDIV_BASE + 31U))
  787. {
  788. /* No VDIV is available with this PRDIV. */
  789. continue;
  790. }
  791. ret_freq = vdiv_cur * ref_div;
  792. if (vdiv_cur >= (uint8_t)FSL_FEATURE_MCG_PLL_VDIV_BASE)
  793. {
  794. if (ret_freq == desireFreq) /* If desire frequency is got. */
  795. {
  796. *prdiv = prdiv_cur - (uint8_t)FSL_FEATURE_MCG_PLL_PRDIV_BASE;
  797. *vdiv = vdiv_cur - (uint8_t)FSL_FEATURE_MCG_PLL_VDIV_BASE;
  798. return ret_freq / 2U;
  799. }
  800. /* New PRDIV/VDIV is closer. */
  801. if (diff > desireFreq - ret_freq)
  802. {
  803. diff = desireFreq - ret_freq;
  804. ret_prdiv = prdiv_cur;
  805. ret_vdiv = vdiv_cur;
  806. }
  807. }
  808. vdiv_cur++;
  809. if (vdiv_cur <= ((uint8_t)FSL_FEATURE_MCG_PLL_VDIV_BASE + 31U))
  810. {
  811. ret_freq += ref_div;
  812. /* New PRDIV/VDIV is closer. */
  813. if (diff > ret_freq - desireFreq)
  814. {
  815. diff = ret_freq - desireFreq;
  816. ret_prdiv = prdiv_cur;
  817. ret_vdiv = vdiv_cur;
  818. }
  819. }
  820. }
  821. if (0xFFFFFFFFU != diff)
  822. {
  823. /* PRDIV/VDIV found. */
  824. *prdiv = ret_prdiv - (uint8_t)FSL_FEATURE_MCG_PLL_PRDIV_BASE;
  825. *vdiv = ret_vdiv - (uint8_t)FSL_FEATURE_MCG_PLL_VDIV_BASE;
  826. ret_freq = (refFreq / ret_prdiv) * ret_vdiv;
  827. return ret_freq / 2U;
  828. }
  829. else
  830. {
  831. /* No proper PRDIV/VDIV found. */
  832. return 0U;
  833. }
  834. }
  835. /*!
  836. * brief Enables the PLL0 in FLL mode.
  837. *
  838. * This function sets us the PLL0 in FLL mode and reconfigures
  839. * the PLL0. Ensure that the PLL reference
  840. * clock is enabled before calling this function and that the PLL0 is not used as a clock source.
  841. * The function CLOCK_CalcPllDiv gets the correct PLL
  842. * divider values.
  843. *
  844. * param config Pointer to the configuration structure.
  845. */
  846. void CLOCK_EnablePll0(mcg_pll_config_t const *config)
  847. {
  848. assert(config);
  849. uint8_t mcg_c5 = 0U;
  850. mcg_c5 |= MCG_C5_PRDIV0(config->prdiv);
  851. MCG->C5 = mcg_c5; /* Disable the PLL first. */
  852. MCG->C6 = (uint8_t)((MCG->C6 & ~MCG_C6_VDIV0_MASK) | MCG_C6_VDIV0(config->vdiv));
  853. /* Set enable mode. */
  854. MCG->C5 |= ((uint8_t)kMCG_PllEnableIndependent | (uint8_t)config->enableMode);
  855. /* Wait for PLL lock. */
  856. while (((MCG->S & MCG_S_LOCK0_MASK)) == 0U)
  857. {
  858. }
  859. }
  860. /*!
  861. * brief Sets the OSC0 clock monitor mode.
  862. *
  863. * This function sets the OSC0 clock monitor mode. See ref mcg_monitor_mode_t for details.
  864. *
  865. * param mode Monitor mode to set.
  866. */
  867. void CLOCK_SetOsc0MonitorMode(mcg_monitor_mode_t mode)
  868. {
  869. /* Clear the previous flag, MCG_SC[LOCS0]. */
  870. MCG->SC &= ~(uint8_t)MCG_SC_ATMF_MASK;
  871. if (kMCG_MonitorNone == mode)
  872. {
  873. MCG->C6 &= ~(uint8_t)MCG_C6_CME0_MASK;
  874. }
  875. else
  876. {
  877. if (kMCG_MonitorInt == mode)
  878. {
  879. MCG->C2 &= ~(uint8_t)MCG_C2_LOCRE0_MASK;
  880. }
  881. else
  882. {
  883. MCG->C2 |= MCG_C2_LOCRE0_MASK;
  884. }
  885. MCG->C6 |= MCG_C6_CME0_MASK;
  886. }
  887. }
  888. /*!
  889. * brief Sets the PLL0 clock monitor mode.
  890. *
  891. * This function sets the PLL0 clock monitor mode. See ref mcg_monitor_mode_t for details.
  892. *
  893. * param mode Monitor mode to set.
  894. */
  895. void CLOCK_SetPll0MonitorMode(mcg_monitor_mode_t mode)
  896. {
  897. uint8_t mcg_c8;
  898. /* Clear previous flag. */
  899. MCG->S = MCG_S_LOLS0_MASK;
  900. if (kMCG_MonitorNone == mode)
  901. {
  902. MCG->C6 &= (uint8_t)(~MCG_C6_LOLIE0_MASK);
  903. }
  904. else
  905. {
  906. mcg_c8 = MCG->C8;
  907. if (kMCG_MonitorInt == mode)
  908. {
  909. mcg_c8 &= (uint8_t)(~MCG_C8_LOLRE_MASK);
  910. }
  911. else
  912. {
  913. mcg_c8 |= MCG_C8_LOLRE_MASK;
  914. }
  915. MCG->C8 = mcg_c8;
  916. MCG->C6 |= MCG_C6_LOLIE0_MASK;
  917. }
  918. }
  919. /*!
  920. * brief Gets the MCG status flags.
  921. *
  922. * This function gets the MCG clock status flags. All status flags are
  923. * returned as a logical OR of the enumeration ref _mcg_status_flags_t. To
  924. * check a specific flag, compare the return value with the flag.
  925. *
  926. * Example:
  927. * code
  928. * To check the clock lost lock status of OSC0 and PLL0.
  929. * uint32_t mcgFlags;
  930. *
  931. * mcgFlags = CLOCK_GetStatusFlags();
  932. *
  933. * if (mcgFlags & kMCG_Osc0LostFlag)
  934. * {
  935. * OSC0 clock lock lost. Do something.
  936. * }
  937. * if (mcgFlags & kMCG_Pll0LostFlag)
  938. * {
  939. * PLL0 clock lock lost. Do something.
  940. * }
  941. * endcode
  942. *
  943. * return Logical OR value of the ref _mcg_status_flags_t.
  944. */
  945. uint32_t CLOCK_GetStatusFlags(void)
  946. {
  947. uint32_t ret = 0U;
  948. uint8_t mcg_s = MCG->S;
  949. if ((MCG->SC & MCG_SC_LOCS0_MASK) != 0U)
  950. {
  951. ret |= (uint32_t)kMCG_Osc0LostFlag;
  952. }
  953. if ((mcg_s & MCG_S_OSCINIT0_MASK) != 0U)
  954. {
  955. ret |= (uint32_t)kMCG_Osc0InitFlag;
  956. }
  957. if ((mcg_s & MCG_S_LOLS0_MASK) != 0U)
  958. {
  959. ret |= (uint32_t)kMCG_Pll0LostFlag;
  960. }
  961. if ((mcg_s & MCG_S_LOCK0_MASK) != 0U)
  962. {
  963. ret |= (uint32_t)kMCG_Pll0LockFlag;
  964. }
  965. return ret;
  966. }
  967. /*!
  968. * brief Clears the MCG status flags.
  969. *
  970. * This function clears the MCG clock lock lost status. The parameter is a logical
  971. * OR value of the flags to clear. See ref _mcg_status_flags_t.
  972. *
  973. * Example:
  974. * code
  975. * To clear the clock lost lock status flags of OSC0 and PLL0.
  976. *
  977. * CLOCK_ClearStatusFlags(kMCG_Osc0LostFlag | kMCG_Pll0LostFlag);
  978. * endcode
  979. *
  980. * param mask The status flags to clear. This is a logical OR of members of the
  981. * enumeration ref _mcg_status_flags_t.
  982. */
  983. void CLOCK_ClearStatusFlags(uint32_t mask)
  984. {
  985. if ((mask & (uint32_t)kMCG_Osc0LostFlag) != 0UL)
  986. {
  987. MCG->SC &= (uint8_t)(~MCG_SC_ATMF_MASK);
  988. }
  989. if ((mask & (uint32_t)kMCG_Pll0LostFlag) != 0UL)
  990. {
  991. MCG->S = MCG_S_LOLS0_MASK;
  992. }
  993. }
  994. /*!
  995. * brief Initializes the OSC0.
  996. *
  997. * This function initializes the OSC0 according to the board configuration.
  998. *
  999. * param config Pointer to the OSC0 configuration structure.
  1000. */
  1001. void CLOCK_InitOsc0(osc_config_t const *config)
  1002. {
  1003. uint8_t range = CLOCK_GetOscRangeFromFreq(config->freq);
  1004. OSC_SetCapLoad(OSC0, config->capLoad);
  1005. MCG->C2 = (uint8_t)((MCG->C2 & ~OSC_MODE_MASK) | MCG_C2_RANGE(range) | (uint8_t)config->workMode);
  1006. OSC_SetExtRefClkConfig(OSC0, &config->oscerConfig);
  1007. if ((kOSC_ModeExt != config->workMode) && ((OSC0->CR & OSC_CR_ERCLKEN_MASK) != 0U))
  1008. {
  1009. /* Wait for stable. */
  1010. while (0U == (MCG->S & MCG_S_OSCINIT0_MASK))
  1011. {
  1012. }
  1013. }
  1014. }
  1015. /*!
  1016. * brief Deinitializes the OSC0.
  1017. *
  1018. * This function deinitializes the OSC0.
  1019. */
  1020. void CLOCK_DeinitOsc0(void)
  1021. {
  1022. OSC0->CR = 0U;
  1023. MCG->C2 &= ~(uint8_t)OSC_MODE_MASK;
  1024. }
  1025. /*!
  1026. * brief Set the Slow IRC frequency based on the trimmed value
  1027. *
  1028. * param freq The Slow IRC frequency input clock frequency in Hz.
  1029. */
  1030. void CLOCK_SetSlowIrcFreq(uint32_t freq)
  1031. {
  1032. s_slowIrcFreq = freq;
  1033. }
  1034. /*!
  1035. * brief Set the Fast IRC frequency based on the trimmed value
  1036. *
  1037. * param freq The Fast IRC frequency input clock frequency in Hz.
  1038. */
  1039. void CLOCK_SetFastIrcFreq(uint32_t freq)
  1040. {
  1041. s_fastIrcFreq = freq;
  1042. }
  1043. /*!
  1044. * brief Auto trims the internal reference clock.
  1045. *
  1046. * This function trims the internal reference clock by using the external clock. If
  1047. * successful, it returns the kStatus_Success and the frequency after
  1048. * trimming is received in the parameter p actualFreq. If an error occurs,
  1049. * the error code is returned.
  1050. *
  1051. * param extFreq External clock frequency, which should be a bus clock.
  1052. * param desireFreq Frequency to trim to.
  1053. * param actualFreq Actual frequency after trimming.
  1054. * param atms Trim fast or slow internal reference clock.
  1055. * retval kStatus_Success ATM success.
  1056. * retval kStatus_MCG_AtmBusClockInvalid The bus clock is not in allowed range for the ATM.
  1057. * retval kStatus_MCG_AtmDesiredFreqInvalid MCGIRCLK could not be trimmed to the desired frequency.
  1058. * retval kStatus_MCG_AtmIrcUsed Could not trim because MCGIRCLK is used as a bus clock source.
  1059. * retval kStatus_MCG_AtmHardwareFail Hardware fails while trimming.
  1060. */
  1061. status_t CLOCK_TrimInternalRefClk(uint32_t extFreq, uint32_t desireFreq, uint32_t *actualFreq, mcg_atm_select_t atms)
  1062. {
  1063. uint32_t multi; /* extFreq / desireFreq */
  1064. uint32_t actv; /* Auto trim value. */
  1065. uint8_t mcg_sc;
  1066. status_t status = kStatus_Success;
  1067. static const uint32_t trimRange[2][2] = {
  1068. /* Min Max */
  1069. {TRIM_SIRC_MIN, TRIM_SIRC_MAX}, /* Slow IRC. */
  1070. {TRIM_FIRC_MIN, TRIM_FIRC_MAX} /* Fast IRC. */
  1071. };
  1072. if ((extFreq > TRIM_REF_CLK_MAX) || (extFreq < TRIM_REF_CLK_MIN))
  1073. {
  1074. status = kStatus_MCG_AtmBusClockInvalid;
  1075. }
  1076. /* Check desired frequency range. */
  1077. else if ((desireFreq < trimRange[atms][0]) || (desireFreq > trimRange[atms][1]))
  1078. {
  1079. status = kStatus_MCG_AtmDesiredFreqInvalid;
  1080. }
  1081. /*
  1082. Make sure internal reference clock is not used to generate bus clock.
  1083. Here only need to check (MCG_S_IREFST == 1).
  1084. */
  1085. else if (MCG_S_IREFST(kMCG_FllSrcInternal) == (MCG->S & MCG_S_IREFST_MASK))
  1086. {
  1087. status = kStatus_MCG_AtmIrcUsed;
  1088. }
  1089. else
  1090. {
  1091. multi = extFreq / desireFreq;
  1092. actv = multi * 21U;
  1093. if (kMCG_AtmSel4m == atms)
  1094. {
  1095. actv *= 128U;
  1096. }
  1097. /* Now begin to start trim. */
  1098. MCG->ATCVL = (uint8_t)actv;
  1099. MCG->ATCVH = (uint8_t)(actv >> 8U);
  1100. mcg_sc = MCG->SC;
  1101. mcg_sc &= ~(uint8_t)(MCG_SC_ATMS_MASK | MCG_SC_LOCS0_MASK);
  1102. mcg_sc |= (MCG_SC_ATMF_MASK | MCG_SC_ATMS(atms));
  1103. MCG->SC = (mcg_sc | MCG_SC_ATME_MASK);
  1104. /* Wait for MCG finished. */
  1105. while (0U != (MCG->SC & MCG_SC_ATME_MASK))
  1106. {
  1107. }
  1108. /* Error occurs? */
  1109. if (0U != (MCG->SC & MCG_SC_ATMF_MASK))
  1110. {
  1111. /* Clear the failed flag. */
  1112. MCG->SC = mcg_sc;
  1113. status = kStatus_MCG_AtmHardwareFail;
  1114. }
  1115. else
  1116. {
  1117. *actualFreq = extFreq / multi;
  1118. if (kMCG_AtmSel4m == atms)
  1119. {
  1120. s_fastIrcFreq = *actualFreq;
  1121. }
  1122. else
  1123. {
  1124. s_slowIrcFreq = *actualFreq;
  1125. }
  1126. }
  1127. }
  1128. return status;
  1129. }
  1130. /*!
  1131. * brief Gets the current MCG mode.
  1132. *
  1133. * This function checks the MCG registers and determines the current MCG mode.
  1134. *
  1135. * return Current MCG mode or error code; See ref mcg_mode_t.
  1136. */
  1137. mcg_mode_t CLOCK_GetMode(void)
  1138. {
  1139. mcg_mode_t mode = kMCG_ModeError;
  1140. uint32_t clkst = (uint32_t)MCG_S_CLKST_VAL;
  1141. uint32_t irefst = (uint32_t)MCG_S_IREFST_VAL;
  1142. uint32_t lp = (uint32_t)MCG_C2_LP_VAL;
  1143. uint32_t pllst = MCG_S_PLLST_VAL;
  1144. /*------------------------------------------------------------------
  1145. Mode and Registers
  1146. ____________________________________________________________________
  1147. Mode | CLKST | IREFST | PLLST | LP
  1148. ____________________________________________________________________
  1149. FEI | 00(FLL) | 1(INT) | 0(FLL) | X
  1150. ____________________________________________________________________
  1151. FEE | 00(FLL) | 0(EXT) | 0(FLL) | X
  1152. ____________________________________________________________________
  1153. FBE | 10(EXT) | 0(EXT) | 0(FLL) | 0(NORMAL)
  1154. ____________________________________________________________________
  1155. FBI | 01(INT) | 1(INT) | 0(FLL) | 0(NORMAL)
  1156. ____________________________________________________________________
  1157. BLPI | 01(INT) | 1(INT) | 0(FLL) | 1(LOW POWER)
  1158. ____________________________________________________________________
  1159. BLPE | 10(EXT) | 0(EXT) | X | 1(LOW POWER)
  1160. ____________________________________________________________________
  1161. PEE | 11(PLL) | 0(EXT) | 1(PLL) | X
  1162. ____________________________________________________________________
  1163. PBE | 10(EXT) | 0(EXT) | 1(PLL) | O(NORMAL)
  1164. ____________________________________________________________________
  1165. PBI | 01(INT) | 1(INT) | 1(PLL) | 0(NORMAL)
  1166. ____________________________________________________________________
  1167. PEI | 11(PLL) | 1(INT) | 1(PLL) | X
  1168. ____________________________________________________________________
  1169. ----------------------------------------------------------------------*/
  1170. if (clkst == (uint32_t)kMCG_ClkOutStatFll)
  1171. {
  1172. if ((uint32_t)kMCG_FllSrcExternal == irefst)
  1173. {
  1174. mode = kMCG_ModeFEE;
  1175. }
  1176. else
  1177. {
  1178. mode = kMCG_ModeFEI;
  1179. }
  1180. }
  1181. else if (clkst == (uint32_t)kMCG_ClkOutStatInt)
  1182. {
  1183. if (0U != lp)
  1184. {
  1185. mode = kMCG_ModeBLPI;
  1186. }
  1187. else
  1188. {
  1189. {
  1190. mode = kMCG_ModeFBI;
  1191. }
  1192. }
  1193. }
  1194. else if (clkst == (uint32_t)kMCG_ClkOutStatExt)
  1195. {
  1196. if (0U != lp)
  1197. {
  1198. mode = kMCG_ModeBLPE;
  1199. }
  1200. else
  1201. {
  1202. if ((uint32_t)kMCG_PllstPll == pllst)
  1203. {
  1204. mode = kMCG_ModePBE;
  1205. }
  1206. else
  1207. {
  1208. mode = kMCG_ModeFBE;
  1209. }
  1210. }
  1211. }
  1212. else if (clkst == (uint32_t)kMCG_ClkOutStatPll)
  1213. {
  1214. {
  1215. mode = kMCG_ModePEE;
  1216. }
  1217. }
  1218. else
  1219. {
  1220. /*do nothing*/
  1221. }
  1222. return mode;
  1223. }
  1224. /*!
  1225. * brief Sets the MCG to FEI mode.
  1226. *
  1227. * This function sets the MCG to FEI mode. If setting to FEI mode fails
  1228. * from the current mode, this function returns an error.
  1229. *
  1230. * param dmx32 DMX32 in FEI mode.
  1231. * param drs The DCO range selection.
  1232. * param fllStableDelay Delay function to ensure that the FLL is stable. Passing
  1233. * NULL does not cause a delay.
  1234. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1235. * retval kStatus_Success Switched to the target mode successfully.
  1236. * note If p dmx32 is set to kMCG_Dmx32Fine, the slow IRC must not be trimmed
  1237. * to a frequency above 32768 Hz.
  1238. */
  1239. status_t CLOCK_SetFeiMode(mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1240. {
  1241. uint8_t mcg_c4;
  1242. bool change_drs = false;
  1243. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1244. mcg_mode_t mode = CLOCK_GetMode();
  1245. if (!((kMCG_ModeFEI == mode) || (kMCG_ModeFBI == mode) || (kMCG_ModeFBE == mode) || (kMCG_ModeFEE == mode)))
  1246. {
  1247. return kStatus_MCG_ModeUnreachable;
  1248. }
  1249. #endif
  1250. mcg_c4 = MCG->C4;
  1251. /*
  1252. Errata: ERR007993
  1253. Workaround: Invert MCG_C4[DMX32] or change MCG_C4[DRST_DRS] before
  1254. reference clock source changes, then reset to previous value after
  1255. reference clock changes.
  1256. */
  1257. if ((uint8_t)kMCG_FllSrcExternal == MCG_S_IREFST_VAL)
  1258. {
  1259. change_drs = true;
  1260. /* Change the LSB of DRST_DRS. */
  1261. MCG->C4 ^= (1U << MCG_C4_DRST_DRS_SHIFT);
  1262. }
  1263. /* Set CLKS and IREFS. */
  1264. MCG->C1 = (uint8_t)(((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_IREFS_MASK))) |
  1265. (MCG_C1_CLKS(kMCG_ClkOutSrcOut) /* CLKS = 0 */
  1266. | MCG_C1_IREFS(kMCG_FllSrcInternal))); /* IREFS = 1 */
  1267. /* Wait and check status. */
  1268. while ((uint8_t)kMCG_FllSrcInternal != MCG_S_IREFST_VAL)
  1269. {
  1270. }
  1271. /* Errata: ERR007993 */
  1272. if (change_drs)
  1273. {
  1274. MCG->C4 = mcg_c4;
  1275. }
  1276. /* In FEI mode, the MCG_C4[DMX32] is set to 0U. */
  1277. MCG->C4 = (uint8_t)((mcg_c4 & ~(MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) |
  1278. (MCG_C4_DMX32(dmx32) | MCG_C4_DRST_DRS(drs)));
  1279. /* Check MCG_S[CLKST] */
  1280. while ((uint8_t)kMCG_ClkOutStatFll != MCG_S_CLKST_VAL)
  1281. {
  1282. }
  1283. /* Wait for FLL stable time. */
  1284. if (NULL != fllStableDelay)
  1285. {
  1286. fllStableDelay();
  1287. }
  1288. return kStatus_Success;
  1289. }
  1290. /*!
  1291. * brief Sets the MCG to FEE mode.
  1292. *
  1293. * This function sets the MCG to FEE mode. If setting to FEE mode fails
  1294. * from the current mode, this function returns an error.
  1295. *
  1296. * param frdiv FLL reference clock divider setting, FRDIV.
  1297. * param dmx32 DMX32 in FEE mode.
  1298. * param drs The DCO range selection.
  1299. * param fllStableDelay Delay function to make sure FLL is stable. Passing
  1300. * NULL does not cause a delay.
  1301. *
  1302. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1303. * retval kStatus_Success Switched to the target mode successfully.
  1304. */
  1305. status_t CLOCK_SetFeeMode(uint8_t frdiv, mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1306. {
  1307. uint8_t mcg_c4;
  1308. bool change_drs = false;
  1309. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1310. mcg_mode_t mode = CLOCK_GetMode();
  1311. if (!((kMCG_ModeFEE == mode) || (kMCG_ModeFBI == mode) || (kMCG_ModeFBE == mode) || (kMCG_ModeFEI == mode)))
  1312. {
  1313. return kStatus_MCG_ModeUnreachable;
  1314. }
  1315. #endif
  1316. mcg_c4 = MCG->C4;
  1317. /*
  1318. Errata: ERR007993
  1319. Workaround: Invert MCG_C4[DMX32] or change MCG_C4[DRST_DRS] before
  1320. reference clock source changes, then reset to previous value after
  1321. reference clock changes.
  1322. */
  1323. if ((uint8_t)kMCG_FllSrcInternal == MCG_S_IREFST_VAL)
  1324. {
  1325. change_drs = true;
  1326. /* Change the LSB of DRST_DRS. */
  1327. MCG->C4 ^= (1U << MCG_C4_DRST_DRS_SHIFT);
  1328. }
  1329. /* Set CLKS and IREFS. */
  1330. MCG->C1 = (uint8_t)((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_FRDIV_MASK | MCG_C1_IREFS_MASK)) |
  1331. (MCG_C1_CLKS(kMCG_ClkOutSrcOut) /* CLKS = 0 */
  1332. | MCG_C1_FRDIV(frdiv) /* FRDIV */
  1333. | MCG_C1_IREFS(kMCG_FllSrcExternal))); /* IREFS = 0 */
  1334. /* If use external crystal as clock source, wait for it stable. */
  1335. {
  1336. if (0U != (MCG->C2 & MCG_C2_EREFS_MASK))
  1337. {
  1338. while (0U == (MCG->S & MCG_S_OSCINIT0_MASK))
  1339. {
  1340. }
  1341. }
  1342. }
  1343. /* Wait and check status. */
  1344. while ((uint8_t)kMCG_FllSrcExternal != MCG_S_IREFST_VAL)
  1345. {
  1346. }
  1347. /* Errata: ERR007993 */
  1348. if (change_drs)
  1349. {
  1350. MCG->C4 = mcg_c4;
  1351. }
  1352. /* Set DRS and DMX32. */
  1353. mcg_c4 = (uint8_t)((mcg_c4 & ~(MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) |
  1354. (MCG_C4_DMX32(dmx32) | MCG_C4_DRST_DRS(drs)));
  1355. MCG->C4 = mcg_c4;
  1356. /* Wait for DRST_DRS update. */
  1357. while (MCG->C4 != mcg_c4)
  1358. {
  1359. }
  1360. /* Check MCG_S[CLKST] */
  1361. while ((uint8_t)kMCG_ClkOutStatFll != MCG_S_CLKST_VAL)
  1362. {
  1363. }
  1364. /* Wait for FLL stable time. */
  1365. if (NULL != fllStableDelay)
  1366. {
  1367. fllStableDelay();
  1368. }
  1369. return kStatus_Success;
  1370. }
  1371. /*!
  1372. * brief Sets the MCG to FBI mode.
  1373. *
  1374. * This function sets the MCG to FBI mode. If setting to FBI mode fails
  1375. * from the current mode, this function returns an error.
  1376. *
  1377. * param dmx32 DMX32 in FBI mode.
  1378. * param drs The DCO range selection.
  1379. * param fllStableDelay Delay function to make sure FLL is stable. If the FLL
  1380. * is not used in FBI mode, this parameter can be NULL. Passing
  1381. * NULL does not cause a delay.
  1382. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1383. * retval kStatus_Success Switched to the target mode successfully.
  1384. * note If p dmx32 is set to kMCG_Dmx32Fine, the slow IRC must not be trimmed
  1385. * to frequency above 32768 Hz.
  1386. */
  1387. status_t CLOCK_SetFbiMode(mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1388. {
  1389. uint8_t mcg_c4;
  1390. bool change_drs = false;
  1391. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1392. mcg_mode_t mode = CLOCK_GetMode();
  1393. if (!((kMCG_ModeFEE == mode) || (kMCG_ModeFBI == mode) || (kMCG_ModeFBE == mode) || (kMCG_ModeFEI == mode) ||
  1394. (kMCG_ModeBLPI == mode)))
  1395. {
  1396. return kStatus_MCG_ModeUnreachable;
  1397. }
  1398. #endif
  1399. mcg_c4 = MCG->C4;
  1400. MCG->C2 &= ~(uint8_t)MCG_C2_LP_MASK; /* Disable lowpower. */
  1401. /*
  1402. Errata: ERR007993
  1403. Workaround: Invert MCG_C4[DMX32] or change MCG_C4[DRST_DRS] before
  1404. reference clock source changes, then reset to previous value after
  1405. reference clock changes.
  1406. */
  1407. if ((uint8_t)kMCG_FllSrcExternal == MCG_S_IREFST_VAL)
  1408. {
  1409. change_drs = true;
  1410. /* Change the LSB of DRST_DRS. */
  1411. MCG->C4 ^= (1U << MCG_C4_DRST_DRS_SHIFT);
  1412. }
  1413. /* Set CLKS and IREFS. */
  1414. MCG->C1 = (uint8_t)((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_IREFS_MASK)) |
  1415. (MCG_C1_CLKS(kMCG_ClkOutSrcInternal) /* CLKS = 1 */
  1416. | MCG_C1_IREFS(kMCG_FllSrcInternal))); /* IREFS = 1 */
  1417. /* Wait and check status. */
  1418. while ((uint8_t)kMCG_FllSrcInternal != MCG_S_IREFST_VAL)
  1419. {
  1420. }
  1421. /* Errata: ERR007993 */
  1422. if (change_drs)
  1423. {
  1424. MCG->C4 = mcg_c4;
  1425. }
  1426. while ((uint8_t)kMCG_ClkOutStatInt != MCG_S_CLKST_VAL)
  1427. {
  1428. }
  1429. MCG->C4 = (uint8_t)((mcg_c4 & ~(MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) |
  1430. (MCG_C4_DMX32(dmx32) | MCG_C4_DRST_DRS(drs)));
  1431. /* Wait for FLL stable time. */
  1432. if (NULL != fllStableDelay)
  1433. {
  1434. fllStableDelay();
  1435. }
  1436. return kStatus_Success;
  1437. }
  1438. /*!
  1439. * brief Sets the MCG to FBE mode.
  1440. *
  1441. * This function sets the MCG to FBE mode. If setting to FBE mode fails
  1442. * from the current mode, this function returns an error.
  1443. *
  1444. * param frdiv FLL reference clock divider setting, FRDIV.
  1445. * param dmx32 DMX32 in FBE mode.
  1446. * param drs The DCO range selection.
  1447. * param fllStableDelay Delay function to make sure FLL is stable. If the FLL
  1448. * is not used in FBE mode, this parameter can be NULL. Passing NULL
  1449. * does not cause a delay.
  1450. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1451. * retval kStatus_Success Switched to the target mode successfully.
  1452. */
  1453. status_t CLOCK_SetFbeMode(uint8_t frdiv, mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1454. {
  1455. uint8_t mcg_c4;
  1456. bool change_drs = false;
  1457. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1458. mcg_mode_t mode = CLOCK_GetMode();
  1459. if (!((kMCG_ModeFEE == mode) || (kMCG_ModeFBI == mode) || (kMCG_ModeFBE == mode) || (kMCG_ModeFEI == mode) ||
  1460. (kMCG_ModePBE == mode) || (kMCG_ModeBLPE == mode)))
  1461. {
  1462. return kStatus_MCG_ModeUnreachable;
  1463. }
  1464. #endif
  1465. /* Change to FLL mode. */
  1466. MCG->C6 &= ~(uint8_t)MCG_C6_PLLS_MASK;
  1467. while ((MCG->S & MCG_S_PLLST_MASK) != 0U)
  1468. {
  1469. }
  1470. /* Set LP bit to enable the FLL */
  1471. MCG->C2 &= ~(uint8_t)MCG_C2_LP_MASK;
  1472. mcg_c4 = MCG->C4;
  1473. /*
  1474. Errata: ERR007993
  1475. Workaround: Invert MCG_C4[DMX32] or change MCG_C4[DRST_DRS] before
  1476. reference clock source changes, then reset to previous value after
  1477. reference clock changes.
  1478. */
  1479. if ((uint8_t)kMCG_FllSrcInternal == MCG_S_IREFST_VAL)
  1480. {
  1481. change_drs = true;
  1482. /* Change the LSB of DRST_DRS. */
  1483. MCG->C4 ^= (1U << MCG_C4_DRST_DRS_SHIFT);
  1484. }
  1485. /* Set CLKS and IREFS. */
  1486. MCG->C1 = (uint8_t)((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_FRDIV_MASK | MCG_C1_IREFS_MASK)) |
  1487. (MCG_C1_CLKS(kMCG_ClkOutSrcExternal) /* CLKS = 2 */
  1488. | MCG_C1_FRDIV(frdiv) /* FRDIV = frdiv */
  1489. | MCG_C1_IREFS(kMCG_FllSrcExternal))); /* IREFS = 0 */
  1490. /* If use external crystal as clock source, wait for it stable. */
  1491. {
  1492. if (0U != (MCG->C2 & MCG_C2_EREFS_MASK))
  1493. {
  1494. while (0U == (MCG->S & MCG_S_OSCINIT0_MASK))
  1495. {
  1496. }
  1497. }
  1498. }
  1499. /* Wait for Reference clock Status bit to clear */
  1500. while ((uint8_t)kMCG_FllSrcExternal != MCG_S_IREFST_VAL)
  1501. {
  1502. }
  1503. /* Errata: ERR007993 */
  1504. if (change_drs)
  1505. {
  1506. MCG->C4 = mcg_c4;
  1507. }
  1508. /* Set DRST_DRS and DMX32. */
  1509. mcg_c4 = (uint8_t)((mcg_c4 & ~(MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) |
  1510. (MCG_C4_DMX32(dmx32) | MCG_C4_DRST_DRS(drs)));
  1511. /* Wait for clock status bits to show clock source is ext ref clk */
  1512. while ((uint8_t)kMCG_ClkOutStatExt != MCG_S_CLKST_VAL)
  1513. {
  1514. }
  1515. /* Wait for fll stable time. */
  1516. if (NULL != fllStableDelay)
  1517. {
  1518. fllStableDelay();
  1519. }
  1520. return kStatus_Success;
  1521. }
  1522. /*!
  1523. * brief Sets the MCG to BLPI mode.
  1524. *
  1525. * This function sets the MCG to BLPI mode. If setting to BLPI mode fails
  1526. * from the current mode, this function returns an error.
  1527. *
  1528. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1529. * retval kStatus_Success Switched to the target mode successfully.
  1530. */
  1531. status_t CLOCK_SetBlpiMode(void)
  1532. {
  1533. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1534. if (MCG_S_CLKST_VAL != (uint8_t)kMCG_ClkOutStatInt)
  1535. {
  1536. return kStatus_MCG_ModeUnreachable;
  1537. }
  1538. #endif /* MCG_CONFIG_CHECK_PARAM */
  1539. /* Set LP. */
  1540. MCG->C2 |= MCG_C2_LP_MASK;
  1541. return kStatus_Success;
  1542. }
  1543. /*!
  1544. * brief Sets the MCG to BLPE mode.
  1545. *
  1546. * This function sets the MCG to BLPE mode. If setting to BLPE mode fails
  1547. * from the current mode, this function returns an error.
  1548. *
  1549. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1550. * retval kStatus_Success Switched to the target mode successfully.
  1551. */
  1552. status_t CLOCK_SetBlpeMode(void)
  1553. {
  1554. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1555. if (MCG_S_CLKST_VAL != (uint8_t)kMCG_ClkOutStatExt)
  1556. {
  1557. return kStatus_MCG_ModeUnreachable;
  1558. }
  1559. #endif
  1560. /* Set LP bit to enter BLPE mode. */
  1561. MCG->C2 |= MCG_C2_LP_MASK;
  1562. return kStatus_Success;
  1563. }
  1564. /*!
  1565. * brief Sets the MCG to PBE mode.
  1566. *
  1567. * This function sets the MCG to PBE mode. If setting to PBE mode fails
  1568. * from the current mode, this function returns an error.
  1569. *
  1570. * param pllcs The PLL selection, PLLCS.
  1571. * param config Pointer to the PLL configuration.
  1572. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1573. * retval kStatus_Success Switched to the target mode successfully.
  1574. *
  1575. * note
  1576. * 1. The parameter \c pllcs selects the PLL. For platforms with
  1577. * only one PLL, the parameter pllcs is kept for interface compatibility.
  1578. * 2. The parameter \c config is the PLL configuration structure. On some
  1579. * platforms, it is possible to choose the external PLL directly, which renders the
  1580. * configuration structure not necessary. In this case, pass in NULL.
  1581. * For example: CLOCK_SetPbeMode(kMCG_OscselOsc, kMCG_PllClkSelExtPll, NULL);
  1582. */
  1583. status_t CLOCK_SetPbeMode(mcg_pll_clk_select_t pllcs, mcg_pll_config_t const *config)
  1584. {
  1585. assert(config);
  1586. /*
  1587. This function is designed to change MCG to PBE mode from PEE/BLPE/FBE,
  1588. but with this workflow, the source mode could be all modes except PEI/PBI.
  1589. */
  1590. MCG->C2 &= (uint8_t)(~MCG_C2_LP_MASK); /* Disable lowpower. */
  1591. /* Change to use external clock first. */
  1592. MCG->C1 = (uint8_t)((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_IREFS_MASK)) | MCG_C1_CLKS(kMCG_ClkOutSrcExternal));
  1593. /* Wait for CLKST clock status bits to show clock source is ext ref clk */
  1594. while ((MCG->S & (MCG_S_IREFST_MASK | MCG_S_CLKST_MASK)) !=
  1595. (MCG_S_IREFST(kMCG_FllSrcExternal) | MCG_S_CLKST(kMCG_ClkOutStatExt)))
  1596. {
  1597. }
  1598. /* Disable PLL first, then configure PLL. */
  1599. MCG->C6 &= (uint8_t)(~MCG_C6_PLLS_MASK);
  1600. while ((MCG->S & MCG_S_PLLST_MASK) != 0U)
  1601. {
  1602. }
  1603. /* Configure the PLL. */
  1604. {
  1605. CLOCK_EnablePll0(config);
  1606. }
  1607. /* Change to PLL mode. */
  1608. MCG->C6 |= MCG_C6_PLLS_MASK;
  1609. /* Wait for PLL mode changed. */
  1610. while (((MCG->S & MCG_S_PLLST_MASK)) == 0U)
  1611. {
  1612. }
  1613. return kStatus_Success;
  1614. }
  1615. /*!
  1616. * brief Sets the MCG to PEE mode.
  1617. *
  1618. * This function sets the MCG to PEE mode.
  1619. *
  1620. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1621. * retval kStatus_Success Switched to the target mode successfully.
  1622. *
  1623. * note This function only changes the CLKS to use the PLL/FLL output. If the
  1624. * PRDIV/VDIV are different than in the PBE mode, set them up
  1625. * in PBE mode and wait. When the clock is stable, switch to PEE mode.
  1626. */
  1627. status_t CLOCK_SetPeeMode(void)
  1628. {
  1629. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1630. mcg_mode_t mode = CLOCK_GetMode();
  1631. if (kMCG_ModePBE != mode)
  1632. {
  1633. return kStatus_MCG_ModeUnreachable;
  1634. }
  1635. #endif
  1636. /* Change to use PLL/FLL output clock first. */
  1637. MCG->C1 = (uint8_t)((MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcOut));
  1638. /* Wait for clock status bits to update */
  1639. while (MCG_S_CLKST_VAL != (uint8_t)kMCG_ClkOutStatPll)
  1640. {
  1641. }
  1642. return kStatus_Success;
  1643. }
  1644. /*!
  1645. * brief Switches the MCG to FBE mode from the external mode.
  1646. *
  1647. * This function switches the MCG from external modes (PEE/PBE/BLPE/FEE) to the FBE mode quickly.
  1648. * The external clock is used as the system clock source and PLL is disabled. However,
  1649. * the FLL settings are not configured. This is a lite function with a small code size, which is useful
  1650. * during the mode switch. For example, to switch from PEE mode to FEI mode:
  1651. *
  1652. * code
  1653. * CLOCK_ExternalModeToFbeModeQuick();
  1654. * CLOCK_SetFeiMode(...);
  1655. * endcode
  1656. *
  1657. * retval kStatus_Success Switched successfully.
  1658. * retval kStatus_MCG_ModeInvalid If the current mode is not an external mode, do not call this function.
  1659. */
  1660. status_t CLOCK_ExternalModeToFbeModeQuick(void)
  1661. {
  1662. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1663. if ((MCG->S & MCG_S_IREFST_MASK) != 0U)
  1664. {
  1665. return kStatus_MCG_ModeInvalid;
  1666. }
  1667. #endif /* MCG_CONFIG_CHECK_PARAM */
  1668. /* Disable low power */
  1669. MCG->C2 &= (uint8_t)(~MCG_C2_LP_MASK);
  1670. MCG->C1 = (uint8_t)((MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcExternal));
  1671. while (MCG_S_CLKST_VAL != (uint8_t)kMCG_ClkOutStatExt)
  1672. {
  1673. }
  1674. /* Disable PLL. */
  1675. MCG->C6 &= ~(uint8_t)MCG_C6_PLLS_MASK;
  1676. while ((MCG->S & MCG_S_PLLST_MASK) != 0U)
  1677. {
  1678. }
  1679. return kStatus_Success;
  1680. }
  1681. /*!
  1682. * brief Switches the MCG to FBI mode from internal modes.
  1683. *
  1684. * This function switches the MCG from internal modes (PEI/PBI/BLPI/FEI) to the FBI mode quickly.
  1685. * The MCGIRCLK is used as the system clock source and PLL is disabled. However,
  1686. * FLL settings are not configured. This is a lite function with a small code size, which is useful
  1687. * during the mode switch. For example, to switch from PEI mode to FEE mode:
  1688. *
  1689. * code
  1690. * CLOCK_InternalModeToFbiModeQuick();
  1691. * CLOCK_SetFeeMode(...);
  1692. * endcode
  1693. *
  1694. * retval kStatus_Success Switched successfully.
  1695. * retval kStatus_MCG_ModeInvalid If the current mode is not an internal mode, do not call this function.
  1696. */
  1697. status_t CLOCK_InternalModeToFbiModeQuick(void)
  1698. {
  1699. #if (defined(MCG_CONFIG_CHECK_PARAM) && MCG_CONFIG_CHECK_PARAM)
  1700. if ((MCG->S & MCG_S_IREFST_MASK) == 0U)
  1701. {
  1702. return kStatus_MCG_ModeInvalid;
  1703. }
  1704. #endif
  1705. /* Disable low power */
  1706. MCG->C2 &= ~(uint8_t)MCG_C2_LP_MASK;
  1707. MCG->C1 = (uint8_t)((MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcInternal));
  1708. while (MCG_S_CLKST_VAL != (uint8_t)kMCG_ClkOutStatInt)
  1709. {
  1710. }
  1711. return kStatus_Success;
  1712. }
  1713. /*!
  1714. * brief Sets the MCG to FEI mode during system boot up.
  1715. *
  1716. * This function sets the MCG to FEI mode from the reset mode. It can also be used to
  1717. * set up MCG during system boot up.
  1718. *
  1719. * param dmx32 DMX32 in FEI mode.
  1720. * param drs The DCO range selection.
  1721. * param fllStableDelay Delay function to ensure that the FLL is stable.
  1722. *
  1723. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1724. * retval kStatus_Success Switched to the target mode successfully.
  1725. * note If p dmx32 is set to kMCG_Dmx32Fine, the slow IRC must not be trimmed
  1726. * to frequency above 32768 Hz.
  1727. */
  1728. status_t CLOCK_BootToFeiMode(mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1729. {
  1730. return CLOCK_SetFeiMode(dmx32, drs, fllStableDelay);
  1731. }
  1732. /*!
  1733. * brief Sets the MCG to FEE mode during system bootup.
  1734. *
  1735. * This function sets MCG to FEE mode from the reset mode. It can also be used to
  1736. * set up the MCG during system boot up.
  1737. *
  1738. * param oscsel OSC clock select, OSCSEL.
  1739. * param frdiv FLL reference clock divider setting, FRDIV.
  1740. * param dmx32 DMX32 in FEE mode.
  1741. * param drs The DCO range selection.
  1742. * param fllStableDelay Delay function to ensure that the FLL is stable.
  1743. *
  1744. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1745. * retval kStatus_Success Switched to the target mode successfully.
  1746. */
  1747. status_t CLOCK_BootToFeeMode(
  1748. mcg_oscsel_t oscsel, uint8_t frdiv, mcg_dmx32_t dmx32, mcg_drs_t drs, void (*fllStableDelay)(void))
  1749. {
  1750. (void)CLOCK_SetExternalRefClkConfig(oscsel);
  1751. return CLOCK_SetFeeMode(frdiv, dmx32, drs, fllStableDelay);
  1752. }
  1753. /*!
  1754. * brief Sets the MCG to BLPI mode during system boot up.
  1755. *
  1756. * This function sets the MCG to BLPI mode from the reset mode. It can also be used to
  1757. * set up the MCG during system boot up.
  1758. *
  1759. * param fcrdiv Fast IRC divider, FCRDIV.
  1760. * param ircs The internal reference clock to select, IRCS.
  1761. * param ircEnableMode The MCGIRCLK enable mode, OR'ed value of ref _mcg_irclk_enable_mode.
  1762. *
  1763. * retval kStatus_MCG_SourceUsed Could not change MCGIRCLK setting.
  1764. * retval kStatus_Success Switched to the target mode successfully.
  1765. */
  1766. status_t CLOCK_BootToBlpiMode(uint8_t fcrdiv, mcg_irc_mode_t ircs, uint8_t ircEnableMode)
  1767. {
  1768. /* If reset mode is FEI mode, set MCGIRCLK and always success. */
  1769. (void)CLOCK_SetInternalRefClkConfig(ircEnableMode, ircs, fcrdiv);
  1770. /* If reset mode is not BLPI, first enter FBI mode. */
  1771. MCG->C1 = (uint8_t)((MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcInternal));
  1772. while (MCG_S_CLKST_VAL != (uint8_t)kMCG_ClkOutStatInt)
  1773. {
  1774. }
  1775. /* Enter BLPI mode. */
  1776. MCG->C2 |= MCG_C2_LP_MASK;
  1777. return kStatus_Success;
  1778. }
  1779. /*!
  1780. * brief Sets the MCG to BLPE mode during system boot up.
  1781. *
  1782. * This function sets the MCG to BLPE mode from the reset mode. It can also be used to
  1783. * set up the MCG during system boot up.
  1784. *
  1785. * param oscsel OSC clock select, MCG_C7[OSCSEL].
  1786. *
  1787. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1788. * retval kStatus_Success Switched to the target mode successfully.
  1789. */
  1790. status_t CLOCK_BootToBlpeMode(mcg_oscsel_t oscsel)
  1791. {
  1792. (void)CLOCK_SetExternalRefClkConfig(oscsel);
  1793. /* Set to FBE mode. */
  1794. MCG->C1 = (uint8_t)((MCG->C1 & ~(MCG_C1_CLKS_MASK | MCG_C1_IREFS_MASK)) |
  1795. (MCG_C1_CLKS(kMCG_ClkOutSrcExternal) /* CLKS = 2 */
  1796. | MCG_C1_IREFS(kMCG_FllSrcExternal))); /* IREFS = 0 */
  1797. /* If use external crystal as clock source, wait for it stable. */
  1798. {
  1799. if (0U != (MCG->C2 & MCG_C2_EREFS_MASK))
  1800. {
  1801. while (0U == (MCG->S & MCG_S_OSCINIT0_MASK))
  1802. {
  1803. }
  1804. }
  1805. }
  1806. /* Wait for MCG_S[CLKST] and MCG_S[IREFST]. */
  1807. while ((MCG->S & (MCG_S_IREFST_MASK | MCG_S_CLKST_MASK)) !=
  1808. (MCG_S_IREFST(kMCG_FllSrcExternal) | MCG_S_CLKST(kMCG_ClkOutStatExt)))
  1809. {
  1810. }
  1811. /* In FBE now, start to enter BLPE. */
  1812. MCG->C2 |= MCG_C2_LP_MASK;
  1813. return kStatus_Success;
  1814. }
  1815. /*!
  1816. * brief Sets the MCG to PEE mode during system boot up.
  1817. *
  1818. * This function sets the MCG to PEE mode from reset mode. It can also be used to
  1819. * set up the MCG during system boot up.
  1820. *
  1821. * param oscsel OSC clock select, MCG_C7[OSCSEL].
  1822. * param pllcs The PLL selection, PLLCS.
  1823. * param config Pointer to the PLL configuration.
  1824. *
  1825. * retval kStatus_MCG_ModeUnreachable Could not switch to the target mode.
  1826. * retval kStatus_Success Switched to the target mode successfully.
  1827. */
  1828. status_t CLOCK_BootToPeeMode(mcg_oscsel_t oscsel, mcg_pll_clk_select_t pllcs, mcg_pll_config_t const *config)
  1829. {
  1830. assert(config);
  1831. (void)CLOCK_SetExternalRefClkConfig(oscsel);
  1832. (void)CLOCK_SetPbeMode(pllcs, config);
  1833. /* Change to use PLL output clock. */
  1834. MCG->C1 = (uint8_t)((MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcOut));
  1835. while (MCG_S_CLKST_VAL != (uint8_t)kMCG_ClkOutStatPll)
  1836. {
  1837. }
  1838. return kStatus_Success;
  1839. }
  1840. /*
  1841. The transaction matrix. It defines the path for mode switch, the row is for
  1842. current mode and the column is target mode.
  1843. For example, switch from FEI to PEE:
  1844. 1. Current mode FEI, next mode is mcgModeMatrix[FEI][PEE] = FBE, so swith to FBE.
  1845. 2. Current mode FBE, next mode is mcgModeMatrix[FBE][PEE] = PBE, so swith to PBE.
  1846. 3. Current mode PBE, next mode is mcgModeMatrix[PBE][PEE] = PEE, so swith to PEE.
  1847. Thus the MCG mode has changed from FEI to PEE.
  1848. */
  1849. static const mcg_mode_t mcgModeMatrix[8][8] = {
  1850. {kMCG_ModeFEI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFEE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE,
  1851. kMCG_ModeFBE}, /* FEI */
  1852. {kMCG_ModeFEI, kMCG_ModeFBI, kMCG_ModeBLPI, kMCG_ModeFEE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE,
  1853. kMCG_ModeFBE}, /* FBI */
  1854. {kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeBLPI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFBI,
  1855. kMCG_ModeFBI}, /* BLPI */
  1856. {kMCG_ModeFEI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFEE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE,
  1857. kMCG_ModeFBE}, /* FEE */
  1858. {kMCG_ModeFEI, kMCG_ModeFBI, kMCG_ModeFBI, kMCG_ModeFEE, kMCG_ModeFBE, kMCG_ModeBLPE, kMCG_ModePBE,
  1859. kMCG_ModePBE}, /* FBE */
  1860. {kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeBLPE, kMCG_ModePBE,
  1861. kMCG_ModePBE}, /* BLPE */
  1862. {kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeFBE, kMCG_ModeBLPE, kMCG_ModePBE,
  1863. kMCG_ModePEE}, /* PBE */
  1864. {kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE, kMCG_ModePBE,
  1865. kMCG_ModePBE} /* PEE */
  1866. /* FEI FBI BLPI FEE FBE BLPE PBE PEE */
  1867. };
  1868. /*!
  1869. * brief Sets the MCG to a target mode.
  1870. *
  1871. * This function sets MCG to a target mode defined by the configuration
  1872. * structure. If switching to the target mode fails, this function
  1873. * chooses the correct path.
  1874. *
  1875. * param config Pointer to the target MCG mode configuration structure.
  1876. * return Return kStatus_Success if switched successfully; Otherwise, it returns an error code #_mcg_status.
  1877. *
  1878. * note If the external clock is used in the target mode, ensure that it is
  1879. * enabled. For example, if the OSC0 is used, set up OSC0 correctly before calling this
  1880. * function.
  1881. */
  1882. status_t CLOCK_SetMcgConfig(const mcg_config_t *config)
  1883. {
  1884. mcg_mode_t next_mode;
  1885. status_t status = kStatus_Success;
  1886. mcg_pll_clk_select_t pllcs = kMCG_PllClkSelPll0;
  1887. /* Re-configure MCGIRCLK, if MCGIRCLK is used as system clock source, then change to FEI/PEI first. */
  1888. if (MCG_S_CLKST_VAL == (uint8_t)kMCG_ClkOutStatInt)
  1889. {
  1890. MCG->C2 &= ~(uint8_t)MCG_C2_LP_MASK; /* Disable lowpower. */
  1891. {
  1892. (void)CLOCK_SetFeiMode(config->dmx32, config->drs, CLOCK_FllStableDelay);
  1893. }
  1894. }
  1895. /* Configure MCGIRCLK. */
  1896. (void)CLOCK_SetInternalRefClkConfig(config->irclkEnableMode, config->ircs, config->fcrdiv);
  1897. next_mode = CLOCK_GetMode();
  1898. do
  1899. {
  1900. next_mode = mcgModeMatrix[next_mode][config->mcgMode];
  1901. switch (next_mode)
  1902. {
  1903. case kMCG_ModeFEI:
  1904. status = CLOCK_SetFeiMode(config->dmx32, config->drs, CLOCK_FllStableDelay);
  1905. break;
  1906. case kMCG_ModeFEE:
  1907. status = CLOCK_SetFeeMode(config->frdiv, config->dmx32, config->drs, CLOCK_FllStableDelay);
  1908. break;
  1909. case kMCG_ModeFBI:
  1910. status = CLOCK_SetFbiMode(config->dmx32, config->drs, NULL);
  1911. break;
  1912. case kMCG_ModeFBE:
  1913. status = CLOCK_SetFbeMode(config->frdiv, config->dmx32, config->drs, NULL);
  1914. break;
  1915. case kMCG_ModeBLPI:
  1916. status = CLOCK_SetBlpiMode();
  1917. break;
  1918. case kMCG_ModeBLPE:
  1919. status = CLOCK_SetBlpeMode();
  1920. break;
  1921. case kMCG_ModePBE:
  1922. /* If target mode is not PBE or PEE, then only need to set CLKS = EXT here. */
  1923. if ((kMCG_ModePEE == config->mcgMode) || (kMCG_ModePBE == config->mcgMode))
  1924. {
  1925. {
  1926. status = CLOCK_SetPbeMode(pllcs, &config->pll0Config);
  1927. }
  1928. }
  1929. else
  1930. {
  1931. MCG->C1 = (uint8_t)((MCG->C1 & ~MCG_C1_CLKS_MASK) | MCG_C1_CLKS(kMCG_ClkOutSrcExternal));
  1932. while (MCG_S_CLKST_VAL != (uint8_t)kMCG_ClkOutStatExt)
  1933. {
  1934. }
  1935. }
  1936. break;
  1937. case kMCG_ModePEE:
  1938. status = CLOCK_SetPeeMode();
  1939. break;
  1940. default:
  1941. assert(false);
  1942. break;
  1943. }
  1944. if (kStatus_Success != status)
  1945. {
  1946. break;
  1947. }
  1948. } while (next_mode != config->mcgMode);
  1949. if (status == kStatus_Success)
  1950. {
  1951. if ((config->pll0Config.enableMode & (uint8_t)kMCG_PllEnableIndependent) != 0U)
  1952. {
  1953. CLOCK_EnablePll0(&config->pll0Config);
  1954. }
  1955. else
  1956. {
  1957. MCG->C5 &= ~(uint8_t)kMCG_PllEnableIndependent;
  1958. }
  1959. }
  1960. return status;
  1961. }