system_LPC122x.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /******************************************************************************
  2. * @file: system_LPC122x.c
  3. * @purpose: CMSIS Cortex-M0 Device Peripheral Access Layer Source File
  4. * for the NXP LPC122x Device Series
  5. * @version: V1.0
  6. * @date: 26. Nov. 2008
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (C) 2008 ARM Limited. All rights reserved.
  10. *
  11. * ARM Limited (ARM) is supplying this software for use with Cortex-M3
  12. * processor based microcontrollers. This file can be freely distributed
  13. * within development tools that are supporting such ARM based processors.
  14. *
  15. * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
  16. * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
  18. * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
  19. * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
  20. *
  21. ******************************************************************************/
  22. #include <stdint.h>
  23. #include "LPC122x.h"
  24. /*
  25. //-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
  26. */
  27. /*--------------------- Clock Configuration ----------------------------------
  28. //
  29. // <e> Clock Configuration
  30. // <h> System Controls and Status Register (SCS)
  31. // <o1.4> OSCRANGE: Main Oscillator Range Select
  32. // <0=> 1 MHz to 20 MHz
  33. // <1=> 15 MHz to 24 MHz
  34. // <e1.5> OSCEN: Main Oscillator Enable
  35. // </e>
  36. // </h>
  37. //
  38. // <h> Clock Source Select Register (CLKSRCSEL)
  39. // <o2.0..1> CLKSRC: PLL Clock Source Selection
  40. // <0=> Internal RC oscillator
  41. // <1=> Main oscillator
  42. // <2=> RTC oscillator
  43. // </h>
  44. //
  45. // <e3> PLL0 Configuration (Main PLL)
  46. // <h> PLL0 Configuration Register (PLL0CFG)
  47. // <i> F_cco0 = (2 * M * F_in) / N
  48. // <i> F_in must be in the range of 32 kHz to 50 MHz
  49. // <i> F_cco0 must be in the range of 275 MHz to 550 MHz
  50. // <o4.0..14> MSEL: PLL Multiplier Selection
  51. // <6-32768><#-1>
  52. // <i> M Value
  53. // <o4.16..23> NSEL: PLL Divider Selection
  54. // <1-256><#-1>
  55. // <i> N Value
  56. // </h>
  57. // </e>
  58. //
  59. //
  60. // <h> CPU Clock Configuration Register (CCLKCFG)
  61. // <o7.0..7> CCLKSEL: Divide Value for CPU Clock from PLL0
  62. // <0-255>
  63. // <i> Divide is CCLKSEL + 1. Only 0 and odd values are valid.
  64. // </h>
  65. //
  66. //
  67. // </e>
  68. */
  69. #define CLOCK_SETUP 1
  70. #define SYS_PLL_SETUP 1
  71. #define SYS_PLLSRCSEL_Val 0x00000001
  72. #define SYS_PLL_M_Val 0x00000003
  73. #define SYS_PLL_P_Val 0x00000001
  74. #define MAIN_CLKSRCSEL_Val 0x00000003
  75. #define SYS_AHB_DIV_Val 0x01 /* 1 through 255, 0 will disable the output. */
  76. /*
  77. //-------- <<< end of configuration section >>> ------------------------------
  78. */
  79. /*----------------------------------------------------------------------------
  80. DEFINES
  81. *----------------------------------------------------------------------------*/
  82. /*----------------------------------------------------------------------------
  83. Define clocks
  84. *----------------------------------------------------------------------------*/
  85. #define XTAL (12000000UL) /* Oscillator frequency */
  86. #define OSC_CLK ( XTAL) /* Main oscillator frequency */
  87. #define IRC_OSC ( 4000000UL) /* Internal RC oscillator frequency */
  88. #define WDT_OSC ( 250000UL) /* WDT oscillator frequency */
  89. /*----------------------------------------------------------------------------
  90. Clock Variable definitions
  91. *----------------------------------------------------------------------------*/
  92. uint32_t ClockSource = IRC_OSC;
  93. uint32_t SystemFrequency = IRC_OSC; /*!< System Clock Frequency (Core Clock) */
  94. uint32_t SystemAHBFrequency = IRC_OSC;
  95. /**
  96. * Misc. clock generation modules
  97. *
  98. * @param none
  99. * @return none
  100. *
  101. * @brief Setup the microcontroller system.
  102. * Initialize the System and update the SystemFrequency variable.
  103. */
  104. void SystemPLL_Setup ( void )
  105. {
  106. uint32_t regVal;
  107. LPC_SYSCON->PRESETCTRL &= ~0x00008000; /* Disable 1-Cycle Read Mode */
  108. ClockSource = OSC_CLK;
  109. LPC_SYSCON->SYSPLLCLKSEL = SYS_PLLSRCSEL_Val; /* Select system OSC */
  110. LPC_SYSCON->SYSPLLCLKUEN = 0x01; /* Update clock source */
  111. LPC_SYSCON->SYSPLLCLKUEN = 0x00; /* toggle Update register once */
  112. LPC_SYSCON->SYSPLLCLKUEN = 0x01;
  113. while ( !(LPC_SYSCON->SYSPLLCLKUEN & 0x01) ); /* Wait until updated */
  114. regVal = LPC_SYSCON->SYSPLLCTRL;
  115. regVal &= ~0x1FF;
  116. LPC_SYSCON->SYSPLLCTRL = (regVal | (SYS_PLL_P_Val<<5) | SYS_PLL_M_Val);
  117. /* Enable main system clock, main system clock bit 7 in PDRUNCFG. */
  118. LPC_SYSCON->PDRUNCFG &= ~(0x1<<7);
  119. while ( !(LPC_SYSCON->SYSPLLSTAT & 0x01) ); /* Wait until it's locked */
  120. LPC_SYSCON->MAINCLKSEL = MAIN_CLKSRCSEL_Val; /* Select PLL clock output */
  121. LPC_SYSCON->MAINCLKUEN = 0x01; /* Update MCLK clock source */
  122. LPC_SYSCON->MAINCLKUEN = 0x00; /* Toggle update register once */
  123. LPC_SYSCON->MAINCLKUEN = 0x01;
  124. while ( !(LPC_SYSCON->MAINCLKUEN & 0x01) ); /* Wait until updated */
  125. LPC_SYSCON->SYSAHBCLKDIV = SYS_AHB_DIV_Val; /* SYS AHB clock, 0 will disable output */
  126. #if SYS_PLL_SETUP
  127. /* If the SYS PLL output is selected as the main clock. Even if SYS PLL is
  128. configured and enabled, it doesn't mean it will be selected as the MAIN clock
  129. source. Be careful with MAINCLKSEL value. If SYS PLL is not selected, System
  130. Frequence should be the same as either IRC, external OSC(SYS), or WDT OSC clock. */
  131. SystemFrequency = ClockSource * (SYS_PLL_M_Val+1);
  132. #else
  133. SystemFrequency = ClockSource;
  134. #endif
  135. SystemAHBFrequency = (uint32_t)(SystemFrequency/SYS_AHB_DIV_Val);
  136. return;
  137. }
  138. /**
  139. * Initialize the system
  140. *
  141. * @param none
  142. * @return none
  143. *
  144. * @brief Setup the microcontroller system.
  145. * Initialize the System and update the SystemFrequency variable.
  146. */
  147. void SystemInit (void)
  148. {
  149. uint32_t i;
  150. #ifdef __DEBUG_RAM
  151. LPC_SYSCON->SYSMEMREMAP = 0x1; /* remap to internal RAM */
  152. #else
  153. #ifdef __DEBUG_FLASH
  154. LPC_SYSCON->SYSMEMREMAP = 0x2; /* remap to internal flash */
  155. #endif
  156. #endif
  157. #if 1
  158. /* First, below lines are for debugging only. For future release, WDT is
  159. enabled by bootrom, thus, unless a feed to WDT continuously, or WDT timeout
  160. will occur. If it's happen, WDT interrupt will be pending until a INT_CLEAR
  161. is applied. Below logic is to prevent system from going to the WDT interrupt
  162. during debugging.
  163. Second, all the peripheral clocks seem to be enabled by bootrom, it's
  164. not consistent with the UM. In below lines, only SYS, ROM, RAM, FLASHREG,
  165. FLASHARRAY, and I2C are enabled per UM dated July 14th. */
  166. LPC_WDT->MOD = 0x00;
  167. LPC_WDT->FEED = 0xAA; /* Feeding sequence */
  168. LPC_WDT->FEED = 0x55;
  169. NVIC->ICPR[0] |= 0xFFFFFFFF;
  170. LPC_SYSCON->SYSAHBCLKCTRL = 0x00000001F;
  171. #endif
  172. #if (CLOCK_SETUP) /* Clock Setup */
  173. /* bit 0 default is crystal bypass,
  174. bit1 0=0~20Mhz crystal input, 1=15~50Mhz crystal input. */
  175. LPC_SYSCON->SYSOSCCTRL = 0x00;
  176. /* main system OSC run is cleared, bit 5 in PDRUNCFG register */
  177. LPC_SYSCON->PDRUNCFG &= ~(0x1<<5);
  178. /* Wait 200us for OSC to be stablized, no status
  179. indication, dummy wait. */
  180. for ( i = 0; i < 0x100; i++ );
  181. #if (SYS_PLL_SETUP)
  182. SystemPLL_Setup();
  183. #endif
  184. #endif /* endif CLOCK_SETUP */
  185. /* System clock to the IOCON needs to be enabled or
  186. most of the I/O related peripherals won't work. */
  187. LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16);
  188. return;
  189. }