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:
15:4892fe388435
l053

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 15:4892fe388435 1 /**********************************************************************
bogdanm 15:4892fe388435 2 * $Id$ system_LPC407x_8x_177x_8x.c 2012-01-16
bogdanm 15:4892fe388435 3 *//**
bogdanm 15:4892fe388435 4 * @file system_LPC407x_8x_177x_8x.c
bogdanm 15:4892fe388435 5 * @brief CMSIS Cortex-M3, M4 Device Peripheral Access Layer Source File
bogdanm 15:4892fe388435 6 * for the NXP LPC407x_8x_177x_8x Device Series
bogdanm 15:4892fe388435 7 *
bogdanm 15:4892fe388435 8 * ARM Limited (ARM) is supplying this software for use with
bogdanm 15:4892fe388435 9 * Cortex-M processor based microcontrollers. This file can be
bogdanm 15:4892fe388435 10 * freely distributed within development tools that are supporting
bogdanm 15:4892fe388435 11 * such ARM based processors.
bogdanm 15:4892fe388435 12 *
bogdanm 15:4892fe388435 13 * @version 1.2
bogdanm 15:4892fe388435 14 * @date 20. June. 2012
bogdanm 15:4892fe388435 15 * @author NXP MCU SW Application Team
bogdanm 15:4892fe388435 16 *
bogdanm 15:4892fe388435 17 * Copyright(C) 2012, NXP Semiconductor
bogdanm 15:4892fe388435 18 * All rights reserved.
bogdanm 15:4892fe388435 19 *
bogdanm 15:4892fe388435 20 ***********************************************************************
bogdanm 15:4892fe388435 21 * Software that is described herein is for illustrative purposes only
bogdanm 15:4892fe388435 22 * which provides customers with programming information regarding the
bogdanm 15:4892fe388435 23 * products. This software is supplied "AS IS" without any warranties.
bogdanm 15:4892fe388435 24 * NXP Semiconductors assumes no responsibility or liability for the
bogdanm 15:4892fe388435 25 * use of the software, conveys no license or title under any patent,
bogdanm 15:4892fe388435 26 * copyright, or mask work right to the product. NXP Semiconductors
bogdanm 15:4892fe388435 27 * reserves the right to make changes in the software without
bogdanm 15:4892fe388435 28 * notification. NXP Semiconductors also make no representation or
bogdanm 15:4892fe388435 29 * warranty that such application will be suitable for the specified
bogdanm 15:4892fe388435 30 * use without further testing or modification.
bogdanm 15:4892fe388435 31 **********************************************************************/
bogdanm 15:4892fe388435 32
bogdanm 15:4892fe388435 33 #include <stdint.h>
bogdanm 15:4892fe388435 34 #include "LPC407x_8x_177x_8x.h"
bogdanm 15:4892fe388435 35 #include "system_LPC407x_8x_177x_8x.h"
bogdanm 15:4892fe388435 36
bogdanm 15:4892fe388435 37 #define __CLK_DIV(x,y) (((y) == 0) ? 0: (x)/(y))
bogdanm 15:4892fe388435 38
bogdanm 15:4892fe388435 39 /*
bogdanm 15:4892fe388435 40 //-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
bogdanm 15:4892fe388435 41 */
bogdanm 15:4892fe388435 42 /*--------------------- Clock Configuration ----------------------------------
bogdanm 15:4892fe388435 43 //
bogdanm 15:4892fe388435 44 // <e> Clock Configuration
bogdanm 15:4892fe388435 45 // <h> System Controls and Status Register (SCS - address 0x400F C1A0)
bogdanm 15:4892fe388435 46 // <o1.0> EMC Shift Control Bit
bogdanm 15:4892fe388435 47 // <i> Controls how addresses are output on the EMC address pins for static memories
bogdanm 15:4892fe388435 48 // <0=> Static CS addresses match bus width; AD[1] = 0 for 32 bit, AD[0] = 0 for 16+32 bit (Bit 0 is 0)
bogdanm 15:4892fe388435 49 // <1=> Static CS addresses start at LSB 0 regardless of memory width (Bit 0 is 1)
bogdanm 15:4892fe388435 50 //
bogdanm 15:4892fe388435 51 // <o1.1> EMC Reset Disable Bit
bogdanm 15:4892fe388435 52 // <i> If 0 (zero), all registers and functions of the EMC are initialized upon any reset condition
bogdanm 15:4892fe388435 53 // <i> If 1, EMC is still retained its state through a warm reset
bogdanm 15:4892fe388435 54 // <0=> Both EMC resets are asserted when any type of chip reset event occurs (Bit 1 is 0)
bogdanm 15:4892fe388435 55 // <1=> Portions of EMC will only be reset by POR or BOR event (Bit 1 is 1)
bogdanm 15:4892fe388435 56 //
bogdanm 15:4892fe388435 57 // <o1.2> EMC Burst Control
bogdanm 15:4892fe388435 58 // <i> Set to 1 to prevent multiple sequential accesses to memory via EMC static memory chip selects
bogdanm 15:4892fe388435 59 // <0=> Burst enabled (Bit 2 is 0)
bogdanm 15:4892fe388435 60 // <1=> Bust disbled (Bit 2 is 1)
bogdanm 15:4892fe388435 61 //
bogdanm 15:4892fe388435 62 // <o1.3> MCIPWR Active Level
bogdanm 15:4892fe388435 63 // <i> Selects the active level for the SD card interface signal SD_PWR
bogdanm 15:4892fe388435 64 // <0=> SD_PWR is active low (inverted output of the SD Card interface block) (Bit 3 is 0)
bogdanm 15:4892fe388435 65 // <1=> SD_PWR is active high (follows the output of the SD Card interface block) (Bit 3 is 1)
bogdanm 15:4892fe388435 66 //
bogdanm 15:4892fe388435 67 // <o1.4> Main Oscillator Range Select
bogdanm 15:4892fe388435 68 // <0=> In Range 1 MHz to 20 MHz (Bit 4 is 0)
bogdanm 15:4892fe388435 69 // <1=> In Range 15 MHz to 25 MHz (Bit 4 is 1)
bogdanm 15:4892fe388435 70 //
bogdanm 15:4892fe388435 71 // <o1.5> Main Oscillator enable
bogdanm 15:4892fe388435 72 // <i> 0 (zero) means disabled, 1 means enable
bogdanm 15:4892fe388435 73 //
bogdanm 15:4892fe388435 74 // <o1.6> Main Oscillator status (Read-Only)
bogdanm 15:4892fe388435 75 // </h>
bogdanm 15:4892fe388435 76 //
bogdanm 15:4892fe388435 77 // <h> Clock Source Select Register (CLKSRCSEL - address 0x400F C10C)
bogdanm 15:4892fe388435 78 // <o2.0> CLKSRC: Select the clock source for sysclk to PLL0 clock
bogdanm 15:4892fe388435 79 // <0=> Internal RC oscillator (Bit 0 is 0)
bogdanm 15:4892fe388435 80 // <1=> Main oscillator (Bit 0 is 1)
bogdanm 15:4892fe388435 81 // </h>
bogdanm 15:4892fe388435 82 //
bogdanm 15:4892fe388435 83 // <e3>PLL0 Configuration (Main PLL PLL0CFG - address 0x400F C084)
bogdanm 15:4892fe388435 84 // <i> F_in is in the range of 1 MHz to 25 MHz
bogdanm 15:4892fe388435 85 // <i> F_cco = (F_in * M * 2 * P) is in range of 156 MHz to 320 MHz
bogdanm 15:4892fe388435 86 // <i> PLL out clock = (F_cco / (2 * P)) is in rane of 9.75 MHz to 160 MHz
bogdanm 15:4892fe388435 87 //
bogdanm 15:4892fe388435 88 // <o4.0..4> MSEL: PLL Multiplier Value
bogdanm 15:4892fe388435 89 // <i> M Value
bogdanm 15:4892fe388435 90 // <1-32><#-1>
bogdanm 15:4892fe388435 91 //
bogdanm 15:4892fe388435 92 // <o4.5..6> PSEL: PLL Divider Value
bogdanm 15:4892fe388435 93 // <i> P Value
bogdanm 15:4892fe388435 94 // <0=> 1
bogdanm 15:4892fe388435 95 // <1=> 2
bogdanm 15:4892fe388435 96 // <2=> 4
bogdanm 15:4892fe388435 97 // <3=> 8
bogdanm 15:4892fe388435 98 // </e>
bogdanm 15:4892fe388435 99 //
bogdanm 15:4892fe388435 100 // <e5>PLL1 Configuration (Alt PLL PLL1CFG - address 0x400F C0A4)
bogdanm 15:4892fe388435 101 // <i> F_in is in the range of 1 MHz to 25 MHz
bogdanm 15:4892fe388435 102 // <i> F_cco = (F_in * M * 2 * P) is in range of 156 MHz to 320 MHz
bogdanm 15:4892fe388435 103 // <i> PLL out clock = (F_cco / (2 * P)) is in rane of 9.75 MHz to 160 MHz
bogdanm 15:4892fe388435 104 //
bogdanm 15:4892fe388435 105 // <o6.0..4> MSEL: PLL Multiplier Value
bogdanm 15:4892fe388435 106 // <i> M Value
bogdanm 15:4892fe388435 107 // <1-32><#-1>
bogdanm 15:4892fe388435 108 //
bogdanm 15:4892fe388435 109 // <o6.5..6> PSEL: PLL Divider Value
bogdanm 15:4892fe388435 110 // <i> P Value
bogdanm 15:4892fe388435 111 // <0=> 1
bogdanm 15:4892fe388435 112 // <1=> 2
bogdanm 15:4892fe388435 113 // <2=> 4
bogdanm 15:4892fe388435 114 // <3=> 8
bogdanm 15:4892fe388435 115 // </e>
bogdanm 15:4892fe388435 116 //
bogdanm 15:4892fe388435 117 // <h> CPU Clock Selection Register (CCLKSEL - address 0x400F C104)
bogdanm 15:4892fe388435 118 // <o7.0..4> CCLKDIV: Select the value for divider of CPU clock (CCLK)
bogdanm 15:4892fe388435 119 // <i> 0: The divider is turned off. No clock will be provided to the CPU
bogdanm 15:4892fe388435 120 // <i> n: The input clock is divided by n to produce the CPU clock
bogdanm 15:4892fe388435 121 // <0-31>
bogdanm 15:4892fe388435 122 //
bogdanm 15:4892fe388435 123 // <o7.8> CCLKSEL: Select the input to the divider of CPU clock
bogdanm 15:4892fe388435 124 // <0=> sysclk clock is used
bogdanm 15:4892fe388435 125 // <1=> Main PLL0 clock is used
bogdanm 15:4892fe388435 126 // </h>
bogdanm 15:4892fe388435 127 //
bogdanm 15:4892fe388435 128 // <h> USB Clock Selection Register (USBCLKSEL - 0x400F C108)
bogdanm 15:4892fe388435 129 // <o8.0..4> USBDIV: USB clock (source PLL0) divider selection
bogdanm 15:4892fe388435 130 // <0=> Divider is off and no clock provides to USB subsystem
bogdanm 15:4892fe388435 131 // <4=> Divider value is 4 (The source clock is divided by 4)
bogdanm 15:4892fe388435 132 // <6=> Divider value is 6 (The source clock is divided by 6)
bogdanm 15:4892fe388435 133 //
bogdanm 15:4892fe388435 134 // <o8.8..9> USBSEL: Select the source for USB clock divider
bogdanm 15:4892fe388435 135 // <i> When CPU clock is selected, the USB can be accessed
bogdanm 15:4892fe388435 136 // <i> by software but cannot perform USB functions
bogdanm 15:4892fe388435 137 // <0=> sysclk clock (the clock input to PLL0)
bogdanm 15:4892fe388435 138 // <1=> The clock output from PLL0
bogdanm 15:4892fe388435 139 // <2=> The clock output from PLL1
bogdanm 15:4892fe388435 140 // </h>
bogdanm 15:4892fe388435 141 //
bogdanm 15:4892fe388435 142 // <h> EMC Clock Selection Register (EMCCLKSEL - address 0x400F C100)
bogdanm 15:4892fe388435 143 // <o9.0> EMCDIV: Set the divider for EMC clock
bogdanm 15:4892fe388435 144 // <0=> Divider value is 1
bogdanm 15:4892fe388435 145 // <1=> Divider value is 2 (EMC clock is equal a half of input clock)
bogdanm 15:4892fe388435 146 // </h>
bogdanm 15:4892fe388435 147 //
bogdanm 15:4892fe388435 148 // <h> Peripheral Clock Selection Register (PCLKSEL - address 0x400F C1A8)
bogdanm 15:4892fe388435 149 // <o10.0..4> PCLKDIV: APB Peripheral clock divider
bogdanm 15:4892fe388435 150 // <i> 0: The divider is turned off. No clock will be provided to APB peripherals
bogdanm 15:4892fe388435 151 // <i> n: The input clock is divided by n to produce the APB peripheral clock
bogdanm 15:4892fe388435 152 // <0-31>
bogdanm 15:4892fe388435 153 // </h>
bogdanm 15:4892fe388435 154 //
bogdanm 15:4892fe388435 155 // <h> SPIFI Clock Selection Register (SPIFICLKSEL - address 0x400F C1B4)
bogdanm 15:4892fe388435 156 // <o11.0..4> SPIFIDIV: Set the divider for SPIFI clock
bogdanm 15:4892fe388435 157 // <i> 0: The divider is turned off. No clock will be provided to the SPIFI
bogdanm 15:4892fe388435 158 // <i> n: The input clock is divided by n to produce the SPIFI clock
bogdanm 15:4892fe388435 159 // <0-31>
bogdanm 15:4892fe388435 160 //
bogdanm 15:4892fe388435 161 // <o11.8..9> SPIFISEL: Select the input clock for SPIFI clock divider
bogdanm 15:4892fe388435 162 // <0=> sysclk clock (the clock input to PLL0)
bogdanm 15:4892fe388435 163 // <1=> The clock output from PLL0
bogdanm 15:4892fe388435 164 // <2=> The clock output from PLL1
bogdanm 15:4892fe388435 165 // </h>
bogdanm 15:4892fe388435 166 //
bogdanm 15:4892fe388435 167 // <h> Power Control for Peripherals Register (PCONP - address 0x400F C1C8)
bogdanm 15:4892fe388435 168 // <o12.0> PCLCD: LCD controller power/clock enable (bit 0)
bogdanm 15:4892fe388435 169 // <o12.1> PCTIM0: Timer/Counter 0 power/clock enable (bit 1)
bogdanm 15:4892fe388435 170 // <o12.2> PCTIM1: Timer/Counter 1 power/clock enable (bit 2)
bogdanm 15:4892fe388435 171 // <o12.3> PCUART0: UART 0 power/clock enable (bit 3)
bogdanm 15:4892fe388435 172 // <o12.4> PCUART1: UART 1 power/clock enable (bit 4)
bogdanm 15:4892fe388435 173 // <o12.5> PCPWM0: PWM0 power/clock enable (bit 5)
bogdanm 15:4892fe388435 174 // <o12.6> PCPWM1: PWM1 power/clock enable (bit 6)
bogdanm 15:4892fe388435 175 // <o12.7> PCI2C0: I2C 0 interface power/clock enable (bit 7)
bogdanm 15:4892fe388435 176 // <o12.8> PCUART4: UART 4 power/clock enable (bit 8)
bogdanm 15:4892fe388435 177 // <o12.9> PCRTC: RTC and Event Recorder power/clock enable (bit 9)
bogdanm 15:4892fe388435 178 // <o12.10> PCSSP1: SSP 1 interface power/clock enable (bit 10)
bogdanm 15:4892fe388435 179 // <o12.11> PCEMC: External Memory Controller power/clock enable (bit 11)
bogdanm 15:4892fe388435 180 // <o12.12> PCADC: A/D converter power/clock enable (bit 12)
bogdanm 15:4892fe388435 181 // <o12.13> PCCAN1: CAN controller 1 power/clock enable (bit 13)
bogdanm 15:4892fe388435 182 // <o12.14> PCCAN2: CAN controller 2 power/clock enable (bit 14)
bogdanm 15:4892fe388435 183 // <o12.15> PCGPIO: IOCON, GPIO, and GPIO interrupts power/clock enable (bit 15)
bogdanm 15:4892fe388435 184 // <o12.17> PCMCPWM: Motor Control PWM power/clock enable (bit 17)
bogdanm 15:4892fe388435 185 // <o12.18> PCQEI: Quadrature encoder interface power/clock enable (bit 18)
bogdanm 15:4892fe388435 186 // <o12.19> PCI2C1: I2C 1 interface power/clock enable (bit 19)
bogdanm 15:4892fe388435 187 // <o12.20> PCSSP2: SSP 2 interface power/clock enable (bit 20)
bogdanm 15:4892fe388435 188 // <o12.21> PCSSP0: SSP 0 interface power/clock enable (bit 21)
bogdanm 15:4892fe388435 189 // <o12.22> PCTIM2: Timer 2 power/clock enable (bit 22)
bogdanm 15:4892fe388435 190 // <o12.23> PCTIM3: Timer 3 power/clock enable (bit 23)
bogdanm 15:4892fe388435 191 // <o12.24> PCUART2: UART 2 power/clock enable (bit 24)
bogdanm 15:4892fe388435 192 // <o12.25> PCUART3: UART 3 power/clock enable (bit 25)
bogdanm 15:4892fe388435 193 // <o12.26> PCI2C2: I2C 2 interface power/clock enable (bit 26)
bogdanm 15:4892fe388435 194 // <o12.27> PCI2S: I2S interface power/clock enable (bit 27)
bogdanm 15:4892fe388435 195 // <o12.28> PCSDC: SD Card interface power/clock enable (bit 28)
bogdanm 15:4892fe388435 196 // <o12.29> PCGPDMA: GPDMA function power/clock enable (bit 29)
bogdanm 15:4892fe388435 197 // <o12.30> PCENET: Ethernet block power/clock enable (bit 30)
bogdanm 15:4892fe388435 198 // <o12.31> PCUSB: USB interface power/clock enable (bit 31)
bogdanm 15:4892fe388435 199 // </h>
bogdanm 15:4892fe388435 200 //
bogdanm 15:4892fe388435 201 // <h> Clock Output Configuration Register (CLKOUTCFG)
bogdanm 15:4892fe388435 202 // <o13.0..3> CLKOUTSEL: Clock Source for CLKOUT Selection
bogdanm 15:4892fe388435 203 // <0=> CPU clock
bogdanm 15:4892fe388435 204 // <1=> Main Oscillator
bogdanm 15:4892fe388435 205 // <2=> Internal RC Oscillator
bogdanm 15:4892fe388435 206 // <3=> USB clock
bogdanm 15:4892fe388435 207 // <4=> RTC Oscillator
bogdanm 15:4892fe388435 208 // <5=> unused
bogdanm 15:4892fe388435 209 // <6=> Watchdog Oscillator
bogdanm 15:4892fe388435 210 //
bogdanm 15:4892fe388435 211 // <o13.4..7> CLKOUTDIV: Output Clock Divider
bogdanm 15:4892fe388435 212 // <1-16><#-1>
bogdanm 15:4892fe388435 213 //
bogdanm 15:4892fe388435 214 // <o13.8> CLKOUT_EN: CLKOUT enable
bogdanm 15:4892fe388435 215 // </h>
bogdanm 15:4892fe388435 216 //
bogdanm 15:4892fe388435 217 // </e>
bogdanm 15:4892fe388435 218 */
bogdanm 15:4892fe388435 219
bogdanm 15:4892fe388435 220 #define CLOCK_SETUP 1
bogdanm 15:4892fe388435 221 #define SCS_Val 0x00000020
bogdanm 15:4892fe388435 222 #define CLKSRCSEL_Val 0x00000001
bogdanm 15:4892fe388435 223 #define PLL0_SETUP 1
bogdanm 15:4892fe388435 224 #define PLL0CFG_Val 0x00000009
bogdanm 15:4892fe388435 225 #define PLL1_SETUP 1
bogdanm 15:4892fe388435 226 #define PLL1CFG_Val 0x00000023
bogdanm 15:4892fe388435 227 #define CCLKSEL_Val 0x00000101
bogdanm 15:4892fe388435 228 #define USBCLKSEL_Val 0x00000201
bogdanm 15:4892fe388435 229 #define EMCCLKSEL_Val 0x00000001
bogdanm 15:4892fe388435 230 #define PCLKSEL_Val 0x00000002
bogdanm 15:4892fe388435 231 #define SPIFICLKSEL_Val 0x00000002
bogdanm 15:4892fe388435 232 #define PCONP_Val 0x042887DE
bogdanm 15:4892fe388435 233 #define CLKOUTCFG_Val 0x00000100
bogdanm 15:4892fe388435 234
bogdanm 15:4892fe388435 235 #ifdef CORE_M4
bogdanm 15:4892fe388435 236 #define LPC_CPACR 0xE000ED88
bogdanm 15:4892fe388435 237
bogdanm 15:4892fe388435 238 #define SCB_MVFR0 0xE000EF40
bogdanm 15:4892fe388435 239 #define SCB_MVFR0_RESET 0x10110021
bogdanm 15:4892fe388435 240
bogdanm 15:4892fe388435 241 #define SCB_MVFR1 0xE000EF44
bogdanm 15:4892fe388435 242 #define SCB_MVFR1_RESET 0x11000011
bogdanm 15:4892fe388435 243 #endif
bogdanm 15:4892fe388435 244
bogdanm 15:4892fe388435 245
bogdanm 15:4892fe388435 246 /*--------------------- Flash Accelerator Configuration ----------------------
bogdanm 15:4892fe388435 247 //
bogdanm 15:4892fe388435 248 // <e> Flash Accelerator Configuration register (FLASHCFG - address 0x400F C000)
bogdanm 15:4892fe388435 249 // <o1.12..15> FLASHTIM: Flash Access Time
bogdanm 15:4892fe388435 250 // <0=> 1 CPU clock (for CPU clock up to 20 MHz)
bogdanm 15:4892fe388435 251 // <1=> 2 CPU clocks (for CPU clock up to 40 MHz)
bogdanm 15:4892fe388435 252 // <2=> 3 CPU clocks (for CPU clock up to 60 MHz)
bogdanm 15:4892fe388435 253 // <3=> 4 CPU clocks (for CPU clock up to 80 MHz)
bogdanm 15:4892fe388435 254 // <4=> 5 CPU clocks (for CPU clock up to 100 MHz)
bogdanm 15:4892fe388435 255 // <5=> 6 CPU clocks (for any CPU clock)
bogdanm 15:4892fe388435 256 // </e>
bogdanm 15:4892fe388435 257 */
bogdanm 15:4892fe388435 258
bogdanm 15:4892fe388435 259 #define FLASH_SETUP 1
bogdanm 15:4892fe388435 260 #define FLASHCFG_Val 0x00005000
bogdanm 15:4892fe388435 261
bogdanm 15:4892fe388435 262 /*----------------------------------------------------------------------------
bogdanm 15:4892fe388435 263 Check the register settings
bogdanm 15:4892fe388435 264 *----------------------------------------------------------------------------*/
bogdanm 15:4892fe388435 265 #define CHECK_RANGE(val, min, max) ((val < min) || (val > max))
bogdanm 15:4892fe388435 266 #define CHECK_RSVD(val, mask) (val & mask)
bogdanm 15:4892fe388435 267
bogdanm 15:4892fe388435 268 /* Clock Configuration -------------------------------------------------------*/
bogdanm 15:4892fe388435 269 #if (CHECK_RSVD((SCS_Val), ~0x0000003F))
bogdanm 15:4892fe388435 270 #error "SCS: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 271 #endif
bogdanm 15:4892fe388435 272
bogdanm 15:4892fe388435 273 #if (CHECK_RANGE((CLKSRCSEL_Val), 0, 1))
bogdanm 15:4892fe388435 274 #error "CLKSRCSEL: Value out of range!"
bogdanm 15:4892fe388435 275 #endif
bogdanm 15:4892fe388435 276
bogdanm 15:4892fe388435 277 #if (CHECK_RSVD((PLL0CFG_Val), ~0x0000007F))
bogdanm 15:4892fe388435 278 #error "PLL0CFG: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 279 #endif
bogdanm 15:4892fe388435 280
bogdanm 15:4892fe388435 281 #if (CHECK_RSVD((PLL1CFG_Val), ~0x0000007F))
bogdanm 15:4892fe388435 282 #error "PLL1CFG: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 283 #endif
bogdanm 15:4892fe388435 284
bogdanm 15:4892fe388435 285 #if (CHECK_RSVD((CCLKSEL_Val), ~0x0000011F))
bogdanm 15:4892fe388435 286 #error "CCLKSEL: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 287 #endif
bogdanm 15:4892fe388435 288
bogdanm 15:4892fe388435 289 #if (CHECK_RSVD((USBCLKSEL_Val), ~0x0000031F))
bogdanm 15:4892fe388435 290 #error "USBCLKSEL: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 291 #endif
bogdanm 15:4892fe388435 292
bogdanm 15:4892fe388435 293 #if (CHECK_RSVD((EMCCLKSEL_Val), ~0x00000001))
bogdanm 15:4892fe388435 294 #error "EMCCLKSEL: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 295 #endif
bogdanm 15:4892fe388435 296
bogdanm 15:4892fe388435 297 #if (CHECK_RSVD((PCLKSEL_Val), ~0x0000001F))
bogdanm 15:4892fe388435 298 #error "PCLKSEL: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 299 #endif
bogdanm 15:4892fe388435 300
bogdanm 15:4892fe388435 301 #if (CHECK_RSVD((PCONP_Val), ~0xFFFEFFFF))
bogdanm 15:4892fe388435 302 #error "PCONP: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 303 #endif
bogdanm 15:4892fe388435 304
bogdanm 15:4892fe388435 305 #if (CHECK_RSVD((CLKOUTCFG_Val), ~0x000001FF))
bogdanm 15:4892fe388435 306 #error "CLKOUTCFG: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 307 #endif
bogdanm 15:4892fe388435 308
bogdanm 15:4892fe388435 309 /* Flash Accelerator Configuration -------------------------------------------*/
bogdanm 15:4892fe388435 310 #if (CHECK_RSVD((FLASHCFG_Val), ~0x0000F000))
bogdanm 15:4892fe388435 311 #warning "FLASHCFG: Invalid values of reserved bits!"
bogdanm 15:4892fe388435 312 #endif
bogdanm 15:4892fe388435 313
bogdanm 15:4892fe388435 314
bogdanm 15:4892fe388435 315 /*----------------------------------------------------------------------------
bogdanm 15:4892fe388435 316 DEFINES
bogdanm 15:4892fe388435 317 *----------------------------------------------------------------------------*/
bogdanm 15:4892fe388435 318 /* pll_out_clk = F_cco / (2 � P)
bogdanm 15:4892fe388435 319 F_cco = pll_in_clk � M � 2 � P */
bogdanm 15:4892fe388435 320 #define __M ((PLL0CFG_Val & 0x1F) + 1)
bogdanm 15:4892fe388435 321 #define __PLL0_CLK(__F_IN) (__F_IN * __M)
bogdanm 15:4892fe388435 322 #define __CCLK_DIV (CCLKSEL_Val & 0x1F)
bogdanm 15:4892fe388435 323 #define __PCLK_DIV (PCLKSEL_Val & 0x1F)
bogdanm 15:4892fe388435 324 #define __ECLK_DIV ((EMCCLKSEL_Val & 0x01) + 1)
bogdanm 15:4892fe388435 325
bogdanm 15:4892fe388435 326 /* Determine core clock frequency according to settings */
bogdanm 15:4892fe388435 327 #if (CLOCK_SETUP) /* Clock Setup */
bogdanm 15:4892fe388435 328
bogdanm 15:4892fe388435 329 #if ((CLKSRCSEL_Val & 0x01) == 1) && ((SCS_Val & 0x20)== 0)
bogdanm 15:4892fe388435 330 #error "Main Oscillator is selected as clock source but is not enabled!"
bogdanm 15:4892fe388435 331 #endif
bogdanm 15:4892fe388435 332
bogdanm 15:4892fe388435 333 #if ((CCLKSEL_Val & 0x100) == 0x100) && (PLL0_SETUP == 0)
bogdanm 15:4892fe388435 334 #error "Main PLL is selected as clock source but is not enabled!"
bogdanm 15:4892fe388435 335 #endif
bogdanm 15:4892fe388435 336
bogdanm 15:4892fe388435 337 #if ((CCLKSEL_Val & 0x100) == 0) /* cclk = sysclk */
bogdanm 15:4892fe388435 338 #if ((CLKSRCSEL_Val & 0x01) == 0) /* sysclk = irc_clk */
bogdanm 15:4892fe388435 339 #define __CORE_CLK (IRC_OSC / __CCLK_DIV)
bogdanm 15:4892fe388435 340 #define __PER_CLK (IRC_OSC/ __PCLK_DIV)
bogdanm 15:4892fe388435 341 #define __EMC_CLK (__CORE_CLK/ __ECLK_DIV)
bogdanm 15:4892fe388435 342 #else /* sysclk = osc_clk */
bogdanm 15:4892fe388435 343 #define __CORE_CLK (OSC_CLK / __CCLK_DIV)
bogdanm 15:4892fe388435 344 #define __PER_CLK (OSC_CLK/ __PCLK_DIV)
bogdanm 15:4892fe388435 345 #define __EMC_CLK (__CORE_CLK/ __ECLK_DIV)
bogdanm 15:4892fe388435 346 #endif
bogdanm 15:4892fe388435 347 #else /* cclk = pll_clk */
bogdanm 15:4892fe388435 348 #if ((CLKSRCSEL_Val & 0x01) == 0) /* sysclk = irc_clk */
bogdanm 15:4892fe388435 349 #define __CORE_CLK (__PLL0_CLK(IRC_OSC) / __CCLK_DIV)
bogdanm 15:4892fe388435 350 #define __PER_CLK (__PLL0_CLK(IRC_OSC) / __PCLK_DIV)
bogdanm 15:4892fe388435 351 #define __EMC_CLK (__CORE_CLK / __ECLK_DIV)
bogdanm 15:4892fe388435 352 #else /* sysclk = osc_clk */
bogdanm 15:4892fe388435 353 #define __CORE_CLK (__PLL0_CLK(OSC_CLK) / __CCLK_DIV)
bogdanm 15:4892fe388435 354 #define __PER_CLK (__PLL0_CLK(OSC_CLK) / __PCLK_DIV)
bogdanm 15:4892fe388435 355 #define __EMC_CLK (__CORE_CLK / __ECLK_DIV)
bogdanm 15:4892fe388435 356 #endif
bogdanm 15:4892fe388435 357 #endif
bogdanm 15:4892fe388435 358
bogdanm 15:4892fe388435 359 #else
bogdanm 15:4892fe388435 360 #define __CORE_CLK (IRC_OSC)
bogdanm 15:4892fe388435 361 #define __PER_CLK (IRC_OSC)
bogdanm 15:4892fe388435 362 #define __EMC_CLK (__CORE_CLK)
bogdanm 15:4892fe388435 363 #endif
bogdanm 15:4892fe388435 364
bogdanm 15:4892fe388435 365 /*----------------------------------------------------------------------------
bogdanm 15:4892fe388435 366 Clock Variable definitions
bogdanm 15:4892fe388435 367 *----------------------------------------------------------------------------*/
bogdanm 15:4892fe388435 368 uint32_t SystemCoreClock = __CORE_CLK;/*!< System Clock Frequency (Core Clock)*/
bogdanm 15:4892fe388435 369 uint32_t PeripheralClock = __PER_CLK; /*!< Peripheral Clock Frequency (Pclk) */
bogdanm 15:4892fe388435 370 uint32_t EMCClock = __EMC_CLK; /*!< EMC Clock Frequency */
bogdanm 15:4892fe388435 371 uint32_t USBClock = (48000000UL); /*!< USB Clock Frequency - this value will
bogdanm 15:4892fe388435 372 be updated after call SystemCoreClockUpdate, should be 48MHz*/
bogdanm 15:4892fe388435 373
bogdanm 15:4892fe388435 374
bogdanm 15:4892fe388435 375 /*----------------------------------------------------------------------------
bogdanm 15:4892fe388435 376 Clock functions
bogdanm 15:4892fe388435 377 *----------------------------------------------------------------------------*/
bogdanm 15:4892fe388435 378 void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
bogdanm 15:4892fe388435 379 {
bogdanm 15:4892fe388435 380 /* Determine clock frequency according to clock register values */
bogdanm 15:4892fe388435 381 if ((LPC_SC->CCLKSEL &0x100) == 0) { /* cclk = sysclk */
bogdanm 15:4892fe388435 382 if ((LPC_SC->CLKSRCSEL & 0x01) == 0) { /* sysclk = irc_clk */
bogdanm 15:4892fe388435 383 SystemCoreClock = __CLK_DIV(IRC_OSC , (LPC_SC->CCLKSEL & 0x1F));
bogdanm 15:4892fe388435 384 PeripheralClock = __CLK_DIV(IRC_OSC , (LPC_SC->PCLKSEL & 0x1F));
bogdanm 15:4892fe388435 385 EMCClock = (SystemCoreClock / ((LPC_SC->EMCCLKSEL & 0x01)+1));
bogdanm 15:4892fe388435 386 }
bogdanm 15:4892fe388435 387 else { /* sysclk = osc_clk */
bogdanm 15:4892fe388435 388 if ((LPC_SC->SCS & 0x40) == 0) {
bogdanm 15:4892fe388435 389 SystemCoreClock = 0; /* this should never happen! */
bogdanm 15:4892fe388435 390 PeripheralClock = 0;
bogdanm 15:4892fe388435 391 EMCClock = 0;
bogdanm 15:4892fe388435 392 }
bogdanm 15:4892fe388435 393 else {
bogdanm 15:4892fe388435 394 SystemCoreClock = __CLK_DIV(OSC_CLK , (LPC_SC->CCLKSEL & 0x1F));
bogdanm 15:4892fe388435 395 PeripheralClock = __CLK_DIV(OSC_CLK , (LPC_SC->PCLKSEL & 0x1F));
bogdanm 15:4892fe388435 396 EMCClock = (SystemCoreClock / ((LPC_SC->EMCCLKSEL & 0x01)+1));
bogdanm 15:4892fe388435 397 }
bogdanm 15:4892fe388435 398 }
bogdanm 15:4892fe388435 399 }
bogdanm 15:4892fe388435 400 else { /* cclk = pll_clk */
bogdanm 15:4892fe388435 401 if ((LPC_SC->PLL0STAT & 0x100) == 0) { /* PLL0 not enabled */
bogdanm 15:4892fe388435 402 SystemCoreClock = 0; /* this should never happen! */
bogdanm 15:4892fe388435 403 PeripheralClock = 0;
bogdanm 15:4892fe388435 404 EMCClock = 0;
bogdanm 15:4892fe388435 405 }
bogdanm 15:4892fe388435 406 else {
bogdanm 15:4892fe388435 407 if ((LPC_SC->CLKSRCSEL & 0x01) == 0) { /* sysclk = irc_clk */
bogdanm 15:4892fe388435 408 uint8_t mul = ((LPC_SC->PLL0STAT & 0x1F) + 1);
bogdanm 15:4892fe388435 409 uint8_t cpu_div = (LPC_SC->CCLKSEL & 0x1F);
bogdanm 15:4892fe388435 410 uint8_t per_div = (LPC_SC->PCLKSEL & 0x1F);
bogdanm 15:4892fe388435 411 uint8_t emc_div = (LPC_SC->EMCCLKSEL & 0x01)+1;
bogdanm 15:4892fe388435 412 SystemCoreClock = __CLK_DIV(IRC_OSC * mul , cpu_div);
bogdanm 15:4892fe388435 413 PeripheralClock = __CLK_DIV(IRC_OSC * mul , per_div);
bogdanm 15:4892fe388435 414 EMCClock = SystemCoreClock / emc_div;
bogdanm 15:4892fe388435 415 }
bogdanm 15:4892fe388435 416 else { /* sysclk = osc_clk */
bogdanm 15:4892fe388435 417 if ((LPC_SC->SCS & 0x40) == 0) {
bogdanm 15:4892fe388435 418 SystemCoreClock = 0; /* this should never happen! */
bogdanm 15:4892fe388435 419 PeripheralClock = 0;
bogdanm 15:4892fe388435 420 EMCClock = 0;
bogdanm 15:4892fe388435 421 }
bogdanm 15:4892fe388435 422 else {
bogdanm 15:4892fe388435 423 uint8_t mul = ((LPC_SC->PLL0STAT & 0x1F) + 1);
bogdanm 15:4892fe388435 424 uint8_t cpu_div = (LPC_SC->CCLKSEL & 0x1F);
bogdanm 15:4892fe388435 425 uint8_t per_div = (LPC_SC->PCLKSEL & 0x1F);
bogdanm 15:4892fe388435 426 uint8_t emc_div = (LPC_SC->EMCCLKSEL & 0x01)+1;
bogdanm 15:4892fe388435 427 SystemCoreClock = __CLK_DIV(OSC_CLK * mul , cpu_div);
bogdanm 15:4892fe388435 428 PeripheralClock = __CLK_DIV(OSC_CLK * mul , per_div);
bogdanm 15:4892fe388435 429 EMCClock = SystemCoreClock / emc_div;
bogdanm 15:4892fe388435 430 }
bogdanm 15:4892fe388435 431 }
bogdanm 15:4892fe388435 432 }
bogdanm 15:4892fe388435 433 }
bogdanm 15:4892fe388435 434 /* ---update USBClock------------------*/
bogdanm 15:4892fe388435 435 if(LPC_SC->USBCLKSEL & (0x01<<8))//Use PLL0 as the input to the USB clock divider
bogdanm 15:4892fe388435 436 {
bogdanm 15:4892fe388435 437 switch (LPC_SC->USBCLKSEL & 0x1F)
bogdanm 15:4892fe388435 438 {
bogdanm 15:4892fe388435 439 case 0:
bogdanm 15:4892fe388435 440 USBClock = 0; //no clock will be provided to the USB subsystem
bogdanm 15:4892fe388435 441 break;
bogdanm 15:4892fe388435 442 case 4:
bogdanm 15:4892fe388435 443 case 6:
bogdanm 15:4892fe388435 444 {
bogdanm 15:4892fe388435 445 uint8_t mul = ((LPC_SC->PLL0STAT & 0x1F) + 1);
bogdanm 15:4892fe388435 446 uint8_t usb_div = (LPC_SC->USBCLKSEL & 0x1F);
bogdanm 15:4892fe388435 447 if(LPC_SC->CLKSRCSEL & 0x01) //pll_clk_in = main_osc
bogdanm 15:4892fe388435 448 USBClock = OSC_CLK * mul / usb_div;
bogdanm 15:4892fe388435 449 else //pll_clk_in = irc_clk
bogdanm 15:4892fe388435 450 USBClock = IRC_OSC * mul / usb_div;
bogdanm 15:4892fe388435 451 }
bogdanm 15:4892fe388435 452 break;
bogdanm 15:4892fe388435 453 default:
bogdanm 15:4892fe388435 454 USBClock = 0; /* this should never happen! */
bogdanm 15:4892fe388435 455 }
bogdanm 15:4892fe388435 456 }
bogdanm 15:4892fe388435 457 else if(LPC_SC->USBCLKSEL & (0x02<<8))//usb_input_clk = alt_pll (pll1)
bogdanm 15:4892fe388435 458 {
bogdanm 15:4892fe388435 459 if(LPC_SC->CLKSRCSEL & 0x01) //pll1_clk_in = main_osc
bogdanm 15:4892fe388435 460 USBClock = (OSC_CLK * ((LPC_SC->PLL1STAT & 0x1F) + 1));
bogdanm 15:4892fe388435 461 else //pll1_clk_in = irc_clk
bogdanm 15:4892fe388435 462 USBClock = (IRC_OSC * ((LPC_SC->PLL0STAT & 0x1F) + 1));
bogdanm 15:4892fe388435 463 }
bogdanm 15:4892fe388435 464 else
bogdanm 15:4892fe388435 465 USBClock = 0; /* this should never happen! */
bogdanm 15:4892fe388435 466 }
bogdanm 15:4892fe388435 467
bogdanm 15:4892fe388435 468 /* Determine clock frequency according to clock register values */
bogdanm 15:4892fe388435 469
bogdanm 15:4892fe388435 470 #ifdef CORE_M4
bogdanm 15:4892fe388435 471
bogdanm 15:4892fe388435 472 void fpu_init(void)
bogdanm 15:4892fe388435 473 {
bogdanm 15:4892fe388435 474 // from arm trm manual:
bogdanm 15:4892fe388435 475 // ; CPACR is located at address 0xE000ED88
bogdanm 15:4892fe388435 476 // LDR.W R0, =0xE000ED88
bogdanm 15:4892fe388435 477 // ; Read CPACR
bogdanm 15:4892fe388435 478 // LDR R1, [R0]
bogdanm 15:4892fe388435 479 // ; Set bits 20-23 to enable CP10 and CP11 coprocessors
bogdanm 15:4892fe388435 480 // ORR R1, R1, #(0xF << 20)
bogdanm 15:4892fe388435 481 // ; Write back the modified value to the CPACR
bogdanm 15:4892fe388435 482 // STR R1, [R0]
bogdanm 15:4892fe388435 483
bogdanm 15:4892fe388435 484
bogdanm 15:4892fe388435 485 volatile uint32_t* regCpacr = (uint32_t*) LPC_CPACR;
bogdanm 15:4892fe388435 486 volatile uint32_t* regMvfr0 = (uint32_t*) SCB_MVFR0;
bogdanm 15:4892fe388435 487 volatile uint32_t* regMvfr1 = (uint32_t*) SCB_MVFR1;
bogdanm 15:4892fe388435 488 volatile uint32_t Cpacr;
bogdanm 15:4892fe388435 489 volatile uint32_t Mvfr0;
bogdanm 15:4892fe388435 490 volatile uint32_t Mvfr1;
bogdanm 15:4892fe388435 491 char vfpPresent = 0;
bogdanm 15:4892fe388435 492
bogdanm 15:4892fe388435 493 Mvfr0 = *regMvfr0;
bogdanm 15:4892fe388435 494 Mvfr1 = *regMvfr1;
bogdanm 15:4892fe388435 495
bogdanm 15:4892fe388435 496 vfpPresent = ((SCB_MVFR0_RESET == Mvfr0) && (SCB_MVFR1_RESET == Mvfr1));
bogdanm 15:4892fe388435 497
bogdanm 15:4892fe388435 498 if(vfpPresent)
bogdanm 15:4892fe388435 499 {
bogdanm 15:4892fe388435 500 Cpacr = *regCpacr;
bogdanm 15:4892fe388435 501 Cpacr |= (0xF << 20);
bogdanm 15:4892fe388435 502 *regCpacr = Cpacr; // enable CP10 and CP11 for full access
bogdanm 15:4892fe388435 503 }
bogdanm 15:4892fe388435 504
bogdanm 15:4892fe388435 505 }
bogdanm 15:4892fe388435 506 #endif
bogdanm 15:4892fe388435 507
bogdanm 15:4892fe388435 508 /**
bogdanm 15:4892fe388435 509 * Initialize the system
bogdanm 15:4892fe388435 510 *
bogdanm 15:4892fe388435 511 * @param none
bogdanm 15:4892fe388435 512 * @return none
bogdanm 15:4892fe388435 513 *
bogdanm 15:4892fe388435 514 * @brief Setup the microcontroller system.
bogdanm 15:4892fe388435 515 * Initialize the System.
bogdanm 15:4892fe388435 516 */
bogdanm 15:4892fe388435 517 void SystemInit (void)
bogdanm 15:4892fe388435 518 {
bogdanm 15:4892fe388435 519 #ifndef __CODE_RED
bogdanm 15:4892fe388435 520 #ifdef CORE_M4
bogdanm 15:4892fe388435 521 fpu_init();
bogdanm 15:4892fe388435 522 #endif
bogdanm 15:4892fe388435 523 #endif
bogdanm 15:4892fe388435 524
bogdanm 15:4892fe388435 525 #if (CLOCK_SETUP) /* Clock Setup */
bogdanm 15:4892fe388435 526 LPC_SC->SCS = SCS_Val;
bogdanm 15:4892fe388435 527 if (SCS_Val & (1 << 5)) { /* If Main Oscillator is enabled */
bogdanm 15:4892fe388435 528 while ((LPC_SC->SCS & (1<<6)) == 0);/* Wait for Oscillator to be ready */
bogdanm 15:4892fe388435 529 }
bogdanm 15:4892fe388435 530
bogdanm 15:4892fe388435 531 LPC_SC->CLKSRCSEL = CLKSRCSEL_Val; /* Select Clock Source for sysclk/PLL0*/
bogdanm 15:4892fe388435 532
bogdanm 15:4892fe388435 533 #if (PLL0_SETUP)
bogdanm 15:4892fe388435 534 LPC_SC->PLL0CFG = PLL0CFG_Val;
bogdanm 15:4892fe388435 535 LPC_SC->PLL0CON = 0x01; /* PLL0 Enable */
bogdanm 15:4892fe388435 536 LPC_SC->PLL0FEED = 0xAA;
bogdanm 15:4892fe388435 537 LPC_SC->PLL0FEED = 0x55;
bogdanm 15:4892fe388435 538 while (!(LPC_SC->PLL0STAT & (1<<10)));/* Wait for PLOCK0 */
bogdanm 15:4892fe388435 539 #endif
bogdanm 15:4892fe388435 540
bogdanm 15:4892fe388435 541 #if (PLL1_SETUP)
bogdanm 15:4892fe388435 542 LPC_SC->PLL1CFG = PLL1CFG_Val;
bogdanm 15:4892fe388435 543 LPC_SC->PLL1CON = 0x01; /* PLL1 Enable */
bogdanm 15:4892fe388435 544 LPC_SC->PLL1FEED = 0xAA;
bogdanm 15:4892fe388435 545 LPC_SC->PLL1FEED = 0x55;
bogdanm 15:4892fe388435 546 while (!(LPC_SC->PLL1STAT & (1<<10)));/* Wait for PLOCK1 */
bogdanm 15:4892fe388435 547 #endif
bogdanm 15:4892fe388435 548
bogdanm 15:4892fe388435 549 LPC_SC->CCLKSEL = CCLKSEL_Val; /* Setup Clock Divider */
bogdanm 15:4892fe388435 550 LPC_SC->USBCLKSEL = USBCLKSEL_Val; /* Setup USB Clock Divider */
bogdanm 15:4892fe388435 551 LPC_SC->EMCCLKSEL = EMCCLKSEL_Val; /* EMC Clock Selection */
bogdanm 15:4892fe388435 552 LPC_SC->SPIFICLKSEL = SPIFICLKSEL_Val; /* SPIFI Clock Selection */
bogdanm 15:4892fe388435 553 LPC_SC->PCLKSEL = PCLKSEL_Val; /* Peripheral Clock Selection */
bogdanm 15:4892fe388435 554 LPC_SC->PCONP = PCONP_Val; /* Power Control for Peripherals */
bogdanm 15:4892fe388435 555 LPC_SC->CLKOUTCFG = CLKOUTCFG_Val; /* Clock Output Configuration */
bogdanm 15:4892fe388435 556 #endif
bogdanm 15:4892fe388435 557
bogdanm 15:4892fe388435 558 LPC_SC->PBOOST |= 0x03; /* Power Boost control */
bogdanm 15:4892fe388435 559
bogdanm 15:4892fe388435 560 #if (FLASH_SETUP == 1) /* Flash Accelerator Setup */
bogdanm 15:4892fe388435 561 LPC_SC->FLASHCFG = FLASHCFG_Val|0x03A;
bogdanm 15:4892fe388435 562 #endif
bogdanm 15:4892fe388435 563 #ifndef __CODE_RED
bogdanm 15:4892fe388435 564 #ifdef __RAM_MODE__
bogdanm 15:4892fe388435 565 SCB->VTOR = 0x10000000 & 0x3FFFFF80;
bogdanm 15:4892fe388435 566 #else
bogdanm 15:4892fe388435 567 SCB->VTOR = 0x00000000 & 0x3FFFFF80;
bogdanm 15:4892fe388435 568 #endif
bogdanm 15:4892fe388435 569 #endif
bogdanm 15:4892fe388435 570 SystemCoreClockUpdate();
bogdanm 15:4892fe388435 571 }