LPC1788FBD208笔记(002):SDRAM实验
平台:
MCU————— LPC1788FBD208
SDRAM———— IS42S16160D
XTAL ———— 12.0000M
IS42S16160D挂到LPC1788的EMC_DYCS0,即起始地址为0xA0000000,IS42S16160D大小是256MBit,因此SIZE是0x02000000。
实验依然是依托NXP官方发布的固件库。不过官方的例程用的是K4S561632J和MT48LC8M32LFB5这两种SDRAM,和我现在手头的IS42S16160D都不一样,要做些修改。IS42S16160D和K4S561632J类似,但是用K4S561632J的例程不能正常初始化IS42S16160D。折腾了一晚上,才发现模式寄存器的写法不一样。
参照
http://hi.baidu.com/chinleo/blog/item/1df78eefdf4a2f26acafd5ca.html,SDRAM初始化流程里尤其要注意的就是模式寄存器的设置。
IS42S16160D的模式寄存器要如下配置:
Dummy = *((volatile uint32_t *)(SDRAM_BASE_ADDR | (0x33<<12)));
而官方例程中K4S561632J的模式寄存器配置代码如下:
Dummy = *((volatile uint32_t *)(SDRAM_BASE_ADDR | (
0x32<<13)));
这里为什么是左移12位而不是13位,还没想通。
关于模式寄存器为什么要这样设置,是因为SDRAM控制器会将地址的低13位行地址row发给SDRAM,然后再发高9位列地址colum。上面语句中的移位操作,就是将控制码移到高9位中,发给SDRAM,达到配置芯片的目的。
NXP官方发布的固件库里,EMC_Init的代码太死板了。下面的代码里的配置,是用了哪些引脚,就配置哪些引脚。
SDRAM的试验最主要的就是它的初始化,其他没什么特别的。初始化代码如下:
void SDRAMInit( void )
{
volatile uint32_t i;
volatile unsigned long Dummy;
LPC_IOCON->P2_16 = 0x21; // CAS
LPC_IOCON->P2_17 = 0x21; // RAS
LPC_IOCON->P2_18 = 0x21; // CLKOUT0
LPC_IOCON->P2_20 = 0x21; // DYCS0
LPC_IOCON->P2_24 = 0x21; // CKEOUT0
LPC_IOCON->P2_28 = 0x21; // DQMOUT0
LPC_IOCON->P2_29 = 0x21; // DQMOUT1
LPC_IOCON->P3_0 = 0x21; // D0
LPC_IOCON->P3_1 = 0x21; // D1
LPC_IOCON->P3_2 = 0x21; // D2
LPC_IOCON->P3_3 = 0x21; // D3
LPC_IOCON->P3_4 = 0x21; // D4
LPC_IOCON->P3_5 = 0x21; // D5
LPC_IOCON->P3_6 = 0x21; // D6
LPC_IOCON->P3_7 = 0x21; // D7
LPC_IOCON->P3_8 = 0x21; // D8
LPC_IOCON->P3_9 = 0x21; // D9
LPC_IOCON->P3_10 = 0x21; // D10
LPC_IOCON->P3_11 = 0x21; // D11
LPC_IOCON->P3_12 = 0x21; // D12
LPC_IOCON->P3_13 = 0x21; // D13
LPC_IOCON->P3_14 = 0x21; // D14
LPC_IOCON->P3_15 = 0x21; // D15
LPC_IOCON->P4_0 = 0x21; // A0
LPC_IOCON->P4_1 = 0x21; // A1
LPC_IOCON->P4_2 = 0x21; // A2
LPC_IOCON->P4_3 = 0x21; // A3
LPC_IOCON->P4_4 = 0x21; // A4
LPC_IOCON->P4_5 = 0x21; // A5
LPC_IOCON->P4_6 = 0x21; // A6
LPC_IOCON->P4_7 = 0x21; // A7
LPC_IOCON->P4_8 = 0x21; // A8
LPC_IOCON->P4_9 = 0x21; // A9
LPC_IOCON->P4_10 = 0x21; // A10
LPC_IOCON->P4_11 = 0x21; // A11
LPC_IOCON->P4_12 = 0x21; // A12
LPC_IOCON->P4_13 = 0x21; // A13
LPC_IOCON->P4_14 = 0x21; // A14
LPC_IOCON->P4_15 = 0x21; // A15
LPC_IOCON->P4_16 = 0x21; // A16
LPC_IOCON->P4_17 = 0x21; // A17
LPC_IOCON->P4_18 = 0x21; // A18
LPC_IOCON->P4_19 = 0x21; // A19
LPC_IOCON->P4_20 = 0x21; // A20
LPC_IOCON->P4_21 = 0x21; // A21
LPC_IOCON->P4_22 = 0x21; // A22
LPC_IOCON->P4_24 = 0x21; // OE
LPC_IOCON->P4_25 = 0x21; // WE
LPC_IOCON->P4_30 = 0x21; // CS0
LPC_IOCON->P4_31 = 0x21; // CS1
// EMC_Init();
// Init SDRAM controller
LPC_SC->PCONP |= 0x00000800; // Set Bit PCEMC
/*Init SDRAM controller*/
LPC_SC->EMCDLYCTL |= (8<<0);
/*Set data read delay*/
LPC_SC->EMCDLYCTL |= (8<<8);
LPC_SC->EMCDLYCTL |= (0x08 <<16);
LPC_EMC->Control = 1;
LPC_EMC->DynamicReadConfig = 1;
LPC_EMC->DynamicRasCas0 = 0;
LPC_EMC->DynamicRasCas0 |= (3<<8); // CAS Latency
LPC_EMC->DynamicRasCas0 |= (3<<0); // RAS Latency
LPC_EMC->DynamicRP = P2C(SDRAM_TRP);
LPC_EMC->DynamicRAS = P2C(SDRAM_TRAS);
LPC_EMC->DynamicSREX = P2C(SDRAM_TXSR);
LPC_EMC->DynamicAPR = SDRAM_TAPR;
LPC_EMC->DynamicDAL = SDRAM_TDAL+P2C(SDRAM_TRP);
LPC_EMC->DynamicWR = SDRAM_TWR;
LPC_EMC->DynamicRC = P2C(SDRAM_TRC);
LPC_EMC->DynamicRFC = P2C(SDRAM_TRFC);
LPC_EMC->DynamicXSR = P2C(SDRAM_TXSR);
LPC_EMC->DynamicRRD = P2C(SDRAM_TRRD);
LPC_EMC->DynamicMRD = SDRAM_TMRD;
// 13 row, 9 - col, SDRAM
//LPC_EMC->DynamicConfig0 = 0x0004680;
LPC_EMC->DynamicConfig0 = 0x0000680;
// JEDEC General SDRAM Initialization Sequence
// DELAY to allow power and clocks to stabilize ~100 us
// NOP
LPC_EMC->DynamicControl = 0x0183;
//for(i= 200*30; i;i--);
for(i= 200*90; i;i--);// 2011-11-05 01:15:00 时间加长看效果
// PALL
LPC_EMC->DynamicControl = 0x0103;
LPC_EMC->DynamicRefresh = 2;
for(i= 256; i; --i); // > 128 clk
for(i= 512; i; --i);// 2011-11-05 01:15:00 时间加长看效果
LPC_EMC->DynamicRefresh = P2C(SDRAM_REFRESH) >> 4;
// COMM
LPC_EMC->DynamicControl = 0x00000083; /* Issue MODE command */
Dummy = *((volatile uint32_t *)(SDRAM_BASE_ADDR | (0x33<<12)));
// NORM
LPC_EMC->DynamicControl = 0x0000;
LPC_EMC->DynamicConfig0 |=(1<<19);
for(i = 100000; i;i--);
for(i = 100000; i;i--);// 2011-11-05 01:15:00 时间加长看效果
}