码迷,mamicode.com
首页 > 其他好文 > 详细

嵌入式学习笔记005-flash

时间:2015-04-20 00:38:28      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:嵌入式   tq2440   s3c2440   nor-flash   nand-flash   

  1. flash是用来存储程序的,类似PC机的硬盘,U盘之类的,即使断电数据也不会丢失,与之对应的就是SRAM/SDRAM,而目前嵌入式用的flash主要有nor flash和nand flash两类,nor flash接口可以直接与处理器的地址线相连直接访问,像访问SDRAM那样,所以如果考虑成本的话可以让程序直接上电后从nor flash启动,不过现在追求更快的运行速度,所以都是极小部分运行后将其余所有code copy到SDRAM以求更快的运行速度,nor的缺点也挺多的,价格贵,容量小,更要命的是只能像SDRAM那样读而不能像SDRAM那样写~~~nand flash有点跟nor相反,价格便宜,容量大,不过有个致命的缺点,那就是它不能与处理器的地址线直接相连像操作SDRAM那样,也就是说nand flash像是一个普通的器件而已,几根线连接有点像SD卡那样,这一点必须有清醒的认知,处理器的地址只能映射到nor flash 和 SDRAM,不能映射到nand flash,可以理解它类似SD卡,所以处理器当然不会映射SD卡地址,这就导致一个问题,如果是从nand flash启动怎么办,很明显是没办法启动的,所以S3C2440处理器内置了4k的SRAM,会自动的将nand 前4kcode copy到SRAM并从SRAM开始跑程序,所以,S3C2440有两种启动方式,要么从norflash直接启动,要么从nand flash 启动,不过是先将前4k code copy 到内部SRAM再启动,大于4k部分就无能为力了,所以如果是从nand启动的话必须在前4k实现将后续的code copy 到外设的SDRAM去再跳转到SDRAM去!
  2. 本实验提供一函数接口同时支持从nor 、nand flash 启动并且支持S3C2410 S3C2440处理器,不过只在S3C2440 nandflash验证过!!需要注意的是我是参照韦东山老是稍微改造的,因为我的nand flash是K9F2G08R0A -256M x 8 Bit NAND Flash Memory,所以在原有的code进行修改,主要不同的是写addr地址时钟周期和地址bit不一样,还是就是读指令不一样以及page大小。
  3. 先针对nand 分析,请读者自行测试nor flash,查看tq2440开发板相关引脚和S3C2440手册可以发现:
    技术分享
    技术分享
    所以K9F2G08R0A的配置是page=2KB,address_cycle = 5 8bit_data
    其写地址时序和相关指令如下:
    技术分享
    技术分享
    具体的会在贴code时讲解。
  4. 本实验的目的是将启动code放在nand最前面,跟前几个实验一样,只不过将main函数放在nand的地址4096处,这样处理器只能将前4k code考到内部的SRAM,然后我们再通过操作nand将4096处的一段code copy 到SDRAM再跳转到SDRAM处运行从而检验是否成功操作nand!
    有如下6个文件:
    head.S init.c main.c Makefile nand.c nand.lds

    head.S

1 @******************************************************************************                                                 
2 @ File:head.s
3 @ 功能:设置SDRAM,将程序复制到SDRAM,然后跳到SDRAM继续执行
4 @******************************************************************************
5 
6 .text
7 .global _start
8 _start:
9             ldr     sp, =4096                   @设置堆栈
10             bl      disable_watch_dog           @关WATCH DOG
11             bl      memsetup                    @初始化SDRAM
12             ldr     r0,     =4096               @1.  源地址   = 4096,连接的时候,main.c中的代码都存在NAND Flash地址4096开始处
13             ldr     r1,     =0x30000000         @2. 目标地址=0x30000000,这是SDRAM的起始地址
14             ldr     r2,     =2048               @3.  复制长度= 2048(bytes),对于本实验的main.c,这是足够了
15             bl      CopyCode2Ram                @调用C函数nand_read ,自动识别nand nor flash,同时支持2410、2440
16 
17             ldr     sp, =0x34000000             @设置栈
18             ldr     lr, =halt_loop              @设置返回地址
19             ldr     pc, =main                   @b指令和bl指令只能前后跳转32M的范围,所以这里使用向pc赋值的方法进行跳转
20 halt_loop:
21             b       halt_loop

这个和前一篇博文操作SDRAM类似,只不过一个是从SRAM->SDRAM, 另一个是 nand-> SDRAM,重点在CopyCode2Ram,就是实现nand 到 SDRAM,后后面会提及。

