lpc407x_8x_clk.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * Copyright (C) 2013 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. * $Id: system_LPC407x_8x.c 59014 2012-11-01 15:09:57Z anderslu $
  39. * system_LPC407x_8x.c 2012-01-16
  40. *//**
  41. * @file system_LPC407x_8x.c
  42. * @file system_LPC407x_8x.c
  43. * @brief CMSIS Cortex-M3, M4 Device Peripheral Access Layer Source File
  44. * for the NXP LPC407x_8x_177x_8x Device Series
  45. *
  46. * ARM Limited (ARM) is supplying this software for use with
  47. * Cortex-M processor based microcontrollers. This file can be
  48. * freely distributed within development tools that are supporting
  49. * such ARM based processors.
  50. *
  51. * @version 1.0
  52. * @date 30. May. 2012
  53. * @author NXP MCU SW Application Team
  54. *
  55. * Copyright(C) 2012, NXP Semiconductor
  56. * All rights reserved.
  57. *
  58. ***********************************************************************
  59. * Software that is described herein is for illustrative purposes only
  60. * which provides customers with programming information regarding the
  61. * products. This software is supplied "AS IS" without any warranties.
  62. * NXP Semiconductors assumes no responsibility or liability for the
  63. * use of the software, conveys no license or title under any patent,
  64. * copyright, or mask work right to the product. NXP Semiconductors
  65. * reserves the right to make changes in the software without
  66. * notification. NXP Semiconductors also make no representation or
  67. * warranty that such application will be suitable for the specified
  68. * use without further testing or modification.
  69. **********************************************************************/
  70. /*
  71. * \verbatim
  72. * $Id: $
  73. * \endverbatim
  74. */
  75. #include <stdint.h>
  76. #include <cfg/arch.h>
  77. #include <arch/cm3.h>
  78. #include <arch/cm3/timer.h>
  79. #include <arch/cm3/nxp/lpc407x_8x_clk.h>
  80. #include <cfg/clock.h>
  81. #include <arch/cm3/nxp/lpc407x_8x.h>
  82. #define __CLK_DIV(x,y) (((y) == 0) ? 0: (x)/(y))
  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 - address 0x400F C1A0)
  90. // <o1.0> EMC Shift Control Bit
  91. // <i> Controls how addresses are output on the EMC address pins for static memories
  92. // <0=> Static CS addresses match bus width; AD[1] = 0 for 32 bit, AD[0] = 0 for 16+32 bit (Bit 0 is 0)
  93. // <1=> Static CS addresses start at LSB 0 regardless of memory width (Bit 0 is 1)
  94. //
  95. // <o1.1> EMC Reset Disable Bit
  96. // <i> If 0 (zero), all registers and functions of the EMC are initialized upon any reset condition
  97. // <i> If 1, EMC is still retained its state through a warm reset
  98. // <0=> Both EMC resets are asserted when any type of chip reset event occurs (Bit 1 is 0)
  99. // <1=> Portions of EMC will only be reset by POR or BOR event (Bit 1 is 1)
  100. //
  101. // <o1.2> EMC Burst Control
  102. // <i> Set to 1 to prevent multiple sequential accesses to memory via EMC static memory chip selects
  103. // <0=> Burst enabled (Bit 2 is 0)
  104. // <1=> Bust disbled (Bit 2 is 1)
  105. //
  106. // <o1.3> MCIPWR Active Level
  107. // <i> Selects the active level for the SD card interface signal SD_PWR
  108. // <0=> SD_PWR is active low (inverted output of the SD Card interface block) (Bit 3 is 0)
  109. // <1=> SD_PWR is active high (follows the output of the SD Card interface block) (Bit 3 is 1)
  110. //
  111. // <o1.4> Main Oscillator Range Select
  112. // <0=> In Range 1 MHz to 20 MHz (Bit 4 is 0)
  113. // <1=> In Range 15 MHz to 25 MHz (Bit 4 is 1)
  114. //
  115. // <o1.5> Main Oscillator enable
  116. // <i> 0 (zero) means disabled, 1 means enable
  117. //
  118. // <o1.6> Main Oscillator status (Read-Only)
  119. // </h>
  120. //
  121. // <h> Clock Source Select Register (CLKSRCSEL - address 0x400F C10C)
  122. // <o2.0> CLKSRC: Select the clock source for sysclk to PLL0 clock
  123. // <0=> Internal RC oscillator (Bit 0 is 0)
  124. // <1=> Main oscillator (Bit 0 is 1)
  125. // </h>
  126. //
  127. // <e3>PLL0 Configuration (Main PLL PLL0CFG - address 0x400F C084)
  128. // <i> F_in is in the range of 1 MHz to 25 MHz
  129. // <i> F_cco = (F_in * M * 2 * P) is in range of 156 MHz to 320 MHz
  130. // <i> PLL out clock = (F_cco / (2 * P)) is in rane of 9.75 MHz to 160 MHz
  131. //
  132. // <o4.0..4> MSEL: PLL Multiplier Value
  133. // <i> M Value
  134. // <1-32><#-1>
  135. //
  136. // <o4.5..6> PSEL: PLL Divider Value
  137. // <i> P Value
  138. // <0=> 1
  139. // <1=> 2
  140. // <2=> 4
  141. // <3=> 8
  142. // </e>
  143. //
  144. // <e5>PLL1 Configuration (Alt PLL PLL1CFG - address 0x400F C0A4)
  145. // <i> F_in is in the range of 1 MHz to 25 MHz
  146. // <i> F_cco = (F_in * M * 2 * P) is in range of 156 MHz to 320 MHz
  147. // <i> PLL out clock = (F_cco / (2 * P)) is in rane of 9.75 MHz to 160 MHz
  148. //
  149. // <o6.0..4> MSEL: PLL Multiplier Value
  150. // <i> M Value
  151. // <1-32><#-1>
  152. //
  153. // <o6.5..6> PSEL: PLL Divider Value
  154. // <i> P Value
  155. // <0=> 1
  156. // <1=> 2
  157. // <2=> 4
  158. // <3=> 8
  159. // </e>
  160. //
  161. // <h> CPU Clock Selection Register (CCLKSEL - address 0x400F C104)
  162. // <o7.0..4> CCLKDIV: Select the value for divider of CPU clock (CCLK)
  163. // <i> 0: The divider is turned off. No clock will be provided to the CPU
  164. // <i> n: The input clock is divided by n to produce the CPU clock
  165. // <0-31>
  166. //
  167. // <o7.8> CCLKSEL: Select the input to the divider of CPU clock
  168. // <0=> sysclk clock is used
  169. // <1=> Main PLL0 clock is used
  170. // </h>
  171. //
  172. // <h> USB Clock Selection Register (USBCLKSEL - 0x400F C108)
  173. // <o8.0..4> USBDIV: USB clock (source PLL0) divider selection
  174. // <0=> Divider is off and no clock provides to USB subsystem
  175. // <4=> Divider value is 4 (The source clock is divided by 4)
  176. // <6=> Divider value is 6 (The source clock is divided by 6)
  177. //
  178. // <o8.8..9> USBSEL: Select the source for USB clock divider
  179. // <i> When CPU clock is selected, the USB can be accessed
  180. // <i> by software but cannot perform USB functions
  181. // <0=> sysclk clock (the clock input to PLL0)
  182. // <1=> The clock output from PLL0
  183. // <2=> The clock output from PLL1
  184. // </h>
  185. //
  186. // <h> EMC Clock Selection Register (EMCCLKSEL - address 0x400F C100)
  187. // <o9.0> EMCDIV: Set the divider for EMC clock
  188. // <0=> Divider value is 1
  189. // <1=> Divider value is 2 (EMC clock is equal a half of input clock)
  190. // </h>
  191. //
  192. // <h> Peripheral Clock Selection Register (PCLKSEL - address 0x400F C1A8)
  193. // <o10.0..4> PCLKDIV: APB Peripheral clock divider
  194. // <i> 0: The divider is turned off. No clock will be provided to APB peripherals
  195. // <i> n: The input clock is divided by n to produce the APB peripheral clock
  196. // <0-31>
  197. // </h>
  198. //
  199. // <h> SPIFI Clock Selection Register (SPIFICLKSEL - address 0x400F C1B4)
  200. // <o11.0..4> SPIFIDIV: Set the divider for SPIFI clock
  201. // <i> 0: The divider is turned off. No clock will be provided to the SPIFI
  202. // <i> n: The input clock is divided by n to produce the SPIFI clock
  203. // <0-31>
  204. //
  205. // <o11.8..9> SPIFISEL: Select the input clock for SPIFI clock divider
  206. // <0=> sysclk clock (the clock input to PLL0)
  207. // <1=> The clock output from PLL0
  208. // <2=> The clock output from PLL1
  209. // </h>
  210. //
  211. // <h> Power Control for Peripherals Register (PCONP - address 0x400F C1C8)
  212. // <o12.0> PCLCD: LCD controller power/clock enable (bit 0)
  213. // <o12.1> PCTIM0: Timer/Counter 0 power/clock enable (bit 1)
  214. // <o12.2> PCTIM1: Timer/Counter 1 power/clock enable (bit 2)
  215. // <o12.3> PCUART0: UART 0 power/clock enable (bit 3)
  216. // <o12.4> PCUART1: UART 1 power/clock enable (bit 4)
  217. // <o12.5> PCPWM0: PWM0 power/clock enable (bit 5)
  218. // <o12.6> PCPWM1: PWM1 power/clock enable (bit 6)
  219. // <o12.7> PCI2C0: I2C 0 interface power/clock enable (bit 7)
  220. // <o12.8> PCUART4: UART 4 power/clock enable (bit 8)
  221. // <o12.9> PCRTC: RTC and Event Recorder power/clock enable (bit 9)
  222. // <o12.10> PCSSP1: SSP 1 interface power/clock enable (bit 10)
  223. // <o12.11> PCEMC: External Memory Controller power/clock enable (bit 11)
  224. // <o12.12> PCADC: A/D converter power/clock enable (bit 12)
  225. // <o12.13> PCCAN1: CAN controller 1 power/clock enable (bit 13)
  226. // <o12.14> PCCAN2: CAN controller 2 power/clock enable (bit 14)
  227. // <o12.15> PCGPIO: IOCON, GPIO, and GPIO interrupts power/clock enable (bit 15)
  228. // <o12.17> PCMCPWM: Motor Control PWM power/clock enable (bit 17)
  229. // <o12.18> PCQEI: Quadrature encoder interface power/clock enable (bit 18)
  230. // <o12.19> PCI2C1: I2C 1 interface power/clock enable (bit 19)
  231. // <o12.20> PCSSP2: SSP 2 interface power/clock enable (bit 20)
  232. // <o12.21> PCSSP0: SSP 0 interface power/clock enable (bit 21)
  233. // <o12.22> PCTIM2: Timer 2 power/clock enable (bit 22)
  234. // <o12.23> PCTIM3: Timer 3 power/clock enable (bit 23)
  235. // <o12.24> PCUART2: UART 2 power/clock enable (bit 24)
  236. // <o12.25> PCUART3: UART 3 power/clock enable (bit 25)
  237. // <o12.26> PCI2C2: I2C 2 interface power/clock enable (bit 26)
  238. // <o12.27> PCI2S: I2S interface power/clock enable (bit 27)
  239. // <o12.28> PCSDC: SD Card interface power/clock enable (bit 28)
  240. // <o12.29> PCGPDMA: GPDMA function power/clock enable (bit 29)
  241. // <o12.30> PCENET: Ethernet block power/clock enable (bit 30)
  242. // <o12.31> PCUSB: USB interface power/clock enable (bit 31)
  243. // </h>
  244. //
  245. // <h> Clock Output Configuration Register (CLKOUTCFG)
  246. // <o13.0..3> CLKOUTSEL: Clock Source for CLKOUT Selection
  247. // <0=> CPU clock
  248. // <1=> Main Oscillator
  249. // <2=> Internal RC Oscillator
  250. // <3=> USB clock
  251. // <4=> RTC Oscillator
  252. // <5=> unused
  253. // <6=> Watchdog Oscillator
  254. //
  255. // <o13.4..7> CLKOUTDIV: Output Clock Divider
  256. // <1-16><#-1>
  257. //
  258. // <o13.8> CLKOUT_EN: CLKOUT enable
  259. // </h>
  260. //
  261. // </e>
  262. */
  263. #define CLOCK_SETUP 1
  264. #define SCS_Val 0x00000020
  265. #define CLKSRCSEL_Val 0x00000001
  266. #define PLL0_SETUP 1
  267. #define PLL0CFG_Val 0x00000007
  268. #define PLL1_SETUP 1
  269. #define PLL1CFG_Val 0x0000000C
  270. #define CCLKSEL_Val 0x00000101
  271. #define USBCLKSEL_Val 0x00000102
  272. #define EMCCLKSEL_Val 0x00000000
  273. #define PCLKSEL_Val 0x00000002
  274. #define SPIFICLKSEL_Val 0x00000202
  275. #define PCONP_Val 0x042887DE
  276. #define CLKOUTCFG_Val 0x00000100
  277. #define LPC_CPACR 0xE000ED88
  278. #define SCB_MVFR0 0xE000EF40
  279. #define SCB_MVFR0_RESET 0x10110021
  280. #define SCB_MVFR1 0xE000EF44
  281. #define SCB_MVFR1_RESET 0x11000011
  282. /*--------------------- Flash Accelerator Configuration ----------------------
  283. //
  284. // <e> Flash Accelerator Configuration register (FLASHCFG - address 0x400F C000)
  285. // <o1.12..15> FLASHTIM: Flash Access Time
  286. // <0=> 1 CPU clock (for CPU clock up to 20 MHz)
  287. // <1=> 2 CPU clocks (for CPU clock up to 40 MHz)
  288. // <2=> 3 CPU clocks (for CPU clock up to 60 MHz)
  289. // <3=> 4 CPU clocks (for CPU clock up to 80 MHz)
  290. // <4=> 5 CPU clocks (for CPU clock up to 100 MHz)
  291. // <5=> 6 CPU clocks (for any CPU clock)
  292. // </e>
  293. */
  294. #define FLASH_SETUP 1
  295. #define FLASHCFG_Val 0x00005000
  296. /*----------------------------------------------------------------------------
  297. Check the register settings
  298. *----------------------------------------------------------------------------*/
  299. #define CHECK_RANGE(val, min, max) ((val < min) || (val > max))
  300. #define CHECK_RSVD(val, mask) (val & mask)
  301. /* Clock Configuration -------------------------------------------------------*/
  302. #if (CHECK_RSVD((SCS_Val), ~0x0000003F))
  303. #error "SCS: Invalid values of reserved bits!"
  304. #endif
  305. #if (CHECK_RANGE((CLKSRCSEL_Val), 0, 1))
  306. #error "CLKSRCSEL: Value out of range!"
  307. #endif
  308. #if (CHECK_RSVD((PLL0CFG_Val), ~0x0000007F))
  309. #error "PLL0CFG: Invalid values of reserved bits!"
  310. #endif
  311. #if (CHECK_RSVD((PLL1CFG_Val), ~0x0000007F))
  312. #error "PLL1CFG: Invalid values of reserved bits!"
  313. #endif
  314. #if (CHECK_RSVD((CCLKSEL_Val), ~0x0000011F))
  315. #error "CCLKSEL: Invalid values of reserved bits!"
  316. #endif
  317. #if (CHECK_RSVD((USBCLKSEL_Val), ~0x0000031F))
  318. #error "USBCLKSEL: Invalid values of reserved bits!"
  319. #endif
  320. #if (CHECK_RSVD((EMCCLKSEL_Val), ~0x00000001))
  321. #error "EMCCLKSEL: Invalid values of reserved bits!"
  322. #endif
  323. #if (CHECK_RSVD((PCLKSEL_Val), ~0x0000001F))
  324. #error "PCLKSEL: Invalid values of reserved bits!"
  325. #endif
  326. #if (CHECK_RSVD((PCONP_Val), ~0xFFFEFFFF))
  327. #error "PCONP: Invalid values of reserved bits!"
  328. #endif
  329. #if (CHECK_RSVD((CLKOUTCFG_Val), ~0x000001FF))
  330. #error "CLKOUTCFG: Invalid values of reserved bits!"
  331. #endif
  332. /* Flash Accelerator Configuration -------------------------------------------*/
  333. #if (CHECK_RSVD((FLASHCFG_Val), ~0x0000F000))
  334. #warning "FLASHCFG: Invalid values of reserved bits!"
  335. #endif
  336. /*----------------------------------------------------------------------------
  337. DEFINES
  338. *----------------------------------------------------------------------------*/
  339. /* pll_out_clk = F_cco / (2 × P)
  340. F_cco = pll_in_clk × M × 2 × P */
  341. #define __M(__PLLCFG_Val) ((__PLLCFG_Val & 0x1F) + 1)
  342. #define __PLL0_CLK(__F_IN) (__F_IN * __M(PLL0CFG_Val))
  343. #define __PLL1_CLK (OSC_CLK * __M(PLL1CFG_Val))
  344. #define __CCLK_DIV (CCLKSEL_Val & 0x1F)
  345. #define __PCLK_DIV (PCLKSEL_Val & 0x1F)
  346. #define __ECLK_DIV ((EMCCLKSEL_Val & 0x01) + 1)
  347. #define __SPIFI_DIV (SPIFICLKSEL_Val & 0x1F)
  348. /* Determine core clock frequency according to settings */
  349. #if (CLOCK_SETUP) /* Clock Setup */
  350. #if ((CLKSRCSEL_Val & 0x01) == 1) && ((SCS_Val & 0x20)== 0)
  351. #error "Main Oscillator is selected as clock source but is not enabled!"
  352. #endif
  353. #if ((CCLKSEL_Val & 0x100) == 0x100) && (PLL0_SETUP == 0)
  354. #error "Main PLL is selected as clock source but is not enabled!"
  355. #endif
  356. #if ((CCLKSEL_Val & 0x100) == 0) /* cclk = sysclk */
  357. #if ((CLKSRCSEL_Val & 0x01) == 0) /* sysclk = irc_clk */
  358. #define __CORE_CLK (IRC_OSC / __CCLK_DIV)
  359. #define __PER_CLK (IRC_OSC/ __PCLK_DIV)
  360. #define __EMC_CLK (__CORE_CLK/ __ECLK_DIV)
  361. #else /* sysclk = osc_clk */
  362. #define __CORE_CLK (OSC_CLK / __CCLK_DIV)
  363. #define __PER_CLK (OSC_CLK/ __PCLK_DIV)
  364. #define __EMC_CLK (__CORE_CLK/ __ECLK_DIV)
  365. #endif
  366. #else /* cclk = pll_clk */
  367. #if ((CLKSRCSEL_Val & 0x01) == 0) /* sysclk = irc_clk */
  368. #define __CORE_CLK (__PLL0_CLK(IRC_OSC) / __CCLK_DIV)
  369. #define __PER_CLK (__PLL0_CLK(IRC_OSC) / __PCLK_DIV)
  370. #define __EMC_CLK (__CORE_CLK / __ECLK_DIV)
  371. #else /* sysclk = osc_clk */
  372. #define __CORE_CLK (__PLL0_CLK(OSC_CLK) / __CCLK_DIV)
  373. #define __PER_CLK (__PLL0_CLK(OSC_CLK) / __PCLK_DIV)
  374. #define __EMC_CLK (__CORE_CLK / __ECLK_DIV)
  375. #endif
  376. #endif
  377. #if ((SPIFICLKSEL_Val & 0x300) == 0)
  378. #if ((CLKSRCSEL_Val & 0x01) == 0) /* sysclk = irc_clk */
  379. #define __SPIFI_CLK (IRC_OSC / __SPIFI_DIV)
  380. #else /* sysclk = osc_clk */
  381. #define __SPIFI_CLK (OSC_CLK / __SPIFI_DIV)
  382. #endif
  383. #elif ((SPIFICLKSEL_Val & 0x300) == 1)
  384. #if ((CLKSRCSEL_Val & 0x01) == 0) /* sysclk = irc_clk */
  385. #define __CORE_CLK (__PLL0_CLK(IRC_OSC) / __SPIFI_DIV)
  386. #else /* sysclk = osc_clk */
  387. #define __CORE_CLK (__PLL0_CLK(OSC_CLK) / __SPIFI_DIV)
  388. #endif
  389. #else
  390. #define __SPIFI_CLK (__PLL1_CLK / __SPIFI_DIV)
  391. #endif
  392. #else
  393. #define __CORE_CLK (IRC_OSC)
  394. #define __PER_CLK (IRC_OSC)
  395. #define __EMC_CLK (__CORE_CLK)
  396. #define __SPIFI_CLK (0)
  397. #endif
  398. /*----------------------------------------------------------------------------
  399. Clock Variable definitions
  400. *----------------------------------------------------------------------------*/
  401. uint32_t SystemCoreClock = __CORE_CLK; /*!< System Clock Frequency (Core Clock)*/
  402. uint32_t PeripheralClock = __PER_CLK; /*!< Peripheral Clock Frequency (Pclk) */
  403. uint32_t EMCClock = __EMC_CLK; /*!< EMC Clock Frequency */
  404. uint32_t USBClock = (48000000UL); /*!< USB Clock Frequency */
  405. uint32_t SPIFIClock = __SPIFI_CLK; /*!< SPIFI Clock Frequecy */
  406. /*----------------------------------------------------------------------------
  407. Clock functions
  408. *----------------------------------------------------------------------------*/
  409. void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
  410. {
  411. /* Determine clock frequency according to clock register values */
  412. if ((LPC_SC->CCLKSEL &0x100) == 0) { /* cclk = sysclk */
  413. if ((LPC_SC->CLKSRCSEL & 0x01) == 0) { /* sysclk = irc_clk */
  414. SystemCoreClock = __CLK_DIV(IRC_OSC , (LPC_SC->CCLKSEL & 0x1F));
  415. PeripheralClock = __CLK_DIV(IRC_OSC , (LPC_SC->PCLKSEL & 0x1F));
  416. EMCClock = (SystemCoreClock / ((LPC_SC->EMCCLKSEL & 0x01)+1));
  417. }
  418. else { /* sysclk = osc_clk */
  419. if ((LPC_SC->SCS & 0x40) == 0) {
  420. SystemCoreClock = 0; /* this should never happen! */
  421. PeripheralClock = 0;
  422. EMCClock = 0;
  423. }
  424. else {
  425. SystemCoreClock = __CLK_DIV(OSC_CLK , (LPC_SC->CCLKSEL & 0x1F));
  426. PeripheralClock = __CLK_DIV(OSC_CLK , (LPC_SC->PCLKSEL & 0x1F));
  427. EMCClock = (SystemCoreClock / ((LPC_SC->EMCCLKSEL & 0x01)+1));
  428. }
  429. }
  430. }
  431. else { /* cclk = pll_clk */
  432. if ((LPC_SC->PLL0STAT & 0x100) == 0) { /* PLL0 not enabled */
  433. SystemCoreClock = 0; /* this should never happen! */
  434. PeripheralClock = 0;
  435. EMCClock = 0;
  436. }
  437. else {
  438. if ((LPC_SC->CLKSRCSEL & 0x01) == 0) { /* sysclk = irc_clk */
  439. uint8_t mul = ((LPC_SC->PLL0STAT & 0x1F) + 1);
  440. uint8_t cpu_div = (LPC_SC->CCLKSEL & 0x1F);
  441. uint8_t per_div = (LPC_SC->PCLKSEL & 0x1F);
  442. uint8_t emc_div = (LPC_SC->EMCCLKSEL & 0x01)+1;
  443. SystemCoreClock = __CLK_DIV(IRC_OSC * mul , cpu_div);
  444. PeripheralClock = __CLK_DIV(IRC_OSC * mul , per_div);
  445. EMCClock = SystemCoreClock / emc_div;
  446. }
  447. else { /* sysclk = osc_clk */
  448. if ((LPC_SC->SCS & 0x40) == 0) {
  449. SystemCoreClock = 0; /* this should never happen! */
  450. PeripheralClock = 0;
  451. EMCClock = 0;
  452. }
  453. else {
  454. uint8_t mul = ((LPC_SC->PLL0STAT & 0x1F) + 1);
  455. uint8_t cpu_div = (LPC_SC->CCLKSEL & 0x1F);
  456. uint8_t per_div = (LPC_SC->PCLKSEL & 0x1F);
  457. uint8_t emc_div = (LPC_SC->EMCCLKSEL & 0x01)+1;
  458. SystemCoreClock = __CLK_DIV(OSC_CLK * mul , cpu_div);
  459. PeripheralClock = __CLK_DIV(OSC_CLK * mul , per_div);
  460. EMCClock = SystemCoreClock / emc_div;
  461. }
  462. }
  463. }
  464. }
  465. /* ---update USBClock------------------*/
  466. if(LPC_SC->USBCLKSEL & (0x01<<8))//Use PLL0 as the input to the USB clock divider
  467. {
  468. switch (LPC_SC->USBCLKSEL & 0x1F)
  469. {
  470. case 0:
  471. USBClock = 0; //no clock will be provided to the USB subsystem
  472. break;
  473. case 2:
  474. case 4:
  475. case 6:
  476. {
  477. uint8_t mul = ((LPC_SC->PLL0STAT & 0x1F) + 1);
  478. uint8_t usb_div = (LPC_SC->USBCLKSEL & 0x1F);
  479. if(LPC_SC->CLKSRCSEL & 0x01) //pll_clk_in = main_osc
  480. USBClock = OSC_CLK * mul / usb_div;
  481. else //pll_clk_in = irc_clk
  482. USBClock = IRC_OSC * mul / usb_div;
  483. }
  484. break;
  485. default:
  486. USBClock = 0; /* this should never happen! */
  487. }
  488. }
  489. else if(LPC_SC->USBCLKSEL & (0x02<<8))//usb_input_clk = alt_pll (pll1)
  490. {
  491. USBClock = (OSC_CLK * ((LPC_SC->PLL1STAT & 0x1F) + 1));
  492. }
  493. else
  494. USBClock = 0; /* this should never happen! */
  495. /* ---update SPIFIClock------------------*/
  496. switch((LPC_SC->SPIFICLKSEL & 0x300)>>8)
  497. {
  498. case 0: /*spifi_clk = sysclk*/
  499. if ((LPC_SC->CLKSRCSEL & 0x01) == 0) { /* sysclk = irc_clk */
  500. SPIFIClock = __CLK_DIV(IRC_OSC , (LPC_SC->SPIFICLKSEL & 0x1F));
  501. }
  502. else { /* sysclk = osc_clk */
  503. if ((LPC_SC->SCS & 0x40) == 0) {
  504. SPIFIClock = 0; /* */
  505. }
  506. else {
  507. SPIFIClock = __CLK_DIV(OSC_CLK , (LPC_SC->SPIFICLKSEL & 0x1F));
  508. }
  509. }
  510. break;
  511. case 1: /*spifi_clk = pll0*/
  512. if ((LPC_SC->PLL0STAT & 0x100) == 0) { /* PLL0 not enabled */
  513. SPIFIClock = 0; /* this should never happen! */
  514. }
  515. else {
  516. if ((LPC_SC->CLKSRCSEL & 0x01) == 0) { /* sysclk = irc_clk */
  517. uint8_t mul = ((LPC_SC->PLL0STAT & 0x1F) + 1);
  518. uint8_t spifi_div = (LPC_SC->SPIFICLKSEL & 0x1F);
  519. SPIFIClock = __CLK_DIV(IRC_OSC * mul , spifi_div);
  520. }
  521. else { /* sysclk = osc_clk */
  522. if ((LPC_SC->SCS & 0x40) == 0) {
  523. SPIFIClock = 0; /* */
  524. }
  525. else {
  526. uint8_t mul = ((LPC_SC->PLL0STAT & 0x1F) + 1);
  527. uint8_t spifi_div = (LPC_SC->SPIFICLKSEL & 0x1F);
  528. SPIFIClock = __CLK_DIV(OSC_CLK * mul , spifi_div);
  529. }
  530. }
  531. }
  532. break;
  533. case 2: /*spifi_clk = pll1*/
  534. if ((LPC_SC->PLL1STAT & 0x100) == 0) { /* PLL1 not enabled */
  535. SPIFIClock = 0; /* this should never happen! */
  536. }
  537. else {
  538. if ((LPC_SC->SCS & 0x40) == 0) {
  539. SPIFIClock = 0; /* */
  540. }
  541. else {
  542. uint8_t mul = ((LPC_SC->PLL1STAT & 0x1F) + 1);
  543. uint8_t spifi_div = (LPC_SC->SPIFICLKSEL & 0x1F);
  544. SPIFIClock = __CLK_DIV(OSC_CLK * mul , spifi_div);
  545. }
  546. }
  547. break;
  548. default:
  549. SPIFIClock = 0; /* this should never happen! */
  550. break;
  551. }
  552. }
  553. /**
  554. * @brief Sets System clock frequency to the configured defaults.
  555. * @note This function should be used only after reset.
  556. * @param None
  557. * @retval None
  558. */
  559. int SetSysClock(void)
  560. {
  561. int rc = 0;
  562. #if (CLOCK_SETUP) /* Clock Setup */
  563. LPC_SC->SCS = SCS_Val;
  564. if (SCS_Val & (1 << 5)) { /* If Main Oscillator is enabled */
  565. while ((LPC_SC->SCS & (1<<6)) == 0);/* Wait for Oscillator to be ready */
  566. }
  567. LPC_SC->CLKSRCSEL = CLKSRCSEL_Val; /* Select Clock Source for sysclk/PLL0*/
  568. #if (PLL0_SETUP)
  569. LPC_SC->PLL0CFG = PLL0CFG_Val;
  570. LPC_SC->PLL0CON = 0x01; /* PLL0 Enable */
  571. LPC_SC->PLL0FEED = 0xAA;
  572. LPC_SC->PLL0FEED = 0x55;
  573. while (!(LPC_SC->PLL0STAT & (1<<10)));/* Wait for PLOCK0 */
  574. #endif
  575. #if (PLL1_SETUP)
  576. LPC_SC->PLL1CFG = PLL1CFG_Val;
  577. LPC_SC->PLL1CON = 0x01; /* PLL1 Enable */
  578. LPC_SC->PLL1FEED = 0xAA;
  579. LPC_SC->PLL1FEED = 0x55;
  580. while (!(LPC_SC->PLL1STAT & (1<<10)));/* Wait for PLOCK1 */
  581. #endif
  582. LPC_SC->CCLKSEL = CCLKSEL_Val; /* Setup Clock Divider */
  583. LPC_SC->USBCLKSEL = USBCLKSEL_Val; /* Setup USB Clock Divider */
  584. LPC_SC->EMCCLKSEL = EMCCLKSEL_Val; /* EMC Clock Selection */
  585. LPC_SC->SPIFICLKSEL = SPIFICLKSEL_Val; /* SPIFI Clock Selection */
  586. LPC_SC->PCLKSEL = PCLKSEL_Val; /* Peripheral Clock Selection */
  587. LPC_SC->PCONP = PCONP_Val; /* Power Control for Peripherals */
  588. LPC_SC->CLKOUTCFG = CLKOUTCFG_Val; /* Clock Output Configuration */
  589. #endif
  590. LPC_SC->PBOOST |= 0x03; /* Power Boost control */
  591. #if (FLASH_SETUP == 1) /* Flash Accelerator Setup */
  592. LPC_SC->FLASHCFG = FLASHCFG_Val|0x03A;
  593. #endif
  594. SystemCoreClockUpdate();
  595. return rc;
  596. }
  597. /**
  598. * @brief requests System clock frequency
  599. *
  600. * @note This function should be used only after reset.
  601. * @param None
  602. * @retval None
  603. */
  604. uint32_t SysCtlClockGet(void)
  605. {
  606. SystemCoreClockUpdate();
  607. return SystemCoreClock;
  608. }
  609. /**
  610. * @brief requests frequency of the given clock
  611. *
  612. * @param idx NUT_HWCLK Index
  613. * @retval clock or 0 if idx points to an invalid clock
  614. */
  615. uint32_t Lpc17xx_ClockGet(int idx)
  616. {
  617. SystemCoreClockUpdate();
  618. switch(idx) {
  619. case NUT_HWCLK_CPU:
  620. return SystemCoreClock;
  621. break;
  622. case NUT_HWCLK_PCLK:
  623. return PeripheralClock;
  624. break;
  625. case NUT_HWCLK_EMC:
  626. return EMCClock;
  627. break;
  628. case NUT_HWCLK_USB:
  629. return USBClock;
  630. break;
  631. case NUT_HWCLK_SPIFI:
  632. return SPIFIClock;
  633. break;
  634. default:
  635. return 0;
  636. break;
  637. }
  638. }