📄 flash.h
字号:
#ifndef _FLASH_H
#define _FLASH_H
#include "typedef.h"
//define only for 89c52 testing
#define ClrSCLK() {P1_3=0;}
#define SetSCLK() {P1_3=1;}
#define ClrAt45081CS() {P1_2=0;}
#define SetAt45081CS() {P1_2=1;}
#define ClrSO() {P1_4=0;}
#define SetSO() {P1_4=1;}
#define SI (P1_5)
#define _AT45DB081
#define FlashPageBytes 264
#ifdef _AT45DB041
#define FlashMaxPage 2048
#endif
#ifdef _AT45DB081
#define FlashMaxPage 4096
#endif
#ifdef _AT45DB161
#define FlashMaxPage 8192
#endif
#define FlashMaxByte FlashPageBytes*FlashMaxPage
//the absolute frist write Flash,we must initialize the all flash to 0x00
#define FlashDataValidFlag1 0x55
#define FlashDataValidFlag2 0x00
#define FlashDataValidFlag3 0xFF
#define FlashDataValidFlag4 0xAA
#define FlashMinAddress 0 //the frist was reserved for system using
#define FlashMaxAddress 264*4096 //the maximum Flash address
//system reserved function define
#define FlashDataValidFlagNum 4
//1st CheckSum:the Chinese Character Lib's CheckSum(0xC6000-0x108000)
//2nd CheckSum:the self learn message's CkeckSum(0x74040-0xC6000)
//3rd CheckSum:the operation record's CheckSum(0x5B020-0x74040)
//4th CheckSum:the recall message's CheckSum(0x30048-0x5B020)
//5th CheckSum:the reserved sector's CheckSum(0x210-0x30048)
//6th CheckSum:the frist 528 bytes(0x00-0x210)(they are used for saving some system message)
#define FlashDataCheckSumBytesNum 40 //40 bytes check sum
#define FlashReWritePageNumByteNum 2 //2 bytes Autopage rewrite
//save the Data valid flag,if the 4 bytes are not same as 4 ValidFlag above,must initialize
//all flash bytes to 0x00,start from 0x00,occupy 4 bytes
#define FlashAdd_DataValidFlag 0x00
//20 checksum,occupy 40 bytes,one checksum 2 bytes,low byte ahead.
#define FlashAdd_CheckSum (FlashAdd_DataValidFlag+FlashDataValidFlagNum)
//2 bytes Autopage rewrite for At45DB081
#define FlashAdd_ReWritePageNum (FlashAdd_CheckSum+FlashDataCheckSumBytesNum)
//flash start address:for this,applition program can use
#define FlashAdd_DataStartAddress 66 //start address from 66
//all flash is divided 6 parts
#define FlashDataRangeNum 6
#define FlashRangeNum_SystemUsed 0
#define FlashRangeNum_ReservedSector 1
#define FlashRangeNum_RecallRecord 2
#define FlashRangeNum_OptRecord 3
#define FlashRangeNum_SelfLrn 4
#define FlashRangeNum_HanZiLib 5
#define FlashDataPassCodeLen 6 //6 bytes passcode
#define FlashDataPassCodeNum 5 //5 number passcode
#define FlashDataPassCodeBytes (FlashDataPassCodeLen*FlashDataPassCodeNum)//30 bytes passcode,5 number and per 6 bytes
//5 passcode,occupy 30 bytes,one passcode 6 bytes,low byte ahead.
#define FlashAdd_PassCode FlashAdd_DataStartAddress
//system message used
#define FlashAdd_SystemUsed FlashAdd_DataStartAddress
#define FlashNum_SystemUsed (0x210-FlashAdd_DataStartAddress)
//reserved for 191KBytes
#define FlashAdd_ReservedSector 0x210
#define FlashNum_ReservedSector 0x2FE38
//recall data stored for 172KBytes
#define FlashAdd_RecallRecord 0x30048
#define FlashNum_RecallRecord 0x2AFD8
//operation record stored for 100KBytes
#define FlashAdd_OptRecord 0x5B020
#define FlashNum_OptRecord 0x19020
//Self learn message stored for 328KBytes
#define FlashAdd_SelfLrn 0x74040
#define FlashNum_SelfLrn 0x38FA0
#define FlashAdd_Logic 0xACFE0//store logic for 100KBytes
#define FlashNum_Logic 0x19020//store logic for 100KBytes
//Chinese character stored for 260KBytes,the address lies the Top 260KBytes of Flash(from 0xC6000)
#define FlashAdd_HanZiLib 0xC6000
#define FlashNum_HanZiLib 0x42000
//Operation Record define
#define LockIDByteLen 3
#define LockIDOptSortByteLen 1
extern uchar FlashWriteData(ulong FlashAdd,ulong ByteNum,uchar * Data);//write flash
//return 1:write successfully, return 0:fail to write
extern void FlashReadData(ulong FlashAdd,ulong ByteNum,uchar * Data);//read flash
extern void At45DB081_ReadBytesInPage(ulong Add,uint ByteNum,uchar * Data);//read bytes from one page
extern uchar At45DB081_WritBytesInPage(ulong Add,uint ByteNum,uchar * Data);//write bytes to one page
//if return 1:write successfully,return 0:write fail
extern uchar FlashSelfTest(void);
//return 0x00:test true;return 10XX XXXX(the bit 7=1):error,the low 7 bits note
//the error CheckSum sector(<64);return 0xff,all datas in flash are invalid
extern uchar FlashGetCheckSum(uchar RangeNum);//get check sum from Flash
//return 1:true return 0:error
extern uchar FlashMakeCheckSum(uchar RangeNum);//make check sum and write to Flash
//return 1:wrire true return 0:error
extern uchar FlashWritePassCode(uchar PassCodeNum,uchar * PassCodeAdd);
//write passcode to Flash,return 1:write successfully,return 1:Write Fail
extern uchar FlashComparePassCode(uchar *PassCodeAdd);
//compare the *PassCodeAdd with the Flash Saved,if true,return the PassCode Number,else return 0xff
extern uchar FlashWriteOptRecord(ulong FlashAdd,uchar *Data,uint DataLen);//write operation record to Flash
//return False:fail,return True:success
extern uchar FlashWriteSelfLrn(ulong FlashAdd,uchar *Data,uint DataLen);//write operation record to Flash
//return False:fail,return True:success
extern ulong FlashReadOptRecord(ulong FlashAdd,uchar * OptData);
//if FlashAdd=0,frist read
//Read operation Item,if True:return the Next Operation Item address.
//When no next Operation Item,return 0
uchar FlashMakeCheckSum(uchar RangeNum);//make check sum to Flash
//return True:wrire true return False:error
void At45DB081_Buf1Write(uint Add,uint ByteNum,uchar * Data);
void At45DB081_Buf1Read(uint Add,uint ByteNum,uchar * Data);
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -