www.pudn.com > w29c040.rar > w29c040.c


#define DEBUG_FLASH 
 
#include  
#include  
 
#include "comsub.h" 
#include "flash.h" 
 
/*    FLASH ADDR DEFINE    */ 
#define FLASH_PAGE    0xe000 
#define    FLASH_BASE    0x8000 
#define FPAGE_SIZE    0x4000 
#define SECTOR_SIZE    256 
 
/*    Fast functions define for send header of byte sequence         */ 
/* FLASH_BASE+0x1555 = 0x9555 */ 
#define BLoadTo5555(data)        XBYTE[FLASH_PAGE]=1;\ 
                    XBYTE[0x9555]=data 
/* FLASH_BASE+0x2aaa = 0xaaaa */ 
#define BLoadTo2aaa(data)        XBYTE[FLASH_PAGE]=0;\ 
                    XBYTE[0xaaaa]=data 
 
/*----------------------Timing Waveforms of W29C040-------------------------- 
page write timing diagram: 
 
addr    -[ 5555 ]-[ 2aaa ]-[ 2aaa ]-??? 
data    --[  AA  ]-[  55  ]-[  A0  ]-??? 
#CE    __      ____      ____      ____      ____      ___ 
      \____/////\____/////\____/////\____/////\____//// 
#OE       ____      ____      ____      ____      ____ 
    __/    \\\\\/    \\\\\/    \\\\\/    \\\\\/    \\\\\ 
#WE    __|TWP |____| TBLC____|     ____      ____      ___ 
      \____/    \____/    \____/    \____/    \____/ 
           |TWPH| 
       SW0       SW1       SW2       BYTE0 ----- 
 
TWP > 70nS 
TWPH > 100nS 
TBLC < 200uS 
-----------------------------------------------------------------------------*/ 
 
 
/*----------------------------------------------------------------------------- 
Function     : flash_CheckType 
Description  : It can check have flash or not in your hardware 
Inputs       : NONE 
Outputs      : 1 OR 0 
Remarks      : note 1     if ==0xff the boot block programing lockout featrue is actived 
             if ==0xfe the lockout featrue is inactived and the block can be programmed 
-----------------------------------------------------------------------------*/ 
bit  flash_CheckType(void) 
{ 
    BYTE temp1,temp2,temp3,temp4; 
 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x80); 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x60); 
    shortdelay(10); 
    XBYTE[ FLASH_PAGE ] = 0; 
    temp1 = XBYTE[FLASH_BASE+0];        /* read flash address = 00000 */ 
    temp2 = XBYTE[FLASH_BASE+1];        /* read flash address = 00001 */ 
    temp3 = XBYTE[FLASH_BASE+2];        /* read flash address = 00002 */ 
    XBYTE[ FLASH_PAGE ] = 0x1f;        /* read flash address = 7fff2 */ 
    temp4 = XBYTE[FLASH_BASE+0x3ff2]; 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0xf0); 
    shortdelay(10); 
    if( (temp1==0xda) 
        &&(temp2==0x46) 
        &&((temp3==0xfe)||(temp3==0xff))        /*note 1*/ 
        &&((temp4==0xfe)||(temp4==0xff)) ) 
        return 1; 
    else 
        return 0; 
} 
 
 
/*----------------------------------------------------------------------------- 
Function     : flash_ProtDisable() and flash_ProtEnable() 
Description  : functions for software data protecion disable or enable 
Inputs       : NONE 
Outputs      : 1 OR 0 
Remarks      : 
-----------------------------------------------------------------------------*/ 
#if 0 
void flash_ProtDisable(void) 
{ 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x80); 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x20); 
    delay_n_ms(10); 
} 
 
void flash_ProtEnable(void) 
{ 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0xa0); 
} 
#endif 
 
/*----------------------------------------------------------------------------- 
Function     : flash_BootBlkSetOnFirst16K() flash_BootBlkSetOnLast16K() 
Description  : 
Inputs       : NONE 
Outputs      : 
Remarks      : 
-----------------------------------------------------------------------------*/ 
#if 0 
void flash_BootBlkSetOnFirst16K(void) 
{ 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x80); 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x40); 
    XBYTE[ FLASH_PAGE ] = 0; 
    XBYTE[ FLASH_BASE ] = 0; 
    shortdelay(10); 
} 
 
 
void flash_BootBlkSetOnLast16K(void) 
{ 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x80); 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x40); 
    XBYTE[ FLASH_PAGE ] = 0x1f; 
    XBYTE[ FLASH_BASE+0x3fff ] = 0xff; 
    shortdelay(10); 
} 
#endif 
 
/*----------------------------------------------------------------------------- 
Function     : flash_EraseChip 
Description  : Erase flash chip, The data will all lost in the flash, Be careful use! 
Inputs       : NONE 
Outputs      : NONE 
Remarks      : 
-----------------------------------------------------------------------------*/ 
#if 0 
void flash_EraseChip(void) 
{ 
    BYTE check,k; 
 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x80); 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0x10); 
    delay_n_ms(50); 
} 
#endif 
 
/*----------------------------------------------------------------------------- 
Function     : flash_ReadByte 
Description  : read 1 byte from any address of flash(0x00000-0x7ffff) 
Inputs       : flash address 
Outputs      : Data in the flash address 
Remarks      : 
-----------------------------------------------------------------------------*/ 
BYTE flash_ReadByte(long FAddr) 
{ 
        register WORD offset; 
        register BYTE page_no; 
        register BYTE result; 
 
        offset = FLASH_BASE+FAddr%FPAGE_SIZE; 
        page_no = FAddr/FPAGE_SIZE; 
    XBYTE[ FLASH_PAGE ] = page_no; 
    result = XBYTE[ offset ]; 
 
    return(result); 
} 
 
/*----------------------------------------------------------------------------- 
Function     : flash_WriteSector 
Description  : write 1 sector data to flash, It Must satisfy this term FAddr/256=0 
Inputs       : FAddr     sector start address, 
         *buf     Data buffer 
Outputs      : if written success then return 1, else return 0 
Remarks      : Ihe best is read data from flash and check after written over 
-----------------------------------------------------------------------------*/ 
bit  flash_WriteSector(long FAddr, BYTE *buf) 
{ 
    register WORD offset; 
    register BYTE page_no; 
    register int i; 
 
        offset = FLASH_BASE + FAddr%FPAGE_SIZE; 
    page_no = FAddr/FPAGE_SIZE; 
           EA=0; 
    BLoadTo5555(0xaa); 
    BLoadTo2aaa(0x55); 
    BLoadTo5555(0xa0); 
         XBYTE[ FLASH_PAGE ] = page_no; 
    for(i=0;i