📄 eeprom.c
字号:
#include <C8051F020.h>
#include "EEPROM.h"
#include"delay.h"
//#include "absacc.h"
#define EEPROMPAGE 128
#define SST_ID 0xBF /* SST Manufacturer抯 ID code */
#define SST_29LE020 0x12 /* SST 29EE020 device code */
#define Base_ROM_ADD 0x4000
#define DATA P7 // 数据端口引脚(AD7..0)
#define ADDR P6 // 地址端口引脚(A15..8)
#define AreaSelect P4 /* 段选择,P4.0~P4.3 ; P4.5 ALE; P4.6 RD; P4.7 WR; 均为低有效*/
/* 其中,P4^6 // 读选通RD(低电平有效)
P4^7 //写选通WR(低电平有效)
P4^5 //地址锁存信号ALE(低电平有效)
*/
void eeprom_Unlock();
void eeprom_Lock();
void SYSCLK_Init (void);
void delay(UINT16 ms);
void Enable_Chip_Data_Protection();
void Check_Data_Polling (UINT8 xdata *Dst, UINT8 TrueData);
void Check_Toggle_Ready (UINT8 xdata *Dst);
/******************************************************************************************************/
/* PROCEDURE: Check_SST_29EE020 */
/* */
/* This procedure decides whether a physical hardware device has a SST */
/* 29EE020 2 Mbit Page Mode EEPROM installed or not. */
/* */
/* */
/* Input: */
/* None */
/* */
/* Output: */
/* return -1: indicates not a SST 29EE020 */
/* return 0: indicates is a SST 29EE020 */
/*****************************************************************************************************/
int Check_SST_29EE020()
{
volatile UINT8 xdata *Tmp;
UINT8 SST_id1;
UINT8 SST_id2;
int ReturnStatus;
/* Issue the Software Product ID code to 29EE020 */
AreaSelect|=1;
Tmp = (UINT8 xdata *)0x5555; /* set up address to be C000:5555h */
*Tmp = 0xAA; /* write data 0xAA to the address */
AreaSelect&=~(1);
Tmp = (UINT8 xdata *)0x6AAA; /* set up address to be C000:2AAAh */
*Tmp = 0x55; /* write data 0x55 to the address */
AreaSelect|=1;
Tmp = (UINT8 xdata *)0x5555; /* set up address to be C000:5555h */
*Tmp = 0x90; /* write data 0x90 to the address */
delay(10);
/* Read the product ID from 29LE020 */
AreaSelect&=~(1);
Tmp = (UINT8 xdata *)0x4000; /* set up address to be C000:0000h */
SST_id1 = *Tmp; /* get first ID byte */
Tmp = (UINT8 xdata *)0x4001; /* set up address to be C000:0001h */
SST_id2 = *Tmp;
if ((SST_id1 == SST_ID) && (SST_id2 ==SST_29LE020))
ReturnStatus = 0;
else
ReturnStatus = -1;
/* Issue the Soffware Product ID Exit code thus returning the 29EE020 */
/* to the read operating mode */
AreaSelect|=1;
Tmp = (UINT8 xdata *)0x5555; /* set up address to be C000:5555h */
*Tmp = 0xAA; /* write data 0xAA to the address */
AreaSelect&=~(1);
Tmp = (UINT8 xdata *)0x6AAA; /* set up address to be C000:2AAAh */
*Tmp = 0x55; /* write data 0x55 to the address */
AreaSelect|=1;
Tmp = (UINT8 xdata *)0x5555; /* set up address to be C000:5555h */
*Tmp =0xF0; /* write data 0xF0 to the address */
delay(10);
return(ReturnStatus);
}
/******************************************************************************************************/
/* PROCEDURE: Write_29EE020 */
/* */
/* This procedure can be used to write a total of 128 bytes at one write cycle to the */
/* SST抯 29EE020. */
/* */
/* Input: */
/* SRC SOURCE address containing the data which will be */
/* written into the 29EE020. */
/* Dst DESTINATION address which will be written with the */
/* data passed in from ds:si */
/* */
/* Output: */
/* None */
/******************************************************************************************************/
void Write_29EE020 (UINT8 blockno, UINT8 xdata *Src, UINT8 xdata *Dst)
{
UINT8 xdata *Temp;
UINT8 xdata *SourceBuf;
UINT8 xdata *DestBuf;
int Index;
SourceBuf = Src;
DestBuf = Dst;
/************************************************************************************/
/* WRITTEN OPERATION */
/* */
/* Issue the 3-byte "enable protection" sequence followed by 128 bytes */
/* of data written to the 29EE020. */
/************************************************************************************/
AreaSelect &= 0xF0;
AreaSelect |= 1;
Temp = (UINT8 xdata *)0x5555; /* set up address to be C000:555h */
*Temp = 0xAA; /* write data 0xAA to the address */
AreaSelect &= ~1;
Temp = (UINT8 xdata *)0x6AAA; /* set up address to be C000:2AAAh */
*Temp = 0x55; /* write data 0x55 to the address */
AreaSelect |= 1;
Temp = (UINT8 xdata *)0x5555; /* set up address to be C000:5555h */
*Temp = 0xA0; /* write data 0xA0 to the address */
AreaSelect &= 0xF0;
AreaSelect|=blockno;
for (Index = 0; Index < EEPROMPAGE; Index++)
{
*DestBuf++ = *SourceBuf++; /* transfer data from source to destination */
}
delay(1); /* wait 1ms to start writing */
Check_Toggle_Ready(Dst); /* wait for TOGGLE bit to get ready */
}
/******************************************************************************************************/
/* PROCEDURE: Check_Toggle_Ready */
/* */
/* During the internal write cycle, any consecutive read operation */
/* on DQ6 will produce alternating 0抯 and 1抯 i.e. toggling between */
/* 0 and 1. When the write cycle is completed, DQ6 of the data will */
/* stop toggling. After the DQ6 data bit stops toggling, the device is ready */
/* for next operation. */
/* */
/* Input: */
/* Dst must already set-up by the caller */
/* */
/* Output: */
/* None */
/******************************************************************************************************/
void Check_Toggle_Ready (UINT8 xdata *Dst)
{
UINT8 Loop = 1;
UINT8 PreData;
UINT8 CurrData;
unsigned long TimeOut = 0;
PreData = *Dst;
PreData = PreData & 0x40;
while ((TimeOut< 0x07FFFFFF) && (Loop))
{
CurrData = *Dst;
CurrData = CurrData & 0x40;
if (PreData == CurrData)
Loop = 0; /* ready to exit the while loop */
PreData = CurrData;
TimeOut++;
}
}
/******************************************************************************************************/
/* PROCEDURE: Check_Data_Polling */
/* */
/* During the internal write cycle, any attempt to read DQ7 of the last byte loaded during */
/* the page/byte-load cycle will receive the complement of the true data. Once the */
/* write cycle is completed, DQ7 will show true data. */
/* */
/* Input: */
/* Dst must already set-up by the caller */
/* True Datathis is the original (true) data */
/* */
/* Output: */
/* None */
/******************************************************************************************************/
void Check_Data_Polling (UINT8 xdata *Dst, UINT8 TrueData)
{
UINT8 Loop = 1;
UINT8 CurrData;
unsigned long TimeOut = 0;
TrueData = TrueData & 0x80;
while ((TimeOut< 0x07FFFFFF) && (Loop))
{
CurrData = *Dst;
CurrData = CurrData & 0x80;
if (TrueData == CurrData)
Loop = 0; /* ready to exit the while loop */
TimeOut++;
}
}
/******************************************************************************************************/
/* PROCEDURE: Enable_Chip_Data_Protection */
/* */
/* This procedure ENABLES the data protection feature on the 29EE020 */
/* 2 Mbit Page Mode EEPROM. After calling this routine, the chip cannot be written */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -