mbed library sources

Fork of mbed-src by mbed official

Committer:
lzbpli
Date:
Thu Jul 07 06:48:59 2016 +0000
Revision:
636:b0d178e9fa10
Parent:
458:84b15e04105a
l053

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 10:3bc89ef62ce7 1 /******************************************************************************
emilmont 10:3bc89ef62ce7 2 * @file: system_LPC8xx.c
emilmont 10:3bc89ef62ce7 3 * @purpose: CMSIS Cortex-M0+ Device Peripheral Access Layer Source File
emilmont 10:3bc89ef62ce7 4 * for the NXP LPC8xx Device Series
emilmont 10:3bc89ef62ce7 5 * @version: V1.0
emilmont 10:3bc89ef62ce7 6 * @date: 16. Aug. 2012
emilmont 10:3bc89ef62ce7 7 *----------------------------------------------------------------------------
emilmont 10:3bc89ef62ce7 8 *
emilmont 10:3bc89ef62ce7 9 * Copyright (C) 2012 ARM Limited. All rights reserved.
emilmont 10:3bc89ef62ce7 10 *
emilmont 10:3bc89ef62ce7 11 * ARM Limited (ARM) is supplying this software for use with Cortex-M0+
emilmont 10:3bc89ef62ce7 12 * processor based microcontrollers. This file can be freely distributed
emilmont 10:3bc89ef62ce7 13 * within development tools that are supporting such ARM based processors.
emilmont 10:3bc89ef62ce7 14 *
emilmont 10:3bc89ef62ce7 15 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
emilmont 10:3bc89ef62ce7 16 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
emilmont 10:3bc89ef62ce7 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
emilmont 10:3bc89ef62ce7 18 * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
emilmont 10:3bc89ef62ce7 19 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
emilmont 10:3bc89ef62ce7 20 *
emilmont 10:3bc89ef62ce7 21 ******************************************************************************/
emilmont 10:3bc89ef62ce7 22 #include <stdint.h>
emilmont 10:3bc89ef62ce7 23 #include "LPC8xx.h"
emilmont 10:3bc89ef62ce7 24
emilmont 10:3bc89ef62ce7 25 /*
emilmont 10:3bc89ef62ce7 26 //-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
emilmont 10:3bc89ef62ce7 27 */
emilmont 10:3bc89ef62ce7 28
emilmont 10:3bc89ef62ce7 29 /*--------------------- Clock Configuration ----------------------------------
emilmont 10:3bc89ef62ce7 30 //
emilmont 10:3bc89ef62ce7 31 // <e> Clock Configuration
emilmont 10:3bc89ef62ce7 32 // <h> System Oscillator Control Register (SYSOSCCTRL)
emilmont 10:3bc89ef62ce7 33 // <o1.0> BYPASS: System Oscillator Bypass Enable
emilmont 10:3bc89ef62ce7 34 // <i> If enabled then PLL input (sys_osc_clk) is fed
emilmont 10:3bc89ef62ce7 35 // <i> directly from XTALIN and XTALOUT pins.
emilmont 10:3bc89ef62ce7 36 // <o1.9> FREQRANGE: System Oscillator Frequency Range
emilmont 10:3bc89ef62ce7 37 // <i> Determines frequency range for Low-power oscillator.
emilmont 10:3bc89ef62ce7 38 // <0=> 1 - 20 MHz
emilmont 10:3bc89ef62ce7 39 // <1=> 15 - 25 MHz
emilmont 10:3bc89ef62ce7 40 // </h>
emilmont 10:3bc89ef62ce7 41 //
emilmont 10:3bc89ef62ce7 42 // <h> Watchdog Oscillator Control Register (WDTOSCCTRL)
emilmont 10:3bc89ef62ce7 43 // <o2.0..4> DIVSEL: Select Divider for Fclkana
emilmont 17:151ab7482c89 44 // <i> wdt_osc_clk = Fclkana/ (2 * (1 + DIVSEL))
emilmont 10:3bc89ef62ce7 45 // <0-31>
emilmont 10:3bc89ef62ce7 46 // <o2.5..8> FREQSEL: Select Watchdog Oscillator Analog Output Frequency (Fclkana)
emilmont 10:3bc89ef62ce7 47 // <0=> Undefined
emilmont 10:3bc89ef62ce7 48 // <1=> 0.5 MHz
emilmont 10:3bc89ef62ce7 49 // <2=> 0.8 MHz
emilmont 10:3bc89ef62ce7 50 // <3=> 1.1 MHz
emilmont 10:3bc89ef62ce7 51 // <4=> 1.4 MHz
emilmont 10:3bc89ef62ce7 52 // <5=> 1.6 MHz
emilmont 10:3bc89ef62ce7 53 // <6=> 1.8 MHz
emilmont 10:3bc89ef62ce7 54 // <7=> 2.0 MHz
emilmont 10:3bc89ef62ce7 55 // <8=> 2.2 MHz
emilmont 10:3bc89ef62ce7 56 // <9=> 2.4 MHz
emilmont 10:3bc89ef62ce7 57 // <10=> 2.6 MHz
emilmont 10:3bc89ef62ce7 58 // <11=> 2.7 MHz
emilmont 10:3bc89ef62ce7 59 // <12=> 2.9 MHz
emilmont 10:3bc89ef62ce7 60 // <13=> 3.1 MHz
emilmont 10:3bc89ef62ce7 61 // <14=> 3.2 MHz
emilmont 10:3bc89ef62ce7 62 // <15=> 3.4 MHz
emilmont 10:3bc89ef62ce7 63 // </h>
emilmont 10:3bc89ef62ce7 64 //
emilmont 10:3bc89ef62ce7 65 // <h> System PLL Control Register (SYSPLLCTRL)
emilmont 10:3bc89ef62ce7 66 // <i> F_clkout = M * F_clkin = F_CCO / (2 * P)
emilmont 10:3bc89ef62ce7 67 // <i> F_clkin must be in the range of 10 MHz to 25 MHz
emilmont 10:3bc89ef62ce7 68 // <i> F_CCO must be in the range of 156 MHz to 320 MHz
emilmont 10:3bc89ef62ce7 69 // <o3.0..4> MSEL: Feedback Divider Selection
emilmont 10:3bc89ef62ce7 70 // <i> M = MSEL + 1
emilmont 10:3bc89ef62ce7 71 // <0-31>
emilmont 10:3bc89ef62ce7 72 // <o3.5..6> PSEL: Post Divider Selection
emilmont 10:3bc89ef62ce7 73 // <0=> P = 1
emilmont 10:3bc89ef62ce7 74 // <1=> P = 2
emilmont 10:3bc89ef62ce7 75 // <2=> P = 4
emilmont 10:3bc89ef62ce7 76 // <3=> P = 8
emilmont 10:3bc89ef62ce7 77 // </h>
emilmont 10:3bc89ef62ce7 78 //
emilmont 10:3bc89ef62ce7 79 // <h> System PLL Clock Source Select Register (SYSPLLCLKSEL)
emilmont 10:3bc89ef62ce7 80 // <o4.0..1> SEL: System PLL Clock Source
emilmont 10:3bc89ef62ce7 81 // <0=> IRC Oscillator
emilmont 10:3bc89ef62ce7 82 // <1=> System Oscillator
emilmont 10:3bc89ef62ce7 83 // <2=> Reserved
emilmont 10:3bc89ef62ce7 84 // <3=> CLKIN pin
emilmont 10:3bc89ef62ce7 85 // </h>
emilmont 10:3bc89ef62ce7 86 //
emilmont 10:3bc89ef62ce7 87 // <h> Main Clock Source Select Register (MAINCLKSEL)
emilmont 10:3bc89ef62ce7 88 // <o5.0..1> SEL: Clock Source for Main Clock
emilmont 10:3bc89ef62ce7 89 // <0=> IRC Oscillator
emilmont 10:3bc89ef62ce7 90 // <1=> Input Clock to System PLL
emilmont 10:3bc89ef62ce7 91 // <2=> WDT Oscillator
emilmont 10:3bc89ef62ce7 92 // <3=> System PLL Clock Out
emilmont 10:3bc89ef62ce7 93 // </h>
emilmont 10:3bc89ef62ce7 94 //
emilmont 10:3bc89ef62ce7 95 // <h> System AHB Clock Divider Register (SYSAHBCLKDIV)
emilmont 10:3bc89ef62ce7 96 // <o6.0..7> DIV: System AHB Clock Divider
emilmont 10:3bc89ef62ce7 97 // <i> Divides main clock to provide system clock to core, memories, and peripherals.
emilmont 10:3bc89ef62ce7 98 // <i> 0 = is disabled
emilmont 10:3bc89ef62ce7 99 // <0-255>
emilmont 10:3bc89ef62ce7 100 // </h>
emilmont 10:3bc89ef62ce7 101 // </e>
emilmont 10:3bc89ef62ce7 102 */
mbed_official 132:8b018d7c1d67 103 #define CLOCK_SETUP 1 // 1 == IRC: 2 == System Oscillator 12Mhz Xtal:
mbed_official 132:8b018d7c1d67 104
mbed_official 458:84b15e04105a 105 //Fixed to use PLL
mbed_official 132:8b018d7c1d67 106 #if (CLOCK_SETUP == 1)
mbed_official 458:84b15e04105a 107 //use PLL for IRC
mbed_official 132:8b018d7c1d67 108 #define SYSOSCCTRL_Val 0x00000000 // Reset: 0x000
mbed_official 132:8b018d7c1d67 109 #define WDTOSCCTRL_Val 0x00000000 // Reset: 0x000
mbed_official 458:84b15e04105a 110 #define SYSPLLCTRL_Val 0x00000004 // Reset: 0x000 MSEL=4 => M=5; PSEL=0 => 2P=2; PLLCLKOUT = (12x5) = 60MHz
mbed_official 458:84b15e04105a 111 #define SYSPLLCLKSEL_Val 0x00000000 // Reset: 0x000 Select IRC
mbed_official 458:84b15e04105a 112 #define MAINCLKSEL_Val 0x00000003 // Reset: 0x000 MainClock = PLLCLKOUT
mbed_official 458:84b15e04105a 113 #define SYSAHBCLKDIV_Val 0x00000002 // Reset: 0x001 DIV=2 => SYSTEMCORECLK = 60 / 2 = 30MHz
mbed_official 458:84b15e04105a 114
mbed_official 132:8b018d7c1d67 115 #elif (CLOCK_SETUP == 2)
mbed_official 458:84b15e04105a 116 //use PLL for XTAL
mbed_official 132:8b018d7c1d67 117 #define SYSOSCCTRL_Val 0x00000000 // Reset: 0x000
mbed_official 132:8b018d7c1d67 118 #define WDTOSCCTRL_Val 0x00000000 // Reset: 0x000
mbed_official 458:84b15e04105a 119 #define SYSPLLCTRL_Val 0x00000004 // Reset: 0x000 MSEL=4 => M=5; PSEL=0 => 2P=2; PLLCLKOUT = (12x5) = 60MHz
mbed_official 458:84b15e04105a 120 #define SYSPLLCLKSEL_Val 0x00000001 // Reset: 0x000 Select XTAL
mbed_official 458:84b15e04105a 121 #define MAINCLKSEL_Val 0x00000003 // Reset: 0x000 MainClock = PLLCLKOUT
mbed_official 458:84b15e04105a 122 #define SYSAHBCLKDIV_Val 0x00000002 // Reset: 0x001 DIV=2 => SYSTEMCORECLK = 60 / 2 = 30MHz
mbed_official 132:8b018d7c1d67 123 #endif
emilmont 10:3bc89ef62ce7 124
emilmont 10:3bc89ef62ce7 125 /*
emilmont 10:3bc89ef62ce7 126 //-------- <<< end of configuration section >>> ------------------------------
emilmont 10:3bc89ef62ce7 127 */
emilmont 10:3bc89ef62ce7 128
emilmont 10:3bc89ef62ce7 129 /*----------------------------------------------------------------------------
emilmont 10:3bc89ef62ce7 130 Check the register settings
emilmont 10:3bc89ef62ce7 131 *----------------------------------------------------------------------------*/
emilmont 10:3bc89ef62ce7 132 #define CHECK_RANGE(val, min, max) ((val < min) || (val > max))
emilmont 10:3bc89ef62ce7 133 #define CHECK_RSVD(val, mask) (val & mask)
emilmont 10:3bc89ef62ce7 134
emilmont 10:3bc89ef62ce7 135 /* Clock Configuration -------------------------------------------------------*/
emilmont 10:3bc89ef62ce7 136 #if (CHECK_RSVD((SYSOSCCTRL_Val), ~0x00000003))
emilmont 10:3bc89ef62ce7 137 #error "SYSOSCCTRL: Invalid values of reserved bits!"
emilmont 10:3bc89ef62ce7 138 #endif
emilmont 10:3bc89ef62ce7 139
emilmont 10:3bc89ef62ce7 140 #if (CHECK_RSVD((WDTOSCCTRL_Val), ~0x000001FF))
emilmont 10:3bc89ef62ce7 141 #error "WDTOSCCTRL: Invalid values of reserved bits!"
emilmont 10:3bc89ef62ce7 142 #endif
emilmont 10:3bc89ef62ce7 143
emilmont 10:3bc89ef62ce7 144 #if (CHECK_RANGE((SYSPLLCLKSEL_Val), 0, 3))
emilmont 10:3bc89ef62ce7 145 #error "SYSPLLCLKSEL: Value out of range!"
emilmont 10:3bc89ef62ce7 146 #endif
emilmont 10:3bc89ef62ce7 147
emilmont 10:3bc89ef62ce7 148 #if (CHECK_RSVD((SYSPLLCTRL_Val), ~0x000001FF))
emilmont 10:3bc89ef62ce7 149 #error "SYSPLLCTRL: Invalid values of reserved bits!"
emilmont 10:3bc89ef62ce7 150 #endif
emilmont 10:3bc89ef62ce7 151
emilmont 10:3bc89ef62ce7 152 #if (CHECK_RSVD((MAINCLKSEL_Val), ~0x00000003))
emilmont 10:3bc89ef62ce7 153 #error "MAINCLKSEL: Invalid values of reserved bits!"
emilmont 10:3bc89ef62ce7 154 #endif
emilmont 10:3bc89ef62ce7 155
emilmont 10:3bc89ef62ce7 156 #if (CHECK_RANGE((SYSAHBCLKDIV_Val), 0, 255))
emilmont 10:3bc89ef62ce7 157 #error "SYSAHBCLKDIV: Value out of range!"
emilmont 10:3bc89ef62ce7 158 #endif
emilmont 10:3bc89ef62ce7 159
emilmont 10:3bc89ef62ce7 160
emilmont 10:3bc89ef62ce7 161 /*----------------------------------------------------------------------------
emilmont 10:3bc89ef62ce7 162 DEFINES
emilmont 10:3bc89ef62ce7 163 *----------------------------------------------------------------------------*/
emilmont 10:3bc89ef62ce7 164
emilmont 10:3bc89ef62ce7 165 /*----------------------------------------------------------------------------
emilmont 10:3bc89ef62ce7 166 Define clocks
emilmont 10:3bc89ef62ce7 167 *----------------------------------------------------------------------------*/
emilmont 10:3bc89ef62ce7 168 #define __XTAL (12000000UL) /* Oscillator frequency */
emilmont 10:3bc89ef62ce7 169 #define __SYS_OSC_CLK ( __XTAL) /* Main oscillator frequency */
emilmont 10:3bc89ef62ce7 170 #define __IRC_OSC_CLK (12000000UL) /* Internal RC oscillator frequency */
emilmont 10:3bc89ef62ce7 171 #define __CLKIN_CLK (12000000UL) /* CLKIN pin frequency */
emilmont 10:3bc89ef62ce7 172
emilmont 10:3bc89ef62ce7 173
emilmont 10:3bc89ef62ce7 174 #define __FREQSEL ((WDTOSCCTRL_Val >> 5) & 0x0F)
emilmont 10:3bc89ef62ce7 175 #define __DIVSEL (((WDTOSCCTRL_Val & 0x1F) << 1) + 2)
emilmont 10:3bc89ef62ce7 176
emilmont 10:3bc89ef62ce7 177 #if (CLOCK_SETUP) /* Clock Setup */
emilmont 10:3bc89ef62ce7 178 #if (__FREQSEL == 0)
emilmont 10:3bc89ef62ce7 179 #define __WDT_OSC_CLK ( 0) /* undefined */
emilmont 10:3bc89ef62ce7 180 #elif (__FREQSEL == 1)
emilmont 10:3bc89ef62ce7 181 #define __WDT_OSC_CLK ( 500000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 182 #elif (__FREQSEL == 2)
emilmont 10:3bc89ef62ce7 183 #define __WDT_OSC_CLK ( 800000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 184 #elif (__FREQSEL == 3)
emilmont 10:3bc89ef62ce7 185 #define __WDT_OSC_CLK (1100000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 186 #elif (__FREQSEL == 4)
emilmont 10:3bc89ef62ce7 187 #define __WDT_OSC_CLK (1400000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 188 #elif (__FREQSEL == 5)
emilmont 10:3bc89ef62ce7 189 #define __WDT_OSC_CLK (1600000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 190 #elif (__FREQSEL == 6)
emilmont 10:3bc89ef62ce7 191 #define __WDT_OSC_CLK (1800000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 192 #elif (__FREQSEL == 7)
emilmont 10:3bc89ef62ce7 193 #define __WDT_OSC_CLK (2000000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 194 #elif (__FREQSEL == 8)
emilmont 10:3bc89ef62ce7 195 #define __WDT_OSC_CLK (2200000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 196 #elif (__FREQSEL == 9)
emilmont 10:3bc89ef62ce7 197 #define __WDT_OSC_CLK (2400000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 198 #elif (__FREQSEL == 10)
emilmont 10:3bc89ef62ce7 199 #define __WDT_OSC_CLK (2600000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 200 #elif (__FREQSEL == 11)
emilmont 10:3bc89ef62ce7 201 #define __WDT_OSC_CLK (2700000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 202 #elif (__FREQSEL == 12)
emilmont 10:3bc89ef62ce7 203 #define __WDT_OSC_CLK (2900000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 204 #elif (__FREQSEL == 13)
emilmont 10:3bc89ef62ce7 205 #define __WDT_OSC_CLK (3100000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 206 #elif (__FREQSEL == 14)
emilmont 10:3bc89ef62ce7 207 #define __WDT_OSC_CLK (3200000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 208 #else
emilmont 10:3bc89ef62ce7 209 #define __WDT_OSC_CLK (3400000 / __DIVSEL)
emilmont 10:3bc89ef62ce7 210 #endif
emilmont 10:3bc89ef62ce7 211
emilmont 10:3bc89ef62ce7 212 /* sys_pllclkin calculation */
emilmont 10:3bc89ef62ce7 213 #if ((SYSPLLCLKSEL_Val & 0x03) == 0)
emilmont 10:3bc89ef62ce7 214 #define __SYS_PLLCLKIN (__IRC_OSC_CLK)
emilmont 10:3bc89ef62ce7 215 #elif ((SYSPLLCLKSEL_Val & 0x03) == 1)
emilmont 10:3bc89ef62ce7 216 #define __SYS_PLLCLKIN (__SYS_OSC_CLK)
emilmont 10:3bc89ef62ce7 217 #elif ((SYSPLLCLKSEL_Val & 0x03) == 3)
emilmont 10:3bc89ef62ce7 218 #define __SYS_PLLCLKIN (__CLKIN_CLK)
emilmont 10:3bc89ef62ce7 219 #else
emilmont 10:3bc89ef62ce7 220 #define __SYS_PLLCLKIN (0)
emilmont 10:3bc89ef62ce7 221 #endif
emilmont 10:3bc89ef62ce7 222
emilmont 10:3bc89ef62ce7 223 #define __SYS_PLLCLKOUT (__SYS_PLLCLKIN * ((SYSPLLCTRL_Val & 0x01F) + 1))
emilmont 10:3bc89ef62ce7 224
emilmont 10:3bc89ef62ce7 225 /* main clock calculation */
emilmont 10:3bc89ef62ce7 226 #if ((MAINCLKSEL_Val & 0x03) == 0)
emilmont 10:3bc89ef62ce7 227 #define __MAIN_CLOCK (__IRC_OSC_CLK)
emilmont 10:3bc89ef62ce7 228 #elif ((MAINCLKSEL_Val & 0x03) == 1)
emilmont 10:3bc89ef62ce7 229 #define __MAIN_CLOCK (__SYS_PLLCLKIN)
emilmont 10:3bc89ef62ce7 230 #elif ((MAINCLKSEL_Val & 0x03) == 2)
emilmont 10:3bc89ef62ce7 231 #if (__FREQSEL == 0)
emilmont 10:3bc89ef62ce7 232 #error "MAINCLKSEL: WDT Oscillator selected but FREQSEL is undefined!"
emilmont 10:3bc89ef62ce7 233 #else
emilmont 10:3bc89ef62ce7 234 #define __MAIN_CLOCK (__WDT_OSC_CLK)
emilmont 10:3bc89ef62ce7 235 #endif
emilmont 10:3bc89ef62ce7 236 #elif ((MAINCLKSEL_Val & 0x03) == 3)
emilmont 10:3bc89ef62ce7 237 #define __MAIN_CLOCK (__SYS_PLLCLKOUT)
emilmont 10:3bc89ef62ce7 238 #else
emilmont 10:3bc89ef62ce7 239 #define __MAIN_CLOCK (0)
emilmont 10:3bc89ef62ce7 240 #endif
emilmont 10:3bc89ef62ce7 241
emilmont 10:3bc89ef62ce7 242 #define __SYSTEM_CLOCK (__MAIN_CLOCK / SYSAHBCLKDIV_Val)
emilmont 10:3bc89ef62ce7 243
emilmont 10:3bc89ef62ce7 244 #else
emilmont 10:3bc89ef62ce7 245 #define __SYSTEM_CLOCK (__IRC_OSC_CLK)
emilmont 10:3bc89ef62ce7 246 #endif // CLOCK_SETUP
emilmont 10:3bc89ef62ce7 247
emilmont 10:3bc89ef62ce7 248
emilmont 10:3bc89ef62ce7 249 /*----------------------------------------------------------------------------
emilmont 10:3bc89ef62ce7 250 Clock Variable definitions
emilmont 10:3bc89ef62ce7 251 *----------------------------------------------------------------------------*/
mbed_official 458:84b15e04105a 252 uint32_t MainClock = __MAIN_CLOCK; /*!< Main Clock Frequency */
emilmont 10:3bc89ef62ce7 253 uint32_t SystemCoreClock = __SYSTEM_CLOCK;/*!< System Clock Frequency (Core Clock)*/
emilmont 10:3bc89ef62ce7 254
mbed_official 458:84b15e04105a 255 //Replaced SystemCoreClock with MainClock
emilmont 10:3bc89ef62ce7 256 /*----------------------------------------------------------------------------
emilmont 10:3bc89ef62ce7 257 Clock functions
emilmont 10:3bc89ef62ce7 258 *----------------------------------------------------------------------------*/
emilmont 10:3bc89ef62ce7 259 void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
emilmont 10:3bc89ef62ce7 260 {
emilmont 10:3bc89ef62ce7 261 uint32_t wdt_osc = 0;
emilmont 10:3bc89ef62ce7 262
emilmont 10:3bc89ef62ce7 263 /* Determine clock frequency according to clock register values */
emilmont 10:3bc89ef62ce7 264 switch ((LPC_SYSCON->WDTOSCCTRL >> 5) & 0x0F) {
emilmont 10:3bc89ef62ce7 265 case 0: wdt_osc = 0; break;
emilmont 10:3bc89ef62ce7 266 case 1: wdt_osc = 500000; break;
emilmont 10:3bc89ef62ce7 267 case 2: wdt_osc = 800000; break;
emilmont 10:3bc89ef62ce7 268 case 3: wdt_osc = 1100000; break;
emilmont 10:3bc89ef62ce7 269 case 4: wdt_osc = 1400000; break;
emilmont 10:3bc89ef62ce7 270 case 5: wdt_osc = 1600000; break;
emilmont 10:3bc89ef62ce7 271 case 6: wdt_osc = 1800000; break;
emilmont 10:3bc89ef62ce7 272 case 7: wdt_osc = 2000000; break;
emilmont 10:3bc89ef62ce7 273 case 8: wdt_osc = 2200000; break;
emilmont 10:3bc89ef62ce7 274 case 9: wdt_osc = 2400000; break;
emilmont 10:3bc89ef62ce7 275 case 10: wdt_osc = 2600000; break;
emilmont 10:3bc89ef62ce7 276 case 11: wdt_osc = 2700000; break;
emilmont 10:3bc89ef62ce7 277 case 12: wdt_osc = 2900000; break;
emilmont 10:3bc89ef62ce7 278 case 13: wdt_osc = 3100000; break;
emilmont 10:3bc89ef62ce7 279 case 14: wdt_osc = 3200000; break;
emilmont 10:3bc89ef62ce7 280 case 15: wdt_osc = 3400000; break;
emilmont 10:3bc89ef62ce7 281 }
emilmont 10:3bc89ef62ce7 282 wdt_osc /= ((LPC_SYSCON->WDTOSCCTRL & 0x1F) << 1) + 2;
emilmont 10:3bc89ef62ce7 283
emilmont 10:3bc89ef62ce7 284 switch (LPC_SYSCON->MAINCLKSEL & 0x03) {
emilmont 10:3bc89ef62ce7 285 case 0: /* Internal RC oscillator */
mbed_official 458:84b15e04105a 286 MainClock = __IRC_OSC_CLK;
emilmont 10:3bc89ef62ce7 287 break;
emilmont 10:3bc89ef62ce7 288 case 1: /* Input Clock to System PLL */
emilmont 10:3bc89ef62ce7 289 switch (LPC_SYSCON->SYSPLLCLKSEL & 0x03) {
emilmont 10:3bc89ef62ce7 290 case 0: /* Internal RC oscillator */
mbed_official 458:84b15e04105a 291 MainClock = __IRC_OSC_CLK;
emilmont 10:3bc89ef62ce7 292 break;
emilmont 10:3bc89ef62ce7 293 case 1: /* System oscillator */
mbed_official 458:84b15e04105a 294 MainClock = __SYS_OSC_CLK;
emilmont 10:3bc89ef62ce7 295 break;
emilmont 10:3bc89ef62ce7 296 case 2: /* Reserved */
mbed_official 458:84b15e04105a 297 MainClock = 0;
emilmont 10:3bc89ef62ce7 298 break;
emilmont 10:3bc89ef62ce7 299 case 3: /* CLKIN pin */
mbed_official 458:84b15e04105a 300 MainClock = __CLKIN_CLK;
emilmont 10:3bc89ef62ce7 301 break;
emilmont 10:3bc89ef62ce7 302 }
emilmont 10:3bc89ef62ce7 303 break;
emilmont 10:3bc89ef62ce7 304 case 2: /* WDT Oscillator */
mbed_official 458:84b15e04105a 305 MainClock = wdt_osc;
emilmont 10:3bc89ef62ce7 306 break;
emilmont 10:3bc89ef62ce7 307 case 3: /* System PLL Clock Out */
emilmont 10:3bc89ef62ce7 308 switch (LPC_SYSCON->SYSPLLCLKSEL & 0x03) {
emilmont 10:3bc89ef62ce7 309 case 0: /* Internal RC oscillator */
mbed_official 458:84b15e04105a 310 MainClock = __IRC_OSC_CLK * ((LPC_SYSCON->SYSPLLCTRL & 0x01F) + 1);
emilmont 10:3bc89ef62ce7 311 break;
emilmont 10:3bc89ef62ce7 312 case 1: /* System oscillator */
mbed_official 458:84b15e04105a 313 MainClock = __SYS_OSC_CLK * ((LPC_SYSCON->SYSPLLCTRL & 0x01F) + 1);
emilmont 10:3bc89ef62ce7 314 break;
emilmont 10:3bc89ef62ce7 315 case 2: /* Reserved */
mbed_official 458:84b15e04105a 316 MainClock = 0;
emilmont 10:3bc89ef62ce7 317 break;
emilmont 10:3bc89ef62ce7 318 case 3: /* CLKIN pin */
mbed_official 458:84b15e04105a 319 MainClock = __CLKIN_CLK * ((LPC_SYSCON->SYSPLLCTRL & 0x01F) + 1);
emilmont 10:3bc89ef62ce7 320 break;
emilmont 10:3bc89ef62ce7 321 }
mbed_official 458:84b15e04105a 322 break;
emilmont 10:3bc89ef62ce7 323 }
emilmont 10:3bc89ef62ce7 324
mbed_official 458:84b15e04105a 325 SystemCoreClock = MainClock / LPC_SYSCON->SYSAHBCLKDIV;
emilmont 10:3bc89ef62ce7 326 }
emilmont 10:3bc89ef62ce7 327
emilmont 10:3bc89ef62ce7 328 /**
emilmont 10:3bc89ef62ce7 329 * Initialize the system
emilmont 10:3bc89ef62ce7 330 *
emilmont 10:3bc89ef62ce7 331 * @param none
emilmont 10:3bc89ef62ce7 332 * @return none
emilmont 10:3bc89ef62ce7 333 *
emilmont 10:3bc89ef62ce7 334 * @brief Setup the microcontroller system.
emilmont 10:3bc89ef62ce7 335 * Initialize the System.
emilmont 10:3bc89ef62ce7 336 */
emilmont 10:3bc89ef62ce7 337 void SystemInit (void) {
emilmont 10:3bc89ef62ce7 338 volatile uint32_t i;
emilmont 10:3bc89ef62ce7 339
emilmont 10:3bc89ef62ce7 340 /* System clock to the IOCON & the SWM need to be enabled or
emilmont 10:3bc89ef62ce7 341 most of the I/O related peripherals won't work. */
emilmont 10:3bc89ef62ce7 342 LPC_SYSCON->SYSAHBCLKCTRL |= ( (0x1 << 7) | (0x1 << 18) );
emilmont 10:3bc89ef62ce7 343
emilmont 10:3bc89ef62ce7 344 #if (CLOCK_SETUP) /* Clock Setup */
emilmont 10:3bc89ef62ce7 345
emilmont 10:3bc89ef62ce7 346 #if ((SYSPLLCLKSEL_Val & 0x03) == 1)
emilmont 10:3bc89ef62ce7 347 LPC_IOCON->PIO0_8 &= ~(0x3 << 3);
emilmont 10:3bc89ef62ce7 348 LPC_IOCON->PIO0_9 &= ~(0x3 << 3);
emilmont 10:3bc89ef62ce7 349 LPC_SWM->PINENABLE0 &= ~(0x3 << 4);
emilmont 10:3bc89ef62ce7 350 LPC_SYSCON->PDRUNCFG &= ~(0x1 << 5); /* Power-up System Osc */
emilmont 10:3bc89ef62ce7 351 LPC_SYSCON->SYSOSCCTRL = SYSOSCCTRL_Val;
emilmont 10:3bc89ef62ce7 352 for (i = 0; i < 200; i++) __NOP();
emilmont 10:3bc89ef62ce7 353 #endif
emilmont 10:3bc89ef62ce7 354 #if ((SYSPLLCLKSEL_Val & 0x03) == 3)
emilmont 10:3bc89ef62ce7 355 LPC_IOCON->PIO0_1 &= ~(0x3 << 3);
emilmont 10:3bc89ef62ce7 356 LPC_SWM->PINENABLE0 &= ~(0x1 << 7);
emilmont 10:3bc89ef62ce7 357 for (i = 0; i < 200; i++) __NOP();
emilmont 10:3bc89ef62ce7 358 #endif
emilmont 10:3bc89ef62ce7 359
emilmont 10:3bc89ef62ce7 360 LPC_SYSCON->SYSPLLCLKSEL = SYSPLLCLKSEL_Val; /* Select PLL Input */
emilmont 10:3bc89ef62ce7 361 LPC_SYSCON->SYSPLLCLKUEN = 0x01; /* Update Clock Source */
emilmont 10:3bc89ef62ce7 362 while (!(LPC_SYSCON->SYSPLLCLKUEN & 0x01)); /* Wait Until Updated */
emilmont 10:3bc89ef62ce7 363 #if ((MAINCLKSEL_Val & 0x03) == 3) /* Main Clock is PLL Out */
emilmont 10:3bc89ef62ce7 364 LPC_SYSCON->SYSPLLCTRL = SYSPLLCTRL_Val;
emilmont 10:3bc89ef62ce7 365 LPC_SYSCON->PDRUNCFG &= ~(0x1 << 7); /* Power-up SYSPLL */
emilmont 10:3bc89ef62ce7 366 while (!(LPC_SYSCON->SYSPLLSTAT & 0x01)); /* Wait Until PLL Locked */
emilmont 10:3bc89ef62ce7 367 #endif
emilmont 10:3bc89ef62ce7 368
emilmont 10:3bc89ef62ce7 369 #if (((MAINCLKSEL_Val & 0x03) == 2) )
emilmont 10:3bc89ef62ce7 370 LPC_SYSCON->WDTOSCCTRL = WDTOSCCTRL_Val;
emilmont 10:3bc89ef62ce7 371 LPC_SYSCON->PDRUNCFG &= ~(0x1 << 6); /* Power-up WDT Clock */
emilmont 10:3bc89ef62ce7 372 for (i = 0; i < 200; i++) __NOP();
emilmont 10:3bc89ef62ce7 373 #endif
emilmont 10:3bc89ef62ce7 374
emilmont 10:3bc89ef62ce7 375 LPC_SYSCON->MAINCLKSEL = MAINCLKSEL_Val; /* Select PLL Clock Output */
emilmont 10:3bc89ef62ce7 376 LPC_SYSCON->MAINCLKUEN = 0x01; /* Update MCLK Clock Source */
emilmont 10:3bc89ef62ce7 377 while (!(LPC_SYSCON->MAINCLKUEN & 0x01)); /* Wait Until Updated */
emilmont 10:3bc89ef62ce7 378
emilmont 10:3bc89ef62ce7 379 LPC_SYSCON->SYSAHBCLKDIV = SYSAHBCLKDIV_Val;
emilmont 10:3bc89ef62ce7 380 #endif
emilmont 10:3bc89ef62ce7 381 }