📄 flashic.c
字号:
/******************************************************************************************
* 目的:Flash芯片驱动及对外基本读写函数
* 功能:提供Flash芯片 檫除 读取 写入 分页查询 功能
* 模块依赖:Flash芯片驱动资料
* 注意:模块将依据FLASH芯片读取速度和程序空间改变 目前具体处理方式不明
******************************************************************************************/
#include "FlashIC.h"
#include "compiler.h"
//#define DEBUGFLASH
#ifdef DEBUGFLASH
#include "SCI.h"
#endif
//////////////////////////////////////////////////////////////////////////
// Deifine Of FLASH IC Address
//////////////////////////////////////////////////////////////////////////
#define SPointer (( unsigned char volatile __xdata *)(0x8000))
#define LIB1_H_PAGE (*(unsigned char volatile __xdata *)(0xFF02))
//////////////////////////////////////////////////////////////////////////
// Deifine Of FLASH COMMAND
//////////////////////////////////////////////////////////////////////////
#define HIGH_ADDR 0xAAA //16BIT MODE.
#define LOW_ADDR 0x555
#define FLASH_CMD_RST 0xF0
#define FLASH_CMD_BE1 0x80
#define FLASH_CMD_BE2 0x30
#define FLASH_CMD_CE1 0x80
#define FLASH_CMD_CE2 0x10
//////////////////////////////////////////////////////////////////////////
// Deifine Of FLASH FUN
//////////////////////////////////////////////////////////////////////////
#define SetLibPage(haddr_page) LIB1_H_PAGE = (unsigned char)(haddr_page)
//////////////////////////////////////////////////////////////////////////
// Deifine Of FLASH FIST BLOCK PAGE SIZE
//////////////////////////////////////////////////////////////////////////
#define LOWPAGESIZE 0x2000 //第一块单页面长度 8K
#define LOWPAGESIG 0x1FFF //单页面长度 - 1
//////////////////////////////////////////////////////////////////////////
struct ICseek //模块数据结构
{
unsigned long SimAddress; //当前地址
unsigned char SimPage; //当前页面
} TagSeek;
/*
**-------------------------------------------------------------------------------------
* FLASH 控制命令
**-------------------------------------------------------------------------------------
*/
void FlashCmd()
{
SetLibPage( 0 );
SPointer[HIGH_ADDR] = 0XAA; //8BIT MODE.
SPointer[LOW_ADDR] = 0X55;
}
/*
**-------------------------------------------------------------------------------------
* FLASH 读写控制芯片刷新
**-------------------------------------------------------------------------------------
*/
void FlashRST()
{
FlashCmd();
SPointer[0] = FLASH_CMD_RST;
}
/*
**-------------------------------------------------------------------------------------
** 初始化待资料文件文件
** 定位片外FLASH地址
**-------------------------------------------------------------------------------------
*/
char InitFlash( )
{
TagSeek.SimAddress = 0;
TagSeek.SimPage = 0;
//初始化FLASH
//SelectFlashIC( FLASH0 );
FlashRST();
return 1;
}
/*
**-------------------------------------------------------------------------------------
** 模拟FSEEK
** SeekFlag: 模式 CURRENT: 当前位置 BEGIN: 文件起始
** Address: 偏移地址
**-------------------------------------------------------------------------------------
*/
void SPSeek(int SeekFlag,long Address)
{
if( SeekFlag==BEGIN ) TagSeek.SimAddress = Address;
else TagSeek.SimAddress += Address;
TagSeek.SimPage = (unsigned char)(TagSeek.SimAddress/PAGESIZE);
SetLibPage( TagSeek.SimPage );
#ifdef DEBUGFLASH
SCIsend( 'S' );
SCIsend( TagSeek.SimPage );
#endif
}
/*
**-------------------------------------------------------------------------------------
** 模拟 FGETC
** 返回: 当前位置字符
**-------------------------------------------------------------------------------------
*/
unsigned char SPGetCH( )
{
unsigned char OutputCH;
if( !( TagSeek.SimAddress & PAGESIG ) )
{
TagSeek.SimPage = (unsigned char)(TagSeek.SimAddress/PAGESIZE);
SetLibPage( TagSeek.SimPage );
}
OutputCH = SPointer[ (unsigned int)( TagSeek.SimAddress & PAGESIG ) ];
TagSeek.SimAddress++;
#ifdef DEBUGFLASH
SCIsend( 'R' );
SCIsend( TagSeek.SimPage );
#endif
return OutputCH;
}
/*
**-------------------------------------------------------------------------------------
** 模拟 FGETS
** Lenth: 提取长度
** Buf: 输出内容
** 返回: 实际提取个数
**-------------------------------------------------------------------------------------
*/
int SPGets( unsigned char *Buf, int Lenth )
{
int i;
unsigned int Offset;
Offset =( unsigned int )( TagSeek.SimAddress & PAGESIG );
if( !Offset )
{
TagSeek.SimPage =( unsigned int )( TagSeek.SimAddress / PAGESIZE );
SetLibPage( TagSeek.SimPage );
}
for( i=0; i<Lenth; i++ )
{
if( Offset == PAGESIZE )
{
TagSeek.SimPage++;
SetLibPage( TagSeek.SimPage );
Offset = 0;
}
Buf[i] = SPointer[ Offset++ ];
}
TagSeek.SimAddress += (unsigned long)Lenth;
return Lenth;
}
/*
**-------------------------------------------------------------------------------------
** 模拟 FTELL
** 返回: 当前逻辑地址
**-------------------------------------------------------------------------------------
*/
long SPTell()
{
return TagSeek.SimAddress;
}
//----------------------------------------------------------------- End Of File -------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -