下面是GD官网上的初始化SDRAM的源码:
GD32F20x Firmware Library的Example中有EXMC访问SDRAM的例程的源码(没有权限发资料链接)
void SDRAM_Init(uint32_t EXMC_Bank)
{
GPIO_InitPara GPIO_InitStructure;
EXMC_SDRAMInitPara EXMC_SDRAMInitStructure;
EXMC_SDRAMTimingInitPara EXMC_SDRAMTimingInitStructure;
EXMC_SDRAMCommandPara EXMC_SDRAMCommandStructure;
uint32_t tmpr = 0, bank_sdram, bank_select;
uint32_t timeout = SDRAM_TIMEOUT;
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOB | RCC_APB2PERIPH_GPIOC | RCC_APB2PERIPH_GPIOD |
RCC_APB2PERIPH_GPIOE | RCC_APB2PERIPH_GPIOF | RCC_APB2PERIPH_GPIOG , ENABLE);
RCC_APB2Periph2Clock_Enable(RCC_APB2PERIPH2_GPIOH , ENABLE);
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_AF , ENABLE);
RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_EXMC , ENABLE);
/* Common GPIO configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
/* EXMC gpio pin PC0: EXMC_NWE remap */
GPIO_PinRemapConfig2(PCFR6, PCFR6_REMAP_EXMC_SDNWE_PC0, ENABLE);
/* GPIOC configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* GPIOD configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_8 | GPIO_PIN_9 |
GPIO_PIN_10 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* GPIOE configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_7 | GPIO_PIN_8 |
GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 |
GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* GPIOF configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_11 | GPIO_PIN_12 |
GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* GPIOG configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_4 |
GPIO_PIN_5 | GPIO_PIN_8 | GPIO_PIN_15;
GPIO_Init(GPIOG, &GPIO_InitStructure);
/* GPIOH configuration,PH2:EXMC_CKE0,PH3:EXMC_NE0 */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_Init(GPIOH, &GPIO_InitStructure);
/* Specify which SDRAM to read and write */
if(EXMC_Bank == EXMC_BANK5_SDRAM)
{
bank_sdram = EXMC_BANK5_SDRAM;
bank_select = EXMC_BANK_SELECT_1;
}
else
{
bank_sdram = EXMC_BANK6_SDRAM;
bank_select = EXMC_BANK_SELECT_2;
}
/* EXMC SDRAM device initialization sequence --------------------------------*/
/* Step 1 : Configure SDRAM timing registers --------------------------------*/
/* LMRD: 2 Clock cycles */
EXMC_SDRAMTimingInitStructure.EXMC_LoadModeRegisterDelay = 2;
/* XSRD: min = 67ns */
EXMC_SDRAMTimingInitStructure.EXMC_ExitSelfRefreshDelay = 5;
/* RASD: min=42ns , max=120k (ns) */
EXMC_SDRAMTimingInitStructure.EXMC_RowAddressSelectDelay = 3;
/* ARFD: min=60ns */
EXMC_SDRAMTimingInitStructure.EXMC_AutoRefreshDelay = 4;
/* WRD: min=1 Clock cycles +6ns */
EXMC_SDRAMTimingInitStructure.EXMC_WriteRecoveryDelay = 2;
/* RPD: min=18ns */
EXMC_SDRAMTimingInitStructure.EXMC_RowPrechargeDelay = 2;
/* RCD: min=18ns */
EXMC_SDRAMTimingInitStructure.EXMC_RowToColumnDelay = 2;
/* Step 1 : Configure SDRAM control registers ---------------------------------*/
EXMC_SDRAMInitStructure.EXMC_SDRAMBank = bank_sdram;
EXMC_SDRAMInitStructure.EXMC_ColumnAddressWidth = EXMC_COLUMN_ADDRESS_WIDTH_9;
EXMC_SDRAMInitStructure.EXMC_RowAddressWidth = EXMC_ROW_ADDRESS_WIDTH_13;
EXMC_SDRAMInitStructure.EXMC_DataWidth = EXMC_DATA_WIDTH_16;
EXMC_SDRAMInitStructure.EXMC_InternalBankNumber = EXMC_INTERNAL_BANK_NUMBER_4;
EXMC_SDRAMInitStructure.EXMC_CASLatency = EXMC_CAS_LATENCY_3;
EXMC_SDRAMInitStructure.EXMC_WriteProtection = EXMC_WRITE_PROTECTION_DISABLE;
EXMC_SDRAMInitStructure.EXMC_SDClockConfig = EXMC_SDCLK_PERIOD_2;
EXMC_SDRAMInitStructure.EXMC_BrustReadSwitch = EXMC_BRUST_READ_ENABLE;
EXMC_SDRAMInitStructure.EXMC_PipelineReadDelay = EXMC_PIPELINE_READ_DELAY_1;
EXMC_SDRAMInitStructure.EXMC_SDRAMTimingParaStruct = &EXMC_SDRAMTimingInitStructure;
/* EXMC SDRAM bank initialization */
EXMC_SDRAM_Init(&EXMC_SDRAMInitStructure);
/* Step 3 : Configure CKE high command---------------------------------------*/
EXMC_SDRAMCommandStructure.EXMC_Command = EXMC_COMMAND_CKE_HIGH;
EXMC_SDRAMCommandStructure.EXMC_BankSelect = bank_select;
EXMC_SDRAMCommandStructure.EXMC_AutoRefreshNumber = 1;
EXMC_SDRAMCommandStructure.EXMC_ModeRegisterContent = 0;
/* Wait until the SDRAM controller is ready */
while((EXMC_GetBitState(bank_sdram, EXMC_FLAG_READY) != RESET) && (timeout > 0))
{
timeout--;
}
/* Send the command */
EXMC_SDRAM_CmdConfig(&EXMC_SDRAMCommandStructure);
/* Step 4 : Insert 10 ms delay----------------------------------------------*/
Delay_1ms(10);
/* Step 5 : Configure precharge all command----------------------------------*/
EXMC_SDRAMCommandStructure.EXMC_Command = EXMC_COMMAND_PRECHARGE;
EXMC_SDRAMCommandStructure.EXMC_BankSelect = bank_select;
EXMC_SDRAMCommandStructure.EXMC_AutoRefreshNumber = 1;
EXMC_SDRAMCommandStructure.EXMC_ModeRegisterContent = 0;
/* Wait until the SDRAM controller is ready */
timeout = SDRAM_TIMEOUT;
while((EXMC_GetBitState(bank_sdram, EXMC_FLAG_READY) != RESET) && (timeout > 0))
{
timeout--;
}
/* Send the command */
EXMC_SDRAM_CmdConfig(&EXMC_SDRAMCommandStructure);
/* Step 6 : Configure Auto-Refresh command-----------------------------------*/
EXMC_SDRAMCommandStructure.EXMC_Command = EXMC_COMMAND_AUTO_REFRESH;
EXMC_SDRAMCommandStructure.EXMC_BankSelect = bank_select;
EXMC_SDRAMCommandStructure.EXMC_AutoRefreshNumber = 8;
EXMC_SDRAMCommandStructure.EXMC_ModeRegisterContent = 0;
/* Wait until the SDRAM controller is ready */
timeout = SDRAM_TIMEOUT;
while((EXMC_GetBitState(bank_sdram, EXMC_FLAG_READY) != RESET) && (timeout > 0))
{
timeout--;
}
/* Send the command */
EXMC_SDRAM_CmdConfig(&EXMC_SDRAMCommandStructure);
/* Step 7 : Configure load mode register command-----------------------------*/
/* Program mode register */
tmpr = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_1 |
SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |
SDRAM_MODEREG_CAS_LATENCY_3 |
SDRAM_MODEREG_OPERATING_MODE_STANDARD |
SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
EXMC_SDRAMCommandStructure.EXMC_Command = EXMC_COMMAND_LOAD;
EXMC_SDRAMCommandStructure.EXMC_BankSelect = bank_select;
EXMC_SDRAMCommandStructure.EXMC_AutoRefreshNumber = 1;
EXMC_SDRAMCommandStructure.EXMC_ModeRegisterContent = tmpr;
/* Wait until the SDRAM controller is ready */
timeout = SDRAM_TIMEOUT;
while((EXMC_GetBitState(bank_sdram, EXMC_FLAG_READY) != RESET) && (timeout > 0))
{
timeout--;
}
/* Send the command */
EXMC_SDRAM_CmdConfig(&EXMC_SDRAMCommandStructure);
/* Step 8 : Set the auto-refresh rate counter--------------------------------*/
/* 64ms, 8192-cycle refresh, 64ms/8192=7.81us */
/* SDCLK_Freq = SYS_Freq/2 */
/* (7.81 us * SDCLK_Freq) - 20 */
EXMC_SDRAM_SetRefreshCount(448);
/* Wait until the SDRAM controller is ready */
timeout = SDRAM_TIMEOUT;
while((EXMC_GetBitState(bank_sdram, EXMC_FLAG_READY) != RESET) && (timeout > 0))
{
timeout--;
}
} |