lpc177x_8x_clk.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright (C) 2012 by Ole Reinhardt (ole.reinhardt@embedded-it.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /**************************************************************************/
  35. /* The follwing copyright notice applies to the clock initialisation code */
  36. /************************************************************************
  37. *
  38. *
  39. *
  40. * $Id: system_LPC177x_8x.c 7485 2011-06-03 07:57:16Z sgg06786 $
  41. * system_LPC177x_8x.c 2011-06-02
  42. *//**
  43. * @file system_LPC177x_8x.c
  44. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer Source File
  45. * for the NXP LPC177x_8x Device Series
  46. *
  47. * ARM Limited (ARM) is supplying this software for use with
  48. * Cortex-M processor based microcontrollers. This file can be
  49. * freely distributed within development tools that are supporting
  50. * such ARM based processors.
  51. *
  52. * @version 1.0
  53. * @date 02. June. 2011
  54. * @author NXP MCU SW Application Team
  55. *
  56. * Copyright(C) 2011, NXP Semiconductor
  57. * All rights reserved.
  58. *
  59. ***********************************************************************
  60. * Software that is described herein is for illustrative purposes only
  61. * which provides customers with programming information regarding the
  62. * products. This software is supplied "AS IS" without any warranties.
  63. * NXP Semiconductors assumes no responsibility or liability for the
  64. * use of the software, conveys no license or title under any patent,
  65. * copyright, or mask work right to the product. NXP Semiconductors
  66. * reserves the right to make changes in the software without
  67. * notification. NXP Semiconductors also make no representation or
  68. * warranty that such application will be suitable for the specified
  69. * use without further testing or modification.
  70. **********************************************************************/
  71. /*
  72. * \verbatim
  73. * $Id: $
  74. * \endverbatim
  75. */
  76. #include <stdint.h>
  77. #include <cfg/arch.h>
  78. #include <arch/cm3.h>
  79. #include <arch/cm3/timer.h>
  80. #include <arch/cm3/nxp/lpc177x_8x_clk.h>
  81. #include <cfg/clock.h>
  82. #include <arch/cm3/nxp/lpc177x_8x.h>
  83. /*
  84. //-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
  85. */
  86. /*--------------------- Clock Configuration ----------------------------------
  87. //
  88. // <e> Clock Configuration
  89. // <h> System Controls and Status Register (SCS)
  90. // <o1.0> EMC_SHIFT: EMC Shift enable
  91. // <0=> Static CS addresses match bus width; AD[1] = 0 for 32 bit, AD[0] = 0 for 16+32 bit
  92. // <1=> Static CS addresses start at LSB 0 regardless of memory width
  93. // <o1.1> EMC_RESET: EMC Reset disable
  94. // <0=> EMC will be reset by any chip reset
  95. // <1=> Portions of EMC will only be reset by POR or BOR
  96. // <o1.2> EMC_BURST: EMC Burst disable
  97. // <o1.3> MCIPWR_LEVEL: SD card interface signal SD_PWR Active Level selection
  98. // <0=> SD_PWR is active low
  99. // <1=> SD_PWR is active high
  100. // <o1.4> OSCRANGE: Main Oscillator Range Select
  101. // <0=> 1 MHz to 20 MHz
  102. // <1=> 15 MHz to 25 MHz
  103. // <o1.5> OSCEN: Main Oscillator enable
  104. // </h>
  105. //
  106. // <h> Clock Source Select Register (CLKSRCSEL)
  107. // <o2.0> CLKSRC: sysclk and PLL0 clock source selection
  108. // <0=> Internal RC oscillator
  109. // <1=> Main oscillator
  110. // </h>
  111. //
  112. // <e3> PLL0 Configuration (Main PLL)
  113. // <h> PLL0 Configuration Register (PLL0CFG)
  114. // <i> PLL out clock = (F_cco / (2 * P))
  115. // <i> F_cco = (F_in * M * 2 * P)
  116. // <i> F_in must be in the range of 1 MHz to 25 MHz
  117. // <i> F_cco must be in the range of 9.75 MHz to 160 MHz
  118. // <o4.0..4> MSEL: PLL Multiplier Selection
  119. // <i> M Value
  120. // <1-32><#-1>
  121. // <o4.5..6> PSEL: PLL Divider Selection
  122. // <i> P Value
  123. // <0=> 1
  124. // <1=> 2
  125. // <2=> 4
  126. // <3=> 8
  127. // </h>
  128. // </e>
  129. //
  130. // <e5> PLL1 Configuration (Alt PLL)
  131. // <h> PLL1 Configuration Register (PLL1CFG)
  132. // <i> PLL out clock = (F_cco / (2 * P))
  133. // <i> F_cco = (F_in * M * 2 * P)
  134. // <i> F_in must be in the range of 1 MHz to 25 MHz
  135. // <i> F_cco must be in the range of 9.75 MHz to 160 MHz
  136. // <o6.0..4> MSEL: PLL Multiplier Selection
  137. // <i> M Value
  138. // <1-32><#-1>
  139. // <o6.5..6> PSEL: PLL Divider Selection
  140. // <i> P Value
  141. // <0=> 1
  142. // <1=> 2
  143. // <2=> 4
  144. // <3=> 8
  145. // </h>
  146. // </e>
  147. //
  148. // <h> CPU Clock Selection Register (CCLKSEL)
  149. // <o7.0..4> CCLKDIV: CPU clock (CCLK) divider
  150. // <i> 0: The divider is turned off. No clock will be provided to the CPU
  151. // <i> n: The input clock is divided by n to produce the CPU clock
  152. // <0-31>
  153. // <o7.8> CCLKSEL: CPU clock divider input clock selection
  154. // <0=> sysclk clock
  155. // <1=> PLL0 clock
  156. // </h>
  157. //
  158. // <h> USB Clock Selection Register (USBCLKSEL)
  159. // <o8.0..4> USBDIV: USB clock (source PLL0) divider selection
  160. // <0=> USB clock off
  161. // <4=> PLL0 / 4 (PLL0 must be 192Mhz)
  162. // <6=> PLL0 / 6 (PLL0 must be 288Mhz)
  163. // <o8.8..9> USBSEL: USB clock divider input clock selection
  164. // <i> When CPU clock is selected, the USB can be accessed
  165. // <i> by software but cannot perform USB functions
  166. // <0=> CPU clock
  167. // <1=> PLL0 clock
  168. // <2=> PLL1 clock
  169. // </h>
  170. //
  171. // <h> EMC Clock Selection Register (EMCCLKSEL)
  172. // <o9.0> EMCDIV: EMC clock selection
  173. // <0=> CPU clock
  174. // <1=> CPU clock / 2
  175. // </h>
  176. //
  177. // <h> Peripheral Clock Selection Register (PCLKSEL)
  178. // <o10.0..4> PCLKDIV: APB Peripheral clock divider
  179. // <i> 0: The divider is turned off. No clock will be provided to APB peripherals
  180. // <i> n: The input clock is divided by n to produce the APB peripheral clock
  181. // <0-31>
  182. // </h>
  183. //
  184. // <h> Power Control for Peripherals Register (PCONP)
  185. // <o11.0> PCLCD: LCD controller power/clock enable
  186. // <o11.1> PCTIM0: Timer/Counter 0 power/clock enable
  187. // <o11.2> PCTIM1: Timer/Counter 1 power/clock enable
  188. // <o11.3> PCUART0: UART 0 power/clock enable
  189. // <o11.4> PCUART1: UART 1 power/clock enable
  190. // <o11.5> PCPWM0: PWM0 power/clock enable
  191. // <o11.6> PCPWM1: PWM1 power/clock enable
  192. // <o11.7> PCI2C0: I2C 0 interface power/clock enable
  193. // <o11.8> PCUART4: UART 4 power/clock enable
  194. // <o11.9> PCRTC: RTC and Event Recorder power/clock enable
  195. // <o11.10> PCSSP1: SSP 1 interface power/clock enable
  196. // <o11.11> PCEMC: External Memory Controller power/clock enable
  197. // <o11.12> PCADC: A/D converter power/clock enable
  198. // <o11.13> PCCAN1: CAN controller 1 power/clock enable
  199. // <o11.14> PCCAN2: CAN controller 2 power/clock enable
  200. // <o11.15> PCGPIO: IOCON, GPIO, and GPIO interrupts power/clock enable
  201. // <o11.17> PCMCPWM: Motor Control PWM power/clock enable
  202. // <o11.18> PCQEI: Quadrature encoder interface power/clock enable
  203. // <o11.19> PCI2C1: I2C 1 interface power/clock enable
  204. // <o11.20> PCSSP2: SSP 2 interface power/clock enable
  205. // <o11.21> PCSSP0: SSP 0 interface power/clock enable
  206. // <o11.22> PCTIM2: Timer 2 power/clock enable
  207. // <o11.23> PCTIM3: Timer 3 power/clock enable
  208. // <o11.24> PCUART2: UART 2 power/clock enable
  209. // <o11.25> PCUART3: UART 3 power/clock enable
  210. // <o11.26> PCI2C2: I2C 2 interface power/clock enable
  211. // <o11.27> PCI2S: I2S interface power/clock enable
  212. // <o11.28> PCSDC: SD Card interface power/clock enable
  213. // <o11.29> PCGPDMA: GPDMA function power/clock enable
  214. // <o11.30> PCENET: Ethernet block power/clock enable
  215. // <o11.31> PCUSB: USB interface power/clock enable
  216. // </h>
  217. //
  218. // <h> Clock Output Configuration Register (CLKOUTCFG)
  219. // <o12.0..3> CLKOUTSEL: Clock Source for CLKOUT Selection
  220. // <0=> CPU clock
  221. // <1=> Main Oscillator
  222. // <2=> Internal RC Oscillator
  223. // <3=> USB clock
  224. // <4=> RTC Oscillator
  225. // <5=> unused
  226. // <6=> Watchdog Oscillator
  227. // <o12.4..7> CLKOUTDIV: Output Clock Divider
  228. // <1-16><#-1>
  229. // <o12.8> CLKOUT_EN: CLKOUT enable
  230. // </h>
  231. //
  232. // </e>
  233. */
  234. #define CLOCK_SETUP 1
  235. #define SCS_Val 0x00000021
  236. #define CLKSRCSEL_Val 0x00000001
  237. #define PLL0_SETUP 1
  238. #define PLL0CFG_Val 0x00000009
  239. #define PLL1_SETUP 1
  240. #define PLL1CFG_Val 0x00000023
  241. #define CCLKSEL_Val (0x00000001|(1<<8))
  242. #define USBCLK_SETUP 1
  243. #define USBCLKSEL_Val (0x00000001|(0x02<<8))
  244. #define EMCCLKSEL_Val 0x00000001
  245. #define PCLKSEL_Val 0x00000002
  246. #define PCONP_Val 0x042887DE
  247. #define CLKOUTCFG_Val 0x00000100
  248. /*--------------------- Flash Accelerator Configuration ----------------------
  249. //
  250. // <e> Flash Accelerator Configuration
  251. // <o1.12..15> FLASHTIM: Flash Access Time
  252. // <0=> 1 CPU clock (for CPU clock up to 20 MHz)
  253. // <1=> 2 CPU clocks (for CPU clock up to 40 MHz)
  254. // <2=> 3 CPU clocks (for CPU clock up to 60 MHz)
  255. // <3=> 4 CPU clocks (for CPU clock up to 80 MHz)
  256. // <4=> 5 CPU clocks (for CPU clock up to 100 MHz)
  257. // <5=> 6 CPU clocks (for any CPU clock)
  258. // </e>
  259. */
  260. #define FLASH_SETUP 1
  261. #define FLASHCFG_Val 0x00005000
  262. /*----------------------------------------------------------------------------
  263. Check the register settings
  264. *----------------------------------------------------------------------------*/
  265. #define CHECK_RANGE(val, min, max) ((val < min) || (val > max))
  266. #define CHECK_RSVD(val, mask) (val & mask)
  267. /* Clock Configuration -------------------------------------------------------*/
  268. #if (CHECK_RSVD((SCS_Val), ~0x0000003F))
  269. #error "SCS: Invalid values of reserved bits!"
  270. #endif
  271. #if (CHECK_RANGE((CLKSRCSEL_Val), 0, 1))
  272. #error "CLKSRCSEL: Value out of range!"
  273. #endif
  274. #if (CHECK_RSVD((PLL0CFG_Val), ~0x0000007F))
  275. #error "PLL0CFG: Invalid values of reserved bits!"
  276. #endif
  277. #if (CHECK_RSVD((PLL1CFG_Val), ~0x0000007F))
  278. #error "PLL1CFG: Invalid values of reserved bits!"
  279. #endif
  280. #if (CHECK_RSVD((CCLKSEL_Val), ~0x0000011F))
  281. #error "CCLKSEL: Invalid values of reserved bits!"
  282. #endif
  283. #if (CHECK_RSVD((USBCLKSEL_Val), ~0x0000031F))
  284. #error "USBCLKSEL: Invalid values of reserved bits!"
  285. #endif
  286. #if (CHECK_RSVD((EMCCLKSEL_Val), ~0x00000001))
  287. #error "EMCCLKSEL: Invalid values of reserved bits!"
  288. #endif
  289. #if (CHECK_RSVD((PCLKSEL_Val), ~0x0000001F))
  290. #error "PCLKSEL: Invalid values of reserved bits!"
  291. #endif
  292. #if (CHECK_RSVD((PCONP_Val), ~0xFFFEFFFF))
  293. #error "PCONP: Invalid values of reserved bits!"
  294. #endif
  295. #if (CHECK_RSVD((CLKOUTCFG_Val), ~0x000001FF))
  296. #error "CLKOUTCFG: Invalid values of reserved bits!"
  297. #endif
  298. /* Flash Accelerator Configuration -------------------------------------------*/
  299. #if (CHECK_RSVD((FLASHCFG_Val), ~0x0000F000))
  300. #warning "FLASHCFG: Invalid values of reserved bits!"
  301. #endif
  302. /*----------------------------------------------------------------------------
  303. DEFINES
  304. *----------------------------------------------------------------------------*/
  305. /* pll_out_clk = F_cco / (2 × P)
  306. F_cco = pll_in_clk × M × 2 × P */
  307. #define __M ((PLL0CFG_Val & 0x1F) + 1)
  308. #define __PLL0_CLK(__F_IN) (__F_IN * __M)
  309. #define __CCLK_DIV (CCLKSEL_Val & 0x1F)
  310. #define __PCLK_DIV (PCLKSEL_Val & 0x1F)
  311. #define __ECLK_DIV ((EMCCLKSEL_Val & 0x01) + 1)
  312. /* Determine core clock frequency according to settings */
  313. #if (CLOCK_SETUP) /* Clock Setup */
  314. #if ((CLKSRCSEL_Val & 0x01) == 1) && ((SCS_Val & 0x20)== 0)
  315. #error "Main Oscillator is selected as clock source but is not enabled!"
  316. #endif
  317. #if ((CCLKSEL_Val & 0x100) == 0x100) && (PLL0_SETUP == 0)
  318. #error "Main PLL is selected as clock source but is not enabled!"
  319. #endif
  320. #if ((CCLKSEL_Val & 0x100) == 0) /* cclk = sysclk */
  321. #if ((CLKSRCSEL_Val & 0x01) == 0) /* sysclk = irc_clk */
  322. #define __CORE_CLK (IRC_OSC / __CCLK_DIV)
  323. #define __PER_CLK (IRC_OSC/ __PCLK_DIV)
  324. #define __EMC_CLK (IRC_OSC/ __ECLK_DIV)
  325. #else /* sysclk = osc_clk */
  326. #define __CORE_CLK (OSC_CLK / __CCLK_DIV)
  327. #define __PER_CLK (OSC_CLK/ __PCLK_DIV)
  328. #define __EMC_CLK (OSC_CLK/ __ECLK_DIV)
  329. #endif
  330. #else /* cclk = pll_clk */
  331. #if ((CLKSRCSEL_Val & 0x01) == 0) /* sysclk = irc_clk */
  332. #define __CORE_CLK (__PLL0_CLK(IRC_OSC) / __CCLK_DIV)
  333. #define __PER_CLK (__PLL0_CLK(IRC_OSC) / __PCLK_DIV)
  334. #define __EMC_CLK (__PLL0_CLK(IRC_OSC) / __ECLK_DIV)
  335. #else /* sysclk = osc_clk */
  336. #define __CORE_CLK (__PLL0_CLK(OSC_CLK) / __CCLK_DIV)
  337. #define __PER_CLK (__PLL0_CLK(OSC_CLK) / __PCLK_DIV)
  338. #define __EMC_CLK (__PLL0_CLK(OSC_CLK) / __ECLK_DIV)
  339. #endif
  340. #endif
  341. #else
  342. #define __CORE_CLK (IRC_OSC)
  343. #define __PER_CLK (IRC_OSC)
  344. #define __EMC_CLK (IRC_OSC)
  345. #endif
  346. /*----------------------------------------------------------------------------
  347. Clock Variable definitions
  348. *----------------------------------------------------------------------------*/
  349. uint32_t SystemCoreClock = __CORE_CLK;/*!< System Clock Frequency (Core Clock)*/
  350. uint32_t PeripheralClock = __PER_CLK; /*!< Peripheral Clock Frequency (Pclk) */
  351. uint32_t EMCClock = __EMC_CLK; /*!< EMC Clock Frequency */
  352. uint32_t USBClock = (48000000UL); /*!< USB Clock Frequency - this value will be updated after call SystemCoreClockUpdate, should be 48MHz*/
  353. /*----------------------------------------------------------------------------
  354. Clock functions
  355. *----------------------------------------------------------------------------*/
  356. void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
  357. {
  358. /* Determine clock frequency according to clock register values */
  359. if ((LPC_SC->CCLKSEL &0x100) == 0) { /* cclk = sysclk */
  360. if ((LPC_SC->CLKSRCSEL & 0x01) == 0) { /* sysclk = irc_clk */
  361. SystemCoreClock = (IRC_OSC / (LPC_SC->CCLKSEL & 0x1F));
  362. PeripheralClock = (IRC_OSC / (LPC_SC->PCLKSEL & 0x1F));
  363. EMCClock = (IRC_OSC / ((LPC_SC->EMCCLKSEL & 0x01)+1));
  364. } else { /* sysclk = osc_clk */
  365. if ((LPC_SC->SCS & 0x40) == 0) {
  366. SystemCoreClock = 0; /* this should never happen! */
  367. PeripheralClock = 0;
  368. EMCClock = 0;
  369. } else {
  370. SystemCoreClock = (OSC_CLK / (LPC_SC->CCLKSEL & 0x1F));
  371. PeripheralClock = (OSC_CLK / (LPC_SC->PCLKSEL & 0x1F));
  372. EMCClock = (OSC_CLK / ((LPC_SC->EMCCLKSEL & 0x01)+1));
  373. }
  374. }
  375. } else { /* cclk = pll_clk */
  376. if ((LPC_SC->PLL0STAT & 0x100) == 0) { /* PLL0 not enabled */
  377. SystemCoreClock = 0; /* this should never happen! */
  378. PeripheralClock = 0;
  379. EMCClock = 0;
  380. } else {
  381. if ((LPC_SC->CLKSRCSEL & 0x01) == 0) { /* sysclk = irc_clk */
  382. SystemCoreClock = (IRC_OSC * ((LPC_SC->PLL0STAT & 0x1F) + 1) / (LPC_SC->CCLKSEL & 0x1F));
  383. PeripheralClock = (IRC_OSC * ((LPC_SC->PLL0STAT & 0x1F) + 1) / (LPC_SC->PCLKSEL & 0x1F));
  384. EMCClock = (IRC_OSC * ((LPC_SC->PLL0STAT & 0x1F) + 1) / ((LPC_SC->EMCCLKSEL & 0x01)+1));
  385. } else { /* sysclk = osc_clk */
  386. if ((LPC_SC->SCS & 0x40) == 0) {
  387. SystemCoreClock = 0; /* this should never happen! */
  388. PeripheralClock = 0;
  389. EMCClock = 0;
  390. } else {
  391. SystemCoreClock = (OSC_CLK * ((LPC_SC->PLL0STAT & 0x1F) + 1) / (LPC_SC->CCLKSEL & 0x1F));
  392. PeripheralClock = (OSC_CLK * ((LPC_SC->PLL0STAT & 0x1F) + 1) / (LPC_SC->PCLKSEL & 0x1F));
  393. EMCClock = (OSC_CLK * ((LPC_SC->PLL0STAT & 0x1F) + 1) / ((LPC_SC->EMCCLKSEL & 0x01)+1));
  394. }
  395. }
  396. }
  397. }
  398. /* ---update USBClock------------------*/
  399. if(LPC_SC->USBCLKSEL & (0x01<<8)) { //Use PLL0 as the input to the USB clock divider
  400. switch (LPC_SC->USBCLKSEL & 0x1F) {
  401. case 0:
  402. USBClock = 0; //no clock will be provided to the USB subsystem
  403. break;
  404. case 4:
  405. case 6:
  406. if(LPC_SC->CLKSRCSEL & 0x01) //pll_clk_in = main_osc
  407. USBClock = (OSC_CLK * ((LPC_SC->PLL0STAT & 0x1F) + 1) / (LPC_SC->USBCLKSEL & 0x1F));
  408. else //pll_clk_in = irc_clk
  409. USBClock = (IRC_OSC * ((LPC_SC->PLL0STAT & 0x1F) + 1) / (LPC_SC->USBCLKSEL & 0x1F));
  410. break;
  411. default:
  412. USBClock = 0; /* this should never happen! */
  413. }
  414. } else
  415. if(LPC_SC->USBCLKSEL & (0x02<<8)) { //usb_input_clk = alt_pll (pll1)
  416. if(LPC_SC->CLKSRCSEL & 0x01) { //pll1_clk_in = main_osc
  417. USBClock = (OSC_CLK * ((LPC_SC->PLL1STAT & 0x1F) + 1));
  418. } else { //pll1_clk_in = irc_clk
  419. USBClock = (IRC_OSC * ((LPC_SC->PLL0STAT & 0x1F) + 1));
  420. }
  421. } else {
  422. USBClock = 0; /* this should never happen! */
  423. }
  424. }
  425. /**
  426. * @brief Sets System clock frequency to the configured defaults.
  427. * @note This function should be used only after reset.
  428. * @param None
  429. * @retval None
  430. */
  431. int SetSysClock(void)
  432. {
  433. int rc = 0;
  434. #if (CLOCK_SETUP) /* Clock Setup */
  435. LPC_SC->SCS = SCS_Val;
  436. if (SCS_Val & (1 << 5)) { /* If Main Oscillator is enabled */
  437. while ((LPC_SC->SCS & (1<<6)) == 0);/* Wait for Oscillator to be ready */
  438. }
  439. LPC_SC->CLKSRCSEL = CLKSRCSEL_Val; /* Select Clock Source for sysclk/PLL0*/
  440. #if (PLL0_SETUP)
  441. LPC_SC->PLL0CFG = PLL0CFG_Val;
  442. LPC_SC->PLL0CON = 0x01; /* PLL0 Enable */
  443. LPC_SC->PLL0FEED = 0xAA;
  444. LPC_SC->PLL0FEED = 0x55;
  445. while (!(LPC_SC->PLL0STAT & (1<<10)));/* Wait for PLOCK0 */
  446. #endif
  447. #if (PLL1_SETUP)
  448. LPC_SC->PLL1CFG = PLL1CFG_Val;
  449. LPC_SC->PLL1CON = 0x01; /* PLL1 Enable */
  450. LPC_SC->PLL1FEED = 0xAA;
  451. LPC_SC->PLL1FEED = 0x55;
  452. while (!(LPC_SC->PLL1STAT & (1<<10)));/* Wait for PLOCK1 */
  453. #endif
  454. LPC_SC->CCLKSEL = CCLKSEL_Val; /* Setup Clock Divider */
  455. LPC_SC->USBCLKSEL = USBCLKSEL_Val; /* Setup USB Clock Divider */
  456. LPC_SC->EMCCLKSEL = EMCCLKSEL_Val; /* EMC Clock Selection */
  457. LPC_SC->PCLKSEL = PCLKSEL_Val; /* Peripheral Clock Selection */
  458. LPC_SC->PCONP = PCONP_Val; /* Power Control for Peripherals */
  459. LPC_SC->CLKOUTCFG = CLKOUTCFG_Val; /* Clock Output Configuration */
  460. #endif
  461. #if (FLASH_SETUP == 1) /* Flash Accelerator Setup */
  462. LPC_SC->FLASHCFG = FLASHCFG_Val|0x03A;
  463. #endif
  464. SystemCoreClockUpdate();
  465. return rc;
  466. }
  467. /**
  468. * @brief requests System clock frequency
  469. *
  470. * @note This function should be used only after reset.
  471. * @param None
  472. * @retval None
  473. */
  474. uint32_t SysCtlClockGet(void)
  475. {
  476. SystemCoreClockUpdate();
  477. return SystemCoreClock;
  478. }
  479. /**
  480. * @brief requests frequency of the given clock
  481. *
  482. * @param idx NUT_HWCLK Index
  483. * @retval clock or 0 if idx points to an invalid clock
  484. */
  485. uint32_t Lpc17xx_ClockGet(int idx)
  486. {
  487. SystemCoreClockUpdate();
  488. switch(idx) {
  489. case NUT_HWCLK_CPU:
  490. return SystemCoreClock;
  491. break;
  492. case NUT_HWCLK_PCLK:
  493. return PeripheralClock;
  494. break;
  495. case NUT_HWCLK_EMC:
  496. return EMCClock;
  497. break;
  498. case NUT_HWCLK_USB:
  499. return USBClock;
  500. break;
  501. default:
  502. return 0;
  503. break;
  504. }
  505. }