]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/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
9 * Copyright (C) 2009-2011 ARM Limited. All rights reserved.
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.
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.
23 ******************************************************************************/
30 /** @addtogroup LPC17xx_System
35 //-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
38 /*--------------------- Clock Configuration ----------------------------------
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
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
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
64 // <o4.16..23> NSEL: PLL Divider Selection
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
77 // <i> M Value (for USB maximum value is 4)
78 // <o6.5..6> PSEL: PLL Divider Selection
87 // <h> CPU Clock Configuration Register (CCLKCFG)
88 // <o7.0..7> CCLKSEL: Divide Value for CPU Clock from PLL0
92 // <h> USB Clock Configuration Register (USBCLKCFG)
93 // <o8.0..3> USBSEL: Divide Value for USB Clock from PLL0
95 // <i> Divide is USBSEL + 1
98 // <h> Peripheral Clock Selection Register 0 (PCLKSEL0)
99 // <o9.0..1> PCLK_WDT: Peripheral Clock Selection for WDT
100 // <0=> Pclk = Cclk / 4
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
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
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
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
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
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
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
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
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
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
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
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
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
167 // <2=> Pclk = Cclk / 2
168 // <3=> Pclk = Hclk / 6
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
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
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
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
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
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
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
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
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
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
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
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
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
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
240 // <2=> Pclk = Cclk / 2
241 // <3=> Pclk = Hclk / 8
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
274 // <h> Clock Output Configuration Register (CLKOUTCFG)
275 // <o12.0..3> CLKOUTSEL: Selects clock source for CLKOUT
277 // <1=> Main oscillator
278 // <2=> Internal RC oscillator
280 // <4=> RTC oscillator
281 // <o12.4..7> CLKOUTDIV: Selects clock divider for CLKOUT
283 // <o12.8> CLKOUT_EN: CLKOUT enable control
291 /** @addtogroup LPC17xx_System_Defines LPC17xx System Defines
295 #define CLOCK_SETUP 1
296 #define SCS_Val 0x00000020
297 #define CLKSRCSEL_Val 0x00000001
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
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
314 #define PCLKSEL0_Val 0x00000000
315 #define PCLKSEL1_Val 0x00000000
316 #define PCONP_Val 0x042887DE
317 #define CLKOUTCFG_Val 0x00000000
320 /*--------------------- Flash Accelerator Configuration ----------------------
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)
332 #define FLASH_SETUP 1
333 #define FLASHCFG_Val 0x0000303A
336 //-------- <<< end of configuration section >>> ------------------------------
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)
345 /* Clock Configuration -------------------------------------------------------*/
346 #if (CHECK_RSVD((SCS_Val), ~0x00000030))
347 #error "SCS: Invalid values of reserved bits!"
350 #if (CHECK_RANGE((CLKSRCSEL_Val), 0, 2))
351 #error "CLKSRCSEL: Value out of range!"
354 #if (CHECK_RSVD((PLL0CFG_Val), ~0x00FF7FFF))
355 #error "PLL0CFG: Invalid values of reserved bits!"
358 #if (CHECK_RSVD((PLL1CFG_Val), ~0x0000007F))
359 #error "PLL1CFG: Invalid values of reserved bits!"
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!"
368 #if (CHECK_RANGE((CCLKCFG_Val), 2, 255))
369 #error "CCLKCFG: Value out of range!"
372 #if (CHECK_RSVD((USBCLKCFG_Val), ~0x0000000F))
373 #error "USBCLKCFG: Invalid values of reserved bits!"
376 #if (CHECK_RSVD((PCLKSEL0_Val), 0x000C0C00))
377 #error "PCLKSEL0: Invalid values of reserved bits!"
380 #if (CHECK_RSVD((PCLKSEL1_Val), 0x03000300))
381 #error "PCLKSEL1: Invalid values of reserved bits!"
384 #if (CHECK_RSVD((PCONP_Val), 0x10100821))
385 #error "PCONP: Invalid values of reserved bits!"
388 #if (CHECK_RSVD((CLKOUTCFG_Val), ~0x000001FF))
389 #error "CLKOUTCFG: Invalid values of reserved bits!"
392 /* Flash Accelerator Configuration -------------------------------------------*/
393 #if (CHECK_RSVD((FLASHCFG_Val), ~0x0000F07F))
394 #error "FLASHCFG: Invalid values of reserved bits!"
398 /*----------------------------------------------------------------------------
400 *----------------------------------------------------------------------------*/
402 /*----------------------------------------------------------------------------
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 */
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)
417 /* Determine core clock frequency according to settings */
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)
424 #define __CORE_CLK (__FCCO(IRC_OSC) / __CCLK_DIV)
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)
432 #define __CORE_CLK (IRC_OSC / __CCLK_DIV)
441 /** @addtogroup LPC17xx_System_Public_Variables LPC17xx System Public Variables
444 /*----------------------------------------------------------------------------
445 Clock Variable definitions
446 *----------------------------------------------------------------------------*/
447 uint32_t SystemCoreClock
= __CORE_CLK
;/*!< System Clock Frequency (Core Clock)*/
454 /** @addtogroup LPC17xx_System_Public_Functions LPC17xx System Public Functions
459 * Update SystemCoreClock variable
464 * @brief Updates the SystemCoreClock with current core Clock
465 * retrieved from cpu registers.
466 */void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
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));
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));
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));
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);
497 case 1: /* Main oscillator => PLL0 */
498 SystemCoreClock
= OSC_CLK
/ ((LPC_SC
->CCLKCFG
& 0xFF)+ 1);
500 case 2: /* RTC oscillator => PLL0 */
501 SystemCoreClock
= RTC_CLK
/ ((LPC_SC
->CCLKCFG
& 0xFF)+ 1);
509 * Initialize the system
514 * @brief Setup the microcontroller system.
515 * Initialize the System.
517 void SystemInit (void)
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 */
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 -
529 LPC_SC
->PCLKSEL0
= PCLKSEL0_Val
; /* Peripheral Clock Selection */
530 LPC_SC
->PCLKSEL1
= PCLKSEL1_Val
;
533 LPC_SC
->CLKSRCSEL
= CLKSRCSEL_Val
; /* Select Clock Source for PLL0 */
535 LPC_SC
->PLL0CFG
= PLL0CFG_Val
; /* configure PLL0 */
536 LPC_SC
->PLL0FEED
= 0xAA;
537 LPC_SC
->PLL0FEED
= 0x55;
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 */
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 */
551 LPC_SC
->PLL1CFG
= PLL1CFG_Val
;
552 LPC_SC
->PLL1FEED
= 0xAA;
553 LPC_SC
->PLL1FEED
= 0x55;
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 */
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 */
565 LPC_SC
->USBCLKCFG
= USBCLKCFG_Val
; /* Setup USB Clock Divider */
568 LPC_SC
->PCONP
= PCONP_Val
; /* Power Control for Peripherals */
570 LPC_SC
->CLKOUTCFG
= CLKOUTCFG_Val
; /* Clock Output Configuration */
573 #if (FLASH_SETUP == 1) /* Flash Accelerator Setup */
574 LPC_SC
->FLASHCFG
= (LPC_SC
->FLASHCFG
& ~0x0000F000) | FLASHCFG_Val
;