nand flash相关

关于nandflash的说明,请参考其他。

现在先贴出来韦东山先生的代码,作我学习之用。

 @************************************************
@ File:head.s
@ 功能:设置SDRAM,将程序复制到SDRAM,然后跳到SDRAM继续执行
@************************************************ .text
.global _start
_start:
@函数disable_watch_dog, memsetup, init_nand, nand_read_ll在init.c中定义
ldr sp, = @设置堆栈
bl disable_watch_dog @关WATCH DOG
bl memsetup @初始化SDRAM
bl nand_init @初始化NAND Flash @将NAND Flash中地址4096开始的1024字节代码(main.c编译得到)复制到SDRAM中
@nand_read_ll函数需要3个参数:
ldr r0, =0x30000000 @1. 目标地址=0x30000000,这是SDRAM的起始地址
mov r1, # @2. 源地址 = ,连接的时候,main.c中的代码都存在NAND Flash地址4096开始处
mov r2, # @3. 复制长度= (bytes),对于本实验的main.c,这是足够了
bl nand_read @调用C函数nand_read ldr sp, =0x34000000 @设置栈
ldr lr, =halt_loop @设置返回地址
ldr pc, =main @b指令和bl指令只能前后跳转32M的范围,所以这里使用向pc赋值的方法进行跳转 halt_loop:
b halt_loop

上面的代码的作用很明显,现在主要说明地址相关的东西;

ldr     sp, =4096               @设置堆栈   首先上电后4k代码会自动拷贝到steppingstone里面,disable_watch_dog,memsetup等函数都是在这4k范围里面。

后面的链接脚本如下:

 SECTIONS {
firtst 0x00000000 : { head.o init.o nand.o} @在nand flash里面地址为0x0000_0000初存放head.o,init.o,nand.o等
second 0x30000000 : AT() { main.o } @在4096处,注意此时是4k以外,这个代码就是指出当代码大小大于4k时的处理方法。
}                                @可以看到main.o的运行地址是0x3000_0000,说明运行4k以后的代码需要复制到sdram里面执行。

上面说明main函数是在刚才提到的4k范围以外。所以head文件里面会有将4096(4k)的main函数编译出来的东西拷贝到3000_0000也就是sdram的地方。

这个在以后程序比较大的时候基本是比较通用的函数,所以一定得花时间搞明白。上面简单的说明来地址映射方面的问题。

再看init.c,这个文件只是关闭看门狗和初始化sdram:

 /* WOTCH DOG register */
#define WTCON (*(volatile unsigned long *)0x53000000) /* SDRAM regisers */
#define MEM_CTL_BASE 0x48000000 //定义寄存器的方法 void disable_watch_dog();
void memsetup(); /*上电后,WATCH DOG默认是开着的,要把它关掉 */
void disable_watch_dog()
{
WTCON = ;
} /* 设置控制SDRAM的13个寄存器 */
void memsetup()
{
int i = ;
unsigned long *p = (unsigned long *)MEM_CTL_BASE; /* SDRAM 13个寄存器的值 */
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
}; for(; i < ; i++)
p[i] = mem_cfg_val[i];
}

上面设置sdram寄存器的方法是用c语言,现在贴出汇编的方法来比较;

 memsetup:
@ 设置存储控制器以便使用SDRAM等外设 mov r1, #MEM_CTL_BASE @ 存储控制器的13个寄存器的开始地址
adrl r2, mem_cfg_val @ 这13个值的起始存储地址
add r3, r1, # @ * =
:
ldr r4, [r2], # @ 读取设置值,并让r2加4
str r4, [r1], # @ 将此值写入寄存器,并让r1加4
cmp r1, r3 @ 判断是否设置完所有13个寄存器
bne 1b @ 若没有写成,继续
mov pc, lr @ 返回 .align
mem_cfg_val:
@ 存储控制器13个寄存器的设置值
.long 0x22011110 @ BWSCON
.long 0x00000700 @ BANKCON0
.long 0x00000700 @ BANKCON1
.long 0x00000700 @ BANKCON2
.long 0x00000700 @ BANKCON3
.long 0x00000700 @ BANKCON4
.long 0x00000700 @ BANKCON5
.long 0x00018005 @ BANKCON6
.long 0x00018005 @ BANKCON7
.long 0x008C07A3 @ REFRESH
.long 0x000000B1 @ BANKSIZE
.long 0x00000030 @ MRSRB6
.long 0x00000030 @ MRSRB7

同样是往这13个寄存器写值。

而main函数只是led的亮灭,这里就不贴出来了

现在主要看nand.c文件。

先贴出来如下:

//===================================我是分解线=============================

//@nand.c

 #define LARGER_NAND_PAGE

 #define GSTATUS1        (*(volatile unsigned int *)0x560000B0)
