在线时间1 小时
UID3723986
注册时间2021-4-26
NXP金币0
该用户从未签到
新手上路
- 积分
- 15
- 最后登录
- 2021-4-30
|
楼主 |
发表于 2021-4-27 14:50:31
|
显示全部楼层
这个函数是配置外部存储器控制器的:
void SystemInit_ExtMemCtl (void) {
uint32_t emcdivby2_buf[emcdivby2_szw];
uint32_t div, n;
uint16_t wtemp = wtemp;
/* Select and enable EMC branch clock */
LPC_CCU1->CLK_M4_EMC_CFG = (0 << 2) | (0 << 1) | 1;
while (!(LPC_CCU1->CLK_M4_EMC_STAT & 1));
/* Set EMC clock output delay */
if (SystemCoreClock < 80000000UL) {
LPC_SCU->EMCDELAYCLK = EMC_CLK_DLY_TIM_0; /* No EMC clock out delay */
}
else {
LPC_SCU->EMCDELAYCLK = EMC_CLK_DLY_TIM_2; /* 2.0 ns EMC clock out delay */
}
/* Configure EMC port pins */
LPC_SCU->SFSP1_0 = EMC_PIN_SET | 2; /* P1_0: A5 */
LPC_SCU->SFSP1_1 = EMC_PIN_SET | 2; /* P1_1: A6 */
LPC_SCU->SFSP1_2 = EMC_PIN_SET | 2; /* P1_2: A7 */
LPC_SCU->SFSP1_3 = EMC_PIN_SET | 3; /* P1_3: OE */
LPC_SCU->SFSP1_4 = EMC_PIN_SET | 3; /* P1_4: BLS0 */
LPC_SCU->SFSP1_5 = EMC_PIN_SET | 3; /* P1_5: CS0 */
LPC_SCU->SFSP1_6 = EMC_PIN_SET | 3; /* P1_6: WE */
LPC_SCU->SFSP1_7 = EMC_PIN_SET | 3; /* P1_7: D0 */
LPC_SCU->SFSP1_8 = EMC_PIN_SET | 3; /* P1_8: D1 */
LPC_SCU->SFSP1_9 = EMC_PIN_SET | 3; /* P1_9: D2 */
LPC_SCU->SFSP1_10 = EMC_PIN_SET | 3; /* P1_10: D3 */
LPC_SCU->SFSP1_11 = EMC_PIN_SET | 3; /* P1_11: D4 */
LPC_SCU->SFSP1_12 = EMC_PIN_SET | 3; /* P1_12: D5 */
LPC_SCU->SFSP1_13 = EMC_PIN_SET | 3; /* P1_13: D6 */
LPC_SCU->SFSP1_14 = EMC_PIN_SET | 3; /* P1_14: D7 */
#ifdef CPU_208PIN
LPC_SCU->SFSP1_18 = EMC_PIN_SET | 7; /* P1_18: D10 */
LPC_SCU->SFSP1_20 = EMC_PIN_SET | 7; /* P1_20: D11 */
#endif
LPC_SCU->SFSP2_0 = EMC_PIN_SET | 2; /* P2_0: A13 */
LPC_SCU->SFSP2_1 = EMC_PIN_SET | 2; /* P2_1: A12 */
LPC_SCU->SFSP2_2 = EMC_PIN_SET | 2; /* P2_2: A11 */
LPC_SCU->SFSP2_6 = EMC_PIN_SET | 2; /* P2_6: A10 */
LPC_SCU->SFSP2_7 = EMC_PIN_SET | 3; /* P2_7: A9 */
LPC_SCU->SFSP2_8 = EMC_PIN_SET | 3; /* P2_8: A8 */
LPC_SCU->SFSP2_9 = EMC_PIN_SET | 3; /* P2_9: A0 */
LPC_SCU->SFSP2_10 = EMC_PIN_SET | 3; /* P2_10: A1 */
LPC_SCU->SFSP2_11 = EMC_PIN_SET | 3; /* P2_11: A2 */
LPC_SCU->SFSP2_12 = EMC_PIN_SET | 3; /* P2_12: A3 */
LPC_SCU->SFSP2_13 = EMC_PIN_SET | 3; /* P2_13: A4 */
LPC_SCU->SFSP5_0 = EMC_PIN_SET | 2; /* P5_0: D12 */
LPC_SCU->SFSP5_1 = EMC_PIN_SET | 2; /* P5_1: D13 */
LPC_SCU->SFSP5_2 = EMC_PIN_SET | 2; /* P5_2: D14 */
LPC_SCU->SFSP5_3 = EMC_PIN_SET | 2; /* P5_3: D15 */
LPC_SCU->SFSP5_4 = EMC_PIN_SET | 2; /* P5_4: D8 */
LPC_SCU->SFSP5_5 = EMC_PIN_SET | 2; /* P5_5: D9 */
#ifdef CPU_256PIN
LPC_SCU->SFSP5_6 = EMC_PIN_SET | 2; /* P5_6: D10 */
LPC_SCU->SFSP5_7 = EMC_PIN_SET | 2; /* P5_7: D11 */
#endif
LPC_SCU->SFSP6_1 = EMC_PIN_SET | 1; /* P6_1: DYCS1 */
LPC_SCU->SFSP6_2 = EMC_PIN_SET | 1; /* P6_3: CKEOUT1 */
LPC_SCU->SFSP6_3 = EMC_PIN_SET | 3; /* P6_3: CS1 */
LPC_SCU->SFSP6_4 = EMC_PIN_SET | 3; /* P6_4: CAS */
LPC_SCU->SFSP6_5 = EMC_PIN_SET | 3; /* P6_5: RAS */
LPC_SCU->SFSP6_6 = EMC_PIN_SET | 1; /* P6_6: BLS1 */
LPC_SCU->SFSP6_7 = EMC_PIN_SET | 1; /* P6_7: A15 */
LPC_SCU->SFSP6_8 = EMC_PIN_SET | 1; /* P6_8: A14 */
LPC_SCU->SFSP6_9 = EMC_PIN_SET | 3; /* P6_9: DYCS0 */
LPC_SCU->SFSP6_10 = EMC_PIN_SET | 3; /* P6_10: DQMOUT1 */
LPC_SCU->SFSP6_11 = EMC_PIN_SET | 3; /* P6_11: CKEOUT0 */
LPC_SCU->SFSP6_12 = EMC_PIN_SET | 3; /* P6_12: DQMOUT0 */
LPC_SCU->SFSPA_4 = EMC_PIN_SET | 3; /* PA_4: A23 */
// LPC_SCU->SFSPD_0 = EMC_PIN_SET | 2; /* PD_0: DQMOUT2 */
// LPC_SCU->SFSPD_1 = EMC_PIN_SET | 2; /* PD_1: CKEOUT2 */
// LPC_SCU->SFSPD_2 = EMC_PIN_SET | 2; /* PD_2: D16 */
// LPC_SCU->SFSPD_3 = EMC_PIN_SET | 2; /* PD_3: D17 */
// LPC_SCU->SFSPD_4 = EMC_PIN_SET | 2; /* PD_4: D18 */
// LPC_SCU->SFSPD_5 = EMC_PIN_SET | 2; /* PD_5: D19 */
// LPC_SCU->SFSPD_6 = EMC_PIN_SET | 2; /* PD_6: D20 */
// LPC_SCU->SFSPD_7 = EMC_PIN_SET | 2; /* PD_7: D21 */
// LPC_SCU->SFSPD_8 = EMC_PIN_SET | 2; /* PD_8: D22 */
// LPC_SCU->SFSPD_9 = EMC_PIN_SET | 2; /* PD_9: D23 */
LPC_SCU->SFSPD_10 = EMC_PIN_SET | 2; /* PD_10: BLS3 */
LPC_SCU->SFSPD_11 = EMC_PIN_SET | 2; /* PD_11: CS3 */
LPC_SCU->SFSPD_12 = EMC_PIN_SET | 2; /* PD_12: CS2 */
LPC_SCU->SFSPD_13 = EMC_PIN_SET | 2; /* PD_13: BLS2 */
LPC_SCU->SFSPD_14 = EMC_PIN_SET | 2; /* PD_14: DYCS2 */
LPC_SCU->SFSPD_15 = EMC_PIN_SET | 2; /* PD_15: A17 */
LPC_SCU->SFSPD_16 = EMC_PIN_SET | 2; /* PD_16: A16 */
LPC_SCU->SFSPE_0 = EMC_PIN_SET | 3; /* PE_0: A18 */
LPC_SCU->SFSPE_1 = EMC_PIN_SET | 3; /* PE_1: A19 */
LPC_SCU->SFSPE_2 = EMC_PIN_SET | 3; /* PE_2: A20 */
LPC_SCU->SFSPE_3 = EMC_PIN_SET | 3; /* PE_3: A21 */
LPC_SCU->SFSPE_4 = EMC_PIN_SET | 3; /* PE_4: A22 */
// LPC_SCU->SFSPE_5 = EMC_PIN_SET | 3; /* PE_5: D24 */
// LPC_SCU->SFSPE_6 = EMC_PIN_SET | 3; /* PE_6: D25 */
// LPC_SCU->SFSPE_7 = EMC_PIN_SET | 3; /* PE_7: D26 */
// LPC_SCU->SFSPE_8 = EMC_PIN_SET | 3; /* PE_8: D27 */
// LPC_SCU->SFSPE_9 = EMC_PIN_SET | 3; /* PE_9: D28 */
// LPC_SCU->SFSPE_10 = EMC_PIN_SET | 3; /* PE_10: D29 */
// LPC_SCU->SFSPE_11 = EMC_PIN_SET | 3; /* PE_11: D30 */
// LPC_SCU->SFSPE_12 = EMC_PIN_SET | 3; /* PE_12: D31 */
// LPC_SCU->SFSPE_13 = EMC_PIN_SET | 3; /* PE_13: DQMOUT3 */
// LPC_SCU->SFSPE_14 = EMC_PIN_SET | 3; /* PE_14: DYCS3 */
// LPC_SCU->SFSPE_15 = EMC_PIN_SET | 3; /* PE_15: CKEOUT3 */
LPC_EMC->CONTROL = 0x00000001; /* EMC Enable */
LPC_EMC->CONFIG = 0x00000000; /* Little-endian, Clock Ratio 1:1 */
div = 0;
if (SystemCoreClock > 120000000UL) {
/* Use EMC clock divider and EMC clock output delay */
div = 1;
/* Following code must be executed in RAM to ensure stable operation */
/* LPC_CCU1->CLK_M4_EMCDIV_CFG = (1 << 5) | (1 << 2) | (1 << 1) | 1; */
/* LPC_CREG->CREG6 |= (1 << 16); // EMC_CLK_DIV divided by 2 */
/* while (!(LPC_CCU1->CLK_M4_EMCDIV_STAT & 1)); */
/* This code configures EMC clock divider and is executed in RAM */
for (n = 0; n < emcdivby2_szw; n++) {
emcdivby2_buf[n] = *((uint32_t *)emcdivby2_ram + n);
*((uint32_t *)emcdivby2_ram + n) = *((uint32_t *)emcdivby2_opc + n);
}
__ISB();
((emcdivby2 )(emcdivby2_ram+1))(&LPC_CREG->CREG6, &LPC_CCU1->CLK_M4_EMCDIV_CFG, (1 << 5) | (1 << 2) | (1 << 1) | 1);
for (n = 0; n < emcdivby2_szw; n++) {
*((uint32_t *)emcdivby2_ram + n) = emcdivby2_buf[n];
}
}
/* Configure EMC clock-out pins */
LPC_SCU->SFSCLK_0 = EMC_PIN_SET | 0; /* CLK0 */
LPC_SCU->SFSCLK_1 = EMC_PIN_SET | 0; /* CLK1 */
LPC_SCU->SFSCLK_2 = EMC_PIN_SET | 0; /* CLK2 */
LPC_SCU->SFSCLK_3 = EMC_PIN_SET | 0; /* CLK3 */
/* Static memory configuration (chip select 0) */
//#if (USE_EXT_STAT_MEM_CS0)
// LPC_EMC->STATICCONFIG0 = (1 << 7) | /* Byte lane state: use WE signal */
// (2 << 0) ; /* Memory width 32-bit */
// LPC_EMC->STATICWAITOEN0 = (0 << 0) ; /* Wait output enable: No delay */
//
// /* Set Static Memory Read Delay for 90ns External NOR Flash */
// LPC_EMC->STATICWAITRD0 = 1 + EMC_NANOSEC(90, SystemCoreClock, div);
// LPC_EMC->STATICCONFIG0 |= (1 << 19) ; /* Enable buffer */
//#endif
#if (USE_EXT_STAT_MEM_CS0)
LPC_EMC->STATICCONFIG0 = (1 << 7) | /* Byte lane state: use WE signal */
(1 << 0) ; /* Memory width 32-bit */
LPC_EMC->STATICWAITOEN0 = (0 << 0) ; /* Wait output enable: No delay */
/* Set Static Memory Read Delay for 90ns External NOR Flash */
LPC_EMC->STATICWAITRD0 = 1 + EMC_NANOSEC(90, SystemCoreClock, div);
//LPC_EMC->STATICCONFIG0 |= (1 << 19) ; /* Enable buffer */
#endif
#if (USE_EXT_STAT_MEM_CS1)
LPC_EMC->STATICCONFIG1 = (1 << 7) | /* Byte lane state: use WE signal */
(1 << 0) ; /* Memory width 32-bit */
LPC_EMC->STATICWAITOEN1 = (0 << 0) ; /* Wait output enable: No delay */
/* Set Static Memory Read Delay for 90ns External NOR Flash */
LPC_EMC->STATICWAITRD1 = 1 + EMC_NANOSEC(90, SystemCoreClock, div);
//LPC_EMC->STATICCONFIG1 |= (1 << 19) ; /* Enable buffer */
#endif
#if (USE_EXT_STAT_MEM_CS2)
LPC_EMC->STATICCONFIG2 = (0 << 8) | //延长等待使能
(1 << 7) | /* Byte lane state: use WE signal */
(1 << 0) ; /* Memory width 8-bit */
LPC_EMC->STATICWAITWEN2 = 0x05;//片选到写使能的延迟
LPC_EMC->STATICWAITWR2 = 0x12;//写延迟
LPC_EMC->STATICWAITOEN2 = 0x08;//(5 << 0) ; /* Wait output enable: No delay */
/* Set Static Memory Read Delay for 90ns External NOR Flash */
LPC_EMC->STATICWAITRD2 = 0x0a;//1 + EMC_NANOSEC(90, SystemCoreClock, div);
//LPC_EMC->STATICCONFIG2 |= (1 << 19) ; /* Enable buffer */
#endif
#if (USE_EXT_STAT_MEM_CS3)
LPC_EMC->STATICCONFIG3 = (1 << 7) | /* Byte lane state: use WE signal */
(1 << 0) ; /* Memory width 16-bit */
LPC_EMC->STATICWAITWEN3 = 0;//片选到写使能的延迟
LPC_EMC->STATICWAITWR3 = 0x01;//写延迟
LPC_EMC->STATICWAITOEN3 = 0x00 ; /* Wait output enable: No delay */
/* Set Static Memory Read Delay for 90ns External NOR Flash */
LPC_EMC->STATICWAITRD3 =0x04;// 1 + EMC_NANOSEC(90, SystemCoreClock, div);
//LPC_EMC->STATICCONFIG3 |= (1 << 19) ; /* Enable buffer */
LPC_EMC->STATICWAITTURN3 =0x00;
#endif
/* Dynamic memory configuration (chip select 0) */
#if (USE_EXT_DYN_MEM_CS0)
/* Set Address mapping: 128Mb(4Mx32), 4 banks, row len = 12, column len = 8 */
// LPC_EMC->DYNAMICCONFIG0 = (1 << 14) | /* AM[14] = 1 */
// (0 << 12) | /* AM[12] = 0 */
// (2 << 9) | /* AM[11:9] = 2 */
// (2 << 7) ; /* AM[8:7] = 2 */
/* Set Address mapping: 64Mb(4Mx16), 4 banks, row len = 12, column len = 8 */
LPC_EMC->DYNAMICCONFIG0 = (0 << 14) | /* AM[14] = 0 */
(0 << 12) | /* AM[12] = 0 */
(1 << 9) | /* AM[11:9] = 1 */
(1 << 7) ; /* AM[8:7] = 1 */
LPC_EMC->DYNAMICRASCAS0 = 0x00000303; /* Latency: RAS 3, CAS 3 CCLK cyc.*/
LPC_EMC->DYNAMICREADCONFIG = 0x00000001; /* Command delayed by 1/2 CCLK */
LPC_EMC->DYNAMICRP = EMC_NANOSEC (20, SystemCoreClock, div);
LPC_EMC->DYNAMICRAS = EMC_NANOSEC (42, SystemCoreClock, div);
LPC_EMC->DYNAMICSREX = EMC_NANOSEC (63, SystemCoreClock, div);
LPC_EMC->DYNAMICAPR = EMC_NANOSEC (70, SystemCoreClock, div);
LPC_EMC->DYNAMICDAL = EMC_NANOSEC (70, SystemCoreClock, div);
LPC_EMC->DYNAMICWR = EMC_NANOSEC (30, SystemCoreClock, div);
LPC_EMC->DYNAMICRC = EMC_NANOSEC (63, SystemCoreClock, div);
LPC_EMC->DYNAMICRFC = EMC_NANOSEC (63, SystemCoreClock, div);
LPC_EMC->DYNAMICXSR = EMC_NANOSEC (63, SystemCoreClock, div);
LPC_EMC->DYNAMICRRD = EMC_NANOSEC (14, SystemCoreClock, div);
LPC_EMC->DYNAMICMRD = EMC_NANOSEC (30, SystemCoreClock, div);
// LPC_EMC->DYNAMICRP = 0x0f;//EMC_NANOSEC (20, SystemCoreClock, div);
// LPC_EMC->DYNAMICRAS = 0x0f;//EMC_NANOSEC (42, SystemCoreClock, div);
// LPC_EMC->DYNAMICSREX = 0x0f;//EMC_NANOSEC (63, SystemCoreClock, div);
// LPC_EMC->DYNAMICAPR = 0x0f;//EMC_NANOSEC (70, SystemCoreClock, div);
// LPC_EMC->DYNAMICDAL = 0x0f;//EMC_NANOSEC (70, SystemCoreClock, div);
// LPC_EMC->DYNAMICWR = 0x0f;//EMC_NANOSEC (30, SystemCoreClock, div);
// LPC_EMC->DYNAMICRC = 0x1f;//EMC_NANOSEC (63, SystemCoreClock, div);
// LPC_EMC->DYNAMICRFC = 0x1f;//EMC_NANOSEC (63, SystemCoreClock, div);
// LPC_EMC->DYNAMICXSR = 0x1f;//EMC_NANOSEC (63, SystemCoreClock, div);
// LPC_EMC->DYNAMICRRD = 0x0f;//EMC_NANOSEC (14, SystemCoreClock, div);
// LPC_EMC->DYNAMICMRD = 0x0f;//EMC_NANOSEC (30, SystemCoreClock, div);
WaitUs (100);
LPC_EMC->DYNAMICCONTROL = 0x00000183; /* Issue NOP command */
WaitUs (10);
LPC_EMC->DYNAMICCONTROL = 0x00000103; /* Issue PALL command */
WaitUs (1);
LPC_EMC->DYNAMICCONTROL = 0x00000183; /* Issue NOP command */
WaitUs (1);
LPC_EMC->DYNAMICREFRESH = EMC_NANOSEC( 200, SystemCoreClock, div) / 16 + 1;
WaitUs (10);
// LPC_EMC->DYNAMICREFRESH = EMC_NANOSEC(15625, SystemCoreClock, div) / 16 + 1;
LPC_EMC->DYNAMICREFRESH = EMC_NANOSEC(15625, SystemCoreClock, div) / 16 + 1;
WaitUs (10);
LPC_EMC->DYNAMICCONTROL = 0x00000083; /* Issue MODE command */
/* Mode register: Burst Length: 4, Burst Type: Sequential, CAS Latency: 3 */
//WR_MODE(((3 << 4) | 2) << 12);
wtemp = *((volatile uint16_t *)(SDRAM_ADDR_BASE | (0x33<<11)));
WaitUs (10);
LPC_EMC->DYNAMICCONTROL = 0x00000002; /* Issue NORMAL command */
LPC_EMC->DYNAMICCONFIG0 |= (1 << 19); /* Enable buffer */
#endif
} |
|