- NOR FLASH 支持XIP,既程式可以直接在NOR FLASH執行,無需複製到內存中,這是因為NOR FLASH的接口與RAM相同,可以隨機訪問任意地址數據。
- FLASH 儲存單元由擦除單元組成,也稱為塊(block),要寫入數據時,需要確保這個block已被擦除。
- 擦/寫 一個NOR FLASH 塊需要4s,擦/寫 一個NAND FLASH僅需2ms。
- NOR FLASH用於儲存程式,NAND FLASH用儲存數據。
- FLASH 可靠性依據:(1)位反轉(2)壞軌(3)可擦除次數
- NAND FLASH 發生位反轉機率較NOR FLASH 高,若發生在關鍵代碼處,則可能造成系統崩壞,一般需要透過 EDC/ECC 進行錯誤檢查/恢復機制
- 嵌入式Linux系統,NOR FLASH 常用jffs2文件系統;NAND FLASH常用yaffs文件系統,在更底層,MTD驅動程式實現對它們的讀、寫、擦除操作,也實現 EDC/ECC 校驗
NOR FLASH 腳位電路圖 |
NAND FLASH 特性:
Large-Block array organization |
NAND FLASH 電路圖 |
- 由上圖電路腳位可以看到NAND FLASH沒有地址腳位,僅有IO0~IO7數據腳位,可以觀察知NAND FLASH是透過數據腳位來傳送命令、地址、數據。
- 當 寫入/讀 命令、地址、數據時,都需要將CE#、 WE# / RE# 信號拉低,此時NAND FLASH將 寫/讀 資料(命令or地址or數據)傳入IO數據線上,當 WE# / RE# 信號上升沿時,將資料發送,即可透過IO數據線交換資料。
- 命令鎖存信號CLE、地址鎖存信號ALE用來分辨,鎖存命令or地址,當讀/寫命令時,CLE信號拉高,當讀/寫地址時,ALE信號拉高,當讀/寫命令時,CLE和ALE信號為低電位。
- R/B為狀態腳位,當NAND FLASH正在 讀/寫 動作時,信號為LOW,完成動作時,信號為HIGH。
PIN DESCRIPTION |
NAND FLASH 操作:
- WRITE COMMAND
- WRITE ADDRESS
- WRITE DATA
- READ DATA
上面已列出讀/寫操作的時序圖,NAND FLASH訊號發送的時間要求如下表
S3C2440能夠控制的時間參數如下圖,
S3C2440重要暫存器操作如下圖,
nand flash 操作流程 |
實驗:將超過NAND FLASH儲存內量4K大小的程式碼,複製到SDRAM上執行,小於4K的程式碼,系統上電時,會直接經由硬體copy到SRAM執行。
程式流程:
(1)選中晶片
(2)發送00h
(3)發出位址
(4)發30h
(5)等待就緒
(6)讀一頁數據
關鍵程式碼:
===================================
.text
.global _start
_start:
@函数disable_watch_dog, memsetup, init_nand,
nand_read_ll在init.c中定义
ldr sp, =4096 @设置堆栈
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, #4096 @2. 源地址 = 4096,连接的时候,
main.c中的代码都存在NAND Flash
地址4096开始处
mov r2, #2048 @3. 复制长度= 2048(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
===================================
/* 初始化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 = (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);
/* 使能NAND Flash控制器, 初始化ECC, 禁止片选 */
s3c2440nand->NFCONT = (1<<4)|(1<<1)|(1<<0);
}
/* 复位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 ; /* 地址或长度不对齐 */
}
#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(0);
/* Write Address */
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_deselect_chip();
return ;
}
==============================
/* 复位 */
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_nand_deselect_chip(void)
{
s3c2440nand->NFCONT |= (1<<1);
}
=================================
/* 发出地址 */
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_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=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) & 0x03; /* Row Address A28~A29 */
for(i=0; i<10; i++);
}
================================
/* 读取数据 */
static unsigned char s3c2440_read_data(void)
{
volatile unsigned char *p = (volatile unsigned char *)&s3c2440nand->NFDATA;
return *p;
}
================================
參考資料
- 韋東山 嵌入式Linux應用開發完全手冊
No comments:
Post a Comment