#define BUSY 1 #define NAND_SECTOR_SIZE 512 @小页
#define NAND_BLOCK_MASK (NAND_SECTOR_SIZE - 1) 511 0b1_1111_1111 #define NAND_SECTOR_SIZE_LP 2048 @2k 大页
#define NAND_BLOCK_MASK_LP (NAND_SECTOR_SIZE_LP - 1) @2047 0b111_1111_1111 typedef unsigned int S3C24X0_REG32; /* NAND FLASH (see S3C2410 manual chapter 6) */
typedef struct {
S3C24X0_REG32 NFCONF;
S3C24X0_REG32 NFCMD;
S3C24X0_REG32 NFADDR;
S3C24X0_REG32 NFDATA;
S3C24X0_REG32 NFSTAT;
S3C24X0_REG32 NFECC;
} S3C2410_NAND; /* NAND FLASH (see S3C2440 manual chapter 6, www.100ask.net) */
typedef struct {
S3C24X0_REG32 NFCONF;
S3C24X0_REG32 NFCONT;
S3C24X0_REG32 NFCMD;
S3C24X0_REG32 NFADDR;
S3C24X0_REG32 NFDATA;
S3C24X0_REG32 NFMECCD0;
S3C24X0_REG32 NFMECCD1;
S3C24X0_REG32 NFSECCD;
S3C24X0_REG32 NFSTAT;
S3C24X0_REG32 NFESTAT0;
S3C24X0_REG32 NFESTAT1;
S3C24X0_REG32 NFMECC0;
S3C24X0_REG32 NFMECC1;
S3C24X0_REG32 NFSECC;
S3C24X0_REG32 NFSBLK;
S3C24X0_REG32 NFEBLK;
} S3C2440_NAND; typedef struct {
void (*nand_reset)(void);
void (*wait_idle)(void);
void (*nand_select_chip)(void);
void (*nand_deselect_chip)(void);
void (*write_cmd)(int cmd);
void (*write_addr)(unsigned int addr);
unsigned char (*read_data)(void);
}t_nand_chip; static S3C2410_NAND * s3c2410nand = (S3C2410_NAND *)0x4e000000; //nand flash相关寄存器的初始地址。s3c2410nand是S3C2410_NAND的实例化。
static S3C2440_NAND * s3c2440nand = (S3C2440_NAND *)0x4e000000;
 static t_nand_chip nand_chip;

 /* 供外部调用的函数 */   head.S中用到
