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