sysinit_5410x.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * @brief LPC5410X Chip specific SystemInit
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #include "chip.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. /*****************************************************************************
  36. * Public types/enumerations/variables
  37. ****************************************************************************/
  38. /*****************************************************************************
  39. * Private functions
  40. ****************************************************************************/
  41. /* Sets the best FLASH clock arte for the passed frequency */
  42. static void setupFlashClocks(uint32_t freq)
  43. {
  44. /* v17.0 ROM support only - coarse FLASH clocking timing.
  45. FLASH access is setup based on voltage for v17.1 and later ROMs
  46. as part of the power library. */
  47. if (Chip_POWER_GetROMVersion() == LPC5410X_ROMVER_0) {
  48. if (freq < 20000000) {
  49. Chip_SYSCON_SetFLASHAccess(SYSCON_FLASH_1CYCLE);
  50. }
  51. else if (freq < 48000000) {
  52. Chip_SYSCON_SetFLASHAccess(SYSCON_FLASH_2CYCLE);
  53. }
  54. else if (freq < 72000000) {
  55. Chip_SYSCON_SetFLASHAccess(SYSCON_FLASH_3CYCLE);
  56. }
  57. else {
  58. Chip_SYSCON_SetFLASHAccess(SYSCON_FLASH_4CYCLE);
  59. }
  60. }
  61. }
  62. /*****************************************************************************
  63. * Public functions
  64. ****************************************************************************/
  65. /* Clock and PLL initialization based on the internal oscillator */
  66. void Chip_SetupIrcClocking(uint32_t iFreq)
  67. {
  68. PLL_CONFIG_T pllConfig;
  69. PLL_SETUP_T pllSetup;
  70. PLL_ERROR_T pllError;
  71. /* Turn on the IRC by clearing the power down bit */
  72. Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_IRC_OSC | SYSCON_PDRUNCFG_PD_IRC);
  73. /* Select the PLL input to the IRC */
  74. Chip_Clock_SetSystemPLLSource(SYSCON_PLLCLKSRC_IRC);
  75. /* Setup FLASH access û½øÈ¥??*/
  76. setupFlashClocks(iFreq);
  77. /* Power down PLL to change the PLL divider ratio */
  78. Chip_SYSCON_PowerDown(SYSCON_PDRUNCFG_PD_SYS_PLL);
  79. /* Setup PLL configuration */
  80. pllConfig.desiredRate = iFreq;
  81. pllConfig.InputRate = 0;
  82. pllConfig.flags = PLL_CONFIGFLAG_FORCENOFRACT;
  83. pllError = Chip_Clock_SetupPLLData(&pllConfig, &pllSetup);
  84. if (pllError == PLL_ERROR_SUCCESS) {
  85. pllSetup.flags = PLL_SETUPFLAG_WAITLOCK | PLL_SETUPFLAG_ADGVOLT;
  86. pllError = Chip_Clock_SetupSystemPLLPrec(&pllSetup);
  87. }
  88. /* Set system clock divider to 1 */
  89. Chip_Clock_SetSysClockDiv(1);
  90. /* Set main clock source to the system PLL. This will drive 24MHz
  91. for the main clock and 24MHz for the system clock */
  92. Chip_Clock_SetMainClockSource(SYSCON_MAINCLKSRC_PLLOUT);
  93. /* ASYSNC SYSCON needs to be on or all serial peripheral won't work.
  94. Be careful if PLL is used or not, ASYNC_SYSCON source needs to be
  95. selected carefully. */
  96. Chip_SYSCON_Enable_ASYNC_Syscon(true);
  97. Chip_Clock_SetAsyncSysconClockDiv(1);
  98. Chip_Clock_SetAsyncSysconClockSource(SYSCON_ASYNC_IRC);
  99. }
  100. /* Clock and PLL initialization based on the external clock input */
  101. void Chip_SetupExtInClocking(uint32_t iFreq)
  102. {
  103. PLL_CONFIG_T pllConfig;
  104. PLL_SETUP_T pllSetup;
  105. PLL_ERROR_T pllError;
  106. /* IOCON clock left on, this is needed is CLKIN is used. */
  107. Chip_Clock_EnablePeriphClock(SYSCON_CLOCK_IOCON);
  108. /* Select external clock input pin */
  109. Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 22, (IOCON_MODE_PULLUP |
  110. IOCON_FUNC1 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF));
  111. /* Select the PLL input to the EXT clock input */
  112. Chip_Clock_SetSystemPLLSource(SYSCON_PLLCLKSRC_CLKIN);
  113. /* Setup FLASH access */
  114. setupFlashClocks(iFreq);
  115. /* Power down PLL to change the PLL divider ratio */
  116. Chip_SYSCON_PowerDown(SYSCON_PDRUNCFG_PD_SYS_PLL);
  117. /* Setup PLL configuration */
  118. pllConfig.desiredRate = iFreq;
  119. pllConfig.InputRate = 0;
  120. pllConfig.flags = PLL_CONFIGFLAG_FORCENOFRACT;
  121. pllError = Chip_Clock_SetupPLLData(&pllConfig, &pllSetup);
  122. if (pllError == PLL_ERROR_SUCCESS) {
  123. pllSetup.flags = PLL_SETUPFLAG_WAITLOCK | PLL_SETUPFLAG_ADGVOLT;
  124. pllError = Chip_Clock_SetupSystemPLLPrec(&pllSetup);
  125. }
  126. /* Set system clock divider to 1 */
  127. Chip_Clock_SetSysClockDiv(1);
  128. /* Set main clock source to the system PLL. This will drive 24MHz
  129. for the main clock and 24MHz for the system clock */
  130. Chip_Clock_SetMainClockSource(SYSCON_MAINCLKSRC_PLLOUT);
  131. /* ASYSNC SYSCON needs to be on or all serial peripheral won't work.
  132. Be careful if PLL is used or not, ASYNC_SYSCON source needs to be
  133. selected carefully. */
  134. Chip_SYSCON_Enable_ASYNC_Syscon(true);
  135. Chip_Clock_SetAsyncSysconClockDiv(1);
  136. Chip_Clock_SetAsyncSysconClockSource(SYSCON_ASYNC_IRC);
  137. }
  138. /* Set up and initialize hardware prior to call to main */
  139. void Chip_SystemInit(void)
  140. {
  141. /* Initial internal clocking @100MHz */
  142. Chip_SetupIrcClocking(100000000);
  143. }