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

📄 nandflash_k9f1208.c

📁 ARM S3C2410处理器 nandflash(k9f1280)驱动程序 开发环境:ads1.2
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
*********************************************************
* Copyright (c)
* All rights reserved.				            
*
* 文件名称:nandflash_k9f1208.c
* 文件标识:
* 摘    要:S3C2410 SmartMedia SAMSUNG 64MB (k9f1208) Test Program
* 当前版本:1.0
* 作    者:刘征
* 完成日期:2005.4.3
*
* 取代版本:
* 作    者:
* 完成日期:
*********************************************************
*/

/*
*********************************************************
*   					 头文件
*********************************************************
*/
#include <string.h>
#include "def.h"
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "2410slib.h" 

/*
*********************************************************
*   					 宏定义
*********************************************************
*/
#define BAD_CHECK   (1)
#define ECC_CHECK   (0)

/*
*********************************************************
*   					 函数原型
*********************************************************
*/
static U16 NF_CheckId(void);
static int NF_EraseBlock(U32 blockNum);
static int NF_ReadPage(U32 block,U32 page,U8 *buffer);
static int NF_WritePage(U32 block,U32 page,U8 *buffer);
//buffer size is 512 bytes
static int NF_IsBadBlock(U32 block);
static int NF_MarkBadBlock(U32 block);
static void NF_Reset(void);
static void NF_Init(void);

int DownloadData(void);
static void InputTargetBlock(void);

/*
*********************************************************
*   					 变量
*********************************************************
*/
static U32 srcAddress;
static U32 targetBlock;     // Block number (0 ~ 4095)
static U32 targetSize;      // Total byte size 

U32 downloadAddress;
U32 downloadProgramSize=0x0;

/*
*********************************************************
* 函数介绍:本函数是NAND FLASH K9F1208 编程测试函数。				
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void K9F1208_Program(void)
{
    int i;
    int programError=0;
    U8 *srcPt,*saveSrcPt;
    U32 blockIndex;

    downloadAddress=0x30100000;
    Uart_Printf("\n[SMC(K9F1208V0M) NAND Flash writing program]\n");

    NF_Init();//初始化
    
    rINTMSK = BIT_ALLMSK;   
    targetSize=downloadProgramSize;
    if(targetSize==0)
    {
        srcAddress=0x30100000;          
    }
    else 
        srcAddress=downloadAddress+4; //to discard the data head for the size
        
    InputTargetBlock();	//从串口接收烧写文件的各种信息 srcAddress,targetBlock,targetSize will be determined
    
    Uart_Printf("source base address(0x3010000x)=0x%x\n",srcAddress);
    Uart_Printf("target start block number      =%d\n",targetBlock);
    Uart_Printf("target size        (0x4000*n)  =0x%x\n",targetSize);

    srcPt=(U8 *)srcAddress;
    blockIndex=targetBlock;
    while(1)
    {
        saveSrcPt=srcPt;    

        #if BAD_CHECK     //坏块检测  
            if(NF_IsBadBlock(blockIndex))   // 1:bad 0:good
            {
                blockIndex++;   // for next block
                continue;
            }
        #endif
        
        if(!NF_EraseBlock(blockIndex))//块擦除
        {
            blockIndex++;   // for next block
            continue;
        }
        
        for(i=0;i<32;i++)
        {
            if(!NF_WritePage(blockIndex,i,srcPt))// block num, page num, buffer
            {
                programError=1;
                break;
            }
            #if ECC_CHECK    
                if(!NF_ReadPage(blockIndex,i,srcPt))
                {
                    Uart_Printf("ECC Error(block=%d,page=%d!!!\n",blockIndex,i);
                }
            #endif      
            srcPt+=512; // Increase buffer addr one pase size
            Uart_Printf(".");
            if((U32)srcPt>=(srcAddress+targetSize)) // Check end of buffer
                break;  // Exit for loop
        }
        
        if(programError==1)
        {
            blockIndex++;
            srcPt=saveSrcPt;
            programError=0;
            continue;
        }
        
        if((U32)srcPt>=(srcAddress+targetSize))
            break;  // Exit while loop
        blockIndex++;
    }
}

/*
*********************************************************
* 函数介绍:本函数是NAND FLASH K9F1208 擦除程序。				
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void K9F1208_Erase(void)
{
    int i;
    U32 blockIndex=0;

    Uart_Printf("\n[ SMC(K9F1208V0M) NAND Flash Erase Program ]\n");
    Uart_Printf("\nNow NAND Flash Erasing ....\n");    

    NF_Init();//初始化

    rINTMSK = BIT_ALLMSK;  //close all intterrupt

    for(blockIndex=0;blockIndex<4096;blockIndex++)
    {
        #if BAD_CHECK       //坏块检测
            if(NF_IsBadBlock(blockIndex))   // 1:bad 0:good
            {
                blockIndex++;   // for next block
                continue;
            }
        #endif
        
        if(!NF_EraseBlock(blockIndex))
        {
            blockIndex++;   // for next block
            continue;
        }       
    }
    Uart_Printf("\nSMC(K9F1208V0M) NAND Flash Erase Completed !!!\n");    
}

/*
*********************************************************
* 函数介绍:本函数是输入块号及大小。				
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
static void InputTargetBlock(void)
{
    Uart_Printf("\nSource size:0h~%xh\n",downloadProgramSize);
    Uart_Printf("\nAvailable target block number: 0~4095\n");
    Uart_Printf("\nInput target block number:");
    targetBlock=Uart_GetIntNum();   // Block number(0~4095)
    if(targetSize==0)
    {
        Uart_Printf("Input target size(0x4000*n):");
        targetSize=Uart_GetIntNum();    // Total byte size
    }
}

/*
*********************************************************
* 函数介绍:本函数是输出坏块号。				
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void K9F1208_PrintBadBlockNum(void)
{
    int i;
    U16 id;

    Uart_Printf("\n[SMC(K9F1208V0M) NAND Flash bad block check]\n");
    
    NF_Init();
    id=NF_CheckId();//ID校验
    Uart_Printf("ID=%x(0xec76)\n",id);
    if(id!=0xec76)//这里要给成K9F1208的ID号!!!!!!!!
    return;
    
    for(i=0;i<4096;i++)
    {
    	NF_IsBadBlock(i);   // Print bad block
    }
}

/*
*********************************************************
* 函数介绍:本函数是输出一页数据。				
* 输入参数:无
* 输出参数:无
* 返回值  :无
*********************************************************
*/
void K9F1208_PrintBlock(void)// Printf one page
{
    int i,j;
    U16 id;
    U32 block,page;
    U8  buffer[512];

    Uart_Printf("\n[SMC(K9F1208V0M) NAND Flash block read]\n"); 
    
    NF_Init();
    id=NF_CheckId();
    Uart_Printf("ID=%x(0xec76)\n",id);
    if(id!=0xec76)
    return;

    Uart_Printf("Input target block number :");
    block=Uart_GetIntNum();
    Uart_Printf("Input target page number :");   
    page=Uart_GetIntNum();

    NF_ReadPage(block,page,buffer);
    Uart_Printf("block=%d,page=%d:",block,page);
    for(j=0;j<512;j++)
    {
        if(j%16==0)
        Uart_Printf("\n%3xh:",j);
        Uart_Printf("%02x ",buffer[j]);
    }
    Uart_Printf("\n");      
}

