📄 w29c040.c
字号:
#define DEBUG_FLASH
#include <reg51.h>
#include <absacc.h>
#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<SECTOR_SIZE;i++)
{
// XBYTE[ FLASH_PAGE ] = page_no;
XBYTE[offset++] = *buf++;
}
EA=1;
delay_n_ms(10);
return(1);
}
/*-----------------------------------------------------------------------------
Function : flash_ReadSector
Description : read 1 sector data from flash, It Must satisfy this term FAddr/256=0.
Inputs : FAddr sector start address,
*buf Data buffer
Outputs : NONE
Remarks :
-----------------------------------------------------------------------------*/
void flash_ReadSector(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;
XBYTE[ FLASH_PAGE ] = page_no;
for(i=0;i<SECTOR_SIZE;i++)
*buf++=XBYTE[offset++];
}
/*-----------------------------------------------------------------------------
Function : delay_n_ms
Description : Not accurate, you can use timer2 if 51 mcu have it.
Inputs :
Outputs : NONE
Remarks :
-----------------------------------------------------------------------------*/
void delay_n_ms(int n_ms)
{
register BYTE n_us;
while(n_ms--)
{
n_us=200;
while(n_us--);
}
}
/*-----------------------------------------------------------------------------
Function : delay_n_us
Description : Not accurate
Inputs :
Outputs : NONE
Remarks :
-----------------------------------------------------------------------------*/
void shortdelay(unsigned char n_us)
{
register BYTE k;
k=n_us/2;
while(k--);
}
#ifdef DEBUG_FLASH
void debug_flash(void)
{
char WriteBuf[256];
char ReadBuf[256];
int i;
flash_CheckType();
i = flash_ReadByte(0x7ffff);
for(i=0; i<256 ;i++)
WriteBuf[i]=0Xff;
for(i=0; i<256 ;i++)
ReadBuf[i]=0x00;
flash_WriteSector(0x100,&WriteBuf[0]);
flash_ReadSector(0x100,&ReadBuf[0]);
flash_ReadSector(0x100,&ReadBuf[0]);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -