clock_config.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * How to set up clock using clock driver functions:
  32. *
  33. * 1. Setup clock sources.
  34. *
  35. * 2. Setup voltage for the fastest of the clock outputs
  36. *
  37. * 3. Set up wait states of the flash.
  38. *
  39. * 4. Set up all dividers.
  40. *
  41. * 5. Set up all selectors to provide selected clocks.
  42. */
  43. /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
  44. !!ClocksProfile
  45. product: Clocks v1.0
  46. processor: LPC54608J512
  47. package_id: LPC54608J512ET180
  48. mcu_data: ksdk2_0
  49. processor_version: 0.0.0
  50. board: LPCXpresso54608
  51. * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
  52. #include "fsl_power.h"
  53. #include "fsl_clock.h"
  54. #include "clock_config.h"
  55. /*******************************************************************************
  56. * Definitions
  57. ******************************************************************************/
  58. /*******************************************************************************
  59. * Variables
  60. ******************************************************************************/
  61. /* System clock frequency. */
  62. extern uint32_t SystemCoreClock;
  63. /*******************************************************************************
  64. ********************* Configuration BOARD_BootClockFRO12M ***********************
  65. ******************************************************************************/
  66. /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
  67. !!Configuration
  68. name: BOARD_BootClockFRO12M
  69. outputs:
  70. - {id: System_clock.outFreq, value: 12 MHz}
  71. settings:
  72. - {id: SYSCON.EMCCLKDIV.scale, value: '1', locked: true}
  73. * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
  74. /*******************************************************************************
  75. * Variables for BOARD_BootClockFRO12M configuration
  76. ******************************************************************************/
  77. /*******************************************************************************
  78. * Code for BOARD_BootClockFRO12M configuration
  79. ******************************************************************************/
  80. void BOARD_BootClockFRO12M(void)
  81. {
  82. /*!< Set up the clock sources */
  83. /*!< Set up FRO */
  84. POWER_DisablePD(kPDRUNCFG_PD_FRO_EN); /*!< Ensure FRO is on */
  85. CLOCK_AttachClk(
  86. kFRO12M_to_MAIN_CLK); /*!< Switch to FRO 12MHz first to ensure we can change voltage without accidentally
  87. being below the voltage for current speed */
  88. CLOCK_SetupFROClocking(12000000U); /*!< Set up FRO to the 12 MHz, just for sure */
  89. POWER_SetVoltageForFreq(
  90. 12000000U); /*!< Set voltage for the one of the fastest clock outputs: System clock output */
  91. CLOCK_SetFLASHAccessCyclesForFreq(12000000U); /*!< Set FLASH wait states for core */
  92. /*!< Set up dividers */
  93. CLOCK_SetClkDiv(kCLOCK_DivAhbClk, 1U, false); /*!< Reset divider counter and set divider to value 1 */
  94. /*!< Set up clock selectors - Attach clocks to the peripheries */
  95. CLOCK_AttachClk(kFRO12M_to_MAIN_CLK); /*!< Switch MAIN_CLK to FRO12M */
  96. /*!< Set SystemCoreClock variable. */
  97. SystemCoreClock = BOARD_BOOTCLOCKFRO12M_CORE_CLOCK;
  98. }
  99. /*******************************************************************************
  100. ********************** Configuration BOARD_BootClockFROHF48M ***********************
  101. ******************************************************************************/
  102. /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
  103. !!Configuration
  104. name: BOARD_BootClockFROHF48M
  105. outputs:
  106. - {id: System_clock.outFreq, value: 48 MHz}
  107. settings:
  108. - {id: SYSCON.MAINCLKSELA.sel, value: SYSCON.fro_hf}
  109. * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
  110. /*******************************************************************************
  111. * Variables for BOARD_BootClockFROHF48M configuration
  112. ******************************************************************************/
  113. /*******************************************************************************
  114. * Code for BOARD_BootClockFROHF48M configuration
  115. ******************************************************************************/
  116. void BOARD_BootClockFROHF48M(void)
  117. {
  118. /*!< Set up the clock sources */
  119. /*!< Set up FRO */
  120. POWER_DisablePD(kPDRUNCFG_PD_FRO_EN); /*!< Ensure FRO is on */
  121. CLOCK_AttachClk(
  122. kFRO12M_to_MAIN_CLK); /*!< Switch to FRO 12MHz first to ensure we can change voltage without accidentally
  123. being below the voltage for current speed */
  124. POWER_SetVoltageForFreq(
  125. 48000000U); /*!< Set voltage for the one of the fastest clock outputs: System clock output */
  126. CLOCK_SetFLASHAccessCyclesForFreq(48000000U); /*!< Set FLASH wait states for core */
  127. CLOCK_SetupFROClocking(48000000U); /*!< Set up high frequency FRO output to selected frequency */
  128. /*!< Set up dividers */
  129. CLOCK_SetClkDiv(kCLOCK_DivAhbClk, 1U, false); /*!< Reset divider counter and set divider to value 1 */
  130. /*!< Set up clock selectors - Attach clocks to the peripheries */
  131. CLOCK_AttachClk(kFRO_HF_to_MAIN_CLK); /*!< Switch MAIN_CLK to FRO_HF */
  132. /*!< Set SystemCoreClock variable. */
  133. SystemCoreClock = BOARD_BOOTCLOCKFROHF48M_CORE_CLOCK;
  134. }
  135. /*******************************************************************************
  136. ********************* Configuration BOARD_BootClockFROHF96M **********************
  137. ******************************************************************************/
  138. /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
  139. !!Configuration
  140. name: BOARD_BootClockFROHF96M
  141. outputs:
  142. - {id: System_clock.outFreq, value: 96 MHz}
  143. settings:
  144. - {id: SYSCON.MAINCLKSELA.sel, value: SYSCON.fro_hf}
  145. sources:
  146. - {id: SYSCON.fro_hf.outFreq, value: 96 MHz}
  147. * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
  148. /*******************************************************************************
  149. * Variables for BOARD_BootClockFROHF96M configuration
  150. ******************************************************************************/
  151. /*******************************************************************************
  152. * Code for BOARD_BootClockFROHF96M configuration
  153. ******************************************************************************/
  154. void BOARD_BootClockFROHF96M(void)
  155. {
  156. /*!< Set up the clock sources */
  157. /*!< Set up FRO */
  158. POWER_DisablePD(kPDRUNCFG_PD_FRO_EN); /*!< Ensure FRO is on */
  159. CLOCK_AttachClk(
  160. kFRO12M_to_MAIN_CLK); /*!< Switch to FRO 12MHz first to ensure we can change voltage without accidentally
  161. being below the voltage for current speed */
  162. POWER_SetVoltageForFreq(
  163. 96000000U); /*!< Set voltage for the one of the fastest clock outputs: System clock output */
  164. CLOCK_SetFLASHAccessCyclesForFreq(96000000U); /*!< Set FLASH wait states for core */
  165. CLOCK_SetupFROClocking(96000000U); /*!< Set up high frequency FRO output to selected frequency */
  166. /*!< Set up dividers */
  167. CLOCK_SetClkDiv(kCLOCK_DivAhbClk, 1U, false); /*!< Reset divider counter and set divider to value 1 */
  168. /*!< Set up clock selectors - Attach clocks to the peripheries */
  169. CLOCK_AttachClk(kFRO_HF_to_MAIN_CLK); /*!< Switch MAIN_CLK to FRO_HF */
  170. /*!< Set SystemCoreClock variable. */
  171. SystemCoreClock = BOARD_BOOTCLOCKFROHF96M_CORE_CLOCK;
  172. }
  173. /*******************************************************************************
  174. ********************* Configuration BOARD_BootClockPLL180M **********************
  175. ******************************************************************************/
  176. /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
  177. !!Configuration
  178. name: BOARD_BootClockPLL180M
  179. outputs:
  180. - {id: FRO12M_clock.outFreq, value: 12 MHz}
  181. - {id: FROHF_clock.outFreq, value: 48 MHz}
  182. - {id: SYSPLL_clock.outFreq, value: 180 MHz}
  183. - {id: System_clock.outFreq, value: 180 MHz}
  184. settings:
  185. - {id: SYSCON.M_MULT.scale, value: '30', locked: true}
  186. - {id: SYSCON.N_DIV.scale, value: '1', locked: true}
  187. - {id: SYSCON.PDEC.scale, value: '2', locked: true}
  188. - {id: SYSCON_PDRUNCFG0_PDEN_SYS_PLL_CFG, value: Power_up}
  189. sources:
  190. - {id: SYSCON._clk_in.outFreq, value: 12 MHz, enabled: true}
  191. * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
  192. /*******************************************************************************
  193. * Variables for BOARD_BootClockPLL180M configuration
  194. ******************************************************************************/
  195. /*******************************************************************************
  196. * Code for BOARD_BootClockPLL180M configuration
  197. ******************************************************************************/
  198. void BOARD_BootClockPLL180M(void)
  199. {
  200. /*!< Set up the clock sources */
  201. /*!< Set up FRO */
  202. POWER_DisablePD(kPDRUNCFG_PD_FRO_EN); /*!< Ensure FRO is on */
  203. CLOCK_AttachClk(
  204. kFRO12M_to_MAIN_CLK); /*!< Switch to FRO 12MHz first to ensure we can change voltage without accidentally
  205. being below the voltage for current speed */
  206. POWER_SetVoltageForFreq(
  207. 12000000U); /*!< Set voltage for the one of the fastest clock outputs: System clock output */
  208. CLOCK_SetFLASHAccessCyclesForFreq(12000000U); /*!< Set FLASH wait states for core */
  209. /*!< Set up SYS PLL */
  210. const pll_setup_t pllSetup = {
  211. .pllctrl = SYSCON_SYSPLLCTRL_SELI(32U) | SYSCON_SYSPLLCTRL_SELP(16U) | SYSCON_SYSPLLCTRL_SELR(0U),
  212. .pllmdec = (SYSCON_SYSPLLMDEC_MDEC(8191U)),
  213. .pllndec = (SYSCON_SYSPLLNDEC_NDEC(770U)),
  214. .pllpdec = (SYSCON_SYSPLLPDEC_PDEC(98U)),
  215. .pllRate = 180000000U,
  216. .flags = PLL_SETUPFLAG_WAITLOCK | PLL_SETUPFLAG_POWERUP};
  217. CLOCK_AttachClk(kEXT_CLK_to_SYS_PLL); /*!< Set sys pll clock source from external crystal */
  218. CLOCK_SetPLLFreq(&pllSetup); /*!< Configure PLL to the desired value */
  219. POWER_SetVoltageForFreq(
  220. 180000000U); /*!< Set voltage for the one of the fastest clock outputs: System clock output */
  221. CLOCK_SetFLASHAccessCyclesForFreq(180000000U); /*!< Set FLASH wait states for core */
  222. CLOCK_AttachClk(kSYS_PLL_to_MAIN_CLK); /*!< Switch System clock to SYS PLL 180MHz */
  223. /* Set SystemCoreClock variable. */
  224. SystemCoreClock = BOARD_BootClockPLL180M_CORE_CLOCK;
  225. }