void nand_init(void);
void nand_read(unsigned char *buf, unsigned long start_addr, int size); /* NAND Flash操作的总入口, 它们将调用S3C2410或S3C2440的相应函数 */
static void nand_reset(void);
static void wait_idle(void);
static void nand_select_chip(void);
static void nand_deselect_chip(void);
static void write_cmd(int cmd);
static void write_addr(unsigned int addr);
static unsigned char read_data(void); /* S3C2410的NAND Flash处理函数 */
static void s3c2410_nand_reset(void);
static void s3c2410_wait_idle(void);
static void s3c2410_nand_select_chip(void);
static void s3c2410_nand_deselect_chip(void);
static void s3c2410_write_cmd(int cmd);
static void s3c2410_write_addr(unsigned int addr);
static unsigned char s3c2410_read_data(); /* S3C2440的NAND Flash处理函数 */
static void s3c2440_nand_reset(void);
static void s3c2440_wait_idle(void);
static void s3c2440_nand_select_chip(void);
static void s3c2440_nand_deselect_chip(void);
static void s3c2440_write_cmd(int cmd);
static void s3c2440_write_addr(unsigned int addr);
static unsigned char s3c2440_read_data(void); /* S3C2410的NAND Flash操作函数 */ /* 复位 */
static void s3c2410_nand_reset(void)
{
s3c2410_nand_select_chip();
s3c2410_write_cmd(0xff); // 复位命令
s3c2410_wait_idle();
s3c2410_nand_deselect_chip();
} /* 等待NAND Flash就绪 */
static void s3c2410_wait_idle(void)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFSTAT;
while(!(*p & BUSY)) //这里可以用来表示NFSTAT的第一位吗?疑惑
for(i=; i<; i++);
} /* 发出片选信号 */
static void s3c2410_nand_select_chip(void)
{
int i;
s3c2410nand->NFCONF &= ~(<<);
for(i=; i<; i++);
} /* 取消片选信号 */
static void s3c2410_nand_deselect_chip(void)
{
s3c2410nand->NFCONF |= (<<);
} /* 发出命令 */
static void s3c2410_write_cmd(int cmd)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFCMD;
*p = cmd;
} /* 发出地址 */
static void s3c2410_write_addr(unsigned int addr)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFADDR; *p = addr & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
} /* 读取数据 */
static unsigned char s3c2410_read_data(void)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFDATA;
return *p;
} /* S3C2440的NAND Flash操作函数 */ /* 复位 */
static void s3c2440_nand_reset(void)
{
s3c2440_nand_select_chip();
s3c2440_write_cmd(0xff); // 复位命令
s3c2440_wait_idle();
s3c2440_nand_deselect_chip();
} /* 等待NAND Flash就绪 */
static void s3c2440_wait_idle(void)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFSTAT;
while(!(*p & BUSY))
for(i=; i<; i++);
} /* 发出片选信号 */
static void s3c2440_nand_select_chip(void)
{
int i;
s3c2440nand->NFCONT &= ~(<<);
for(i=; i<; i++);
} /* 取消片选信号 */
static void s3c2440_nand_deselect_chip(void)
{
s3c2440nand->NFCONT |= (<<);
} /* 发出命令 */
static void s3c2440_write_cmd(int cmd)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFCMD;
*p = cmd;
} /* 发出地址 */
static void s3c2440_write_addr(unsigned int addr)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFADDR; *p = addr & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
*p = (addr >> ) & 0xff;
for(i=; i<; i++);
} static void s3c2440_write_addr_lp(unsigned int addr)
{
int i;
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFADDR;
int col, page; col = addr & NAND_BLOCK_MASK_LP;
page = addr / NAND_SECTOR_SIZE_LP; *p = col & 0xff; /* Column Address A0~A7 */
for(i=; i<; i++);
*p = (col >> ) & 0x0f; /* Column Address A8~A11 */
for(i=; i<; i++);
*p = page & 0xff; /* Row Address A12~A19 */
for(i=; i<; i++);
*p = (page >> ) & 0xff; /* Row Address A20~A27 */
for(i=; i<; i++);
*p = (page >> ) & 0x03; /* Row Address A28~A29 */
for(i=; i<; i++);
} /* 读取数据 */
static unsigned char s3c2440_read_data(void)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFDATA;
return *p;
} /* 在第一次使用NAND Flash前,复位一下NAND Flash */
static void nand_reset(void)
{
nand_chip.nand_reset();
} static void wait_idle(void)
{
nand_chip.wait_idle();
} static void nand_select_chip(void)
{
int i;
nand_chip.nand_select_chip();
for(i=; i<; i++);
} static void nand_deselect_chip(void)
{
nand_chip.nand_deselect_chip();
} static void write_cmd(int cmd)
{
nand_chip.write_cmd(cmd);
}
static void write_addr(unsigned int addr)
{
nand_chip.write_addr(addr);
} static unsigned char read_data(void)
{
return nand_chip.read_data();
} /* 初始化NAND Flash */
void nand_init(void)
{
#define TACLS 0
#define TWRPH0 3
#define TWRPH1 0 /* 判断是S3C2410还是S3C2440 */
if ((GSTATUS1 == 0x32410000) || (GSTATUS1 == 0x32410002))
{
nand_chip.nand_reset = s3c2410_nand_reset;
nand_chip.wait_idle = s3c2410_wait_idle;
nand_chip.nand_select_chip = s3c2410_nand_select_chip;
nand_chip.nand_deselect_chip = s3c2410_nand_deselect_chip;
nand_chip.write_cmd = s3c2410_write_cmd;
nand_chip.write_addr = s3c2410_write_addr;
nand_chip.read_data = s3c2410_read_data; /* 使能NAND Flash控制器, 初始化ECC, 禁止片选, 设置时序 */
s3c2410nand->NFCONF = (<<)|(<<)|(<<)|(TACLS<<)|(TWRPH0<<)|(TWRPH1<<);
}
else
{
nand_chip.nand_reset = s3c2440_nand_reset;
nand_chip.wait_idle = s3c2440_wait_idle;
nand_chip.nand_select_chip = s3c2440_nand_select_chip;
nand_chip.nand_deselect_chip = s3c2440_nand_deselect_chip;
nand_chip.write_cmd = s3c2440_write_cmd;
#ifdef LARGER_NAND_PAGE
nand_chip.write_addr = s3c2440_write_addr_lp;
#else
nand_chip.write_addr = s3c2440_write_addr;
#endif
nand_chip.read_data = s3c2440_read_data; /* 设置时序 */
s3c2440nand->NFCONF = (TACLS<<)|(TWRPH0<<)|(TWRPH1<<);
/* 使能NAND Flash控制器, 初始化ECC, 禁止片选 */
s3c2440nand->NFCONT = (<<)|(<<)|(<<);
} /* 复位NAND Flash */
nand_reset();
} /* 读函数 */
void nand_read(unsigned char *buf, unsigned long start_addr, int size)
{
int i, j; #ifdef LARGER_NAND_PAGE
if ((start_addr & NAND_BLOCK_MASK_LP) || (size & NAND_BLOCK_MASK_LP)) {
return ; /* 地址或长度不对齐 */ @与2047相与,即与0b111_1111_1111相与,就可以得到2048的倍数,就是对其与否
}
#else
if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK)) {
return ; /* 地址或长度不对齐 */
}
#endif /* 选中芯片 */
nand_select_chip(); for(i=start_addr; i < (start_addr + size);) {
/* 发出READ0命令 */
write_cmd(); /* Write Address */
write_addr(i);
#ifdef LARGER_NAND_PAGE
write_cmd(0x30);
#endif
wait_idle(); #ifdef LARGER_NAND_PAGE
for(j=; j < NAND_SECTOR_SIZE_LP; j++, i++) {
#else
for(j=; j < NAND_SECTOR_SIZE; j++, i++) {
#endif
*buf = read_data();
buf++;
}
} /* 取消片选信号 */
nand_deselect_chip(); return ;
}
上一篇:[跟我学spring学习笔记][DI循环依赖]


下一篇:web前端入门