.text
.global _start
_start:
ldr sp,=4096 @设置栈指针,以下都是C函数,调用前需要设好栈
bl disable_watch_dog @关闭WATCHDOG,否则CPU会不断重启
bl memsetup @设置好存储控制器以使用SDRAM
bl copy_2th_to_sdram @将第二部分代码复制到SDRAM
bl create_page_table @设置页表
bl mmu_init @启动MMU
ldr sp,=0xb4000000 @重设栈指针,指向SDRAM顶端(使用虚拟地址)
ldr pc,=0xb0004000 @跳到SDRAM中继续执行第二部份代码
halt_loop:
b halt_loop
#define WTCON (*(volatile unsigned long *)0x53000000)
#define MEM_CTL_BASE 0X48000000
void disable_watch_dog(void)
{
WTCON=0; //关闭WATCHDOG,WATCHDOG寄存器设为0
}
void memsetup(void)
{
unsigned long const mem_cfg_val[]={ 0x22011110, //BWSCON
0x00000700, //BANKCON0
0x00000700, //BANKCON1
0x00000700, //BANKCON2
0x00000700, //BANKCON3
0x00000700, //BANKCON4
0x00000700, //BANKCON5
0x00018005, //BANKCON6
0x00018005, //BANKCON7
0x008c07a3, //REFRESH
0x000000b1, //BANKSIZE
0x00000030, //MRSRB6
0X00000030, //MRSRB7
};
int i=0;
volatile unsigned long *p=(volatile unsigned long *)MEM_CTL_BASE;
for(;i<13;i++)
{
p[i]=mem_cfg_val[i];
}
}
void copy_2th_to_sdram(void)
{
unsigned int *pdwSrc=(unsigned int *)2048;
unsigned int *pdwDest=(unsigned int *)0x30004000;
while(pdwSrc<(unsigned int *)4096)
{
*pdwDest=*pdwSrc;
pdwDest++;
pdwSrc++;
}
}
void create_page_table(void)
{
//用于段描述符的一些宏定义
#define MMU_FULL_ACCESS (3<<10) //访问权限
#define MMU_DOMAIN (0<<5) //属于那个域
#define MMU_SPECIAL (1<<4) //必须是1
#define MMU_CACHEABLE (1<<3)
#define MMU_BUFFERABLE (1<<2)
#define MMU_SECTION (2)
#define MMU_SECDESC (MMU_FULL_ACCESS|MMU_DOMAIN|MMU_SPECIAL|MMU_SECTION)
#define MMU_SECDESC_WB (MMU_FULL_ACCESS|MMU_DOMAIN|MMU_SPECIAL|MMU_CACHEABLE|\
MMU_BUFFERABLE|MMU_SECTION)
#define MMU_SECTION_SIZE 0x00100000
unsigned long virtuladdr,physicaladdr;
unsigned long *mmu_tlb_base=(unsigned long *)0x30000000;
virtuladdr=0;
physicaladdr=0;
*(mmu_tlb_base+(virtuladdr>>20))=(physicaladdr&0xfff00000)|MMU_SECDESC_WB;
virtuladdr=0xa0000000;
physicaladdr=0x56000000;
*(mmu_tlb_base+(virtuladdr>>20))=(physicaladdr&0xfff00000)|MMU_SECDESC;
virtuladdr=0xb0000000;
physicaladdr=0x30000000;
while(virtuladdr<0xb4000000)
{
*(mmu_tlb_base+(virtuladdr>>20))=(physicaladdr&0xfff00000)|MMU_SECDESC_WB;
virtuladdr+=0x100000;
physicaladdr+=0x100000;
}
}
void mmu_init(void)
{
unsigned long ttb=0x30000000;
__asm__(
"mov r0,#0\n"
"mcr p15,0,r0,c7,c7,0\n" //使无效ICches和DCaches
"mcr p15,0,r0,c7,c10,4\n" //drain write buffer on v4
"mcr p15,0,r0,c8,c7,0\n" //使无效指令,数据TLB
"mov r4,%0\n" //r4=页表基址
"mcr p15,0,r4,c2,c0,0\n" //设置页表基址寄存器
"mvn r0,#0\n"
"mcr p15,0,r0,c3,c0,0\n"
//对控制寄存器,先读出其值,在这基础上修改位,然后在写入
"mrc p15,0,r0,c1,c0,0\n" //读出控制就能器的值
//先清除不需要的位,往下若需要则重新设置
"bic r0,r0,#0x3000\n" //.RVI ..RS B... .CAM
"bic r0,r0,#0x0300\n" //..11 ..... .... ....清除V.I位
"bic r0,r0,#0x0087\n" //.... .... 1... .111清除B/C/A/M
//设置需要的位
"orr r0,r0,#0x0002\n" //开启对其检查
"orr r0,r0,#0x0004\n" //开启DCaches
"orr r0,r0,#0x1000\n" //开启ICaches
"orr r0,r0,#0x0001\n" //使能MMU
"mcr p15,0,r0,c1,c0,0\n" //将修改的值写入控制寄存器
: //无输出
:"r"(ttb) );
}
#define GPFCON (*(volatile unsigned long *)0xa0000050)
#define GPFDAT (*(volatile unsigned long *)0xa0000054)
#define GPF4_OUT (1<<(2*4))
#define GPF5_OUT (1<<(2*5))
#define GPF6_OUT (1<<(2*6))
static inline void wait(volatile unsigned long dly)
{
for(;dly>0;dly--);
}
int main(void)
{
unsigned long i=0;
GPFCON=GPF4_OUT|GPF5_OUT|GPF6_OUT;
while(1)
{
wait(30000);
GPFDAT=(~(i<<4));
if(++i==8)
i=0;
}
return 0;
}
mmu.lds:
SECTIONS{
firtst 0x00000000 : { head.o init.o}
second 0xb0004000 : AT(2048) { leds.o }
}
Makefile:
objs := head.o init.o leds.o
mmu.bin: $(objs)
arm-linux-ld -Tmmu.lds -o mmu_elf $^
arm-linux-objcopy -O binary -S mmu_elf $@
arm-linux-objdump -D -m arm mmu_elf > mmu.dis
%.o:%.c
arm-linux-gcc -Wall -O2 -c -o $@ $<
%.o:%.S
arm-linux-gcc -Wall -O2 -c -o $@ $<
clean:
rm -f mmu.bin mmu_elf mmu.dis *.o
上一篇:ARM笔记: NAND Flash程序
下一篇:ARM筆記:定时器中断的应用
推荐阅读最新更新时间:2024-03-16 15:45