⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nand.c

📁 一个基于s3c2410平台的monitor程序
💻 C
字号:
/************************
/module name: Nand.c
/this file provide the interface to the nand flash
/author:Decell.Zhou
/Version History: v0.1 |070718|first release
*************************/
#include "2410addr.h"
#include "def.h"
#include "option.h"

/*
*define some global constant
*/
#define BUSY 0
#define NAND_PAGE_SIZE 512
#define NAND_PAGE_MASK (NAND_PAGE_SIZE - 1)


/********
*PRIVATE void waitBusy() | wait until the chip is ready
*PRIVATE void enableChip() | enable the chip
*PRIVATE void disableChip() | disable the chip
*PRIVATE int verifyCheck() | check the data tho see weather the result operation is correct
*PRIVATE void delay(int delayTime) | delay some this to let the signal be stable
*/

void delay(int delayTime){
	int i;
	for(i = 0;i < delayTime;i++);
}

int nandRead(U32 targetAddr,U32 sourceAddr,U32 size){
	void waitBusy(void);
	void enableChip(void);
	void disableChip(void);
	int verifyCheck(void);
	void delay(int delayTime);
	
	int rowAddr;
	int columnAddr;
	unsigned char *pTargetAddr = (unsigned char *)targetAddr;
	
	//check the argument to see whether it is correct or not
	//the source address must be the startAdderss of a page  
	//and the size must be N times of a page size
	if((sourceAddr & NAND_PAGE_MASK) || (size & NAND_PAGE_MASK)){
		return 1;
	}
	
	//first enable the chip
	enableChip();
	
	//read the data from the nand flash 
	//because the sourceAddr is the starting address of a Page
	//and the size is N times of a Page size
	//so we start reading form a page end surely will end at the end
	//of a page 
	for(rowAddr = sourceAddr;rowAddr < sourceAddr + size;){
		
		//launch the the "read 1" command, read the first half part of the page 
		//from the nand flash, and then change into the sencond part  
		//automatically
		rNFCMD = 0x0;
		delay(10);
		
		//send the address to the chip
		//A0-A7 should be 0,because we start our reading from the start 
		//address of a block 2^8 = 256
		//A8 is not needed because which half of a page will be read is
		//determine by the command it self 2^9 = 512
		//A9-A16 and A17-A24 is the block address, 2^25 = 32M this chip 
		//is a 32MB nand flash
		rNFADDR = rowAddr & 0xFF;//address cycle 1
		rNFADDR = (rowAddr >> 9) & 0xFF;//address cycle 2
		rNFADDR = (rowAddr >> 17) & 0xFF;//address cycle 3
		rNFADDR = (rowAddr >> 25) & 0xFF;//address cycle 3
		
		//wait until the chip ready
		waitBusy();
		
		//wirte it to the target address
		for(columnAddr = 0;columnAddr < NAND_PAGE_SIZE;columnAddr++ ,rowAddr++){
			*pTargetAddr = rNFDATA & 0xFF;
			pTargetAddr++;
		}
		
	}
	
	//check the result
	if(verifyCheck != 0){//if the verify check return a fail result
		disableChip();
		return 2;//read Fail
	}else{
		disableChip();
		return 0;//operation complete successfully
	}
	
	
	
	
}

void waitBusy(void){
	
	
	//wait until the R/nb pin is high
	for(;(rNFSTAT & 0x1) == BUSY;){
	}
	delay(10);
	
}

void enableChip(void){
	//enable nand flash controler and the chip
	//NFCON: Enable|Rev|Init ECC|Chip Enable|TACLS|Rev|TWRPH0|Rev|TWRPH1
	//Binary:	1  | 00|       1|          0|  111|  0|   111|  0|   111
	//Hex:   0x9777 
	rNFCONF = 0x9777;
	delay(10);
}

void disableChip(void){
	//disable nand flash controler and the chip
	//NFCON: Enable|Rev|Init ECC|Chip Enable|TACLS|Rev|TWRPH0|Rev|TWRPH1
	//Binary:	0  | 00|       1|          1|  111|  0|   111|  0|   111
	//Hex:   0x1F77 
	rNFCONF = 0x1F77;
	delay(10);
}

int verifyCheck(void){
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -