📄 init_sd_ram.c
字号:
#include <main.h>
#define CLKIN (33)//30 // clock-in frequency in MHz
#define TREF 64
#define EXP 1000 //用来校正第一步的数据,因为TREF为ms,clkin为MHz
#define NRA 8192 //4096//
#define TRAS 6 //tras 6周期
#define TRP 3 //trp 3周期
#define MHz_EXP (1e6)
void Setup_SDRAM(void)
{
unsigned short SCLK_Freq, RDIV;
unsigned short tempReg;
tempReg = *pEBIU_SDSTAT;
tempReg = tempReg & 0x0008;
// if(tempReg == 0x0008) //Will power up on next SDRAM access if SDRAM enabled
// {
SCLK_Freq = Check_Freq((unsigned short)CLKIN);//由输入时钟计算出系统时钟频率
//Based on the SCLK frequency derived from the previous function, this
//function now uses the SCLK frequency to derive the correct refresh rate.
RDIV = Calc_Parameters(SCLK_Freq);
//Finally, after the appropriate refresh rate is determined, the following
//function initializes SDRAM using the RDIV value.
Init_SDRAM(RDIV);
// }
}
unsigned short Check_Freq (unsigned short Clkin_Freq)
{
unsigned short tempReg_PLLCTL, tempReg_PLLDIV;
unsigned short MSEL_Mult, SCLK_Mult, Divide_Frequency, SCLK, VCO_Freq;
tempReg_PLLCTL = *pPLL_CTL;
tempReg_PLLDIV = *pPLL_DIV;
Divide_Frequency = tempReg_PLLCTL & 0x0001;
if(Divide_Frequency == 1)
{
Clkin_Freq = Clkin_Freq/2;
}
MSEL_Mult = ((tempReg_PLLCTL & 0x7E00) >> 9);
VCO_Freq = MSEL_Mult * Clkin_Freq;
SCLK_Mult = tempReg_PLLDIV & 0x000f;
SCLK = VCO_Freq/SCLK_Mult;
return SCLK;
}
unsigned short Calc_Parameters(unsigned short SCLK_Freq)//695左右
{
float clk_timing, tempReg_one;
int full_SCLK;
unsigned short tras, trp, tempReg_two, RDIV;
tempReg_one = (float)(SCLK_Freq) * (float)(TREF);
tempReg_one = tempReg_one/(float)(NRA);
tempReg_one = (tempReg_one * (float)(EXP));
// full_SCLK = (int)(SCLK_Freq)*(int)(MHz_EXP);
// clk_timing = 1/((float)(full_SCLK)); //一个指令周期
// tras = (unsigned short)(ceil((float)(SPEC_TRAS)/clk_timing));
// trp = (unsigned short)(ceil((float)(SPEC_TRP)/clk_timing));
// tempReg_two = tras + trp;
tempReg_two = TRAS + TRP;
RDIV = (unsigned short)(tempReg_one) - tempReg_two;
return RDIV;
}
void Init_SDRAM(unsigned short RDIV_Value)
{
// *pEBIU_SDBCTL = 0x00000015;
*pEBIU_SDBCTL = 0x00013;//0x0000013;
ssync();
*pEBIU_SDRRC = RDIV_Value;
ssync();
// *pEBIU_SDGCTL = 0x0091998d;//0x9bbbcd;////0x0091998d;
// ssync();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -