]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_NXP/TARGET_LPC176X/system_LPC17xx.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_NXP / TARGET_LPC176X / system_LPC17xx.c
1 /**************************************************************************//**
2  * @file     system_LPC17xx.c
3  * @brief    CMSIS Cortex-M3 Device System Source File for
4  *           NXP LPC17xx Device Series
5  * @version  V1.11
6  * @date     21. June 2011
7  *
8  * @note
9  * Copyright (C) 2009-2011 ARM Limited. All rights reserved.
10  *
11  * @par
12  * ARM Limited (ARM) is supplying this software for use with Cortex-M
13  * processor based microcontrollers.  This file can be freely distributed
14  * within development tools that are supporting such ARM based processors.
15  *
16  * @par
17  * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
18  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
20  * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
21  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
22  *
23  ******************************************************************************/
24
25
26 #include <stdint.h>
27 #include "LPC17xx.h"
28
29
30 /** @addtogroup LPC17xx_System
31  * @{
32  */
33
34 /*
35 //-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
36 */
37
38 /*--------------------- Clock Configuration ----------------------------------
39 //
40 // <e> Clock Configuration
41 //   <h> System Controls and Status Register (SCS)
42 //     <o1.4>    OSCRANGE: Main Oscillator Range Select
43 //                     <0=>  1 MHz to 20 MHz
44 //                     <1=> 15 MHz to 25 MHz
45 //     <e1.5>       OSCEN: Main Oscillator Enable
46 //     </e>
47 //   </h>
48 //
49 //   <h> Clock Source Select Register (CLKSRCSEL)
50 //     <o2.0..1>   CLKSRC: PLL Clock Source Selection
51 //                     <0=> Internal RC oscillator
52 //                     <1=> Main oscillator
53 //                     <2=> RTC oscillator
54 //   </h>
55 //
56 //   <e3> PLL0 Configuration (Main PLL)
57 //     <h> PLL0 Configuration Register (PLL0CFG)
58 //                     <i> F_cco0 = (2 * M * F_in) / N
59 //                     <i> F_in must be in the range of 32 kHz to 50 MHz
60 //                     <i> F_cco0 must be in the range of 275 MHz to 550 MHz
61 //       <o4.0..14>  MSEL: PLL Multiplier Selection
62 //                     <6-32768><#-1>
63 //                     <i> M Value
64 //       <o4.16..23> NSEL: PLL Divider Selection
65 //                     <1-256><#-1>
66 //                     <i> N Value
67 //     </h>
68 //   </e>
69 //
70 //   <e5> PLL1 Configuration (USB PLL)
71 //     <h> PLL1 Configuration Register (PLL1CFG)
72 //                     <i> F_usb = M * F_osc or F_usb = F_cco1 / (2 * P)
73 //                     <i> F_cco1 = F_osc * M * 2 * P
74 //                     <i> F_cco1 must be in the range of 156 MHz to 320 MHz
75 //       <o6.0..4>   MSEL: PLL Multiplier Selection
76 //                     <1-32><#-1>
77 //                     <i> M Value (for USB maximum value is 4)
78 //       <o6.5..6>   PSEL: PLL Divider Selection
79 //                     <0=> 1
80 //                     <1=> 2
81 //                     <2=> 4
82 //                     <3=> 8
83 //                     <i> P Value
84 //     </h>
85 //   </e>
86 //
87 //   <h> CPU Clock Configuration Register (CCLKCFG)
88 //     <o7.0..7>  CCLKSEL: Divide Value for CPU Clock from PLL0
89 //                     <1-256><#-1>
90 //   </h>
91 //
92 //   <h> USB Clock Configuration Register (USBCLKCFG)
93 //     <o8.0..3>   USBSEL: Divide Value for USB Clock from PLL0
94 //                     <0-15>
95 //                     <i> Divide is USBSEL + 1
96 //   </h>
97 //
98 //   <h> Peripheral Clock Selection Register 0 (PCLKSEL0)
99 //     <o9.0..1>    PCLK_WDT: Peripheral Clock Selection for WDT
100 //                     <0=> Pclk = Cclk / 4
101 //                     <1=> Pclk = Cclk
102 //                     <2=> Pclk = Cclk / 2
103 //                     <3=> Pclk = Hclk / 8
104 //     <o9.2..3>    PCLK_TIMER0: Peripheral Clock Selection for TIMER0
105 //                     <0=> Pclk = Cclk / 4
106 //                     <1=> Pclk = Cclk
107 //                     <2=> Pclk = Cclk / 2
108 //                     <3=> Pclk = Hclk / 8
109 //     <o9.4..5>    PCLK_TIMER1: Peripheral Clock Selection for TIMER1
110 //                     <0=> Pclk = Cclk / 4
111 //                     <1=> Pclk = Cclk
112 //                     <2=> Pclk = Cclk / 2
113 //                     <3=> Pclk = Hclk / 8
114 //     <o9.6..7>    PCLK_UART0: Peripheral Clock Selection for UART0
115 //                     <0=> Pclk = Cclk / 4
116 //                     <1=> Pclk = Cclk
117 //                     <2=> Pclk = Cclk / 2
118 //                     <3=> Pclk = Hclk / 8
119 //     <o9.8..9>    PCLK_UART1: Peripheral Clock Selection for UART1
120 //                     <0=> Pclk = Cclk / 4
121 //                     <1=> Pclk = Cclk
122 //                     <2=> Pclk = Cclk / 2
123 //                     <3=> Pclk = Hclk / 8
124 //     <o9.12..13>  PCLK_PWM1: Peripheral Clock Selection for PWM1
125 //                     <0=> Pclk = Cclk / 4
126 //                     <1=> Pclk = Cclk
127 //                     <2=> Pclk = Cclk / 2
128 //                     <3=> Pclk = Hclk / 8
129 //     <o9.14..15>  PCLK_I2C0: Peripheral Clock Selection for I2C0
130 //                     <0=> Pclk = Cclk / 4
131 //                     <1=> Pclk = Cclk
132 //                     <2=> Pclk = Cclk / 2
133 //                     <3=> Pclk = Hclk / 8
134 //     <o9.16..17>  PCLK_SPI: Peripheral Clock Selection for SPI
135 //                     <0=> Pclk = Cclk / 4
136 //                     <1=> Pclk = Cclk
137 //                     <2=> Pclk = Cclk / 2
138 //                     <3=> Pclk = Hclk / 8
139 //     <o9.20..21>  PCLK_SSP1: Peripheral Clock Selection for SSP1
140 //                     <0=> Pclk = Cclk / 4
141 //                     <1=> Pclk = Cclk
142 //                     <2=> Pclk = Cclk / 2
143 //                     <3=> Pclk = Hclk / 8
144 //     <o9.22..23>  PCLK_DAC: Peripheral Clock Selection for DAC
145 //                     <0=> Pclk = Cclk / 4
146 //                     <1=> Pclk = Cclk
147 //                     <2=> Pclk = Cclk / 2
148 //                     <3=> Pclk = Hclk / 8
149 //     <o9.24..25>  PCLK_ADC: Peripheral Clock Selection for ADC
150 //                     <0=> Pclk = Cclk / 4
151 //                     <1=> Pclk = Cclk
152 //                     <2=> Pclk = Cclk / 2
153 //                     <3=> Pclk = Hclk / 8
154 //     <o9.26..27>  PCLK_CAN1: Peripheral Clock Selection for CAN1
155 //                     <0=> Pclk = Cclk / 4
156 //                     <1=> Pclk = Cclk
157 //                     <2=> Pclk = Cclk / 2
158 //                     <3=> Pclk = Hclk / 6
159 //     <o9.28..29>  PCLK_CAN2: Peripheral Clock Selection for CAN2
160 //                     <0=> Pclk = Cclk / 4
161 //                     <1=> Pclk = Cclk
162 //                     <2=> Pclk = Cclk / 2
163 //                     <3=> Pclk = Hclk / 6
164 //     <o9.30..31>  PCLK_ACF: Peripheral Clock Selection for ACF
165 //                     <0=> Pclk = Cclk / 4
166 //                     <1=> Pclk = Cclk
167 //                     <2=> Pclk = Cclk / 2
168 //                     <3=> Pclk = Hclk / 6
169 //   </h>
170 //
171 //   <h> Peripheral Clock Selection Register 1 (PCLKSEL1)
172 //     <o10.0..1>   PCLK_QEI: Peripheral Clock Selection for the Quadrature Encoder Interface
173 //                     <0=> Pclk = Cclk / 4
174 //                     <1=> Pclk = Cclk
175 //                     <2=> Pclk = Cclk / 2
176 //                     <3=> Pclk = Hclk / 8
177 //     <o10.2..3>   PCLK_GPIO: Peripheral Clock Selection for GPIOs
178 //                     <0=> Pclk = Cclk / 4
179 //                     <1=> Pclk = Cclk
180 //                     <2=> Pclk = Cclk / 2
181 //                     <3=> Pclk = Hclk / 8
182 //     <o10.4..5>   PCLK_PCB: Peripheral Clock Selection for the Pin Connect Block
183 //                     <0=> Pclk = Cclk / 4
184 //                     <1=> Pclk = Cclk
185 //                     <2=> Pclk = Cclk / 2
186 //                     <3=> Pclk = Hclk / 8
187 //     <o10.6..7>   PCLK_I2C1: Peripheral Clock Selection for I2C1
188 //                     <0=> Pclk = Cclk / 4
189 //                     <1=> Pclk = Cclk
190 //                     <2=> Pclk = Cclk / 2
191 //                     <3=> Pclk = Hclk / 8
192 //     <o10.10..11> PCLK_SSP0: Peripheral Clock Selection for SSP0
193 //                     <0=> Pclk = Cclk / 4
194 //                     <1=> Pclk = Cclk
195 //                     <2=> Pclk = Cclk / 2
196 //                     <3=> Pclk = Hclk / 8
197 //     <o10.12..13> PCLK_TIMER2: Peripheral Clock Selection for TIMER2
198 //                     <0=> Pclk = Cclk / 4
199 //                     <1=> Pclk = Cclk
200 //                     <2=> Pclk = Cclk / 2
201 //                     <3=> Pclk = Hclk / 8
202 //     <o10.14..15> PCLK_TIMER3: Peripheral Clock Selection for TIMER3
203 //                     <0=> Pclk = Cclk / 4
204 //                     <1=> Pclk = Cclk
205 //                     <2=> Pclk = Cclk / 2
206 //                     <3=> Pclk = Hclk / 8
207 //     <o10.16..17> PCLK_UART2: Peripheral Clock Selection for UART2
208 //                     <0=> Pclk = Cclk / 4
209 //                     <1=> Pclk = Cclk
210 //                     <2=> Pclk = Cclk / 2
211 //                     <3=> Pclk = Hclk / 8
212 //     <o10.18..19> PCLK_UART3: Peripheral Clock Selection for UART3
213 //                     <0=> Pclk = Cclk / 4
214 //                     <1=> Pclk = Cclk
215 //                     <2=> Pclk = Cclk / 2
216 //                     <3=> Pclk = Hclk / 8
217 //     <o10.20..21> PCLK_I2C2: Peripheral Clock Selection for I2C2
218 //                     <0=> Pclk = Cclk / 4
219 //                     <1=> Pclk = Cclk
220 //                     <2=> Pclk = Cclk / 2
221 //                     <3=> Pclk = Hclk / 8
222 //     <o10.22..23> PCLK_I2S: Peripheral Clock Selection for I2S
223 //                     <0=> Pclk = Cclk / 4
224 //                     <1=> Pclk = Cclk
225 //                     <2=> Pclk = Cclk / 2
226 //                     <3=> Pclk = Hclk / 8
227 //     <o10.26..27> PCLK_RIT: Peripheral Clock Selection for the Repetitive Interrupt Timer
228 //                     <0=> Pclk = Cclk / 4
229 //                     <1=> Pclk = Cclk
230 //                     <2=> Pclk = Cclk / 2
231 //                     <3=> Pclk = Hclk / 8
232 //     <o10.28..29> PCLK_SYSCON: Peripheral Clock Selection for the System Control Block
233 //                     <0=> Pclk = Cclk / 4
234 //                     <1=> Pclk = Cclk
235 //                     <2=> Pclk = Cclk / 2
236 //                     <3=> Pclk = Hclk / 8
237 //     <o10.30..31> PCLK_MC: Peripheral Clock Selection for the Motor Control PWM
238 //                     <0=> Pclk = Cclk / 4
239 //                     <1=> Pclk = Cclk
240 //                     <2=> Pclk = Cclk / 2
241 //                     <3=> Pclk = Hclk / 8
242 //   </h>
243 //
244 //   <h> Power Control for Peripherals Register (PCONP)
245 //     <o11.1>      PCTIM0: Timer/Counter 0 power/clock enable
246 //     <o11.2>      PCTIM1: Timer/Counter 1 power/clock enable
247 //     <o11.3>      PCUART0: UART 0 power/clock enable
248 //     <o11.4>      PCUART1: UART 1 power/clock enable
249 //     <o11.6>      PCPWM1: PWM 1 power/clock enable
250 //     <o11.7>      PCI2C0: I2C interface 0 power/clock enable
251 //     <o11.8>      PCSPI: SPI interface power/clock enable
252 //     <o11.9>      PCRTC: RTC power/clock enable
253 //     <o11.10>     PCSSP1: SSP interface 1 power/clock enable
254 //     <o11.12>     PCAD: A/D converter power/clock enable
255 //     <o11.13>     PCCAN1: CAN controller 1 power/clock enable
256 //     <o11.14>     PCCAN2: CAN controller 2 power/clock enable
257 //     <o11.15>     PCGPIO: GPIOs power/clock enable
258 //     <o11.16>     PCRIT: Repetitive interrupt timer power/clock enable
259 //     <o11.17>     PCMC: Motor control PWM power/clock enable
260 //     <o11.18>     PCQEI: Quadrature encoder interface power/clock enable
261 //     <o11.19>     PCI2C1: I2C interface 1 power/clock enable
262 //     <o11.21>     PCSSP0: SSP interface 0 power/clock enable
263 //     <o11.22>     PCTIM2: Timer 2 power/clock enable
264 //     <o11.23>     PCTIM3: Timer 3 power/clock enable
265 //     <o11.24>     PCUART2: UART 2 power/clock enable
266 //     <o11.25>     PCUART3: UART 3 power/clock enable
267 //     <o11.26>     PCI2C2: I2C interface 2 power/clock enable
268 //     <o11.27>     PCI2S: I2S interface power/clock enable
269 //     <o11.29>     PCGPDMA: GP DMA function power/clock enable
270 //     <o11.30>     PCENET: Ethernet block power/clock enable
271 //     <o11.31>     PCUSB: USB interface power/clock enable
272 //   </h>
273 //
274 //   <h> Clock Output Configuration Register (CLKOUTCFG)
275 //     <o12.0..3>   CLKOUTSEL: Selects clock source for CLKOUT
276 //                     <0=> CPU clock
277 //                     <1=> Main oscillator
278 //                     <2=> Internal RC oscillator
279 //                     <3=> USB clock
280 //                     <4=> RTC oscillator
281 //     <o12.4..7>   CLKOUTDIV: Selects clock divider for CLKOUT
282 //                     <1-16><#-1>
283 //     <o12.8>      CLKOUT_EN: CLKOUT enable control
284 //   </h>
285 //
286 // </e>
287 */
288
289
290
291 /** @addtogroup LPC17xx_System_Defines  LPC17xx System Defines
292   @{
293  */
294
295 #define CLOCK_SETUP           1
296 #define SCS_Val               0x00000020
297 #define CLKSRCSEL_Val         0x00000001
298 #define PLL0_SETUP            1
299
300 #ifdef MCB1700
301 #    define PLL0CFG_Val           0x00050063
302 #    define PLL1_SETUP            1
303 #    define PLL1CFG_Val           0x00000023
304 #    define CCLKCFG_Val           0x00000003
305 #    define USBCLKCFG_Val         0x00000000
306 #else
307 #    define PLL0CFG_Val           0x0000000B
308 #    define PLL1_SETUP            0
309 #    define PLL1CFG_Val           0x00000000
310 #    define CCLKCFG_Val           0x00000002
311 #    define USBCLKCFG_Val         0x00000005
312 #endif
313
314 #define PCLKSEL0_Val          0x00000000
315 #define PCLKSEL1_Val          0x00000000
316 #define PCONP_Val             0x042887DE
317 #define CLKOUTCFG_Val         0x00000000
318
319
320 /*--------------------- Flash Accelerator Configuration ----------------------
321 //
322 // <e> Flash Accelerator Configuration
323 //   <o1.12..15> FLASHTIM: Flash Access Time
324 //               <0=> 1 CPU clock (for CPU clock up to 20 MHz)
325 //               <1=> 2 CPU clocks (for CPU clock up to 40 MHz)
326 //               <2=> 3 CPU clocks (for CPU clock up to 60 MHz)
327 //               <3=> 4 CPU clocks (for CPU clock up to 80 MHz)
328 //               <4=> 5 CPU clocks (for CPU clock up to 100 MHz)
329 //               <5=> 6 CPU clocks (for any CPU clock)
330 // </e>
331 */
332 #define FLASH_SETUP           1
333 #define FLASHCFG_Val          0x0000303A
334
335 /*
336 //-------- <<< end of configuration section >>> ------------------------------
337 */
338
339 /*----------------------------------------------------------------------------
340   Check the register settings
341  *----------------------------------------------------------------------------*/
342 #define CHECK_RANGE(val, min, max)                ((val < min) || (val > max))
343 #define CHECK_RSVD(val, mask)                     (val & mask)
344
345 /* Clock Configuration -------------------------------------------------------*/
346 #if (CHECK_RSVD((SCS_Val),       ~0x00000030))
347    #error "SCS: Invalid values of reserved bits!"
348 #endif
349
350 #if (CHECK_RANGE((CLKSRCSEL_Val), 0, 2))
351    #error "CLKSRCSEL: Value out of range!"
352 #endif
353
354 #if (CHECK_RSVD((PLL0CFG_Val),   ~0x00FF7FFF))
355    #error "PLL0CFG: Invalid values of reserved bits!"
356 #endif
357
358 #if (CHECK_RSVD((PLL1CFG_Val),   ~0x0000007F))
359    #error "PLL1CFG: Invalid values of reserved bits!"
360 #endif
361
362 #if (PLL0_SETUP)            /* if PLL0 is used */
363   #if (CCLKCFG_Val < 2)     /* CCLKSEL must be greater then 1 */
364     #error "CCLKCFG: CCLKSEL must be greater then 1 if PLL0 is used!"
365   #endif
366 #endif
367
368 #if (CHECK_RANGE((CCLKCFG_Val), 2, 255))
369    #error "CCLKCFG: Value out of range!"
370 #endif
371
372 #if (CHECK_RSVD((USBCLKCFG_Val), ~0x0000000F))
373    #error "USBCLKCFG: Invalid values of reserved bits!"
374 #endif
375
376 #if (CHECK_RSVD((PCLKSEL0_Val),   0x000C0C00))
377    #error "PCLKSEL0: Invalid values of reserved bits!"
378 #endif
379
380 #if (CHECK_RSVD((PCLKSEL1_Val),   0x03000300))
381    #error "PCLKSEL1: Invalid values of reserved bits!"
382 #endif
383
384 #if (CHECK_RSVD((PCONP_Val),      0x10100821))
385    #error "PCONP: Invalid values of reserved bits!"
386 #endif
387
388 #if (CHECK_RSVD((CLKOUTCFG_Val), ~0x000001FF))
389    #error "CLKOUTCFG: Invalid values of reserved bits!"
390 #endif
391
392 /* Flash Accelerator Configuration -------------------------------------------*/
393 #if (CHECK_RSVD((FLASHCFG_Val), ~0x0000F07F))
394    #error "FLASHCFG: Invalid values of reserved bits!"
395 #endif
396
397
398 /*----------------------------------------------------------------------------
399   DEFINES
400  *----------------------------------------------------------------------------*/
401
402 /*----------------------------------------------------------------------------
403   Define clocks
404  *----------------------------------------------------------------------------*/
405 #define XTAL        (12000000UL)        /* Oscillator frequency               */
406 #define OSC_CLK     (      XTAL)        /* Main oscillator frequency          */
407 #define RTC_CLK     (   32000UL)        /* RTC oscillator frequency           */
408 #define IRC_OSC     ( 4000000UL)        /* Internal RC oscillator frequency   */
409
410
411 /* F_cco0 = (2 * M * F_in) / N  */
412 #define __M               (((PLL0CFG_Val      ) & 0x7FFF) + 1)
413 #define __N               (((PLL0CFG_Val >> 16) & 0x00FF) + 1)
414 #define __FCCO(__F_IN)    ((2ULL * __M * __F_IN) / __N)
415 #define __CCLK_DIV        (((CCLKCFG_Val      ) & 0x00FF) + 1)
416
417 /* Determine core clock frequency according to settings */
418  #if (PLL0_SETUP)
419     #if   ((CLKSRCSEL_Val & 0x03) == 1)
420         #define __CORE_CLK (__FCCO(OSC_CLK) / __CCLK_DIV)
421     #elif ((CLKSRCSEL_Val & 0x03) == 2)
422         #define __CORE_CLK (__FCCO(RTC_CLK) / __CCLK_DIV)
423     #else
424         #define __CORE_CLK (__FCCO(IRC_OSC) / __CCLK_DIV)
425     #endif
426  #else
427     #if   ((CLKSRCSEL_Val & 0x03) == 1)
428         #define __CORE_CLK (OSC_CLK         / __CCLK_DIV)
429     #elif ((CLKSRCSEL_Val & 0x03) == 2)
430         #define __CORE_CLK (RTC_CLK         / __CCLK_DIV)
431     #else
432         #define __CORE_CLK (IRC_OSC         / __CCLK_DIV)
433     #endif
434  #endif
435
436 /**
437  * @}
438  */
439
440
441 /** @addtogroup LPC17xx_System_Public_Variables  LPC17xx System Public Variables
442   @{
443  */
444 /*----------------------------------------------------------------------------
445   Clock Variable definitions
446  *----------------------------------------------------------------------------*/
447 uint32_t SystemCoreClock = __CORE_CLK;/*!< System Clock Frequency (Core Clock)*/
448
449 /**
450  * @}
451  */
452
453
454 /** @addtogroup LPC17xx_System_Public_Functions  LPC17xx System Public Functions
455   @{
456  */
457
458 /**
459  * Update SystemCoreClock variable
460  *
461  * @param  none
462  * @return none
463  *
464  * @brief  Updates the SystemCoreClock with current core Clock
465  *         retrieved from cpu registers.
466  */void SystemCoreClockUpdate (void)            /* Get Core Clock Frequency      */
467 {
468   /* Determine clock frequency according to clock register values             */
469   if (((LPC_SC->PLL0STAT >> 24) & 3) == 3) { /* If PLL0 enabled and connected */
470     switch (LPC_SC->CLKSRCSEL & 0x03) {
471       case 0:                                /* Int. RC oscillator => PLL0    */
472       case 3:                                /* Reserved, default to Int. RC  */
473         SystemCoreClock = (IRC_OSC *
474                           ((2ULL * ((LPC_SC->PLL0STAT & 0x7FFF) + 1)))  /
475                           (((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1)       /
476                           ((LPC_SC->CCLKCFG & 0xFF)+ 1));
477         break;
478       case 1:                                /* Main oscillator => PLL0       */
479         SystemCoreClock = (OSC_CLK *
480                           ((2ULL * ((LPC_SC->PLL0STAT & 0x7FFF) + 1)))  /
481                           (((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1)       /
482                           ((LPC_SC->CCLKCFG & 0xFF)+ 1));
483         break;
484       case 2:                                /* RTC oscillator => PLL0        */
485         SystemCoreClock = (RTC_CLK *
486                           ((2ULL * ((LPC_SC->PLL0STAT & 0x7FFF) + 1)))  /
487                           (((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1)       /
488                           ((LPC_SC->CCLKCFG & 0xFF)+ 1));
489         break;
490     }
491   } else {
492     switch (LPC_SC->CLKSRCSEL & 0x03) {
493       case 0:                                /* Int. RC oscillator => PLL0    */
494       case 3:                                /* Reserved, default to Int. RC  */
495         SystemCoreClock = IRC_OSC / ((LPC_SC->CCLKCFG & 0xFF)+ 1);
496         break;
497       case 1:                                /* Main oscillator => PLL0       */
498         SystemCoreClock = OSC_CLK / ((LPC_SC->CCLKCFG & 0xFF)+ 1);
499         break;
500       case 2:                                /* RTC oscillator => PLL0        */
501         SystemCoreClock = RTC_CLK / ((LPC_SC->CCLKCFG & 0xFF)+ 1);
502         break;
503     }
504   }
505
506 }
507
508 /**
509  * Initialize the system
510  *
511  * @param  none
512  * @return none
513  *
514  * @brief  Setup the microcontroller system.
515  *         Initialize the System.
516  */
517 void SystemInit (void)
518 {
519 #if (CLOCK_SETUP)                       /* Clock Setup                        */
520   LPC_SC->SCS       = SCS_Val;
521   if (LPC_SC->SCS & (1 << 5)) {             /* If Main Oscillator is enabled  */
522     while ((LPC_SC->SCS & (1<<6)) == 0);/* Wait for Oscillator to be ready    */
523   }
524
525   LPC_SC->CCLKCFG   = CCLKCFG_Val;      /* Setup Clock Divider                */
526   /* Periphral clock must be selected before PLL0 enabling and connecting
527    * - according errata.lpc1768-16.March.2010 -
528    */
529   LPC_SC->PCLKSEL0  = PCLKSEL0_Val;     /* Peripheral Clock Selection         */
530   LPC_SC->PCLKSEL1  = PCLKSEL1_Val;
531
532 #if (PLL0_SETUP)
533   LPC_SC->CLKSRCSEL = CLKSRCSEL_Val;    /* Select Clock Source for PLL0       */
534
535   LPC_SC->PLL0CFG   = PLL0CFG_Val;      /* configure PLL0                     */
536   LPC_SC->PLL0FEED  = 0xAA;
537   LPC_SC->PLL0FEED  = 0x55;
538
539   LPC_SC->PLL0CON   = 0x01;             /* PLL0 Enable                        */
540   LPC_SC->PLL0FEED  = 0xAA;
541   LPC_SC->PLL0FEED  = 0x55;
542   while (!(LPC_SC->PLL0STAT & (1<<26)));/* Wait for PLOCK0                    */
543
544   LPC_SC->PLL0CON   = 0x03;             /* PLL0 Enable & Connect              */
545   LPC_SC->PLL0FEED  = 0xAA;
546   LPC_SC->PLL0FEED  = 0x55;
547   while (!(LPC_SC->PLL0STAT & ((1<<25) | (1<<24))));/* Wait for PLLC0_STAT & PLLE0_STAT */
548 #endif
549
550 #if (PLL1_SETUP)
551   LPC_SC->PLL1CFG   = PLL1CFG_Val;
552   LPC_SC->PLL1FEED  = 0xAA;
553   LPC_SC->PLL1FEED  = 0x55;
554
555   LPC_SC->PLL1CON   = 0x01;             /* PLL1 Enable                        */
556   LPC_SC->PLL1FEED  = 0xAA;
557   LPC_SC->PLL1FEED  = 0x55;
558   while (!(LPC_SC->PLL1STAT & (1<<10)));/* Wait for PLOCK1                    */
559
560   LPC_SC->PLL1CON   = 0x03;             /* PLL1 Enable & Connect              */
561   LPC_SC->PLL1FEED  = 0xAA;
562   LPC_SC->PLL1FEED  = 0x55;
563   while (!(LPC_SC->PLL1STAT & ((1<< 9) | (1<< 8))));/* Wait for PLLC1_STAT & PLLE1_STAT */
564 #else
565   LPC_SC->USBCLKCFG = USBCLKCFG_Val;    /* Setup USB Clock Divider            */
566 #endif
567
568   LPC_SC->PCONP     = PCONP_Val;        /* Power Control for Peripherals      */
569
570   LPC_SC->CLKOUTCFG = CLKOUTCFG_Val;    /* Clock Output Configuration         */
571 #endif
572
573 #if (FLASH_SETUP == 1)                  /* Flash Accelerator Setup            */
574   LPC_SC->FLASHCFG  = (LPC_SC->FLASHCFG & ~0x0000F000) | FLASHCFG_Val;
575 #endif
576 }
577
578 /**
579  * @}
580  */
581
582 /**
583  * @}
584  */