?? eeprom.c
字號(hào):
/***********************************
Name: EEPROMDG128.c
Function:Erase and Program the EEPROM
Author: QIU ZHAOPENG
Data: 2008.10.01
************************************/
/* MODULE IEE1. */
#include "IEE1.h"
/* Definition of DATA and CODE segments for this bean. User can specify where
these segments will be located on "Build options" tab of the selected CPU bean. */
#pragma DATA_SEG IEE1_DATA /* Data section for this module. */
#pragma CODE_SEG IEE1_CODE /* Code section for this module. */
bool IEE1_Wait ; /* Wait status of writting methods */
//dword tmpDword;
//word tmpWord; //用于Word變量的返回
//byte tmpByte; //用于Byte變量的返回
/*
** ===================================================================
** Method : WriteWord (bean IntEEPROM)
**
** Description :
** This method is internal. It is used by Processor Expert
** only.
** ===================================================================
*/
byte WriteWord(word AddrRow,word Data16)
{
if (ESTAT_CBEIF == 0) /* Is command buffer full ? */
return ERR_BUSY; /* If yes then error */
/* ESTAT: PVIOL=1,ACCERR=1 */
ESTAT |= 48; /* Clear error flags */
*(volatile word *) AddrRow = Data16; /* Array address and program data */
/* ECMD: ??=0,CMDB6=0,CMDB5=1,??=0,??=0,CMDB2=0,??=0,CMDB0=0 */
ECMD = 32; /* Word program command */
ESTAT_CBEIF = 1; /* Clear flag command buffer empty */
if ((ESTAT_PVIOL == 1)||(ESTAT_ACCERR == 1)) /* Is protection violation or acces error detected ? */
return ERR_NOTAVAIL; /* If yes then error */
if (IEE1_Wait) { /* Is the flag set ? */
while (ESTAT_CBEIF == 0); /* If yes then wait to buffer empty */
while (ESTAT_CCIF == 0); /* Wait for command completition */
if (*(volatile word *) AddrRow != Data16) /* Was attempt to write data to the given address errorneous? */
return ERR_VALUE; /* If yes then error */
}
return ERR_OK;
}
/*
** ===================================================================
** Method : WriteSector (bean IntEEPROM)
**
** Description :
** This method is internal. It is used by Processor Expert
** only.
** ===================================================================
*/
byte WriteSector(word AddrSec,dword Data32)
{
byte err; /* Temporary variable */
if (ESTAT_CBEIF == 0) /* Is command buffer full ? */
return ERR_BUSY; /* If yes then error */
/* ESTAT: PVIOL=1,ACCERR=1 */
ESTAT |= 48; /* Clear error flags */
*(volatile word *) AddrSec = (word)(Data32 >> 16); /* Array address and program data - higher part */
/* ECMD: ??=0,CMDB6=1,CMDB5=1,??=0,??=0,CMDB2=0,??=0,CMDB0=0 */
ECMD = 96; /* Sector modify command */
ESTAT_CBEIF = 1; /* Clear flag command buffer empty */
if ((ESTAT_PVIOL == 1)||(ESTAT_ACCERR == 1)) /* Is protection violation or acces error detected ? */
return ERR_NOTAVAIL; /* If yes then error */
while (ESTAT_CBEIF == 0); /* Wait to buffer empty */
err=WriteWord(AddrSec + 2,(word)Data32); /* Write lower part */
if (err != ERR_OK) /* Was attemp to write data to the given address errorneous? */
return err; /* If yes then error */
if (IEE1_Wait) { /* Is the flag set ? */
if (*(volatile word *) AddrSec != (word)(Data32 >> 16)) /* Was attempt to write data to the given address errorneous? */
return ERR_VALUE; /* If yes then error */
}
return ERR_OK;
}
/*
** ===================================================================
** Method : IEE1_SetByte (bean IntEEPROM)
**
** Description :
** Method writes given byte to the given address in EEPROM.
** Parameters :
** NAME - DESCRIPTION
** Addr - Address to EEPROM
** Data - Data to write
** Returns :
** --- - Error code
** ===================================================================
*/
byte IEE1_SetByte(word Addr,byte Data)
{
union {
byte b[4];
word w[2];
long l;
} backup;
byte idx;
if ((Addr < EEPROMStart)||(Addr > EEPROMEnd)) /* Is given address out of EEPROM area array ? */
return ERR_RANGE; /* If yes then error */
backup.l = *(volatile dword *)(Addr & 0xFFFC); /* Load sector to variable backup */
backup.b[Addr&0x0003] = Data; /* Store data to variable backup */
Addr &= 0xFFFE; /* Aligned address */
if (*(volatile word *)(Addr) == 0xFFFF) { /* Is given EEPROM row erased ? */
idx = (byte) ((Addr & 0x0002) >> 1); /* Word index in sector */
return (WriteWord(Addr,backup.w[idx])); /* Write new content */
}
else /* Is given address non-erased ? */
return (WriteSector(Addr & 0xFFFC, backup.l)); /* If yes then write new content */
}
/*
** ===================================================================
** Method : IEE1_GetByte (bean IntEEPROM)
**
** Description :
** Method reads byte from the given EEPROM address
** Parameters :
** NAME - DESCRIPTION
** Addr - Address to EEPROM
** * Data - Pointer to returned 8-bit data
** Returns :
** --- - Error code
** ===================================================================
*/
byte IEE1_GetByte(word Addr,byte *Data)
{
if ((Addr < EEPROMStart)||(Addr > EEPROMEnd)) /* Is given address out of EEPROM area array ? */
return ERR_RANGE; /* If yes then error */
*Data = *(volatile byte *) Addr; /* Return data from given address */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : IEE1_SetWord (bean IntEEPROM)
**
** Description :
** Method writes given word to the given address in EEPROM.
** Parameters :
** NAME - DESCRIPTION
** Addr - Address to EEPROM
** Data - Data to write
** Returns :
** --- - Error code
** ===================================================================
*/
byte IEE1_SetWord(word Addr,word Data)
{
if ((Addr < EEPROMStart)||(Addr > (EEPROMEnd - 1))) /* Is given address out of EEPROM area array ? */
return ERR_RANGE; /* If yes then error */
if (Addr & 0x0001) /* Aligned address ? */
return ERR_NOTAVAIL;
if (*(volatile word *)Addr == 0xFFFF) { /* Is given EEPROM row erased ? */
return (WriteWord(Addr,Data)); /* Write new content */
}
else { /* Is given address non-erased ? */
if (Addr & 2) /* Is given address from low part of the sector ? */
return (WriteSector(Addr & 0xFFFC,(dword)Data | ((*(volatile dword *)(Addr & 0xFFFC) & 0xFFFF0000))));
else /* Is given address from high part of the sector ? */
return (WriteSector(Addr,((dword)Data << 16) | (*(volatile word *)(Addr + 2))));
}
}
/*
** ===================================================================
** Method : IEE1_GetWord (bean IntEEPROM)
**
** Description :
** Method reads word from the given EEPROM address
** Parameters :
** NAME - DESCRIPTION
** Addr - Address to EEPROM
** * Data - Pointer to returned 16-bit data
** Returns :
** --- - Error code
** ===================================================================
*/
byte IEE1_GetWord(word Addr,word *Data)
{
if ((Addr < EEPROMStart)||(Addr > (EEPROMEnd - 1))) /* Is given address out of EEPROM area array ? */
return ERR_RANGE; /* If yes then error */
*Data = *(volatile word *) Addr; /* Return data from given address */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : IEE1_SetWait (bean IntEEPROM)
**
** Description :
** Method changes wait status of methods SetByte, SetActByte
** and SetPage.
** Parameters :
** NAME - DESCRIPTION
** Wait - TRUE - methods wait till the write
** operation to EEPROM is finished,
** FALSE - methods do not wait for the end
** of write operation
** Returns : Nothing
** ===================================================================
*/
/*
void IEE1_SetWait(bool Wait)
** This method is implemented as macro. See IEE1.h file. **
*/
/*
** ===================================================================
** Method : IEE1_Busy (bean IntEEPROM)
**
** Description :
** Method return status of EEPROM device
** Parameters : None
** Returns :
** --- - TRUE/FALSE - EEPROM is busy/ready
** ===================================================================
*/
/*
bool IEE1_Busy(void)
** This method is implemented as macro. See IEE1.h file. **
*/
/*
** ===================================================================
** Method : IEE1_Init (bean IntEEPROM)
**
** Description :
** This method is internal. It is used by Processor Expert
** only.
** ===================================================================
*/
void IEE1_Init(void)
{
ECLKDIV = 4a; //16M/8=2M,2M/(1+10)=181.8k in the range 150k--200k
IEE1_Wait = TRUE; /* No wait in loop to command complete */
}
/* END IEE1. */
/*
** ###################################################################
**
** This file was created by UNIS Processor Expert 03.33 for
** the Motorola HCS12 series of microcontrollers.
**
** ###################################################################
*/
/*****************************************************
* 在Eeprom的Addr位置寫入word類型的數(shù)據(jù)Data
*****************************************************/
void WriteEepromWord(word Addr, word Data)
{
word tmpWord;
while (IEE1_Busy());
IEE1_GetWord(Addr, &tmpWord);
if (Data != tmpWord)
{
while (IEE1_Busy());
IEE1_SetWord(Addr, Data);
}
}
/*****************************************************
* 在Eeprom的Addr位置讀取word類型的數(shù)據(jù)
*****************************************************/
word ReadEepromWord(word Addr)
{
word tmpWord;
while (IEE1_Busy());
IEE1_GetWord(Addr, &tmpWord);
return tmpWord;
}
/*****************************************************
* 在Eeprom的Addr位置寫入byte類型的數(shù)據(jù)Data
*****************************************************/
void WriteEepromByte(word Addr, byte Data)
{
byte tmpByte;
while (IEE1_Busy());
IEE1_GetByte(Addr, &tmpByte);
if (Data != tmpByte)
{
while (IEE1_Busy());
IEE1_SetByte(Addr, Data);
}
}
/*****************************************************
* 在Eeprom的Addr位置讀取byte類型的數(shù)據(jù)
*****************************************************/
byte ReadEepromByte(word Addr)
{
byte tmpByte;
while (IEE1_Busy());
IEE1_GetByte(Addr, &tmpByte);
return tmpByte;
}
/*
** ===================================================================
** Method : IEE2_SetLong (bean IntEEPROM)
**
** Description :
** Method writes given long word to the given address in
** EEPROM.
** Parameters :
** NAME - DESCRIPTION
** Addr - Address to EEPROM
** Data - Data to write
** Returns :
** --- - Error code
** ===================================================================
*/
byte IEE2_SetLong(word Addr,dword Data)
{
if ((Addr < EEPROMStart)||(Addr > (EEPROMEnd - 3))) /* Is given address out of EEPROM area array ? */
return ERR_RANGE; /* If yes then error */
if (Addr & 0x0003) /* Aligned address ? */
return ERR_NOTAVAIL;
return (WriteSector(Addr,Data)); /* Write new content of given sector */
}
/*
** ===================================================================
** Method : IEE2_GetLong (bean IntEEPROM)
**
** Description :
** Method reads long word from the given EEPROM address
** Parameters :
** NAME - DESCRIPTION
** Addr - Address to EEPROM
** * Data - Pointer to returned 32-bit data
** Returns :
** --- - Error code
** ===================================================================
*/
byte IEE2_GetLong(word Addr,dword *Data)
{
if ((Addr < EEPROMStart)||(Addr > (EEPROMEnd - 3))) /* Is given address out of EEPROM area array ? */
return ERR_RANGE; /* If yes then error */
*Data = *(volatile dword *) Addr; /* Return data from given address */
return ERR_OK; /* OK */
}
/*****************************************************
* 在Eeprom的Addr位置寫入lonh類型的數(shù)據(jù)Data
*****************************************************/
void WriteEepromDword(word Addr, dword Data)
{
dword tmpDword;
while (IEE1_Busy());
IEE2_GetLong(Addr, &tmpDword);
if (Data != tmpDword)
{
while (IEE1_Busy());
IEE2_SetLong(Addr, Data);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -