📄 flash.c
字号:
#include "stddef.h"
#define FCD_reset 0xf0
#define FCD_erase_suspand 0xb0
#define FCD_erase_resume 0x30
#define FC_address5555 0x5555
#define FC_address2AAA 0x2aaa
#define size_29f040 0x80000
int xdata * xdata point_Flash_page;
void F29040_reset(void)
{
point_Flash_page = 0x10000;
*(point_Flash_page+0x30)=FCD_reset;
}
//--------------------------------------
unsigned int F29040_calc_address(long address) //给出一个地址,自动送出页面地址,且返回页内偏移地址
{
unsigned long segment; unsigned int ret; unsigned int page;
segment=address>>16;
page=address<<16;
point_Flash_page = page; //取页面头地址 给头地址指针
ret=address;
ret&=0x0FFFF;
return(ret); //获取页面内偏移
}
//--------------------------------------
unsigned char F29040_write_a_byte(unsigned long address,unsigned char byte) //向flash写入一个字节,写入之前请调用F29040_reset,返回0:成功,1失败
{
unsigned int offset; unsigned char b1,b2;
F29040_reset();
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0xaa;
offset=F29040_calc_address(FC_address2AAA);
*(point_Flash_page+offset)=0x55;
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0xa0;
offset=F29040_calc_address(address);
*(point_Flash_page+offset)=byte;
do
{
b1=*(point_Flash_page+offset); //读flash数据总线
b1&=0x80; //B1000_0000
b2=byte & 0x80; //B1000_0000
if(b1==b2) return(0); //成功
b1=*(point_Flash_page+offset);
b1&=0x20; //B0010_0000;
}
while(b1==0); //写字节周期完成
b1=*(point_Flash_page+offset); //读flash数据总线
b1&=0x80; // B1000_0000;
b2=byte & 0x80; //B1000_0000;
if(b1==b2) return(0); //成功
else return(1); //失败
}
//----------------------------------------
/*unsigned char F29040_write_64k_byte(unsigned long write_head_address,unsigned in registor[] ) //向Flash的一页64kB写入数据
{
unsigned int offset,i; unsigned char b1,b2;
判断是否写入超出本页数
unsigned int j=0;
j = sizeof( registor ) / sizeof( registor[0] ); //数组元素个数
c1=head_address<<16;
c2=(write_head_address+j)<<16; 判断c2
offset=F29040_calc_address(FC_address5555); // F29040_reset();
*(point_Flash_page+offset)=0xaa;
offset=F29040_calc_address(FC_address2AAA);
*(point_Flash_page+offset)=0x55;
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0xa0;
offset=F29040_calc_address(write_head_address);
for(i=0;i<j;i++)
{
*(point_Flash_page+offset+i)=registor[i];
}
}*/
//--------------------------------------
unsigned char F29040_read_a_byte(long read_address) //向flash读一个字节,写入之前请调用F29040_reset
{
unsigned int offset; unsigned char ret;
F29040_reset();
/* offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0xaa;
offset=F29040_calc_address(FC_address2AAA);
*(point_Flash_page+offset)=0x55;
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0xF0; */
offset=F29040_calc_address(read_address);
ret=*(point_Flash_page+offset);
return(ret);
}
//-----------------------------------------
unsigned char F29040_sector_remove (long remove_sector_address) //区擦除
{
unsigned int offset; unsigned char b1,b2;
F29040_reset();
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0xaa;
offset=F29040_calc_address(FC_address2AAA);
*(point_Flash_page+offset)=0x55;
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0x80;
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0xaa;
offset=F29040_calc_address(FC_address2AAA);
*(point_Flash_page+offset)=0x55;
offset=F29040_calc_address(remove_sector_address);
*(point_Flash_page+offset)=0x30;
// offset=F29040_calc_address(remove_sector_address);
// *(point_Flash_page+offset)=0x30;
do
{
b1=*(point_Flash_page+offset); //读flash数据总线
b1&=0x80; //B1000_0000
b2=0xFF & 0x80; //B1000_0000
if(b1==b2) return(0); //成功
b1=*(point_Flash_page+offset);
b1&=0x20; //B0010_0000;
}
while(b1==0); //写字节周期完成
b1=*(point_Flash_page+offset); //读flash数据总线
b1&=0x80; // B1000_0000;
b2=0xFF & 0x80; //B1000_0000;
if(b1==b2) return(0); //成功
else return(1); //失败
}
//----------------------------------------- //页擦除
void F29040_page_remove (long page_address)
{
unsigned int offset;
F29040_reset();
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0xaa;
offset=F29040_calc_address(FC_address2AAA);
*(point_Flash_page+offset)=0x55;
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0x80;
offset=F29040_calc_address(FC_address5555);
*(point_Flash_page+offset)=0xaa;
offset=F29040_calc_address(FC_address2AAA);
*(point_Flash_page+offset)=0x55;
offset=F29040_calc_address(page_address);
*(point_Flash_page+offset)=0x10;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -