Tiny4412裸机程序,时钟操作

2019-07-13 03:58发布

转载:http://www.techbulo.com/1388.html有了上一节《Exynos4412时钟体系分析》的基础,这一节我们来做几个和时钟有关的实验。其实,Exynos 4412的 IROM代码已经设置了PLL,我们可以通过串口把IROM设置的PLL寄存器值打印出来,这些值打印出来是这样的(摘自韦东山老师的《嵌入式Linux系统开发完全手册_基于4412__上册》):123456789101112131415161718192021222324252627282930CLK_SRC_CPU = 0x01000001CLK_DIV_DMC0 = 0x00111713CLK_DIV_DMC1 = 0x01011171CLK_SRC_TOP0 = 0x01110000CLK_SRC_TOP1 = 0x00001000CLK_DIV_TOP = 0x00015470CLK_SRC_LEFTBUS = 0x00000001CLK_DIV_LEFTBUS = 0x00000013CLK_SRC_RIGHTBUS = 0x00000001CLK_DIV_RIGHTBUS = 0x00000013APLL_LOCK = 0x00000960MPLL_LOCK = 0x00000000EPLL_LOCK = 0x00000FFFVPLL_LOCK = 0x00000FFFCLK_DIV_CPU0 = 0x00773730CLK_DIV_CPU1 = 0x00000077APLL_CON1 = 0x00003800APLL_CON0 = 0xA0640301MPLL_CON1 = 0x00003800MPLL_CON0 = 0xA0640301EPLL_CON2 = 0x00000080EPLL_CON1 = 0x66010000EPLL_CON0 = 0x00600302VPLL_CON2 = 0x00000080VPLL_CON1 = 0x66016000VPLL_CON0 = 0x006F0302CLK_SRC_CPU = 0x01000001CLK_SRC_DMC = 0x00111000CLK_SRC_TOP0 = 0x01110000CLK_SRC_TOP1 = 0x00001000现在来计算 ARMCLK的时钟频率:由上一节《Exynos4412时钟体系分析》的介绍我们知道,ARMCLK 有如下计算公式:如下图所示:CLK_DIV_CPU0由上边打印的寄存器CLK_SRC_CPU 的值为:十六进制:0x01000001二进制:0000 0001 0000 0000 0000 0000 0000 0001① BIT[0] 控制第1个MUX (即 MUXAPLL) ,该位值为1.② BIT[16]控制 第2个 MUX( 即MUXCORE) ,该位值为0.所以由此看出ARMCLK时钟走的是如下的路线:ARM CLK所以:ARMCLK = MUXCORE的输出 / DIVCORE / DIVCORE2ARMCLK = MDIV x FIN / (PDIV x 2 ^ SDIV) / (CORE_RATIO + 1) / (CORE2_RATIO + 1)= 0x64 x 24MHz / (3 x 2 ^ 1) / (0 + 1) / (0 + 1)= 400 MHz 本次实验涉及3个小实验:① 4.system_clock_disable_apll:不使用 APLL,让CPU运行于 24MHz 频率,观察 LED 闪烁是否变慢② 5.system_clock_apll:重新设置APLL,让 CPU 运行于1.4GHz频率,观察 LED 闪烁是否变快③ 6.system_clock_plls:参考厂家提供的u-boot代码,设置所有PLL供后续章节使用第一个小实验实现的目标:不使用 APLL,让CPU运行于 24MHz 频率,观察 LED 闪烁是否变慢一、程序说明我们在前一个实验,《Tiny4412之C语言实现流水灯》的基础上修改。start.S大部分相同,只是增加一条函数调用语句1bl system_clock_init // 调用时钟初始化函数如下图所示:启动文件启动文件链接脚本system_clock.lds的内容和上一个实验key.lds完全相同,只把名字改了改;Makefile的内容也大部分一样,也只是改了改里边文件的名字,led.c文件和LED实验时完全相同,新增加了文件system_clock.c,代码如下:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051// CMU_CPU#define CLK_SRC_CPU (*(volatile unsigned int *)0x10044200)#define CLK_DIV_CPU0 (*(volatile unsigned int *)0x10044500)#define CLK_DIV_CPU1 (*(volatile unsigned int *)0x10044504)// CMU_DMC#define CLK_SRC_DMC (*(volatile unsigned int *)0x10040200)#define CLK_DIV_DMC0 (*(volatile unsigned int *)0x10040500)#define CLK_DIV_DMC1 (*(volatile unsigned int *)0x10040504)// CMU_TOP#define CLK_SRC_TOP0 (*(volatile unsigned int *)0x1003C210)#define CLK_SRC_TOP1 (*(volatile unsigned int *)0x1003C214)#define CLK_DIV_TOP (*(volatile unsigned int *)0x1003C510)// CMU_LEFTBUS#define CLK_SRC_LEFTBUS (*(volatile unsigned int *)0x10034200)#define CLK_DIV_LEFTBUS (*(volatile unsigned int *)0x10034500)// CMU_RIGHTBUS#define CLK_SRC_RIGHTBUS (*(volatile unsigned int *)0x10038200)#define CLK_DIV_RIGHTBUS (*(volatile unsigned int *)0x10038500)// locktime#define APLL_LOCK (*(volatile unsigned int *)0x10044000)#define MPLL_LOCK (*(volatile unsigned int *)0x10044008)#define EPLL_LOCK (*(volatile unsigned int *)0x1003C010)#define VPLL_LOCK (*(volatile unsigned int *)0x1003C020)// APLL#define APLL_CON1 (*(volatile unsigned int *)0x10044104)#define APLL_CON0 (*(volatile unsigned int *)0x10044100)// MPLL#define MPLL_CON0 (*(volatile unsigned int *)0x10040108)#define MPLL_CON1 (*(volatile unsigned int *)0x1004010c)// EPLL#define EPLL_CON2 (*(volatile unsigned int *)0x1003C118)#define EPLL_CON1 (*(volatile unsigned int *)0x1003C114)#define EPLL_CON0 (*(volatile unsigned int *)0x1003C110)// VPLL#define VPLL_CON0 (*(volatile unsigned int *)0x1003C120)#define VPLL_CON1 (*(volatile unsigned int *)0x1003C124)#define VPLL_CON2 (*(volatile unsigned int *)0x1003C128) /* * 函数名: * system_clock_init * 功能: 初始化4412的系统时钟 */void system_clock_init(void){ /* IROM或BL1设置了APLL, * 本程序设置不启动APLL, * 而是使在晶振时钟, 以体验一下LED闪灯变慢 */ CLK_SRC_CPU = 0x0;}没什么可说的,很简单,前部分是后期会用到的一些寄存器地址的定义,主要的是下边system_clock_init这个函数,在这个函数中将CLK_SRC_CPU寄存器的值设为0,这样ARMCLK的频率将走下面这条路径,设置为24MHZ:ARM CLK_SETD二、编译、烧写实验按照前几节介绍的方法,将程序上传到服务器编译,并烧写到SD卡上,给开发板上电,可以明显感觉到LED闪烁的频率大大降低,说明我们设置的时钟起作用了,这里就不上图了(上了图大家也看不出来)。第二个小实验实现的目标:重新设置APLL,让 CPU 运行于1.4GHz频率,观察 LED 闪烁是否变快一、程序说明文件同第一个小实验,只是在它的基础上对system_clock.c文件中的system_clock_init函数进行修改:12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152/** 函数名: system_clock_init* 功 能: 初始化4412的系统时钟* 最终结果: APLL=1.4GHz*/void system_clock_init(void){/** 1. 在设置APLL之前, 先设置时钟源为晶振*/CLK_SRC_CPU = 0x0; /** 2. 设置APLL*/ /* 2.1 设置锁定时间: APLL_CON0中PDIV=3, 所以APLL_LOCK = 270x3 */APLL_LOCK = 270 * 3; /* 2.2 设置分频参数 *//** CORE2_RATIO = 0;* APLL_RATIO = 2;* PCLK_DBG_RATIO = 1;* ATB_RATIO = 6;* PERIPH_RATIO = 7;* COREM1_RATIO = 7;* COREM0_RATIO = 3;* CORE_RATIO = 0;*/CLK_DIV_CPU0 = ((0<<28) | (2<<24) | (1<<20) | (6<<16) | (7<<12) | (7<<8) | (3<<4) | 0); /** CORES_RATIO = 5;* HPM_RATIO = 0;* COPY_RATIO = 6;*/CLK_DIV_CPU1 = ((5 << 8) |(0 << 4) | (6)); /* 2.3 设置控制参数并使能PLL *//* 默认值 */APLL_CON1 = 0x00803800; /** 设置APLL的M,P,S值, APLL输出 = 0xAF x 24MHz / (3 x 2 ^ 0) = 1.4GHz* 使能APLL*/APLL_CON0 = (1<<31 | 0xAF<<16 | 3<<8 | 0x0); /* 3. 设置MUX, 使用APLL的输出 */CLK_SRC_CPU = 0x01000001;}注释的已经很清楚了,需要注意的就是:上电之后 IROM设置了APLL ,CPU工作于APLL提供的时钟;当我们要改变 APLL时,要先使得CPU工作于另一个时钟源,即晶振。设置完APLL后,再让CPU重新工作于APLL提供的时钟。二、编译、烧写实验按照前几节介绍的方法,将程序上传到服务器编译,并烧写到SD卡上,给开发板上电,可以明显感觉到LED闪烁的频率大大提高(比《Tiny4412之C语言实现流水灯》时闪烁的还要快,因为当时CPU运行在400MHZ,现在运行在1.4GHZ),说明我们设置的时钟起作用了,这里就不上图了(上了图大家也看不出来)。第三个小实验实现的目标:参考厂家提供的u-boot代码,设置所有PLL供后续章节使用一、程序说明文件同第一个小实验,只是在它的基础上对system_clock.c文件中的system_clock_init函数进行修改:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118/*函数名:system_clock_init功能:初始化4412的系统时钟最终结果:A=1400000000, M=800000000, E=96000000 V=350000000ARMCLK=1500000000, DMC=400000000, ACLK200=160000000ACLK100=100000000, ACLK160=160000000, ACLK133=133333333*/void system_clock_init(void){/* 1.设置CMU_CPU相关 */CLK_SRC_CPU = 0x0; // 设置CMU_CPU部分中所有的MUX的源 /* 2.设置CMU_DMC相关 *//*CORE_TIMERS_RATIO = 0x0;COPY2_RATIO = 0x0;DMCP_RATIO = 0x1;DMCD_RATIO = 0x1;DMC_RATIO = 0x1;DPHY_RATIO = 0x1;ACP_PCLK_RATIO = 0x1;ACP_RATIO = 0x3;*/CLK_DIV_DMC0 = ((0x0 << 28) | (0x0 << 24) | (0x1 << 20) | (0x1 << 16) | (0x1 << 12) | (0x1 << 8) | (0x1 << 4) | (0x3));CLK_DIV_DMC1 = 0x07071713; /* 3.设置CMU_TOP相关 *//*MUX_ONENAND_SEL = 0x0;MUX_ACLK_133_SEL = 0x0;MUX_ACLK_160_SEL = 0x0;MUX_ACLK_100_SEL = 0x0;MUX_ACLK_200_SEL = 0x0;MUX_VPLL_SEL = 0x1;