clock_config.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /***********************************************************************************************************************
  2. * This file was generated by the MCUXpresso Config Tools. Any manual edits made to this file
  3. * will be overwritten if the respective MCUXpresso Config Tools is used to update this file.
  4. **********************************************************************************************************************/
  5. /*
  6. * How to setup clock using clock driver functions:
  7. *
  8. * 1. CLOCK_SetSimSafeDivs, to make sure core clock, bus clock, flexbus clock
  9. * and flash clock are in allowed range during clock mode switch.
  10. *
  11. * 2. Call CLOCK_Osc0Init to setup OSC clock, if it is used in target mode.
  12. *
  13. * 3. Set MCG configuration, MCG includes three parts: FLL clock, PLL clock and
  14. * internal reference clock(MCGIRCLK). Follow the steps to setup:
  15. *
  16. * 1). Call CLOCK_BootToXxxMode to set MCG to target mode.
  17. *
  18. * 2). If target mode is FBI/BLPI/PBI mode, the MCGIRCLK has been configured
  19. * correctly. For other modes, need to call CLOCK_SetInternalRefClkConfig
  20. * explicitly to setup MCGIRCLK.
  21. *
  22. * 3). Don't need to configure FLL explicitly, because if target mode is FLL
  23. * mode, then FLL has been configured by the function CLOCK_BootToXxxMode,
  24. * if the target mode is not FLL mode, the FLL is disabled.
  25. *
  26. * 4). If target mode is PEE/PBE/PEI/PBI mode, then the related PLL has been
  27. * setup by CLOCK_BootToXxxMode. In FBE/FBI/FEE/FBE mode, the PLL could
  28. * be enabled independently, call CLOCK_EnablePll0 explicitly in this case.
  29. *
  30. * 4. Call CLOCK_SetSimConfig to set the clock configuration in SIM.
  31. */
  32. #include "fsl_smc.h"
  33. #include "clock_config.h"
  34. //#define USE_INT_CLOCK
  35. #ifdef USE_INT_CLOCK
  36. /*******************************************************************************
  37. * Definitions
  38. ******************************************************************************/
  39. #define MCG_IRCLK_DISABLE 0U /*!< MCGIRCLK disabled */
  40. #define MCG_PLL_DISABLE 0U /*!< MCGPLLCLK disabled */
  41. #define OSC_CAP0P 0U /*!< Oscillator 0pF capacitor load */
  42. #define OSC_ER_CLK_DISABLE 0U /*!< Disable external reference clock */
  43. #define SIM_CLKOUT_SEL_FLEXBUS_CLK 0U /*!< CLKOUT pin clock select: FlexBus clock */
  44. #define SIM_OSC32KSEL_RTC32KCLK_CLK 2U /*!< OSC32KSEL select: RTC32KCLK clock (32.768kHz) */
  45. #define SIM_PLLFLLSEL_IRC48MCLK_CLK 3U /*!< PLLFLL select: IRC48MCLK clock */
  46. #define SIM_PLLFLLSEL_MCGPLLCLK_CLK 1U /*!< PLLFLL select: MCGPLLCLK clock */
  47. #define SIM_TRACE_CLK_SEL_CORE_SYSTEM_CLK 1U /*!< Trace clock select: Core/system clock */
  48. /*******************************************************************************
  49. * Variables
  50. ******************************************************************************/
  51. /* System clock frequency. */
  52. extern uint32_t SystemCoreClock;
  53. /*******************************************************************************
  54. * Code
  55. ******************************************************************************/
  56. /*FUNCTION**********************************************************************
  57. *
  58. * Function Name : CLOCK_CONFIG_FllStableDelay
  59. * Description : This function is used to delay for FLL stable.
  60. *
  61. *END**************************************************************************/
  62. static void CLOCK_CONFIG_FllStableDelay(void)
  63. {
  64. uint32_t i = 30000U;
  65. while (i--)
  66. {
  67. __NOP();
  68. }
  69. }
  70. /*******************************************************************************
  71. ************************ BOARD_InitBootClocks function ************************
  72. ******************************************************************************/
  73. void BOARD_InitBootClocks(void)
  74. {
  75. BOARD_BootClockRUN();
  76. }
  77. /*******************************************************************************
  78. ********************** Configuration BOARD_BootClockRUN ***********************
  79. ******************************************************************************/
  80. /*******************************************************************************
  81. * Variables for BOARD_BootClockRUN configuration
  82. ******************************************************************************/
  83. const mcg_config_t mcgConfig_BOARD_BootClockRUN =
  84. {
  85. .mcgMode = kMCG_ModeFBI, /* FBI - FLL Bypassed Internal */
  86. .irclkEnableMode = kMCG_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
  87. .ircs = kMCG_IrcFast, /* Fast internal reference clock selected */
  88. .fcrdiv = 0x1U, /* Fast IRC divider: divided by 2 */
  89. .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
  90. .drs = kMCG_DrsLow, /* Low frequency range */
  91. .dmx32 = kMCG_Dmx32Fine, /* DCO has a default range of 25% */
  92. .oscsel = kMCG_OscselOsc, /* Selects System Oscillator (OSCCLK) */
  93. .pll0Config =
  94. {
  95. .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
  96. .prdiv = 0xeU, /* PLL Reference divider: divided by 15 */
  97. .vdiv = 0xcU, /* VCO divider: multiplied by 36 */
  98. },
  99. };
  100. const sim_clock_config_t simConfig_BOARD_BootClockRUN =
  101. {
  102. .pllFllSel = SIM_PLLFLLSEL_MCGPLLCLK_CLK, /* PLLFLL select: MCGPLLCLK clock */
  103. .er32kSrc = SIM_OSC32KSEL_RTC32KCLK_CLK, /* OSC32KSEL select: RTC32KCLK clock (32.768kHz) */
  104. .clkdiv1 = 0x1240000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV2: /2, OUTDIV3: /3, OUTDIV4: /5 */
  105. };
  106. const osc_config_t oscConfig_BOARD_BootClockRUN =
  107. {
  108. .freq = 0U, /* Oscillator frequency: 0Hz */
  109. .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
  110. .workMode = kOSC_ModeExt, /* Use external clock */
  111. .oscerConfig =
  112. {
  113. .enableMode = kOSC_ErClkEnable, /* Enable external reference clock, disable external reference clock in STOP mode */
  114. }
  115. };
  116. /*******************************************************************************
  117. * Code for BOARD_BootClockRUN configuration
  118. ******************************************************************************/
  119. void BOARD_BootClockRUN(void)
  120. {
  121. /* Set the system clock dividers in SIM to safe value. */
  122. CLOCK_SetSimSafeDivs();
  123. /* Set MCG to FBI mode. */
  124. CLOCK_SetInternalRefClkConfig(mcgConfig_BOARD_BootClockRUN.irclkEnableMode,
  125. mcgConfig_BOARD_BootClockRUN.ircs,
  126. mcgConfig_BOARD_BootClockRUN.fcrdiv);
  127. #if FSL_CLOCK_DRIVER_VERSION >= MAKE_VERSION(2, 2, 0)
  128. CLOCK_SetFbiMode(mcgConfig_BOARD_BootClockRUN.dmx32,
  129. mcgConfig_BOARD_BootClockRUN.drs,
  130. CLOCK_CONFIG_FllStableDelay);
  131. #else
  132. CLOCK_SetFbiMode(mcgConfig_BOARD_BootClockRUN.drs,
  133. CLOCK_CONFIG_FllStableDelay);
  134. #endif
  135. /* Set the clock configuration in SIM module. */
  136. CLOCK_SetSimConfig(&simConfig_BOARD_BootClockRUN);
  137. /* Set SystemCoreClock variable. */
  138. SystemCoreClock = BOARD_BOOTCLOCKRUN_CORE_CLOCK;
  139. /* Set CLKOUT source. */
  140. CLOCK_SetClkOutClock(SIM_CLKOUT_SEL_FLEXBUS_CLK);
  141. /* Set debug trace clock source. */
  142. CLOCK_SetTraceClock(SIM_TRACE_CLK_SEL_CORE_SYSTEM_CLK);
  143. }
  144. /*******************************************************************************
  145. ********************* Configuration BOARD_BootClockVLPR ***********************
  146. ******************************************************************************/
  147. /*******************************************************************************
  148. * Variables for BOARD_BootClockVLPR configuration
  149. ******************************************************************************/
  150. const mcg_config_t mcgConfig_BOARD_BootClockVLPR =
  151. {
  152. .mcgMode = kMCG_ModeBLPI, /* BLPI - Bypassed Low Power Internal */
  153. .irclkEnableMode = MCG_IRCLK_DISABLE, /* MCGIRCLK disabled */
  154. .ircs = kMCG_IrcFast, /* Fast internal reference clock selected */
  155. .fcrdiv = 0x0U, /* Fast IRC divider: divided by 1 */
  156. .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
  157. .drs = kMCG_DrsLow, /* Low frequency range */
  158. .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
  159. .oscsel = kMCG_OscselOsc, /* Selects System Oscillator (OSCCLK) */
  160. .pll0Config =
  161. {
  162. .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
  163. .prdiv = 0x0U, /* PLL Reference divider: divided by 1 */
  164. .vdiv = 0x0U, /* VCO divider: multiplied by 24 */
  165. },
  166. };
  167. const sim_clock_config_t simConfig_BOARD_BootClockVLPR =
  168. {
  169. .pllFllSel = SIM_PLLFLLSEL_IRC48MCLK_CLK, /* PLLFLL select: IRC48MCLK clock */
  170. .er32kSrc = SIM_OSC32KSEL_RTC32KCLK_CLK, /* OSC32KSEL select: RTC32KCLK clock (32.768kHz) */
  171. .clkdiv1 = 0x40000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV2: /1, OUTDIV3: /1, OUTDIV4: /5 */
  172. };
  173. const osc_config_t oscConfig_BOARD_BootClockVLPR =
  174. {
  175. .freq = 0U, /* Oscillator frequency: 0Hz */
  176. .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
  177. .workMode = kOSC_ModeExt, /* Use external clock */
  178. .oscerConfig =
  179. {
  180. .enableMode = OSC_ER_CLK_DISABLE, /* Disable external reference clock */
  181. }
  182. };
  183. /*******************************************************************************
  184. * Code for BOARD_BootClockVLPR configuration
  185. ******************************************************************************/
  186. void BOARD_BootClockVLPR(void)
  187. {
  188. /* Set the system clock dividers in SIM to safe value. */
  189. CLOCK_SetSimSafeDivs();
  190. /* Set MCG to BLPI mode. */
  191. CLOCK_BootToBlpiMode(mcgConfig_BOARD_BootClockVLPR.fcrdiv,
  192. mcgConfig_BOARD_BootClockVLPR.ircs,
  193. mcgConfig_BOARD_BootClockVLPR.irclkEnableMode);
  194. /* Set the clock configuration in SIM module. */
  195. CLOCK_SetSimConfig(&simConfig_BOARD_BootClockVLPR);
  196. /* Set VLPR power mode. */
  197. SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeAll);
  198. #if (defined(FSL_FEATURE_SMC_HAS_LPWUI) && FSL_FEATURE_SMC_HAS_LPWUI)
  199. SMC_SetPowerModeVlpr(SMC, false);
  200. #else
  201. SMC_SetPowerModeVlpr(SMC);
  202. #endif
  203. while (SMC_GetPowerModeState(SMC) != kSMC_PowerStateVlpr)
  204. {
  205. }
  206. /* Set SystemCoreClock variable. */
  207. SystemCoreClock = BOARD_BOOTCLOCKVLPR_CORE_CLOCK;
  208. }
  209. #else //use external clock
  210. #define MCG_IRCLK_DISABLE 0U /*!< MCGIRCLK disabled */
  211. #define MCG_PLL_DISABLE 0U /*!< MCGPLLCLK disabled */
  212. #define OSC_CAP0P 0U /*!< Oscillator 0pF capacitor load */
  213. #define OSC_ER_CLK_DISABLE 0U /*!< Disable external reference clock */
  214. #define SIM_OSC32KSEL_OSC32KCLK_CLK 0U /*!< OSC32KSEL select: OSC32KCLK clock */
  215. #define SIM_PLLFLLSEL_MCGFLLCLK_CLK 0U /*!< PLLFLL select: MCGFLLCLK clock */
  216. /*******************************************************************************
  217. * Variables
  218. ******************************************************************************/
  219. /* System clock frequency. */
  220. extern uint32_t SystemCoreClock;
  221. /*******************************************************************************
  222. * Code
  223. ******************************************************************************/
  224. /*FUNCTION**********************************************************************
  225. *
  226. * Function Name : CLOCK_CONFIG_SetFllExtRefDiv
  227. * Description : Configure FLL external reference divider (FRDIV).
  228. * Param frdiv : The value to set FRDIV.
  229. *
  230. *END**************************************************************************/
  231. static void CLOCK_CONFIG_SetFllExtRefDiv(uint8_t frdiv)
  232. {
  233. MCG->C1 = ((MCG->C1 & ~MCG_C1_FRDIV_MASK) | MCG_C1_FRDIV(frdiv));
  234. }
  235. /*******************************************************************************
  236. ************************ BOARD_InitBootClocks function ************************
  237. ******************************************************************************/
  238. void BOARD_InitBootClocks(void)
  239. {
  240. BOARD_BootClockRUN();
  241. }
  242. /*******************************************************************************
  243. * Variables for BOARD_BootClockRUN configuration
  244. ******************************************************************************/
  245. const mcg_config_t mcgConfig_BOARD_BootClockRUN =
  246. {
  247. .mcgMode = kMCG_ModePBE, /* PBE - PLL Bypassed Enternal */
  248. .irclkEnableMode = MCG_IRCLK_DISABLE, /* MCGIRCLK disabled */
  249. .ircs = kMCG_IrcSlow, /* Slow internal reference clock selected */
  250. .fcrdiv = 0x1U, /* Fast IRC divider: divided by 2 */
  251. .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
  252. .drs = kMCG_DrsLow, /* Low frequency range */
  253. .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
  254. .oscsel = kMCG_OscselOsc, /* Selects System Oscillator (OSCCLK) */
  255. .pll0Config =
  256. {
  257. .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
  258. .prdiv = 0x1U, /* PLL Reference divider: divided by 2 */
  259. .vdiv = 0x0U, /* VCO divider: multiplied by 24 */
  260. },
  261. };
  262. const sim_clock_config_t simConfig_BOARD_BootClockRUN =
  263. {
  264. .pllFllSel = SIM_PLLFLLSEL_MCGFLLCLK_CLK, /* PLLFLL select: MCGFLLCLK clock */
  265. .er32kSrc = SIM_OSC32KSEL_OSC32KCLK_CLK, /* OSC32KSEL select: OSC32KCLK clock */
  266. .clkdiv1 = 0x110000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV2: /1, OUTDIV3: /2, OUTDIV4: /2 */
  267. };
  268. const osc_config_t oscConfig_BOARD_BootClockRUN =
  269. {
  270. .freq = 7370000U, /* Oscillator frequency: 7370000Hz */
  271. .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
  272. .workMode = kOSC_ModeExt, /* Use external clock */
  273. .oscerConfig =
  274. {
  275. .enableMode = OSC_ER_CLK_DISABLE, /* Disable external reference clock */
  276. }
  277. };
  278. /*******************************************************************************
  279. * Code for BOARD_BootClockRUN configuration
  280. ******************************************************************************/
  281. void BOARD_BootClockRUN(void)
  282. {
  283. /* Set the system clock dividers in SIM to safe value. */
  284. CLOCK_SetSimSafeDivs();
  285. /* Initializes OSC0 according to board configuration. */
  286. CLOCK_InitOsc0(&oscConfig_BOARD_BootClockRUN);
  287. CLOCK_SetXtal0Freq(oscConfig_BOARD_BootClockRUN.freq);
  288. /* Configure FLL external reference divider (FRDIV). */
  289. CLOCK_CONFIG_SetFllExtRefDiv(mcgConfig_BOARD_BootClockRUN.frdiv);
  290. /* Set MCG to PBE mode. */
  291. CLOCK_SetExternalRefClkConfig(mcgConfig_BOARD_BootClockRUN.oscsel);
  292. CLOCK_SetPbeMode(kMCG_PllClkSelPll0,
  293. &mcgConfig_BOARD_BootClockRUN.pll0Config);
  294. /* Set the clock configuration in SIM module. */
  295. CLOCK_SetSimConfig(&simConfig_BOARD_BootClockRUN);
  296. /* Set SystemCoreClock variable. */
  297. SystemCoreClock = BOARD_BOOTCLOCKRUN_CORE_CLOCK;
  298. }
  299. #endif