]>
git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_NXP/TARGET_LPC81X/TARGET_LPC812/system_LPC8xx.c
1 /******************************************************************************
2 * @file: system_LPC8xx.c
3 * @purpose: CMSIS Cortex-M0+ Device Peripheral Access Layer Source File
4 * for the NXP LPC8xx Device Series
7 *----------------------------------------------------------------------------
9 * Copyright (C) 2012 ARM Limited. All rights reserved.
11 * ARM Limited (ARM) is supplying this software for use with Cortex-M0+
12 * processor based microcontrollers. This file can be freely distributed
13 * within development tools that are supporting such ARM based processors.
15 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
16 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
18 * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
19 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
21 ******************************************************************************/
26 //-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
29 /*--------------------- Clock Configuration ----------------------------------
31 // <e> Clock Configuration
32 // <h> System Oscillator Control Register (SYSOSCCTRL)
33 // <o1.0> BYPASS: System Oscillator Bypass Enable
34 // <i> If enabled then PLL input (sys_osc_clk) is fed
35 // <i> directly from XTALIN and XTALOUT pins.
36 // <o1.9> FREQRANGE: System Oscillator Frequency Range
37 // <i> Determines frequency range for Low-power oscillator.
42 // <h> Watchdog Oscillator Control Register (WDTOSCCTRL)
43 // <o2.0..4> DIVSEL: Select Divider for Fclkana
44 // <i> wdt_osc_clk = Fclkana/ (2 * (1 + DIVSEL))
46 // <o2.5..8> FREQSEL: Select Watchdog Oscillator Analog Output Frequency (Fclkana)
65 // <h> System PLL Control Register (SYSPLLCTRL)
66 // <i> F_clkout = M * F_clkin = F_CCO / (2 * P)
67 // <i> F_clkin must be in the range of 10 MHz to 25 MHz
68 // <i> F_CCO must be in the range of 156 MHz to 320 MHz
69 // <o3.0..4> MSEL: Feedback Divider Selection
72 // <o3.5..6> PSEL: Post Divider Selection
79 // <h> System PLL Clock Source Select Register (SYSPLLCLKSEL)
80 // <o4.0..1> SEL: System PLL Clock Source
81 // <0=> IRC Oscillator
82 // <1=> System Oscillator
87 // <h> Main Clock Source Select Register (MAINCLKSEL)
88 // <o5.0..1> SEL: Clock Source for Main Clock
89 // <0=> IRC Oscillator
90 // <1=> Input Clock to System PLL
91 // <2=> WDT Oscillator
92 // <3=> System PLL Clock Out
95 // <h> System AHB Clock Divider Register (SYSAHBCLKDIV)
96 // <o6.0..7> DIV: System AHB Clock Divider
97 // <i> Divides main clock to provide system clock to core, memories, and peripherals.
98 // <i> 0 = is disabled
103 #define CLOCK_SETUP 1 // 1 == IRC: 2 == System Oscillator 12Mhz Xtal:
106 #if (CLOCK_SETUP == 1)
108 #define SYSOSCCTRL_Val 0x00000000 // Reset: 0x000
109 #define WDTOSCCTRL_Val 0x00000000 // Reset: 0x000
110 #define SYSPLLCTRL_Val 0x00000004 // Reset: 0x000 MSEL=4 => M=5; PSEL=0 => 2P=2; PLLCLKOUT = (12x5) = 60MHz
111 #define SYSPLLCLKSEL_Val 0x00000000 // Reset: 0x000 Select IRC
112 #define MAINCLKSEL_Val 0x00000003 // Reset: 0x000 MainClock = PLLCLKOUT
113 #define SYSAHBCLKDIV_Val 0x00000002 // Reset: 0x001 DIV=2 => SYSTEMCORECLK = 60 / 2 = 30MHz
115 #elif (CLOCK_SETUP == 2)
117 #define SYSOSCCTRL_Val 0x00000000 // Reset: 0x000
118 #define WDTOSCCTRL_Val 0x00000000 // Reset: 0x000
119 #define SYSPLLCTRL_Val 0x00000004 // Reset: 0x000 MSEL=4 => M=5; PSEL=0 => 2P=2; PLLCLKOUT = (12x5) = 60MHz
120 #define SYSPLLCLKSEL_Val 0x00000001 // Reset: 0x000 Select XTAL
121 #define MAINCLKSEL_Val 0x00000003 // Reset: 0x000 MainClock = PLLCLKOUT
122 #define SYSAHBCLKDIV_Val 0x00000002 // Reset: 0x001 DIV=2 => SYSTEMCORECLK = 60 / 2 = 30MHz
126 //-------- <<< end of configuration section >>> ------------------------------
129 /*----------------------------------------------------------------------------
130 Check the register settings
131 *----------------------------------------------------------------------------*/
132 #define CHECK_RANGE(val, min, max) ((val < min) || (val > max))
133 #define CHECK_RSVD(val, mask) (val & mask)
135 /* Clock Configuration -------------------------------------------------------*/
136 #if (CHECK_RSVD((SYSOSCCTRL_Val), ~0x00000003))
137 #error "SYSOSCCTRL: Invalid values of reserved bits!"
140 #if (CHECK_RSVD((WDTOSCCTRL_Val), ~0x000001FF))
141 #error "WDTOSCCTRL: Invalid values of reserved bits!"
144 #if (CHECK_RANGE((SYSPLLCLKSEL_Val), 0, 3))
145 #error "SYSPLLCLKSEL: Value out of range!"
148 #if (CHECK_RSVD((SYSPLLCTRL_Val), ~0x000001FF))
149 #error "SYSPLLCTRL: Invalid values of reserved bits!"
152 #if (CHECK_RSVD((MAINCLKSEL_Val), ~0x00000003))
153 #error "MAINCLKSEL: Invalid values of reserved bits!"
156 #if (CHECK_RANGE((SYSAHBCLKDIV_Val), 0, 255))
157 #error "SYSAHBCLKDIV: Value out of range!"
161 /*----------------------------------------------------------------------------
163 *----------------------------------------------------------------------------*/
165 /*----------------------------------------------------------------------------
167 *----------------------------------------------------------------------------*/
168 #define __XTAL (12000000UL) /* Oscillator frequency */
169 #define __SYS_OSC_CLK ( __XTAL) /* Main oscillator frequency */
170 #define __IRC_OSC_CLK (12000000UL) /* Internal RC oscillator frequency */
171 #define __CLKIN_CLK (12000000UL) /* CLKIN pin frequency */
174 #define __FREQSEL ((WDTOSCCTRL_Val >> 5) & 0x0F)
175 #define __DIVSEL (((WDTOSCCTRL_Val & 0x1F) << 1) + 2)
177 #if (CLOCK_SETUP) /* Clock Setup */
179 #define __WDT_OSC_CLK ( 0) /* undefined */
180 #elif (__FREQSEL == 1)
181 #define __WDT_OSC_CLK ( 500000 / __DIVSEL)
182 #elif (__FREQSEL == 2)
183 #define __WDT_OSC_CLK ( 800000 / __DIVSEL)
184 #elif (__FREQSEL == 3)
185 #define __WDT_OSC_CLK (1100000 / __DIVSEL)
186 #elif (__FREQSEL == 4)
187 #define __WDT_OSC_CLK (1400000 / __DIVSEL)
188 #elif (__FREQSEL == 5)
189 #define __WDT_OSC_CLK (1600000 / __DIVSEL)
190 #elif (__FREQSEL == 6)
191 #define __WDT_OSC_CLK (1800000 / __DIVSEL)
192 #elif (__FREQSEL == 7)
193 #define __WDT_OSC_CLK (2000000 / __DIVSEL)
194 #elif (__FREQSEL == 8)
195 #define __WDT_OSC_CLK (2200000 / __DIVSEL)
196 #elif (__FREQSEL == 9)
197 #define __WDT_OSC_CLK (2400000 / __DIVSEL)
198 #elif (__FREQSEL == 10)
199 #define __WDT_OSC_CLK (2600000 / __DIVSEL)
200 #elif (__FREQSEL == 11)
201 #define __WDT_OSC_CLK (2700000 / __DIVSEL)
202 #elif (__FREQSEL == 12)
203 #define __WDT_OSC_CLK (2900000 / __DIVSEL)
204 #elif (__FREQSEL == 13)
205 #define __WDT_OSC_CLK (3100000 / __DIVSEL)
206 #elif (__FREQSEL == 14)
207 #define __WDT_OSC_CLK (3200000 / __DIVSEL)
209 #define __WDT_OSC_CLK (3400000 / __DIVSEL)
212 /* sys_pllclkin calculation */
213 #if ((SYSPLLCLKSEL_Val & 0x03) == 0)
214 #define __SYS_PLLCLKIN (__IRC_OSC_CLK)
215 #elif ((SYSPLLCLKSEL_Val & 0x03) == 1)
216 #define __SYS_PLLCLKIN (__SYS_OSC_CLK)
217 #elif ((SYSPLLCLKSEL_Val & 0x03) == 3)
218 #define __SYS_PLLCLKIN (__CLKIN_CLK)
220 #define __SYS_PLLCLKIN (0)
223 #define __SYS_PLLCLKOUT (__SYS_PLLCLKIN * ((SYSPLLCTRL_Val & 0x01F) + 1))
225 /* main clock calculation */
226 #if ((MAINCLKSEL_Val & 0x03) == 0)
227 #define __MAIN_CLOCK (__IRC_OSC_CLK)
228 #elif ((MAINCLKSEL_Val & 0x03) == 1)
229 #define __MAIN_CLOCK (__SYS_PLLCLKIN)
230 #elif ((MAINCLKSEL_Val & 0x03) == 2)
232 #error "MAINCLKSEL: WDT Oscillator selected but FREQSEL is undefined!"
234 #define __MAIN_CLOCK (__WDT_OSC_CLK)
236 #elif ((MAINCLKSEL_Val & 0x03) == 3)
237 #define __MAIN_CLOCK (__SYS_PLLCLKOUT)
239 #define __MAIN_CLOCK (0)
242 #define __SYSTEM_CLOCK (__MAIN_CLOCK / SYSAHBCLKDIV_Val)
245 #define __SYSTEM_CLOCK (__IRC_OSC_CLK)
246 #endif // CLOCK_SETUP
249 /*----------------------------------------------------------------------------
250 Clock Variable definitions
251 *----------------------------------------------------------------------------*/
252 uint32_t MainClock
= __MAIN_CLOCK
; /*!< Main Clock Frequency */
253 uint32_t SystemCoreClock
= __SYSTEM_CLOCK
;/*!< System Clock Frequency (Core Clock)*/
255 //Replaced SystemCoreClock with MainClock
256 /*----------------------------------------------------------------------------
258 *----------------------------------------------------------------------------*/
259 void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
261 uint32_t wdt_osc
= 0;
263 /* Determine clock frequency according to clock register values */
264 switch ((LPC_SYSCON
->WDTOSCCTRL
>> 5) & 0x0F) {
265 case 0: wdt_osc
= 0; break;
266 case 1: wdt_osc
= 500000; break;
267 case 2: wdt_osc
= 800000; break;
268 case 3: wdt_osc
= 1100000; break;
269 case 4: wdt_osc
= 1400000; break;
270 case 5: wdt_osc
= 1600000; break;
271 case 6: wdt_osc
= 1800000; break;
272 case 7: wdt_osc
= 2000000; break;
273 case 8: wdt_osc
= 2200000; break;
274 case 9: wdt_osc
= 2400000; break;
275 case 10: wdt_osc
= 2600000; break;
276 case 11: wdt_osc
= 2700000; break;
277 case 12: wdt_osc
= 2900000; break;
278 case 13: wdt_osc
= 3100000; break;
279 case 14: wdt_osc
= 3200000; break;
280 case 15: wdt_osc
= 3400000; break;
282 wdt_osc
/= ((LPC_SYSCON
->WDTOSCCTRL
& 0x1F) << 1) + 2;
284 switch (LPC_SYSCON
->MAINCLKSEL
& 0x03) {
285 case 0: /* Internal RC oscillator */
286 MainClock
= __IRC_OSC_CLK
;
288 case 1: /* Input Clock to System PLL */
289 switch (LPC_SYSCON
->SYSPLLCLKSEL
& 0x03) {
290 case 0: /* Internal RC oscillator */
291 MainClock
= __IRC_OSC_CLK
;
293 case 1: /* System oscillator */
294 MainClock
= __SYS_OSC_CLK
;
296 case 2: /* Reserved */
299 case 3: /* CLKIN pin */
300 MainClock
= __CLKIN_CLK
;
304 case 2: /* WDT Oscillator */
307 case 3: /* System PLL Clock Out */
308 switch (LPC_SYSCON
->SYSPLLCLKSEL
& 0x03) {
309 case 0: /* Internal RC oscillator */
310 MainClock
= __IRC_OSC_CLK
* ((LPC_SYSCON
->SYSPLLCTRL
& 0x01F) + 1);
312 case 1: /* System oscillator */
313 MainClock
= __SYS_OSC_CLK
* ((LPC_SYSCON
->SYSPLLCTRL
& 0x01F) + 1);
315 case 2: /* Reserved */
318 case 3: /* CLKIN pin */
319 MainClock
= __CLKIN_CLK
* ((LPC_SYSCON
->SYSPLLCTRL
& 0x01F) + 1);
325 SystemCoreClock
= MainClock
/ LPC_SYSCON
->SYSAHBCLKDIV
;
329 * Initialize the system
334 * @brief Setup the microcontroller system.
335 * Initialize the System.
337 void SystemInit (void) {
340 /* System clock to the IOCON & the SWM need to be enabled or
341 most of the I/O related peripherals won't work. */
342 LPC_SYSCON
->SYSAHBCLKCTRL
|= ( (0x1 << 7) | (0x1 << 18) );
344 #if (CLOCK_SETUP) /* Clock Setup */
346 #if ((SYSPLLCLKSEL_Val & 0x03) == 1)
347 LPC_IOCON
->PIO0_8
&= ~(0x3 << 3);
348 LPC_IOCON
->PIO0_9
&= ~(0x3 << 3);
349 LPC_SWM
->PINENABLE0
&= ~(0x3 << 4);
350 LPC_SYSCON
->PDRUNCFG
&= ~(0x1 << 5); /* Power-up System Osc */
351 LPC_SYSCON
->SYSOSCCTRL
= SYSOSCCTRL_Val
;
352 for (i
= 0; i
< 200; i
++) __NOP();
354 #if ((SYSPLLCLKSEL_Val & 0x03) == 3)
355 LPC_IOCON
->PIO0_1
&= ~(0x3 << 3);
356 LPC_SWM
->PINENABLE0
&= ~(0x1 << 7);
357 for (i
= 0; i
< 200; i
++) __NOP();
360 LPC_SYSCON
->SYSPLLCLKSEL
= SYSPLLCLKSEL_Val
; /* Select PLL Input */
361 LPC_SYSCON
->SYSPLLCLKUEN
= 0x01; /* Update Clock Source */
362 while (!(LPC_SYSCON
->SYSPLLCLKUEN
& 0x01)); /* Wait Until Updated */
363 #if ((MAINCLKSEL_Val & 0x03) == 3) /* Main Clock is PLL Out */
364 LPC_SYSCON
->SYSPLLCTRL
= SYSPLLCTRL_Val
;
365 LPC_SYSCON
->PDRUNCFG
&= ~(0x1 << 7); /* Power-up SYSPLL */
366 while (!(LPC_SYSCON
->SYSPLLSTAT
& 0x01)); /* Wait Until PLL Locked */
369 #if (((MAINCLKSEL_Val & 0x03) == 2) )
370 LPC_SYSCON
->WDTOSCCTRL
= WDTOSCCTRL_Val
;
371 LPC_SYSCON
->PDRUNCFG
&= ~(0x1 << 6); /* Power-up WDT Clock */
372 for (i
= 0; i
< 200; i
++) __NOP();
375 LPC_SYSCON
->MAINCLKSEL
= MAINCLKSEL_Val
; /* Select PLL Clock Output */
376 LPC_SYSCON
->MAINCLKUEN
= 0x01; /* Update MCLK Clock Source */
377 while (!(LPC_SYSCON
->MAINCLKUEN
& 0x01)); /* Wait Until Updated */
379 LPC_SYSCON
->SYSAHBCLKDIV
= SYSAHBCLKDIV_Val
;