新聞中心

ARM·Nand Flash的控制

作者: 時間:2016-11-24 來源:網(wǎng)絡(luò) 收藏
【本章總結(jié)】
個人覺得這章內(nèi)容過于復(fù)雜,只需要記住如果nand中4096的程序想要被拷貝出來執(zhí)行,就把這個代碼寫進(jìn)去,紅色部分是真正的代碼,可以復(fù)制過去,直接使用,其他就不做了解了。聽了一個小時,這個老師講的也挺亂的,也沒有人能記住這么多東西吧。。。
【head.S init.c main.c nand.c Makefile】
代碼詳解,這里先把一部分代碼存放在NAND Flash 地址4096之后,當(dāng)程序啟動后通過NAND Flash控制器將他們讀出來、執(zhí)行。
注意:以前的代碼都小于4096,開發(fā)板啟動后他們被自動復(fù)制進(jìn)“Steppingstone”;
現(xiàn)在的代碼在4096之后,需要控制NAND Flash將他們讀出來、執(zhí)行
【nand.lds】
SECTIONS {
firtst 0x00000000 : { head.o init.o nand.o}
second 0x30000000 : AT(4096) { main.o }
}
對于這里,我是這樣理解的 。head.o init.o nand.o都放在Nand flash0地址處,0x0000 0000是他的鏈接地址,也就是放在0x0000 0000處運(yùn)行;mian.o存放在Nand flash 4096地址處,0x3000 0000是他的鏈接地址,也就是放在0x3000 0000處運(yùn)行
(可能也就是前一陣子看到的鏈接地址和加載地址的區(qū)別)
當(dāng)開發(fā)板啟動時,前4k的代碼被考進(jìn)SRAM,所以我們需要在SRAM內(nèi)完成初始化和跳轉(zhuǎn)
【head.s】
SECTIONS {
firtst 0x00000000 : { head.o init.o nand.o}
second 0x30000000 : AT(4096) { main.o }
} @******************************************************************************
@ File:head.s
@ 功能:設(shè)置SDRAM,將程序復(fù)制到SDRAM,然后跳到SDRAM繼續(xù)執(zhí)行
@******************************************************************************
.text
.global _start
_start:
@函數(shù)disable_watch_dog, memsetup, init_nand, nand_read_ll在init.c中定義
ldr sp, =4096 @設(shè)置堆棧
bl disable_watch_dog @關(guān)WATCH DOG
bl memsetup @初始化SDRAM
bl nand_init @初始化NAND Flash
@將NAND Flash中地址4096開始的1024字節(jié)代碼(main.c編譯得到)復(fù)制到SDRAM中
@nand_read_ll函數(shù)需要3個參數(shù):
ldr r0, =0x30000000 @1. 目標(biāo)地址=0x30000000,這是SDRAM的起始地址
mov r1, #4096 @2. 源地址 = 4096,連接的時候,main.c中的代碼都存在NAND Flash地址4096開始處
mov r2, #2048 @3. 復(fù)制長度= 2048(bytes),對于本實驗的main.c,這是足夠了
bl nand_read @調(diào)用C函數(shù)nand_read
ldr sp, =0x34000000 @設(shè)置棧
ldr lr, =halt_loop @設(shè)置返回地址
ldr pc, =main @b指令和bl指令只能前后跳轉(zhuǎn)32M的范圍,所以這里使用向pc賦值的方法進(jìn)行跳轉(zhuǎn)
halt_loop:
b halt_loop
*對于這一串代碼,很多都是前面學(xué)過的知識,這里我們只關(guān)注配置Nand的紅色代碼
bl nand_init
*目標(biāo)地址,源地址,目標(biāo)長度
ldr r0, =0x30000000 @1. 目標(biāo)地址=0x30000000,這是SDRAM的起始地址
mov r1, #4096 @2. 源地址 = 4096,連接的時候,main.c中的代碼都存在NAND Flash地 址4096開始處
mov r2, #2048 @3. 復(fù)制長度= 2048(bytes),對于本實驗的main.c,這是足夠了
bl nand_read @調(diào)用C函數(shù)nand_read
這里我們可以跳轉(zhuǎn)到nand_read中可以看一下
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);
write_addr(i);
#ifdef LARGER_NAND_PAGE
write_cmd(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.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)
#define NAND_SECTOR_SIZE_LP 2048
#define NAND_BLOCK_MASK_LP (NAND_SECTOR_SIZE_LP - 1)
typedef unsigned int S3C24X0_REG32;
typedef struct {
S3C24X0_REG32 NFCONF;
S3C24X0_REG32 NFCMD;
S3C24X0_REG32 NFADDR;
S3C24X0_REG32 NFDATA;
S3C24X0_REG32 NFSTAT;
S3C24X0_REG32 NFECC;
} S3C2410_NAND;
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;
void nand_init(void);
void nand_read(unsigned char *buf, unsigned long start_addr, int size);
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 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();
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);
static void s3c2410_nand_reset(void)
{
s3c2410_nand_select_chip();
s3c2410_write_cmd(0xff); // 復(fù)位命令
s3c2410_wait_idle();
s3c2410_nand_deselect_chip();
}
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;
}
static void s3c2440_nand_reset(void)
{
s3c2440_nand_select_chip();
s3c2440_write_cmd(0xff); // 復(fù)位命令
s3c2440_wait_idle();
s3c2440_nand_deselect_chip();
}
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++);
}

上一頁 1 2 下一頁

關(guān)鍵詞: ARMNandFlas

評論


技術(shù)專區(qū)

關(guān)閉