Update version of EALib.

Dependencies:   FATFileSystem

Fork of EALib by IONX

Committer:
embeddedartists
Date:
Wed Jan 29 11:23:01 2014 +0000
Revision:
11:2d4d65173195
Parent:
0:0fdadbc3d852
Child:
12:15597e45eea0
Updated sdram.cpp to use the hook added in mbed_lib_rev76 to allow some memory to be reserved for the stack.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:0fdadbc3d852 1 /*****************************************************************************
embeddedartists 0:0fdadbc3d852 2 *
embeddedartists 0:0fdadbc3d852 3 * Copyright(C) 2011, Embedded Artists AB
embeddedartists 0:0fdadbc3d852 4 * All rights reserved.
embeddedartists 0:0fdadbc3d852 5 *
embeddedartists 0:0fdadbc3d852 6 ******************************************************************************
embeddedartists 0:0fdadbc3d852 7 * Software that is described herein is for illustrative purposes only
embeddedartists 0:0fdadbc3d852 8 * which provides customers with programming information regarding the
embeddedartists 0:0fdadbc3d852 9 * products. This software is supplied "AS IS" without any warranties.
embeddedartists 0:0fdadbc3d852 10 * Embedded Artists AB assumes no responsibility or liability for the
embeddedartists 0:0fdadbc3d852 11 * use of the software, conveys no license or title under any patent,
embeddedartists 0:0fdadbc3d852 12 * copyright, or mask work right to the product. Embedded Artists AB
embeddedartists 0:0fdadbc3d852 13 * reserves the right to make changes in the software without
embeddedartists 0:0fdadbc3d852 14 * notification. Embedded Artists AB also make no representation or
embeddedartists 0:0fdadbc3d852 15 * warranty that such application will be suitable for the specified
embeddedartists 0:0fdadbc3d852 16 * use without further testing or modification.
embeddedartists 0:0fdadbc3d852 17 *****************************************************************************/
embeddedartists 0:0fdadbc3d852 18
embeddedartists 0:0fdadbc3d852 19
embeddedartists 0:0fdadbc3d852 20
embeddedartists 0:0fdadbc3d852 21 /******************************************************************************
embeddedartists 0:0fdadbc3d852 22 * Includes
embeddedartists 0:0fdadbc3d852 23 *****************************************************************************/
embeddedartists 0:0fdadbc3d852 24
embeddedartists 0:0fdadbc3d852 25 #include "mbed.h"
embeddedartists 0:0fdadbc3d852 26 #include "sdram.h"
embeddedartists 0:0fdadbc3d852 27
embeddedartists 11:2d4d65173195 28 #if defined(TOOLCHAIN_ARM) /* KEIL uVision and mbed online compiler */
embeddedartists 11:2d4d65173195 29 #include "sys_helper.h"
embeddedartists 11:2d4d65173195 30 #endif
embeddedartists 0:0fdadbc3d852 31
embeddedartists 0:0fdadbc3d852 32 /******************************************************************************
embeddedartists 0:0fdadbc3d852 33 * Defines and typedefs
embeddedartists 0:0fdadbc3d852 34 *****************************************************************************/
embeddedartists 0:0fdadbc3d852 35
embeddedartists 0:0fdadbc3d852 36
embeddedartists 0:0fdadbc3d852 37 /******************************************************************************
embeddedartists 0:0fdadbc3d852 38 * External global variables
embeddedartists 0:0fdadbc3d852 39 *****************************************************************************/
embeddedartists 0:0fdadbc3d852 40
embeddedartists 0:0fdadbc3d852 41 /******************************************************************************
embeddedartists 0:0fdadbc3d852 42 * Local variables
embeddedartists 0:0fdadbc3d852 43 *****************************************************************************/
embeddedartists 0:0fdadbc3d852 44
embeddedartists 0:0fdadbc3d852 45 static volatile uint32_t ringosccount[2] = {0,0};
embeddedartists 0:0fdadbc3d852 46
embeddedartists 0:0fdadbc3d852 47 static bool okToUseSdramForHeap = true;
embeddedartists 0:0fdadbc3d852 48 static bool initialized = false;
embeddedartists 0:0fdadbc3d852 49
embeddedartists 0:0fdadbc3d852 50 /******************************************************************************
embeddedartists 0:0fdadbc3d852 51 * Overridden Global Functions
embeddedartists 0:0fdadbc3d852 52 *****************************************************************************/
embeddedartists 0:0fdadbc3d852 53
embeddedartists 0:0fdadbc3d852 54 #if defined(TOOLCHAIN_ARM) /* KEIL uVision and mbed online compiler */
embeddedartists 0:0fdadbc3d852 55 //http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0349c/Cihehbce.html
embeddedartists 0:0fdadbc3d852 56
embeddedartists 0:0fdadbc3d852 57 extern "C" unsigned __rt_heap_extend(unsigned size, void **block) {
embeddedartists 0:0fdadbc3d852 58 static uint32_t lastReturnedBlock = 0;
embeddedartists 0:0fdadbc3d852 59
embeddedartists 0:0fdadbc3d852 60 if (okToUseSdramForHeap && !initialized) {
embeddedartists 0:0fdadbc3d852 61 sdram_init();
embeddedartists 0:0fdadbc3d852 62 }
embeddedartists 0:0fdadbc3d852 63
embeddedartists 0:0fdadbc3d852 64 // Make sure that SDRAM is only returned once (as all of it is returned
embeddedartists 0:0fdadbc3d852 65 // the first time) and only if the user has chosen to do it (via the
embeddedartists 0:0fdadbc3d852 66 // okToUseSdramForHeap variable.
embeddedartists 0:0fdadbc3d852 67 if (okToUseSdramForHeap && lastReturnedBlock==0) {
embeddedartists 0:0fdadbc3d852 68 *block = (void*)SDRAM_BASE;
embeddedartists 0:0fdadbc3d852 69 lastReturnedBlock = SDRAM_BASE;
embeddedartists 0:0fdadbc3d852 70 return SDRAM_SIZE;
embeddedartists 0:0fdadbc3d852 71 }
embeddedartists 0:0fdadbc3d852 72 return 0;
embeddedartists 0:0fdadbc3d852 73 }
embeddedartists 11:2d4d65173195 74
embeddedartists 11:2d4d65173195 75 // Overrides the WEAK function in sys_helper.cpp to allow reserving a specific
embeddedartists 11:2d4d65173195 76 // amount of memory for the stack. Without this function it is possible to allocate
embeddedartists 11:2d4d65173195 77 // so much of the internal RAM that there is no free memory for the stack which
embeddedartists 11:2d4d65173195 78 // in turn causes the program to crash.
embeddedartists 11:2d4d65173195 79 uint32_t __reserved_stack_size() {
embeddedartists 11:2d4d65173195 80 return 0x3000; // Reserve 0x3000 bytes of the IRAM for the stack
embeddedartists 11:2d4d65173195 81 }
embeddedartists 11:2d4d65173195 82
embeddedartists 0:0fdadbc3d852 83 #elif defined(TOOLCHAIN_GCC_CR) /* CodeRed's RedSuite or LPCXpresso IDE */
embeddedartists 0:0fdadbc3d852 84
embeddedartists 0:0fdadbc3d852 85 // NOTE: This way of overriding the implementation of malloc in NEWLIB
embeddedartists 0:0fdadbc3d852 86 // will prevent the internal RAM from being used by malloc as
embeddedartists 0:0fdadbc3d852 87 // it only exposes the SDRAM.
embeddedartists 0:0fdadbc3d852 88
embeddedartists 0:0fdadbc3d852 89 // Dynamic memory allocation related syscall.
embeddedartists 0:0fdadbc3d852 90 extern "C" caddr_t _sbrk(int incr) {
embeddedartists 0:0fdadbc3d852 91 static unsigned char* heap = (unsigned char*)SDRAM_BASE;
embeddedartists 0:0fdadbc3d852 92 unsigned char* prev_heap = heap;
embeddedartists 0:0fdadbc3d852 93 unsigned char* new_heap = heap + incr;
embeddedartists 0:0fdadbc3d852 94
embeddedartists 0:0fdadbc3d852 95 if (okToUseSdramForHeap && !initialized) {
embeddedartists 0:0fdadbc3d852 96 sdram_init();
embeddedartists 0:0fdadbc3d852 97 }
embeddedartists 0:0fdadbc3d852 98 if (!okToUseSdramForHeap) {
embeddedartists 0:0fdadbc3d852 99 //errno = ENOMEM;
embeddedartists 0:0fdadbc3d852 100 return (caddr_t)-1;
embeddedartists 0:0fdadbc3d852 101 }
embeddedartists 0:0fdadbc3d852 102 if (new_heap >= (unsigned char*)(SDRAM_BASE + SDRAM_SIZE)) {
embeddedartists 0:0fdadbc3d852 103 //errno = ENOMEM;
embeddedartists 0:0fdadbc3d852 104 return (caddr_t)-1;
embeddedartists 0:0fdadbc3d852 105 }
embeddedartists 0:0fdadbc3d852 106
embeddedartists 0:0fdadbc3d852 107 heap = new_heap;
embeddedartists 0:0fdadbc3d852 108 return (caddr_t) prev_heap;
embeddedartists 0:0fdadbc3d852 109 }
embeddedartists 0:0fdadbc3d852 110 #endif
embeddedartists 0:0fdadbc3d852 111
embeddedartists 0:0fdadbc3d852 112 /******************************************************************************
embeddedartists 0:0fdadbc3d852 113 * Local Functions
embeddedartists 0:0fdadbc3d852 114 *****************************************************************************/
embeddedartists 0:0fdadbc3d852 115
embeddedartists 0:0fdadbc3d852 116 static void pinConfig(void)
embeddedartists 0:0fdadbc3d852 117 {
embeddedartists 0:0fdadbc3d852 118 LPC_IOCON->P3_0 |= 1; /* D0 @ P3.0 */
embeddedartists 0:0fdadbc3d852 119 LPC_IOCON->P3_1 |= 1; /* D1 @ P3.1 */
embeddedartists 0:0fdadbc3d852 120 LPC_IOCON->P3_2 |= 1; /* D2 @ P3.2 */
embeddedartists 0:0fdadbc3d852 121 LPC_IOCON->P3_3 |= 1; /* D3 @ P3.3 */
embeddedartists 0:0fdadbc3d852 122
embeddedartists 0:0fdadbc3d852 123 LPC_IOCON->P3_4 |= 1; /* D4 @ P3.4 */
embeddedartists 0:0fdadbc3d852 124 LPC_IOCON->P3_5 |= 1; /* D5 @ P3.5 */
embeddedartists 0:0fdadbc3d852 125 LPC_IOCON->P3_6 |= 1; /* D6 @ P3.6 */
embeddedartists 0:0fdadbc3d852 126 LPC_IOCON->P3_7 |= 1; /* D7 @ P3.7 */
embeddedartists 0:0fdadbc3d852 127
embeddedartists 0:0fdadbc3d852 128 LPC_IOCON->P3_8 |= 1; /* D8 @ P3.8 */
embeddedartists 0:0fdadbc3d852 129 LPC_IOCON->P3_9 |= 1; /* D9 @ P3.9 */
embeddedartists 0:0fdadbc3d852 130 LPC_IOCON->P3_10 |= 1; /* D10 @ P3.10 */
embeddedartists 0:0fdadbc3d852 131 LPC_IOCON->P3_11 |= 1; /* D11 @ P3.11 */
embeddedartists 0:0fdadbc3d852 132
embeddedartists 0:0fdadbc3d852 133 LPC_IOCON->P3_12 |= 1; /* D12 @ P3.12 */
embeddedartists 0:0fdadbc3d852 134 LPC_IOCON->P3_13 |= 1; /* D13 @ P3.13 */
embeddedartists 0:0fdadbc3d852 135 LPC_IOCON->P3_14 |= 1; /* D14 @ P3.14 */
embeddedartists 0:0fdadbc3d852 136 LPC_IOCON->P3_15 |= 1; /* D15 @ P3.15 */
embeddedartists 0:0fdadbc3d852 137
embeddedartists 0:0fdadbc3d852 138 LPC_IOCON->P3_16 |= 1; /* D16 @ P3.16 */
embeddedartists 0:0fdadbc3d852 139 LPC_IOCON->P3_17 |= 1; /* D17 @ P3.17 */
embeddedartists 0:0fdadbc3d852 140 LPC_IOCON->P3_18 |= 1; /* D18 @ P3.18 */
embeddedartists 0:0fdadbc3d852 141 LPC_IOCON->P3_19 |= 1; /* D19 @ P3.19 */
embeddedartists 0:0fdadbc3d852 142
embeddedartists 0:0fdadbc3d852 143 LPC_IOCON->P3_20 |= 1; /* D20 @ P3.20 */
embeddedartists 0:0fdadbc3d852 144 LPC_IOCON->P3_21 |= 1; /* D21 @ P3.21 */
embeddedartists 0:0fdadbc3d852 145 LPC_IOCON->P3_22 |= 1; /* D22 @ P3.22 */
embeddedartists 0:0fdadbc3d852 146 LPC_IOCON->P3_23 |= 1; /* D23 @ P3.23 */
embeddedartists 0:0fdadbc3d852 147
embeddedartists 0:0fdadbc3d852 148 LPC_IOCON->P3_24 |= 1; /* D24 @ P3.24 */
embeddedartists 0:0fdadbc3d852 149 LPC_IOCON->P3_25 |= 1; /* D25 @ P3.25 */
embeddedartists 0:0fdadbc3d852 150 LPC_IOCON->P3_26 |= 1; /* D26 @ P3.26 */
embeddedartists 0:0fdadbc3d852 151 LPC_IOCON->P3_27 |= 1; /* D27 @ P3.27 */
embeddedartists 0:0fdadbc3d852 152
embeddedartists 0:0fdadbc3d852 153 LPC_IOCON->P3_28 |= 1; /* D28 @ P3.28 */
embeddedartists 0:0fdadbc3d852 154 LPC_IOCON->P3_29 |= 1; /* D29 @ P3.29 */
embeddedartists 0:0fdadbc3d852 155 LPC_IOCON->P3_30 |= 1; /* D30 @ P3.30 */
embeddedartists 0:0fdadbc3d852 156 LPC_IOCON->P3_31 |= 1; /* D31 @ P3.31 */
embeddedartists 0:0fdadbc3d852 157
embeddedartists 0:0fdadbc3d852 158 LPC_IOCON->P4_0 |= 1; /* A0 @ P4.0 */
embeddedartists 0:0fdadbc3d852 159 LPC_IOCON->P4_1 |= 1; /* A1 @ P4.1 */
embeddedartists 0:0fdadbc3d852 160 LPC_IOCON->P4_2 |= 1; /* A2 @ P4.2 */
embeddedartists 0:0fdadbc3d852 161 LPC_IOCON->P4_3 |= 1; /* A3 @ P4.3 */
embeddedartists 0:0fdadbc3d852 162
embeddedartists 0:0fdadbc3d852 163 LPC_IOCON->P4_4 |= 1; /* A4 @ P4.4 */
embeddedartists 0:0fdadbc3d852 164 LPC_IOCON->P4_5 |= 1; /* A5 @ P4.5 */
embeddedartists 0:0fdadbc3d852 165 LPC_IOCON->P4_6 |= 1; /* A6 @ P4.6 */
embeddedartists 0:0fdadbc3d852 166 LPC_IOCON->P4_7 |= 1; /* A7 @ P4.7 */
embeddedartists 0:0fdadbc3d852 167
embeddedartists 0:0fdadbc3d852 168 LPC_IOCON->P4_8 |= 1; /* A8 @ P4.8 */
embeddedartists 0:0fdadbc3d852 169 LPC_IOCON->P4_9 |= 1; /* A9 @ P4.9 */
embeddedartists 0:0fdadbc3d852 170 LPC_IOCON->P4_10 |= 1; /* A10 @ P4.10 */
embeddedartists 0:0fdadbc3d852 171 LPC_IOCON->P4_11 |= 1; /* A11 @ P4.11 */
embeddedartists 0:0fdadbc3d852 172
embeddedartists 0:0fdadbc3d852 173 LPC_IOCON->P4_12 |= 1; /* A12 @ P4.12 */
embeddedartists 0:0fdadbc3d852 174 LPC_IOCON->P4_13 |= 1; /* A13 @ P4.13 */
embeddedartists 0:0fdadbc3d852 175 LPC_IOCON->P4_14 |= 1; /* A14 @ P4.14 */
embeddedartists 0:0fdadbc3d852 176 #if 0 // not used for SDRAM
embeddedartists 0:0fdadbc3d852 177 LPC_IOCON->P4_15 |= 1; /* A15 @ P4.15 */
embeddedartists 0:0fdadbc3d852 178
embeddedartists 0:0fdadbc3d852 179 LPC_IOCON->P4_16 |= 1; /* A16 @ P4.16 */
embeddedartists 0:0fdadbc3d852 180 LPC_IOCON->P4_17 |= 1; /* A17 @ P4.17 */
embeddedartists 0:0fdadbc3d852 181 LPC_IOCON->P4_18 |= 1; /* A18 @ P4.18 */
embeddedartists 0:0fdadbc3d852 182 LPC_IOCON->P4_19 |= 1; /* A19 @ P4.19 */
embeddedartists 0:0fdadbc3d852 183
embeddedartists 0:0fdadbc3d852 184 LPC_IOCON->P4_20 |= 1; /* A20 @ P4.20 */
embeddedartists 0:0fdadbc3d852 185 LPC_IOCON->P4_21 |= 1; /* A21 @ P4.21 */
embeddedartists 0:0fdadbc3d852 186 LPC_IOCON->P4_22 |= 1; /* A22 @ P4.22 */
embeddedartists 0:0fdadbc3d852 187 LPC_IOCON->P4_23 |= 1; /* A23 @ P4.23 */
embeddedartists 0:0fdadbc3d852 188 #endif
embeddedartists 0:0fdadbc3d852 189
embeddedartists 0:0fdadbc3d852 190 LPC_IOCON->P4_24 |= 1; /* OEN @ P4.24 */
embeddedartists 0:0fdadbc3d852 191 LPC_IOCON->P4_25 |= 1; /* WEN @ P4.25 */
embeddedartists 0:0fdadbc3d852 192 #if 0 // not used for SDRAM
embeddedartists 0:0fdadbc3d852 193 LPC_IOCON->P4_26 |= 1; /* BLSN[0] @ P4.26 */
embeddedartists 0:0fdadbc3d852 194 LPC_IOCON->P4_27 |= 1; /* BLSN[1] @ P4.27 */
embeddedartists 0:0fdadbc3d852 195
embeddedartists 0:0fdadbc3d852 196
embeddedartists 0:0fdadbc3d852 197 LPC_IOCON->P4_28 |= 1; /* BLSN[2] @ P4.28 */
embeddedartists 0:0fdadbc3d852 198 LPC_IOCON->P4_29 |= 1; /* BLSN[3] @ P4.29 */
embeddedartists 0:0fdadbc3d852 199 LPC_IOCON->P4_30 |= 1; /* CSN[0] @ P4.30 */
embeddedartists 0:0fdadbc3d852 200 LPC_IOCON->P4_31 |= 1; /* CSN[1] @ P4.31 */
embeddedartists 0:0fdadbc3d852 201 #endif
embeddedartists 0:0fdadbc3d852 202
embeddedartists 0:0fdadbc3d852 203 LPC_IOCON->P2_14 |= 1; /* CSN[2] @ P2.14 */
embeddedartists 0:0fdadbc3d852 204 LPC_IOCON->P2_15 |= 1; /* CSN[3] @ P2.15 */
embeddedartists 0:0fdadbc3d852 205
embeddedartists 0:0fdadbc3d852 206 LPC_IOCON->P2_16 |= 1; /* CASN @ P2.16 */
embeddedartists 0:0fdadbc3d852 207 LPC_IOCON->P2_17 |= 1; /* RASN @ P2.17 */
embeddedartists 0:0fdadbc3d852 208 LPC_IOCON->P2_18 |= 1; /* CLK[0] @ P2.18 */
embeddedartists 0:0fdadbc3d852 209 #if 0 // not used for SDRAM
embeddedartists 0:0fdadbc3d852 210 LPC_IOCON->P2_19 |= 1; /* CLK[1] @ P2.19 */
embeddedartists 0:0fdadbc3d852 211 #endif
embeddedartists 0:0fdadbc3d852 212
embeddedartists 0:0fdadbc3d852 213 LPC_IOCON->P2_20 |= 1; /* DYCSN[0] @ P2.20 */
embeddedartists 0:0fdadbc3d852 214 #if 0 // not used for SDRAM
embeddedartists 0:0fdadbc3d852 215 LPC_IOCON->P2_21 |= 1; /* DYCSN[1] @ P2.21 */
embeddedartists 0:0fdadbc3d852 216 LPC_IOCON->P2_22 |= 1; /* DYCSN[2] @ P2.22 */
embeddedartists 0:0fdadbc3d852 217 LPC_IOCON->P2_23 |= 1; /* DYCSN[3] @ P2.23 */
embeddedartists 0:0fdadbc3d852 218 #endif
embeddedartists 0:0fdadbc3d852 219
embeddedartists 0:0fdadbc3d852 220 LPC_IOCON->P2_24 |= 1; /* CKE[0] @ P2.24 */
embeddedartists 0:0fdadbc3d852 221 #if 0 // not used for SDRAM
embeddedartists 0:0fdadbc3d852 222 LPC_IOCON->P2_25 |= 1; /* CKE[1] @ P2.25 */
embeddedartists 0:0fdadbc3d852 223 LPC_IOCON->P2_26 |= 1; /* CKE[2] @ P2.26 */
embeddedartists 0:0fdadbc3d852 224 LPC_IOCON->P2_27 |= 1; /* CKE[3] @ P2.27 */
embeddedartists 0:0fdadbc3d852 225 #endif
embeddedartists 0:0fdadbc3d852 226
embeddedartists 0:0fdadbc3d852 227 LPC_IOCON->P2_28 |= 1; /* DQM[0] @ P2.28 */
embeddedartists 0:0fdadbc3d852 228 LPC_IOCON->P2_29 |= 1; /* DQM[1] @ P2.29 */
embeddedartists 0:0fdadbc3d852 229 LPC_IOCON->P2_30 |= 1; /* DQM[2] @ P2.30 */
embeddedartists 0:0fdadbc3d852 230 LPC_IOCON->P2_31 |= 1; /* DQM[3] @ P2.31 */
embeddedartists 0:0fdadbc3d852 231 }
embeddedartists 0:0fdadbc3d852 232
embeddedartists 0:0fdadbc3d852 233
embeddedartists 0:0fdadbc3d852 234 static uint32_t sdram_test( void )
embeddedartists 0:0fdadbc3d852 235 {
embeddedartists 0:0fdadbc3d852 236 volatile uint32_t *wr_ptr;
embeddedartists 0:0fdadbc3d852 237 volatile uint16_t *short_wr_ptr;
embeddedartists 0:0fdadbc3d852 238 uint32_t data;
embeddedartists 0:0fdadbc3d852 239 uint32_t i, j;
embeddedartists 0:0fdadbc3d852 240
embeddedartists 0:0fdadbc3d852 241 wr_ptr = (uint32_t *)SDRAM_BASE;
embeddedartists 0:0fdadbc3d852 242 short_wr_ptr = (uint16_t *)wr_ptr;
embeddedartists 0:0fdadbc3d852 243 /* Clear content before 16 bit access test */
embeddedartists 0:0fdadbc3d852 244 // for (i = 0; i < SDRAM_SIZE/4; i++)
embeddedartists 0:0fdadbc3d852 245 // {
embeddedartists 0:0fdadbc3d852 246 // *wr_ptr++ = 0;
embeddedartists 0:0fdadbc3d852 247 // }
embeddedartists 0:0fdadbc3d852 248
embeddedartists 0:0fdadbc3d852 249 /* 16 bit write */
embeddedartists 0:0fdadbc3d852 250 for (i = 0; i < SDRAM_SIZE/0x40000; i++)
embeddedartists 0:0fdadbc3d852 251 {
embeddedartists 0:0fdadbc3d852 252 for (j = 0; j < 0x100; j++)
embeddedartists 0:0fdadbc3d852 253 {
embeddedartists 0:0fdadbc3d852 254 *short_wr_ptr++ = (i + j);
embeddedartists 0:0fdadbc3d852 255 *short_wr_ptr++ = (i + j) + 1;
embeddedartists 0:0fdadbc3d852 256 }
embeddedartists 0:0fdadbc3d852 257 }
embeddedartists 0:0fdadbc3d852 258
embeddedartists 0:0fdadbc3d852 259 /* Verifying */
embeddedartists 0:0fdadbc3d852 260 wr_ptr = (uint32_t *)SDRAM_BASE;
embeddedartists 0:0fdadbc3d852 261 for (i = 0; i < SDRAM_SIZE/0x40000; i++)
embeddedartists 0:0fdadbc3d852 262 {
embeddedartists 0:0fdadbc3d852 263 for (j = 0; j < 0x100; j++)
embeddedartists 0:0fdadbc3d852 264 {
embeddedartists 0:0fdadbc3d852 265 data = *wr_ptr;
embeddedartists 0:0fdadbc3d852 266 if (data != (((((i + j) + 1) & 0xFFFF) << 16) | ((i + j) & 0xFFFF)))
embeddedartists 0:0fdadbc3d852 267 {
embeddedartists 0:0fdadbc3d852 268 return 0x0;
embeddedartists 0:0fdadbc3d852 269 }
embeddedartists 0:0fdadbc3d852 270 wr_ptr++;
embeddedartists 0:0fdadbc3d852 271 }
embeddedartists 0:0fdadbc3d852 272 }
embeddedartists 0:0fdadbc3d852 273 return 0x1;
embeddedartists 0:0fdadbc3d852 274 }
embeddedartists 0:0fdadbc3d852 275
embeddedartists 0:0fdadbc3d852 276 static uint32_t find_cmddly(void)
embeddedartists 0:0fdadbc3d852 277 {
embeddedartists 0:0fdadbc3d852 278 uint32_t cmddly, cmddlystart, cmddlyend, dwtemp;
embeddedartists 0:0fdadbc3d852 279 uint32_t ppass = 0x0, pass = 0x0;
embeddedartists 0:0fdadbc3d852 280
embeddedartists 0:0fdadbc3d852 281 cmddly = 0x0;
embeddedartists 0:0fdadbc3d852 282 cmddlystart = cmddlyend = 0xFF;
embeddedartists 0:0fdadbc3d852 283
embeddedartists 0:0fdadbc3d852 284 while (cmddly < 32)
embeddedartists 0:0fdadbc3d852 285 {
embeddedartists 0:0fdadbc3d852 286 dwtemp = LPC_SC->EMCDLYCTL & ~0x1F;
embeddedartists 0:0fdadbc3d852 287 LPC_SC->EMCDLYCTL = dwtemp | cmddly;
embeddedartists 0:0fdadbc3d852 288
embeddedartists 0:0fdadbc3d852 289 if (sdram_test() == 0x1)
embeddedartists 0:0fdadbc3d852 290 {
embeddedartists 0:0fdadbc3d852 291 /* Test passed */
embeddedartists 0:0fdadbc3d852 292 if (cmddlystart == 0xFF)
embeddedartists 0:0fdadbc3d852 293 {
embeddedartists 0:0fdadbc3d852 294 cmddlystart = cmddly;
embeddedartists 0:0fdadbc3d852 295 }
embeddedartists 0:0fdadbc3d852 296 ppass = 0x1;
embeddedartists 0:0fdadbc3d852 297 }
embeddedartists 0:0fdadbc3d852 298 else
embeddedartists 0:0fdadbc3d852 299 {
embeddedartists 0:0fdadbc3d852 300 /* Test failed */
embeddedartists 0:0fdadbc3d852 301 if (ppass == 1)
embeddedartists 0:0fdadbc3d852 302 {
embeddedartists 0:0fdadbc3d852 303 cmddlyend = cmddly;
embeddedartists 0:0fdadbc3d852 304 pass = 0x1;
embeddedartists 0:0fdadbc3d852 305 ppass = 0x0;
embeddedartists 0:0fdadbc3d852 306 }
embeddedartists 0:0fdadbc3d852 307 }
embeddedartists 0:0fdadbc3d852 308
embeddedartists 0:0fdadbc3d852 309 /* Try next value */
embeddedartists 0:0fdadbc3d852 310 cmddly++;
embeddedartists 0:0fdadbc3d852 311 }
embeddedartists 0:0fdadbc3d852 312
embeddedartists 0:0fdadbc3d852 313 /* If the test passed, the we can use the average of the min and max values to get an optimal DQSIN delay */
embeddedartists 0:0fdadbc3d852 314 if (pass == 0x1)
embeddedartists 0:0fdadbc3d852 315 {
embeddedartists 0:0fdadbc3d852 316 cmddly = (cmddlystart + cmddlyend) / 2;
embeddedartists 0:0fdadbc3d852 317 }
embeddedartists 0:0fdadbc3d852 318 else if (ppass == 0x1)
embeddedartists 0:0fdadbc3d852 319 {
embeddedartists 0:0fdadbc3d852 320 cmddly = (cmddlystart + 0x1F) / 2;
embeddedartists 0:0fdadbc3d852 321 }
embeddedartists 0:0fdadbc3d852 322 else
embeddedartists 0:0fdadbc3d852 323 {
embeddedartists 0:0fdadbc3d852 324 /* A working value couldn't be found, just pick something safe so the system doesn't become unstable */
embeddedartists 0:0fdadbc3d852 325 cmddly = 0x10;
embeddedartists 0:0fdadbc3d852 326 }
embeddedartists 0:0fdadbc3d852 327
embeddedartists 0:0fdadbc3d852 328 dwtemp = LPC_SC->EMCDLYCTL & ~0x1F;
embeddedartists 0:0fdadbc3d852 329 LPC_SC->EMCDLYCTL = dwtemp | cmddly;
embeddedartists 0:0fdadbc3d852 330
embeddedartists 0:0fdadbc3d852 331 return (pass | ppass);
embeddedartists 0:0fdadbc3d852 332 }
embeddedartists 0:0fdadbc3d852 333
embeddedartists 0:0fdadbc3d852 334 static uint32_t find_fbclkdly(void)
embeddedartists 0:0fdadbc3d852 335 {
embeddedartists 0:0fdadbc3d852 336 uint32_t fbclkdly, fbclkdlystart, fbclkdlyend, dwtemp;
embeddedartists 0:0fdadbc3d852 337 uint32_t ppass = 0x0, pass = 0x0;
embeddedartists 0:0fdadbc3d852 338
embeddedartists 0:0fdadbc3d852 339 fbclkdly = 0x0;
embeddedartists 0:0fdadbc3d852 340 fbclkdlystart = fbclkdlyend = 0xFF;
embeddedartists 0:0fdadbc3d852 341
embeddedartists 0:0fdadbc3d852 342 while (fbclkdly < 32)
embeddedartists 0:0fdadbc3d852 343 {
embeddedartists 0:0fdadbc3d852 344 dwtemp = LPC_SC->EMCDLYCTL & ~0x1F00;
embeddedartists 0:0fdadbc3d852 345 LPC_SC->EMCDLYCTL = dwtemp | (fbclkdly << 8);
embeddedartists 0:0fdadbc3d852 346
embeddedartists 0:0fdadbc3d852 347 if (sdram_test() == 0x1)
embeddedartists 0:0fdadbc3d852 348 {
embeddedartists 0:0fdadbc3d852 349 /* Test passed */
embeddedartists 0:0fdadbc3d852 350 if (fbclkdlystart == 0xFF)
embeddedartists 0:0fdadbc3d852 351 {
embeddedartists 0:0fdadbc3d852 352 fbclkdlystart = fbclkdly;
embeddedartists 0:0fdadbc3d852 353 }
embeddedartists 0:0fdadbc3d852 354 ppass = 0x1;
embeddedartists 0:0fdadbc3d852 355 }
embeddedartists 0:0fdadbc3d852 356 else
embeddedartists 0:0fdadbc3d852 357 {
embeddedartists 0:0fdadbc3d852 358 /* Test failed */
embeddedartists 0:0fdadbc3d852 359 if (ppass == 1)
embeddedartists 0:0fdadbc3d852 360 {
embeddedartists 0:0fdadbc3d852 361 fbclkdlyend = fbclkdly;
embeddedartists 0:0fdadbc3d852 362 pass = 0x1;
embeddedartists 0:0fdadbc3d852 363 ppass = 0x0;
embeddedartists 0:0fdadbc3d852 364 }
embeddedartists 0:0fdadbc3d852 365 }
embeddedartists 0:0fdadbc3d852 366
embeddedartists 0:0fdadbc3d852 367 /* Try next value */
embeddedartists 0:0fdadbc3d852 368 fbclkdly++;
embeddedartists 0:0fdadbc3d852 369 }
embeddedartists 0:0fdadbc3d852 370
embeddedartists 0:0fdadbc3d852 371 /* If the test passed, the we can use the average of the min and max values to get an optimal DQSIN delay */
embeddedartists 0:0fdadbc3d852 372 if (pass == 0x1)
embeddedartists 0:0fdadbc3d852 373 {
embeddedartists 0:0fdadbc3d852 374 fbclkdly = (fbclkdlystart + fbclkdlyend) / 2;
embeddedartists 0:0fdadbc3d852 375 }
embeddedartists 0:0fdadbc3d852 376 else if (ppass == 0x1)
embeddedartists 0:0fdadbc3d852 377 {
embeddedartists 0:0fdadbc3d852 378 fbclkdly = (fbclkdlystart + 0x1F) / 2;
embeddedartists 0:0fdadbc3d852 379 }
embeddedartists 0:0fdadbc3d852 380 else
embeddedartists 0:0fdadbc3d852 381 {
embeddedartists 0:0fdadbc3d852 382 /* A working value couldn't be found, just pick something safe so the system doesn't become unstable */
embeddedartists 0:0fdadbc3d852 383 fbclkdly = 0x10;
embeddedartists 0:0fdadbc3d852 384 }
embeddedartists 0:0fdadbc3d852 385
embeddedartists 0:0fdadbc3d852 386 dwtemp = LPC_SC->EMCDLYCTL & ~0x1F00;
embeddedartists 0:0fdadbc3d852 387 LPC_SC->EMCDLYCTL = dwtemp | (fbclkdly << 8);
embeddedartists 0:0fdadbc3d852 388
embeddedartists 0:0fdadbc3d852 389 return (pass | ppass);
embeddedartists 0:0fdadbc3d852 390 }
embeddedartists 0:0fdadbc3d852 391
embeddedartists 0:0fdadbc3d852 392 static uint32_t calibration( void )
embeddedartists 0:0fdadbc3d852 393 {
embeddedartists 0:0fdadbc3d852 394 uint32_t dwtemp, i;
embeddedartists 0:0fdadbc3d852 395 uint32_t cnt = 0;
embeddedartists 0:0fdadbc3d852 396
embeddedartists 0:0fdadbc3d852 397 for (i = 0; i < 10; i++)
embeddedartists 0:0fdadbc3d852 398 {
embeddedartists 0:0fdadbc3d852 399 dwtemp = LPC_SC->EMCCAL & ~0x4000;
embeddedartists 0:0fdadbc3d852 400 LPC_SC->EMCCAL = dwtemp | 0x4000;
embeddedartists 0:0fdadbc3d852 401
embeddedartists 0:0fdadbc3d852 402 dwtemp = LPC_SC->EMCCAL;
embeddedartists 0:0fdadbc3d852 403 while ((dwtemp & 0x8000) == 0x0000)
embeddedartists 0:0fdadbc3d852 404 {
embeddedartists 0:0fdadbc3d852 405 dwtemp = LPC_SC->EMCCAL;
embeddedartists 0:0fdadbc3d852 406 }
embeddedartists 0:0fdadbc3d852 407 cnt += (dwtemp & 0xFF);
embeddedartists 0:0fdadbc3d852 408 }
embeddedartists 0:0fdadbc3d852 409 return (cnt / 10);
embeddedartists 0:0fdadbc3d852 410 }
embeddedartists 0:0fdadbc3d852 411
embeddedartists 0:0fdadbc3d852 412 /******************************************************************************
embeddedartists 0:0fdadbc3d852 413 * Public Functions
embeddedartists 0:0fdadbc3d852 414 *****************************************************************************/
embeddedartists 0:0fdadbc3d852 415
embeddedartists 0:0fdadbc3d852 416
embeddedartists 0:0fdadbc3d852 417 void adjust_timing( void )
embeddedartists 0:0fdadbc3d852 418 {
embeddedartists 0:0fdadbc3d852 419 uint32_t dwtemp, cmddly, fbclkdly;
embeddedartists 0:0fdadbc3d852 420
embeddedartists 0:0fdadbc3d852 421 /* Current value */
embeddedartists 0:0fdadbc3d852 422 ringosccount[1] = calibration();
embeddedartists 0:0fdadbc3d852 423
embeddedartists 0:0fdadbc3d852 424 dwtemp = LPC_SC->EMCDLYCTL;
embeddedartists 0:0fdadbc3d852 425 cmddly = ((dwtemp & 0x1F) * ringosccount[0] / ringosccount[1]) & 0x1F;
embeddedartists 0:0fdadbc3d852 426 fbclkdly = ((dwtemp & 0x1F00) * ringosccount[0] / ringosccount[1]) & 0x1F00;
embeddedartists 0:0fdadbc3d852 427 LPC_SC->EMCDLYCTL = (dwtemp & ~0x1F1F) | fbclkdly | cmddly;
embeddedartists 0:0fdadbc3d852 428 }
embeddedartists 0:0fdadbc3d852 429
embeddedartists 0:0fdadbc3d852 430 /******************************************************************************
embeddedartists 0:0fdadbc3d852 431 *
embeddedartists 0:0fdadbc3d852 432 * Description:
embeddedartists 0:0fdadbc3d852 433 * Initialize the SDRAM
embeddedartists 0:0fdadbc3d852 434 *
embeddedartists 0:0fdadbc3d852 435 *****************************************************************************/
embeddedartists 0:0fdadbc3d852 436 uint32_t sdram_init (void)
embeddedartists 0:0fdadbc3d852 437 {
embeddedartists 0:0fdadbc3d852 438 uint32_t i;
embeddedartists 0:0fdadbc3d852 439 uint32_t dwtemp = 0;
embeddedartists 0:0fdadbc3d852 440 //uint16_t wtemp = 0;
embeddedartists 0:0fdadbc3d852 441
embeddedartists 0:0fdadbc3d852 442 if (initialized) {
embeddedartists 0:0fdadbc3d852 443 return 0;
embeddedartists 0:0fdadbc3d852 444 }
embeddedartists 0:0fdadbc3d852 445
embeddedartists 0:0fdadbc3d852 446 LPC_SC->PCONP |= 0x00000800;
embeddedartists 0:0fdadbc3d852 447 LPC_SC->EMCDLYCTL = 0x00001010;
embeddedartists 0:0fdadbc3d852 448 LPC_EMC->Control = 0x00000001;
embeddedartists 0:0fdadbc3d852 449 LPC_EMC->Config = 0x00000000;
embeddedartists 0:0fdadbc3d852 450
embeddedartists 0:0fdadbc3d852 451 pinConfig(); //Full 32-bit Data bus, 24-bit Address
embeddedartists 0:0fdadbc3d852 452
embeddedartists 0:0fdadbc3d852 453 /* Configure memory layout, but MUST DISABLE BUFFERs during configuration */
embeddedartists 0:0fdadbc3d852 454 /* 256MB, 8Mx32, 4 banks, row=12, column=9 */
embeddedartists 0:0fdadbc3d852 455 LPC_EMC->DynamicConfig0 = 0x00004480;
embeddedartists 0:0fdadbc3d852 456
embeddedartists 0:0fdadbc3d852 457 /*Configure timing for ISSI IS4x32800D SDRAM*/
embeddedartists 0:0fdadbc3d852 458
embeddedartists 0:0fdadbc3d852 459 #if (SDRAM_SPEED==SDRAM_SPEED_48)
embeddedartists 0:0fdadbc3d852 460 //Timing for 48MHz Bus
embeddedartists 0:0fdadbc3d852 461 LPC_EMC->DynamicRasCas0 = 0x00000201; /* 1 RAS, 2 CAS latency */
embeddedartists 0:0fdadbc3d852 462 LPC_EMC->DynamicReadConfig = 0x00000001; /* Command delayed strategy, using EMCCLKDELAY */
embeddedartists 0:0fdadbc3d852 463 LPC_EMC->DynamicRP = 0x00000000; /* ( n + 1 ) -> 1 clock cycles */
embeddedartists 0:0fdadbc3d852 464 LPC_EMC->DynamicRAS = 0x00000002; /* ( n + 1 ) -> 3 clock cycles */
embeddedartists 0:0fdadbc3d852 465 LPC_EMC->DynamicSREX = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 466 LPC_EMC->DynamicAPR = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 467 LPC_EMC->DynamicDAL = 0x00000002; /* ( n ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 468 LPC_EMC->DynamicWR = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 469 LPC_EMC->DynamicRC = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 470 LPC_EMC->DynamicRFC = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 471 LPC_EMC->DynamicXSR = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 472 LPC_EMC->DynamicRRD = 0x00000000; /* ( n + 1 ) -> 1 clock cycles */
embeddedartists 0:0fdadbc3d852 473 LPC_EMC->DynamicMRD = 0x00000000; /* ( n + 1 ) -> 1 clock cycles */
embeddedartists 0:0fdadbc3d852 474 #elif (SDRAM_SPEED==SDRAM_SPEED_50)
embeddedartists 0:0fdadbc3d852 475 //Timing for 50MHz Bus (with 100MHz M3 Core)
embeddedartists 0:0fdadbc3d852 476 LPC_EMC->DynamicRasCas0 = 0x00000201; /* 1 RAS, 2 CAS latency */
embeddedartists 0:0fdadbc3d852 477 LPC_EMC->DynamicReadConfig = 0x00000001; /* Command delayed strategy, using EMCCLKDELAY */
embeddedartists 0:0fdadbc3d852 478 LPC_EMC->DynamicRP = 0x00000000; /* ( n + 1 ) -> 1 clock cycles */
embeddedartists 0:0fdadbc3d852 479 LPC_EMC->DynamicRAS = 0x00000002; /* ( n + 1 ) -> 3 clock cycles */
embeddedartists 0:0fdadbc3d852 480 LPC_EMC->DynamicSREX = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 481 LPC_EMC->DynamicAPR = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 482 LPC_EMC->DynamicDAL = 0x00000002; /* ( n ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 483 LPC_EMC->DynamicWR = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 484 LPC_EMC->DynamicRC = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 485 LPC_EMC->DynamicRFC = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 486 LPC_EMC->DynamicXSR = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 487 LPC_EMC->DynamicRRD = 0x00000000; /* ( n + 1 ) -> 1 clock cycles */
embeddedartists 0:0fdadbc3d852 488 LPC_EMC->DynamicMRD = 0x00000000; /* ( n + 1 ) -> 1 clock cycles */
embeddedartists 0:0fdadbc3d852 489 #elif (SDRAM_SPEED==SDRAM_SPEED_60)
embeddedartists 0:0fdadbc3d852 490 //Timing for 60 MHz Bus (same as 72MHz)
embeddedartists 0:0fdadbc3d852 491 LPC_EMC->DynamicRasCas0 = 0x00000202; /* 2 RAS, 2 CAS latency */
embeddedartists 0:0fdadbc3d852 492 LPC_EMC->DynamicReadConfig = 0x00000001; /* Command delayed strategy, using EMCCLKDELAY */
embeddedartists 0:0fdadbc3d852 493 LPC_EMC->DynamicRP = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 494 LPC_EMC->DynamicRAS = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 495 LPC_EMC->DynamicSREX = 0x00000005; /* ( n + 1 ) -> 6 clock cycles */
embeddedartists 0:0fdadbc3d852 496 LPC_EMC->DynamicAPR = 0x00000002; /* ( n + 1 ) -> 3 clock cycles */
embeddedartists 0:0fdadbc3d852 497 LPC_EMC->DynamicDAL = 0x00000003; /* ( n ) -> 3 clock cycles */
embeddedartists 0:0fdadbc3d852 498 LPC_EMC->DynamicWR = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 499 LPC_EMC->DynamicRC = 0x00000004; /* ( n + 1 ) -> 5 clock cycles */
embeddedartists 0:0fdadbc3d852 500 LPC_EMC->DynamicRFC = 0x00000004; /* ( n + 1 ) -> 5 clock cycles */
embeddedartists 0:0fdadbc3d852 501 LPC_EMC->DynamicXSR = 0x00000005; /* ( n + 1 ) -> 6 clock cycles */
embeddedartists 0:0fdadbc3d852 502 LPC_EMC->DynamicRRD = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 503 LPC_EMC->DynamicMRD = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 504 #elif (SDRAM_SPEED==SDRAM_SPEED_72)
embeddedartists 0:0fdadbc3d852 505 //Timing for 72 MHz Bus
embeddedartists 0:0fdadbc3d852 506 LPC_EMC->DynamicRasCas0 = 0x00000202; /* 2 RAS, 2 CAS latency */
embeddedartists 0:0fdadbc3d852 507 LPC_EMC->DynamicReadConfig = 0x00000001; /* Command delayed strategy, using EMCCLKDELAY */
embeddedartists 0:0fdadbc3d852 508 LPC_EMC->DynamicRP = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 509 LPC_EMC->DynamicRAS = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 510 LPC_EMC->DynamicSREX = 0x00000005; /* ( n + 1 ) -> 6 clock cycles */
embeddedartists 0:0fdadbc3d852 511 LPC_EMC->DynamicAPR = 0x00000002; /* ( n + 1 ) -> 3 clock cycles */
embeddedartists 0:0fdadbc3d852 512 LPC_EMC->DynamicDAL = 0x00000003; /* ( n ) -> 3 clock cycles */
embeddedartists 0:0fdadbc3d852 513 LPC_EMC->DynamicWR = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 514 LPC_EMC->DynamicRC = 0x00000004; /* ( n + 1 ) -> 5 clock cycles */
embeddedartists 0:0fdadbc3d852 515 LPC_EMC->DynamicRFC = 0x00000004; /* ( n + 1 ) -> 5 clock cycles */
embeddedartists 0:0fdadbc3d852 516 LPC_EMC->DynamicXSR = 0x00000005; /* ( n + 1 ) -> 6 clock cycles */
embeddedartists 0:0fdadbc3d852 517 LPC_EMC->DynamicRRD = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 518 LPC_EMC->DynamicMRD = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 519 #elif (SDRAM_SPEED==SDRAM_SPEED_80)
embeddedartists 0:0fdadbc3d852 520 //Timing for 80 MHz Bus (same as 72MHz)
embeddedartists 0:0fdadbc3d852 521 LPC_EMC->DynamicRasCas0 = 0x00000202; /* 2 RAS, 2 CAS latency */
embeddedartists 0:0fdadbc3d852 522 LPC_EMC->DynamicReadConfig = 0x00000001; /* Command delayed strategy, using EMCCLKDELAY */
embeddedartists 0:0fdadbc3d852 523 LPC_EMC->DynamicRP = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 524 LPC_EMC->DynamicRAS = 0x00000003; /* ( n + 1 ) -> 4 clock cycles */
embeddedartists 0:0fdadbc3d852 525 LPC_EMC->DynamicSREX = 0x00000005; /* ( n + 1 ) -> 6 clock cycles */
embeddedartists 0:0fdadbc3d852 526 LPC_EMC->DynamicAPR = 0x00000002; /* ( n + 1 ) -> 3 clock cycles */
embeddedartists 0:0fdadbc3d852 527 LPC_EMC->DynamicDAL = 0x00000003; /* ( n ) -> 3 clock cycles */
embeddedartists 0:0fdadbc3d852 528 LPC_EMC->DynamicWR = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 529 LPC_EMC->DynamicRC = 0x00000004; /* ( n + 1 ) -> 5 clock cycles */
embeddedartists 0:0fdadbc3d852 530 LPC_EMC->DynamicRFC = 0x00000004; /* ( n + 1 ) -> 5 clock cycles */
embeddedartists 0:0fdadbc3d852 531 LPC_EMC->DynamicXSR = 0x00000005; /* ( n + 1 ) -> 6 clock cycles */
embeddedartists 0:0fdadbc3d852 532 LPC_EMC->DynamicRRD = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 533 LPC_EMC->DynamicMRD = 0x00000001; /* ( n + 1 ) -> 2 clock cycles */
embeddedartists 0:0fdadbc3d852 534 #else
embeddedartists 0:0fdadbc3d852 535 #error UNSUPPORTED SDRAM FREQ
embeddedartists 0:0fdadbc3d852 536 #endif
embeddedartists 0:0fdadbc3d852 537
embeddedartists 0:0fdadbc3d852 538 LPC_EMC->DynamicControl = 0x00000183; /* Issue NOP command */
embeddedartists 0:0fdadbc3d852 539 wait(0.2); /* wait 200ms */
embeddedartists 0:0fdadbc3d852 540 LPC_EMC->DynamicControl = 0x00000103; /* Issue PALL command */
embeddedartists 0:0fdadbc3d852 541 LPC_EMC->DynamicRefresh = 0x00000002; /* ( n * 16 ) -> 32 clock cycles */
embeddedartists 0:0fdadbc3d852 542 for(i = 0; i < 0x80; i++); /* wait 128 AHB clock cycles */
embeddedartists 0:0fdadbc3d852 543
embeddedartists 0:0fdadbc3d852 544
embeddedartists 0:0fdadbc3d852 545 #if (SDRAM_SPEED==SDRAM_SPEED_48)
embeddedartists 0:0fdadbc3d852 546 //Timing for 48MHz Bus
embeddedartists 0:0fdadbc3d852 547 LPC_EMC->DynamicRefresh = 0x0000002E; /* ( n * 16 ) -> 736 clock cycles -> 15.330uS at 48MHz <= 15.625uS ( 64ms / 4096 row ) */
embeddedartists 0:0fdadbc3d852 548 #elif (SDRAM_SPEED==SDRAM_SPEED_50)
embeddedartists 0:0fdadbc3d852 549 //Timing for 50MHz Bus
embeddedartists 0:0fdadbc3d852 550 LPC_EMC->DynamicRefresh = 0x0000003A; /* ( n * 16 ) -> 768 clock cycles -> 15.360uS at 50MHz <= 15.625uS ( 64ms / 4096 row ) */
embeddedartists 0:0fdadbc3d852 551 #elif (SDRAM_SPEED==SDRAM_SPEED_60)
embeddedartists 0:0fdadbc3d852 552 //Timing for 60MHz Bus
embeddedartists 0:0fdadbc3d852 553 LPC_EMC->DynamicRefresh = 0x0000003A; /* ( n * 16 ) -> 928 clock cycles -> 15.466uS at 60MHz <= 15.625uS ( 64ms / 4096 row ) */
embeddedartists 0:0fdadbc3d852 554 #elif (SDRAM_SPEED==SDRAM_SPEED_72)
embeddedartists 0:0fdadbc3d852 555 //Timing for 72MHz Bus
embeddedartists 0:0fdadbc3d852 556 LPC_EMC->DynamicRefresh = 0x00000046; /* ( n * 16 ) -> 1120 clock cycles -> 15.556uS at 72MHz <= 15.625uS ( 64ms / 4096 row ) */
embeddedartists 0:0fdadbc3d852 557 #elif (SDRAM_SPEED==SDRAM_SPEED_80)
embeddedartists 0:0fdadbc3d852 558 //Timing for 80MHz Bus
embeddedartists 0:0fdadbc3d852 559 LPC_EMC->DynamicRefresh = 0x0000004E; /* ( n * 16 ) -> 1248 clock cycles -> 15.600uS at 80MHz <= 15.625uS ( 64ms / 4096 row ) */
embeddedartists 0:0fdadbc3d852 560 #else
embeddedartists 0:0fdadbc3d852 561 #error UNSUPPORTED SDRAM FREQ
embeddedartists 0:0fdadbc3d852 562 #endif
embeddedartists 0:0fdadbc3d852 563
embeddedartists 0:0fdadbc3d852 564 LPC_EMC->DynamicControl = 0x00000083; /* Issue MODE command */
embeddedartists 0:0fdadbc3d852 565 //Timing for 48/60/72MHZ Bus
embeddedartists 0:0fdadbc3d852 566 dwtemp = *((volatile uint32_t *)(SDRAM_BASE | (0x22<<(2+2+9)))); /* 4 burst, 2 CAS latency */
embeddedartists 0:0fdadbc3d852 567 dwtemp = dwtemp;
embeddedartists 0:0fdadbc3d852 568 LPC_EMC->DynamicControl = 0x00000000; /* Issue NORMAL command */
embeddedartists 0:0fdadbc3d852 569 //[re]enable buffers
embeddedartists 0:0fdadbc3d852 570 LPC_EMC->DynamicConfig0 = 0x00084480; /* 256MB, 8Mx32, 4 banks, row=12, column=9 */
embeddedartists 0:0fdadbc3d852 571
embeddedartists 0:0fdadbc3d852 572 /* Nominal value */
embeddedartists 0:0fdadbc3d852 573 ringosccount[0] = calibration();
embeddedartists 0:0fdadbc3d852 574
embeddedartists 0:0fdadbc3d852 575 if (find_cmddly() == 0x0)
embeddedartists 0:0fdadbc3d852 576 {
embeddedartists 0:0fdadbc3d852 577 //while (1); /* fatal error */
embeddedartists 0:0fdadbc3d852 578 return 1;//FALSE;
embeddedartists 0:0fdadbc3d852 579 }
embeddedartists 0:0fdadbc3d852 580
embeddedartists 0:0fdadbc3d852 581 if (find_fbclkdly() == 0x0)
embeddedartists 0:0fdadbc3d852 582 {
embeddedartists 0:0fdadbc3d852 583 //while (1); /* fatal error */
embeddedartists 0:0fdadbc3d852 584 return 1;//FALSE;
embeddedartists 0:0fdadbc3d852 585 }
embeddedartists 0:0fdadbc3d852 586
embeddedartists 0:0fdadbc3d852 587 adjust_timing();
embeddedartists 0:0fdadbc3d852 588
embeddedartists 0:0fdadbc3d852 589 initialized = true;
embeddedartists 0:0fdadbc3d852 590
embeddedartists 0:0fdadbc3d852 591 return 0;//TRUE;
embeddedartists 0:0fdadbc3d852 592 }
embeddedartists 0:0fdadbc3d852 593
embeddedartists 0:0fdadbc3d852 594 void sdram_disableMallocSdram()
embeddedartists 0:0fdadbc3d852 595 {
embeddedartists 0:0fdadbc3d852 596 okToUseSdramForHeap = false;
embeddedartists 0:0fdadbc3d852 597 }
embeddedartists 0:0fdadbc3d852 598
embeddedartists 0:0fdadbc3d852 599