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

📄 nandwriter.c

📁 一个基于s3c2410平台的monitor程序
💻 C
字号:
//================================
//Description: this file provide some method to operate the nand flash
//including erase the flash,write the program into it
//Author:Decell.Zhou
//history:
//================================
#include "nandWriter.h"

extern long dataLenght;

extern void delay(int delayTime);
extern void waitBusy(void);
extern enableChip(void);
extern disableChip(void);


//============[nandWritePage]================
//Description:write the content in the argument into a nand flash page
//Author:Decell.Zhou
//Version:
//arg: pData | char *| the pointer point to the start address of the contend
//	   pageAddr| unsigned long | the destination page address 
//return: 0 | pass
//		  1 | failure
//=========================================
int nandWritePage(unsigned long pageAddr,unsigned char *pData){
	int counter;
	unsigned char status;
	
	enableChip();
	//send the write command "0x80"
	rNFCMD = 0x80;
	delay(10);
	//send the address to the nand flash
	rNFADDR = 0x0;//column address set to 0,A0-A7,we write from the beginning of the page
	rNFADDR = pageAddr & 0xff;//write the row address,cycle 2,A9 - A16
	rNFADDR = (pageAddr >> 8) & 0xff;//write the row address,cycle 3,A17-A24
	rNFADDR = (pageAddr >> 16) & 0xff;//write the row address,cycle 4,A25
	//send the data
	for(counter = 0;counter < 512;counter++){
		rNFDATA = (*pData) & 0xff;
		pData++;
	}
	//send the write confirm command "0x10"
	rNFCMD = 0x10;
	
	waitBusy();
	
	//send the read status command
	rNFCMD = 0x70;
	waitBusy();
	status = rNFDATA;
	if((status & 0x1) == 1){
		disableChip();
		return 1;
	}else{
		disableChip();
		return 0;
	} 
	
	
}

//============[nandWritePage]================
//Description:erase a specify nand flash block
//Author:Decell.Zhou
//Version:
//arg: blockNum | int | the block number you want to erase
//return: 0 | pass
//		  1 | failure
//=========================================
int nandEraseBlock(unsigned long blockNum){
	
	unsigned char status;
	
	
	blockNum = blockNum << 5;//blockNum extended to A14 - A25,

	enableChip();
	
	rNFCMD = 0x70;
	waitBusy();
	status = rNFDATA;
	
	//write the block erase command
	rNFCMD = 0x60;
	delay(10);
	//write address 3 cycle
	rNFADDR = blockNum & 0xff;//address cycle 1,A9-A16
	rNFADDR = (blockNum >> 8) & 0xff;//address cycle 2,A17-A24
	rNFADDR = (blockNum >> 16) & 0xff;//address cycle 3,A25
	//write the erase confirm command
	rNFCMD = 0xd0;
	delay(10);
	waitBusy();
	
	//read status command
	rNFCMD = 0x70;
	waitBusy();
	status = rNFDATA;
	if((status & 0x1) == 1){
		disableChip();
		return 1;
	}else{
		disableChip();
		return 0;
	} 
	
	
}


//=================[nandWrite]============
//Description:the nand flash write operation method
//Author:Decell.Zhou
//version:
//arg:none
//return: 0| complete successfully
//		  1| complete with error		 
int nandWrite(){
	
	
	
	//flash address
	unsigned long desAddr;//the destination flash address
	unsigned long blockNum;//the block address
	//source address
	unsigned char *sourceHead;
	unsigned char *sourceTail;
	
	unsigned long numOfBlocks;//blocks in total
	long counter;
	char i;
	
	Uart_SendString("\n\nplease input the destination address:\n");
	desAddr = Uart_GetIntNum();
	if((desAddr & NAND_PAGE_MASK) == 1){
		Uart_SendString("Warnning! the address within a page will be ingored\n");
	}
	
	//download the program to ram first
	Uart_SendString("Now download the  program to RAM\n");
	if(xmodemTransmit() == 0){// xmodem transmit successful
		Uart_SendString("Download completed successfully!!\n");
		Uart_Printf("program lenght:%ld Bytes\n",dataLenght);
	}else{
		Uart_SendString("Download terminate, retry anagin...\n");
		return 1;
		}				
				
	//write it to nand flash
				
	if(dataLenght == 0){
		Uart_SendString("you must transfer the data to ram first\n");
		return 1;
	}
	
	Uart_Printf("Target Address: 0x%x\n",desAddr);	
	desAddr = desAddr >> 9;//convert to the page address(row address)
	blockNum = desAddr >> 5;//convert to the block address
	
	if((dataLenght % (512 * 32)) == 0){
		numOfBlocks = (dataLenght / (512 * 32));
	}else{
		numOfBlocks = (dataLenght / (512 * 32)) + 1;
	}
	
	Uart_Printf("%ld blocks in total\n",numOfBlocks);
	Uart_Printf("%ld pages in total\n",(dataLenght / 512 + (dataLenght % 512 > 0)));
	
	Uart_SendString("Now Erasing Nand Flash\n");
	for(counter = 0;counter < numOfBlocks;counter ++){
		
		if(nandEraseBlock(blockNum) == 0){
			if(counter % 50 == 0){
				Uart_SendString("\n");
			}
			Uart_SendString("*");

		}
		blockNum ++;
	}
	
	sourceHead = PROGRAM_RO_BASE;
	sourceTail = sourceHead;
	
	Uart_SendString("\nNow Writing it to Nand Flash\n");
	for(counter = 0;counter < numOfBlocks;counter++){
		for(i = 0;i< 32;i++){
			if(nandWritePage(desAddr,sourceHead) == 0){
				Uart_SendString("#");
				desAddr++;
				sourceHead += 512;
			}else{
				i--;
			}
		}
		Uart_SendString("\n");
	}
	
	
	
	return 0 ;
	
		
}

//=================[nandWrite]============
//Description:the nand flash erase operation method
//Author:Decell.Zhou
//version:
//arg:none
//return: 0| complete successfully
//		  1| complete whit error
int nandErase(){

	unsigned int blocks;
	unsigned int blockAddr;
	int counter;
	Uart_SendString("The target device is K9f1208,4096 Blocks in total.\n");
	
	Uart_Printf("Please input the starting block num:\n");
	blockAddr = Uart_GetIntNum();
	Uart_SendString("How many blocks you want to erase?\n");
	blocks = Uart_GetIntNum();
	
	Uart_Printf("blocks = %d",blocks);
	for(counter = 0;counter < blocks;counter++){
		if(nandEraseBlock(blockAddr) == 0){
			blockAddr++;
			Uart_SendString("#");
			if(counter % 50 == 0){
				Uart_SendString("\n");
			}
		}else{
			Uart_SendString("\nBlock Erase Error,retrying...\n");
			counter--;
		}

	}
	
	return 0;
}

⌨️ 快捷键说明

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