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

📄 nand.c

📁 S3C2410 For Wince5.0 NAND Flash 启动的源代码
💻 C
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
--*/
#include <string.h>

#include "def.h"
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "nand.h"

void __RdPage512(UCHAR *bufPt); 
void __RdPage512Unalign (UCHAR *bufPt);

//  Status bit pattern
#define STATUS_READY                0x40          
#define STATUS_ERROR                0x01

#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)));} 

#define     pNFCONF     rNFCONF 
#define     pNFCMD      rNFCMD  
#define     pNFADDR     rNFADDR 
#define     pNFDATA     rNFDATA 
#define     pNFSTAT     rNFSTAT 
#define     pNFECC      rNFECC  

#define NF_CE_L()     NF_nFCE_L()
#define NF_CE_H()     NF_nFCE_H()
#define NF_DATA_R()   rNFDATA
#define NF_ECC()      rNFECC

//
//  Reset the chip
//
void NF_Reset()
{                       
    NF_CE_L();          
    NF_CMD(CMD_RESET);  
    NF_WAITRB();        
    NF_CE_H();          
}

#define TACLS                   1      
#define TWRPH0                  7 
#define TWRPH1                  3  

void NF_Init(void)
{
    rNFCONF=  (1      << 15) | /* Enable/Disable                    */ 
              (1      << 14) | /* Page Size : 512Bytes              */
              (1      << 13) | /* 4 Step Address                    */
              (1      << 12) | /* Initialize ECC                    */
              (1      << 11) | /* nFCE control nFCE = HIGH          */
              (TACLS  <<  8) | /* CLE & ALE = HCLK * (TACLS  + 1)   */
              (TWRPH0 <<  4) | /* TWRPH0    = HCLK * (TWRPH0 + 1)   */
              (TWRPH1 <<  0);  /* TWRPH1    = HCLK * (TWRPH1 + 1)   */
}
void NF_ReadID()
{
    USHORT  wData1, wData2;
    //  First we enable chip select
    NF_CE_L();

    //  Issue commands to the controller
    NF_CMD(CMD_READID);
    NF_ADDR(0x00);

    NF_WAITRB();

    wData1 = NF_DATA_R();
    wData2 = NF_DATA_R();

    NF_CE_H();

    Uart_SendString("Nand Mfg: ");
    Uart_SendDWORD((DWORD)wData1, TRUE);
    Uart_SendString("Nand Dev: ");
    Uart_SendDWORD((DWORD)wData2, TRUE);
}


//  FMD_ReadSector
//
//  Read the content of the sector.
//
//  startSectorAddr: Starting page address
//  pSectorBuff  : Buffer for the data portion
//  pSectorInfoBuff: Buffer for Sector Info structure
//  dwNumSectors : Number of sectors
//
BOOL FMD_ReadSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff,PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
    int i;

    if (!pSectorBuff && !pSectorInfoBuff || dwNumSectors > 1 || !pSectorBuff) 
    {
        Uart_SendString("ERROR_INVALID_PARAMETER\n");
        return FALSE;
    }

    NF_RSTECC();//  Initialize ECC register
    NF_nFCE_L();   //  Enable the chip
    NF_CMD(CMD_RESET);// Issue Read command
    for(i=0;i<500;i++);
    NF_WAITRB();

    NF_CMD(CMD_READ);// Issue Read command
    NF_ADDR(0x00);
    NF_ADDR((startSectorAddr) & 0xff);
    NF_ADDR((startSectorAddr >> 8) & 0xff);
    NF_ADDR((startSectorAddr >> 16) & 0xff);
    for (i = 0; i<100; i++);   // wait tWB(100ns)
    NF_WAITRB();        // wait tR(max 12us)

    //  Handle unaligned buffer pointer
    if( ((DWORD) pSectorBuff) & 0x3) 
       __RdPage512Unalign (pSectorBuff);
     else 
        __RdPage512(pSectorBuff);                // Read page/sector data.

    NF_RDDATA();
    NF_RDDATA();

    NF_CE_H()   

    return (TRUE);
}


⌨️ 快捷键说明

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