高级会员
- 积分
- 600
- 威望
- 301
- 贡献
- 189
- 兑换币
- 195
- 注册时间
- 2015-9-2
- 在线时间
- 55 小时
- 毕业学校
- 西安邮电大学
|
2#
楼主 |
发表于 2015-10-11 21:27:52
|
只看该作者
整个函数是这样的:
unsigned char LPLD_PLL_Setup(unsigned char pll_option)
{
unsigned char pll_freq;
unsigned char prdiv;
if(pll_option<111)
{
prdiv = 24;
}
else if(pll_option<201)
{
prdiv = 12;
}
else if(pll_option<221)
{
prdiv = 11;
}
else
{
return 0;
}
// 这里假设复位后 MCG 模块默认为 FEI 模式
// 首先移动到 FBE 模式
MCG_C2 = 0;
// 振荡器初始化完成后,释放锁存状态下的 oscillator 和 GPIO
SIM_SCGC4 |= SIM_SCGC4_LLWU_MASK;
LLWU_CS |= LLWU_CS_ACKISO_MASK;
// 选择外部 oscilator 、参考分频器 and 清零 IREFS 启动外部osc
// CLKS=2, FRDIV=3, IREFS=0, IRCLKEN=0, IREFSTEN=0
MCG_C1 = MCG_C1_CLKS(2) | MCG_C1_FRDIV(3);
//while (!(MCG_S & MCG_S_OSCINIT_MASK)){}; // 等待 oscillator 初始化
while (MCG_S & MCG_S_IREFST_MASK){}; // 等待参考时钟清零
while (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x2){}; // 等待时钟状态显示为外部参考时钟(ext ref clk)
// 进入FBE模式
// 配置 PLL 参考分频器, PLLCLKEN=0, PLLSTEN=0, PRDIV=5
// 用晶振频率来选择 PRDIV 值. 仅在有频率晶振的时候支持
// 产生 2MHz 的参考时钟给 PLL.
MCG_C5 = MCG_C5_PRDIV(prdiv); // 设置 PLL 匹配晶振的参考分频数
// 确保MCG_C6处于复位状态,禁止LOLIE、PLL、和时钟控制器,清PLL VCO分频器
MCG_C6 = 0x0;
// 选择PLL VCO分频器,系统时钟分频器取决于时钟选项
switch (pll_option) {
case 60:
// 设置系统选项分频
// MCG=PLL, core = MCG, bus = MCG/2, FlexBus = MCG/2, Flash clock= MCG/3
LPLD_Set_SYS_DIV(0,1,1,2); //core=60M, bus=30M, FlexBus=30M, Flash Clk=20M
// 设置 VCO 分频并使能 50MHz PLL, LOLIE=0, PLLS=1, CME=0, VDIV=1
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(6); //VDIV = 6 (2x30) 60MHz
pll_freq = 60;
break;
case 70:
LPLD_Set_SYS_DIV(0,1,1,2); //core=70M, bus=35M, FlexBus=35M, Flash Clk=23.3M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(11); //VDIV = 11 (2x35) 70MHz
pll_freq = 70;
break;
case 80:
LPLD_Set_SYS_DIV(0,1,1,3); //core=80M, bus=40M, FlexBus=40M, Flash Clk=20M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(16); //VDIV = 16 (2x40) 80MHz
pll_freq = 80;
break;
case 90:
LPLD_Set_SYS_DIV(0,1,1,3); //core=90M, bus=45M, FlexBus=45M, Flash Clk=22.5M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(21); //VDIV = 21 (2x45) 90MHz
pll_freq = 90;
break;
case 96:
LPLD_Set_SYS_DIV(0,1,1,3); //core=96M, bus=48M, FlexBus=48M, Flash Clk=22.5M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(24); //VDIV = 24 (2x48) 96MHz
pll_freq = 96;
break;
case 100:
LPLD_Set_SYS_DIV(0,1,1,3); //core=100M, bus=50M, FlexBus=50M, Flash Clk=25M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(26); //VDIV = 26 (2x50) 100MHz
pll_freq = 100;
break;
case 110:
LPLD_Set_SYS_DIV(0,2,2,4); //core=110M, bus=36.7M, FlexBus=36.7M, Flash Clk=22M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(31); //VDIV = 31 (2x55) 110MHz
pll_freq = 110;
break;
case 120:
LPLD_Set_SYS_DIV(0,2,2,4); //core=120M, bus=40M, FlexBus=40M, Flash Clk=24M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(7); //VDIV = 7 (3.846x31) 119.2MHz
pll_freq = 120;
break;
case 130:
LPLD_Set_SYS_DIV(0,2,2,5); //core=130M, bus=43M, FlexBus=43M, Flash Clk=21.6M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(10); //VDIV = 10 (3.846x34) 130.8MHz
pll_freq = 130;
break;
case 150:
LPLD_Set_SYS_DIV(0,2,2,5); //core=150M, bus=50M, FlexBus=50M, Flash Clk=25M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(15); //VDIV = 15 (3.846x39) 150MHz
pll_freq = 150;
break;
case 170:
LPLD_Set_SYS_DIV(0,3,3,6); //core=170M, bus=42.5M, FlexBus=42.5M, Flash Clk=24.3M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(20); //VDIV = 20 (3.846x44) 169.2MHz
pll_freq = 170;
break;
case 180:
LPLD_Set_SYS_DIV(0,3,3,7); //core=180M, bus=45M, FlexBus=45M, Flash Clk=22.5M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(23); //VDIV = 23 (3.846x47) 180.8MHz
pll_freq = 180;
break;
case 200:
LPLD_Set_SYS_DIV(0,3,3,7); //core=200M, bus=50M, FlexBus=50M, Flash Clk=25M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(28); //VDIV = 28 (3.846x52) 200MHz
pll_freq = 200;
break;
case 209:
LPLD_Set_SYS_DIV(0,4,4,8); //core=209M, bus=42M, FlexBus=42M, Flash Clk=23.2M
MCG_C6 = MCG_C6_PLLS_MASK | MCG_C6_VDIV(26); //VDIV = 26 (4.17x50) 208.5MHz
pll_freq = 209;
break;
}
while (!(MCG_S & MCG_S_PLLST_MASK)){}; // wait for PLL status bit to set
while (!(MCG_S & MCG_S_LOCK_MASK)){}; // Wait for LOCK bit to set
// 已经进入PBE模式
// Transition into PEE by setting CLKS to 0
// CLKS=0, FRDIV=3, IREFS=0, IRCLKEN=0, IREFSTEN=0
MCG_C1 &= ~MCG_C1_CLKS_MASK;
// Wait for clock status bits to update
while (((MCG_S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) != 0x3){};
// 已经进入PEE模式
return pll_freq;
}
|
|