init.c

  1 struct mem_reg{                                                                                                                 
  2     unsigned long BWSCON;
  3     unsigned long BANKCON0;
  4     unsigned long BANKCON1;
  5     unsigned long BANKCON2;
  6     unsigned long BANKCON3;
  7     unsigned long BANKCON4;
  8     unsigned long BANKCON5;
  9     unsigned long BANKCON6;
 10     unsigned long BANKCON7;
 11     unsigned long REFRESH;
 12     unsigned long BANKSIZE;
 13     unsigned long MRSRB6;
 14     unsigned long MRSRB7;
 15 } ;
 16 
 17 int disable_watch_dog()
 18 {
 19     (*(volatile unsigned long *)0x53000000) = 0;
 20 
 21     return 0;
 22 }
 23 
 24 int memsetup()
 25 {
 26     struct mem_reg *p_mem_reg_cfg =(struct mem_reg * )0x48000000; //memery registers base address
 27     p_mem_reg_cfg->BWSCON   = 0x22011110;
 28     p_mem_reg_cfg->BANKCON0 = 0x00000700;
 29     p_mem_reg_cfg->BANKCON1 = 0x00000700;
 30     p_mem_reg_cfg->BANKCON2 = 0x00000700;
 31     p_mem_reg_cfg->BANKCON3 = 0x00000700;
 32     p_mem_reg_cfg->BANKCON4 = 0x00000700;
 33     p_mem_reg_cfg->BANKCON5 = 0x00000700;                                                                                       
 34     p_mem_reg_cfg->BANKCON6 = 0x00018005;
 35     p_mem_reg_cfg->BANKCON7 = 0x00018005;
 36     p_mem_reg_cfg->REFRESH  = 0x008c07a3;
 37     p_mem_reg_cfg->BANKSIZE = 0x000000b1;
 38     p_mem_reg_cfg->MRSRB6   = 0x00000030;
 39     p_mem_reg_cfg->MRSRB7   = 0x00000030;
 40 
 41     return 0;
 42 }
 43 

main.c

  1 #define GPBCON          (*(volatile unsigned long *)0x56000010)                                                                 
  2 #define GPBDAT          (*(volatile unsigned long *)0x56000014)
  3 
  4 int main()
  5 {
  6     GPBCON |= ((1<<14));    // set led3 output
  7     GPBDAT &= ~((1<<7) );   // enable led3
  8 
  9     return 0;
 10 }
 11 

Makefile:

  1 objs := head.o init.o nand.o main.o                                                                                             
  2 
  3 nand.bin : $(objs)
  4     arm-linux-ld -Tnand.lds -o nand_elf $^
  5     arm-linux-objcopy -O binary -S nand_elf $@
  6     arm-linux-objdump -D -m arm  nand_elf > nand.dis
  7 
  8 %.o:%.c
  9     arm-linux-gcc -Wall -c -O2 -o $@ $<
 10 
 11 %.o:%.S
 12     arm-linux-gcc -Wall -c -O2 -o $@ $<
 13 
 14 clean:
 15     rm -f  nand.dis nand.bin nand_elf *.o

关于makefile的用法这里就不累赘了~~~

nand.lds

  1 SECTIONS {                                                                                                                      
  2   firtst    0x00000000 : { head.o init.o nand.o}
  3   second    0x30000000 : AT(4096) { main.o }
  4 }
  5 

关于SECTION的重点要理解运行地址和加载地址的区别以及elf格式和bin文件的区别,请看本空间另一博文《…………………..未写………………….》

最后就是nand.c


#define GSTATUS1        (*(volatile unsigned int *)0x560000B0)
#define BUSY            1
#define NAND_SECTOR_SIZE    2048
#define NAND_BLOCK_MASK     (NAND_SECTOR_SIZE - 1)

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;
static S3C2440_NAND * s3c2440nand = (S3C2440_NAND *)0x4e000000;

static t_nand_chip nand_chip;

/* 供外部调用的函数 */
int CopyCode2Ram(unsigned long start_addr, unsigned char *buf, 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);
static void nand_init(void);
static void nand_read(unsigned char *buf, unsigned long start_addr, int size);

/* 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))
        for(i=0; i<10; i++);
}

/* 发出片选信号 */
static void s3c2410_nand_select_chip(void)
{
    int i;
    s3c2410nand->NFCONF &= ~(1<<11);
    for(i=0; i<10; i++);    
}

/* 取消片选信号 */
static void s3c2410_nand_deselect_chip(void)
{
    s3c2410nand->NFCONF |= (1<<11);
}

/* 发出命令 */
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=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++);
}

/* 读取数据 */
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=0; i<10; i++);
}

/* 发出片选信号 */
static void s3c2440_nand_select_chip(void)
{
    int i;
    s3c2440nand->NFCONT &= ~(1<<1);
    for(i=0; i<10; i++);    
}

/* 取消片选信号 */
static void s3c2440_nand_deselect_chip(void)
{
    s3c2440nand->NFCONT |= (1<<1);
}

