高中生
最后登录1970-1-1
在线时间 小时
注册时间2021-2-10
|
楼主 |
发表于 2021-2-15 13:43:49
|
显示全部楼层
/* USER CODE BEGIN PV */
uint16_t ContN;
#pragma location = 0xD0000800
uint16_t sdram_buf[32]; // 故意避开 D000 0000 还是失败 。
/* Private variables ---------------------------------------------------------*/
#define Bank1 (1)
#define Bank2 (2)
#define SDRAM_MODEREG_BURST_LENGTH_1 ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_LENGTH_2 ((uint16_t)0x0001)
#define SDRAM_MODEREG_BURST_LENGTH_4 ((uint16_t)0x0002)
#define SDRAM_MODEREG_BURST_LENGTH_8 ((uint16_t)0x0004)
#define SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_TYPE_INTERLEAVED ((uint16_t)0x0008)
#define SDRAM_MODEREG_CAS_LATENCY_2 ((uint16_t)0x0020)
#define SDRAM_MODEREG_CAS_LATENCY_3 ((uint16_t)0x0030)
#define SDRAM_MODEREG_OPERATING_MODE_STANDARD ((uint16_t)0x0000)
#define SDRAM_MODEREG_WRITEBURST_MODE_PROGRAMMED ((uint16_t)0x0000)
#define SDRAM_MODEREG_WRITEBURST_MODE_SINGLE ((uint16_t)0x0200)
uint8_t SDRAM_SendCommand(uint32_t CommandMode, uint32_t Bank, uint32_t RefreshNum, uint32_t RegVal)
{
uint32_t CommandTarget;
FMC_SDRAM_CommandTypeDef Command;
if(Bank == 1)
CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
else if(Bank == 2)
CommandTarget = FMC_SDRAM_CMD_TARGET_BANK2;
Command.CommandMode = CommandMode;
Command.CommandTarget = CommandTarget;
Command.AutoRefreshNumber = RefreshNum;
Command.ModeRegisterDefinition = RegVal;
if(HAL_SDRAM_SendCommand(&hsdram1, &Command, 0x1000) == HAL_OK)
return 1;
else
return 0;
}
// 在主函数中调用下面的 SDRAM_Init()函数后即可使用SDRAM。
void SDRAM_Init(void)
{
uint32_t temp;
SDRAM_SendCommand(FMC_SDRAM_CMD_CLK_ENABLE,Bank2, 1, 0); //步骤3:使能时钟信号,SDCKE0 = 1
HAL_Delay(1); //步骤4:至少延时200us
SDRAM_SendCommand(FMC_SDRAM_CMD_PALL,Bank2, 1, 0); //步骤5:发送全部预充电命令
SDRAM_SendCommand(FMC_SDRAM_CMD_AUTOREFRESH_MODE,Bank2, 8, 0); //步骤6:设置自动刷新次数
temp = SDRAM_MODEREG_BURST_LENGTH_1 | //设置突发长度:1
SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL | //设置突发类型:连续
SDRAM_MODEREG_CAS_LATENCY_3 | //设置CAS值:3
SDRAM_MODEREG_OPERATING_MODE_STANDARD | //设置操作模式:标准
SDRAM_MODEREG_WRITEBURST_MODE_SINGLE; //设置突发写模式:单点访问
SDRAM_SendCommand(FMC_SDRAM_CMD_LOAD_MODE,Bank2, 1, temp); //步骤7:装载模式寄存器的值
//SDRAM刷新周期是64ms,行数是8192行,时钟频率是168MHz/2=84MHz
//所有COUNT = (64ms/8192)/(1/84us)-20 = 64000*84/8192-20 = 636
HAL_SDRAM_ProgramRefreshRate(&hsdram1,1572); //步骤8:设置刷新速率
}
|
|