韦东山嵌入式Linux学习笔记07--Nandflash

陌路散爱 提交于 2019-11-26 01:05:15

jz2440 v3上面用的nandflash是 K9F2G08U0C, 大小为256MB. 一个页的大小为(2k+64)byte,一个块的大小为(128k+4k)byte,原理图如下:

 

 

 nand的命令集:

 

 

 那应该如何访问nand里面的数据呢?只要读写s32440的相关寄存器,就可以驱动nand的一些引脚.

 

 

 

 

 

 

 Makefile

objs := head.o init.o nand.o main.o #依赖于这些文件

nand.bin : $(objs)
    arm-linux-ld -Tnand.lds    -o nand_elf $^  #链接nand.lds  $^表示所有依赖目标的集合
    arm-linux-objcopy -O binary -S nand_elf $@ #规则中的目标文件集
    arm-linux-objdump -D -m arm  nand_elf > nand.dis

%.o:%.c
    arm-linux-gcc -Wall -c -O2 -o $@ $<

%.o:%.S
    arm-linux-gcc -Wall -c -O2 -o $@ $<

clean:
    rm -f  nand.dis nand.bin nand_elf *.o

nand.lds

SECTIONS { 
  firtst  	0x00000000 : { head.o init.o nand.o}  //第一段的链接地址0x00000000
  second 	0x30000000 : AT(4096) { main.o }  //main放在nand.bin的4096偏移处, 链接地址是0x30000000
}

 head.S

  
.text
.global _start  @start标号作为系统启动最开始执行的地方
_start:
                                            @
            ldr     sp, =4096               @调用c函数之前要先设置好栈的位置,因为内部RAM大小为4k,设置在最末端
            bl      disable_watch_dog       @调用c函数,关看门狗
            bl      memsetup                @设置DDR,这样才能去调用它
            bl      nand_init               @同样,也需要初始化nand

                                            @
                                            @
            ldr     r0,     =0x30000000     @作为nand函数的第一个参数
            mov     r1,     #4096           @作为nand函数的第二个参数
            mov     r2,     #2048           @作为nand函数的第三个参数
            bl      nand_read               @ 

            ldr     sp, =0x34000000         @
            ldr     lr, =halt_loop          @
            ldr     pc, =main               @ת
halt_loop:
            b       halt_loop

 我们先看下nand_init函数

void nand_init(void)
{
#define TACLS   0
#define TWRPH0  3
#define TWRPH1  0

    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;

        s3c2410nand->NFCONF = (1<<15)|(1<<12)|(1<<11)|(TACLS<<8)|(TWRPH0<<4)|(TWRPH1<<0);
    }
    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<<12)|(TWRPH0<<8)|(TWRPH1<<4);
        s3c2440nand->NFCONT = (1<<4)|(1<<1)|(1<<0);
    }

    nand_reset();//
}

  nand_reset函数

static void s3c2440_nand_reset(void)
{
    s3c2440_nand_select_chip();
    s3c2440_write_cmd(0xff);  //写0xff命令代表复位
    s3c2440_wait_idle();
    s3c2440_nand_deselect_chip();
}/////////////////////////////////////static void s3c2440_nand_select_chip(void){    int i;    s3c2440nand->NFCONT &= ~(1<<1); //改位配置成低就选中了芯片    for(i=0; i<10; i++);    }  static void s3c2440_write_cmd(int cmd)//这里的写命令函数其实就是往寄存器NFCMD里面写值{    volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFCMD;    *p = cmd;}static void s3c2410_wait_idle(void){    int i;    volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFSTAT;    while(!(*p & BUSY)) //当这个寄存器的最低位为0时,表示NANDFLASH存在忙状态        for(i=0; i<10; i++);}static void s3c2410_nand_deselect_chip(void)//取消选中与选中时一种相反的操作.{    s3c2410nand->NFCONF |= (1<<11);}

 

 

 

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 ;    
    }
#else
    if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK)) {
        return ;   
    }
#endif	

    nand_select_chip();//选中芯片

    for(i=start_addr; i < (start_addr + size);) {

      write_cmd(0);//先发命令0x00

      /* Write Address */
      write_addr(i);//再发想读的地址
#ifdef LARGER_NAND_PAGE
      write_cmd(0x30);	//再发0x30	
#endif
      wait_idle();//等待空闲

#ifdef LARGER_NAND_PAGE
      for(j=0; j < NAND_SECTOR_SIZE_LP; j++, i++) {
#else
	  for(j=0; j < NAND_SECTOR_SIZE; j++, i++) {
#endif
          *buf = read_data();
          buf++;
      }
    }

    nand_deselect_chip();
    
    return ;
}static unsigned char s3c2410_read_data(void){    volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFDATA;    return *p;}static void s3c2410_write_addr(unsigned int addr) //因为NAND的大小为256MB,用地址来表示需要28位,地址也需要分5次写进去{    int i;    volatile unsigned char *p = (volatile unsigned char *)&s3c2410nand->NFADDR;        *p = addr & 0xff;    for(i=0; i<10; i++);    *p = (addr >> 9) & 0xff;    for(i=0; i<10; i++);    *p = (addr >> 17) & 0xff;    for(i=0; i<10; i++);    *p = (addr >> 25) & 0xff;    for(i=0; i<10; i++);}

  

 

 通过将读出来的地址写道地址偏移为0x30000000之后的内存处.

最后跳到main函数执行,查看效果可知是否拷贝成功.

 

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!