//*************************************************
//**           H/W dependent functions           **
//************************************************* 

// block0: reserved for boot strap
// block1~4095: used for OS image
// badblock SE: xx xx xx xx xx 00 ....
// good block SE: ECC0 ECC1 ECC2 FF FF FF ....

#define WRITEVERIFY  (0)  //verifing is enable at writing.

#define NF_CMD(cmd) 	{rNFCMD=cmd;}
#define NF_ADDR(addr)   {rNFADDR=addr;} 
#define NF_nFCE_L() 	{rNFCONF&=~(1<<11);}
#define NF_nFCE_H() 	{rNFCONF|=(1<<11);}
#define NF_RSTECC() 	{rNFCONF|=(1<<12);}
#define NF_RDDATA()     (rNFDATA)
#define NF_WRDATA(data) {rNFDATA=data;}

#define NF_WAITRB()    {while(!(rNFSTAT&(1<<0)));} 
//wait tWB and check F_RNB pin.   

#define ID_K9F1208V0M   0xec76//??确定ID号

#if 1
// HCLK=100Mhz
#define TACLS       0  //1clk(0ns) 
#define TWRPH0      3  //3clk(25ns)
#define TWRPH1      0  //1clk(10ns)  //TACLS+TWRPH0+TWRPH1>=50ns
#else
// HCLK=50Mhz
#define TACLS       0  //1clk(0ns)
#define TWRPH0      1  //2clk(25ns)
#define TWRPH1      0  //1clk(10ns)
#endif


static U8 seBuf[16]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};

// 1block=(512+16)bytes x 32pages
// 4096block
// A[23:14][13:9]
//  block   page
/*
*********************************************************
* 函数介绍:本函数是底层块擦除程序。				
* 输入参数:block--块号
* 输出参数:无
* 返回值  :无
*********************************************************
*/
static int NF_EraseBlock(U32 block)
{
    U32 blockPage=(block<<5);
    int i;

#if BAD_CHECK//坏块校验
    if(NF_IsBadBlock(block))
    	return 0;
#endif

    NF_nFCE_L();//NF的CE(片选)拉低
    
    NF_CMD(0x60);   // Erase one block 1st command

    NF_ADDR(blockPage&0xff);        // Page number=0
    NF_ADDR((blockPage>>8)&0xff);   
    NF_ADDR((blockPage>>16)&0xff);

    NF_CMD(0xd0);   // Erase one blcok 2nd command
    
    for(i=0;i<10;i++); //wait tWB(100ns)//??????

    NF_WAITRB();    // Wait tBERS max 3ms.
    NF_CMD(0x70);   // Read status command

    if (NF_RDDATA()&0x1) // Erase error
    {   
        NF_nFCE_H();
    	Uart_Printf("[ERASE_ERROR:block#=%d]\n",block);
    	NF_MarkBadBlock(block);
    	return 0;
    }
    else 
    {
        NF_nFCE_H();////NF的CE(片选)拉高
        return 1;
    }
}

/*

⌨️ 快捷键说明

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