/* 发出命令 */
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=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++);
}
*/
static void s3c2440_write_addr(unsigned int addr)
{
    int i,col, page;
    volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFADDR;

    col = addr & NAND_BLOCK_MASK;
    page = addr / NAND_SECTOR_SIZE;

    *p = col & 0xff;            /* Column Address A0~A7 */
    for(i=0; i<10; i++);        
    *p = (col >> 8) & 0x0f;     /* Column Address A8~A11 */
    for(i=0; i<10; i++);
    *p = page & 0xff;           /* Row Address A12~A19 */
    for(i=0; i<10; i++);
    *p = (page >> 8) & 0xff;    /* Row Address A20~A27 */
    for(i=0; i<10; i++);
    *p = (page >> 16) & 0x01;   /* Row Address A28*/
    for(i=0; i<10; 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=0; i<10; 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 */
static 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 = (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;
        nand_chip.write_addr         = s3c2440_write_addr;
        nand_chip.read_data          = s3c2440_read_data;

        /* 设置时序 */
        s3c2440nand->NFCONF = (TACLS<<12)|(TWRPH0<<8)|(TWRPH1<<4);
        /* 使能NAND Flash控制器, 初始化ECC, 禁止片选 */
        s3c2440nand->NFCONT = (1<<4)|(1<<1)|(1<<0);
    }

    /* 复位NAND Flash */
    nand_reset();
}

/* 读函数 */
static void nand_read(unsigned char *buf, unsigned long start_addr, int size)
{
    int i, j;

    if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK)) {
        return ;    /* 地址或长度不对齐 */
    }

    /* 选中芯片 */
    nand_select_chip();

    for(i=start_addr; i < (start_addr + size);) {
      /* 发出READ0命令 */
      write_cmd(0);

      /* Write Address */
      write_addr(i);
    write_cmd(0x30);  //see K9F2G08R0A manual
      wait_idle();

      for(j=0; j < NAND_SECTOR_SIZE; j++, i++) {
          *buf = read_data();
          buf++;
      }
    }

    /* 取消片选信号 */
    nand_deselect_chip();

    return ;
}

static int bBootFrmNORFlash(void)
{
    volatile unsigned int *pdw = (volatile unsigned int *)0;
    unsigned int dwVal;

    /*
     * 鏃犺鏄粠NOR Flash杩樻槸浠嶯AND Flash鍚姩锛?
     * 鍦板潃0澶勪负鎸囦护"b Reset", 鏈哄櫒鐮佷负0xEA00000B锛?
     * 瀵逛簬浠嶯AND Flash鍚姩鐨勬儏鍐碉紝鍏跺紑濮?KB鐨勪唬鐮佷細澶嶅埗鍒癈PU鍐呴儴4K鍐呭瓨涓紝
     * 瀵逛簬浠嶯OR Flash鍚姩鐨勬儏鍐碉紝NOR Flash鐨勫紑濮嬪湴鍧€鍗充负0銆?
     * 瀵逛簬NOR Flash锛屽繀椤婚€氳繃涓€瀹氱殑鍛戒护搴忓垪鎵嶈兘鍐欐暟鎹紝
     * 鎵€浠ュ彲浠ユ牴鎹繖鐐瑰樊鍒潵鍒嗚鲸鏄粠NAND Flash杩樻槸NOR Flash鍚姩:
     * 鍚戝湴鍧€0鍐欏叆涓€涓暟鎹紝鐒跺悗璇诲嚭鏉ワ紝濡傛灉娌℃湁鏀瑰彉鐨勮瘽灏辨槸NOR Flash
     */

    dwVal = *pdw;       
    *pdw = 0x12345678;
    if (*pdw != 0x12345678)
    {
        return 1;
    }
    else
    {
        *pdw = dwVal;
        return 0;
    }
}

int CopyCode2Ram(unsigned long start_addr, unsigned char *buf, int size)
{
    unsigned int *pdwDest;
    unsigned int *pdwSrc;
    int i;

    if (bBootFrmNORFlash())
    {
        pdwDest = (unsigned int *)buf;
        pdwSrc  = (unsigned int *)start_addr;
        /* 浠?NOR Flash鍚姩 */
        for (i = 0; i < size / 4; i++)
        {
            pdwDest[i] = pdwSrc[i];
        }
        return 0;
    }
    else
    {
        /* 鍒濆鍖朜AND Flash */
        nand_init();

        /* 浠?NAND Flash鍚姩 */
        nand_read(buf, start_addr, (size + NAND_BLOCK_MASK)&~(NAND_BLOCK_MASK));
        return 0;
    }
}

CopyCode2Ram()支持从nor或nand copy 到 SDRAM,同时支持S3C2440/S3C2410,不过每次最少copy一个page即2KB

嵌入式学习笔记005-flash

标签:嵌入式   tq2440   s3c2440   nor-flash   nand-flash   

原文地址:http://blog.csdn.net/fzk374470412/article/details/45133625

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!