📄 iee1.c
字号:
/** ###################################################################
** THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
** Filename : IEE1.C
** Project : eeprom
** Processor : MC9S12HZ64CFU
** Beantype : IntEEPROM
** Version : Bean 02.177, Driver 01.19, CPU db: 2.87.443
** Compiler : CodeWarrior HC12 C Compiler
** Date/Time : 2008-12-15, 上午 11:04
** Abstract :
** This device "IntEEPROM" implements internal EEPROM
** Comment :
** The EEPROM array is organized as rows of word (2 bytes), the EEPROM block's
** erase sector size is 2 rows (2 words). Therefore it is preferable
** to use word aligned data for writting - methods SetWord and SetLong -
** with word aligned address.
** Driver expects that all security options of EEPROM are disabled.
** If some security option is enabled methods performing write
** operation (such as SetWord) can return error.
** Settings :
** EEPROM size : 1024 byte
** Initialization:
** Wait in methods : Disabled
** EEPROM clock : 181 kHz
**
** Contents :
** SetByte - byte IEE1_SetByte(IEE1_TAddress Addr, byte Data);
** GetByte - byte IEE1_GetByte(IEE1_TAddress Addr, byte *Data);
** SetWord - byte IEE1_SetWord(IEE1_TAddress Addr, word Data);
** GetWord - byte IEE1_GetWord(IEE1_TAddress Addr, word *Data);
** EraseEeprom - byte IEE1_EraseEeprom(IEE1_TAddress Addr);
**
** (c) Copyright UNIS, spol. s r.o. 1997-2007
** UNIS, spol. s r.o.
** Jundrovska 33
** 624 00 Brno
** Czech Republic
** http : www.processorexpert.com
** mail : info@processorexpert.com
** ###################################################################*/
/* MODULE IEE1. */
#include "IEE1.h"
#pragma DATA_SEG IEE1_DATA
#pragma CODE_SEG IEE1_CODE
/*
** ===================================================================
** Method : WriteWord (bean IntEEPROM)
**
** Description :
** The method writes the word data to EEPROM memory.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static byte WriteWord(IEE1_TAddress 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 = 128; /* 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 */
}
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : WriteSector (bean IntEEPROM)
**
** Description :
** The method writes the data block to EEPROM memory.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static byte WriteSector(IEE1_TAddress AddrSec,dword Data32)
{
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=0,??=0,??=0,CMDB2=0,??=0,CMDB0=0 */
ECMD = 64; /* Sector erase command */
ESTAT = 128; /* 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 & 0xC0) != 0xC0) {} /* Wait for command completition */
ESTAT = 48; /* Clear error flags */
*(volatile word *)(AddrSec) = (word)(Data32 >> 16); /* Array address and program data - higher part */
/* ECMD: ??=0,CMDB6=0,CMDB5=1,??=0,??=0,CMDB2=0,??=0,CMDB0=0 */
ECMD = 32; /* Word program command */
ESTAT = 128; /* 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 & 0xC0) != 0xC0) {} /* If yes then wait for command completition */
return WriteWord(AddrSec + 2,(word)Data32); /* Write lower part */
}
/*
** ===================================================================
** Method : IEE1_SetByte (bean IntEEPROM)
**
** Description :
** This method writes a given byte to a specified address in
** EEPROM. The method also sets address pointer for <SetActByte>
** and <GetActByte> methods (applicable only if these methods
** are enabled). The pointer is set to address passed as the
** parameter.
** Parameters :
** NAME - DESCRIPTION
** Addr - Address to EEPROM
** Data - Data to write
** Returns :
** --- - Error code, possible codes:
** - ERR_OK - OK
** - ERR_SPEED - the bean does not work in the
** active speed mode
** - ERR_BUSY - device is busy
** - ERR_VALUE - verification of written data
** failed (read value does not match with
** written value)
** - ERR_NOTAVAIL - other device-specific
** error
** - ERR_RANGE - parameter Addr is out of range
** ===================================================================
*/
byte IEE1_SetByte(IEE1_TAddress Addr,byte Data)
{
union {
byte b[4];
word w[2];
long l;
} backup;
byte idx;
if((Addr < IEE1_AREA_START) || (Addr > IEE1_AREA_END)) { /* Is given address out of EEPROM area array ? */
return ERR_RANGE; /* If yes then error */
}
if((ESTAT & 192) != 192) { /* Is reading from EEPROM possible? */
return ERR_BUSY; /* If no then error */
}
backup.l = *(volatile dword *)(Addr & 65532); /* Load sector to variable backup */
backup.b[Addr & 3] = Data; /* Store data to variable backup */
Addr &= 65534; /* Aligned address */
if (*(volatile word *)(Addr) == 65535) { /* Is given EEPROM row erased ? */
idx = (byte) ((Addr & 2) >> 1); /* Word index in sector */
return (WriteWord(Addr,backup.w[idx])); /* Write new content */
} else { /* Is given address non-erased ? */
return (WriteSector(Addr & 65532,(dword)backup.l)); /* If yes then write new content */
}
}
/*
** ===================================================================
** Method : IEE1_GetByte (bean IntEEPROM)
**
** Description :
** This method reads a byte from a specified EEPROM address.
** The method also sets address pointer for <SetActByte> and
** <GetActByte> methods (applicable only if these methods are
** enabled). The pointer is set to address passed as the
** parameter.
** Parameters :
** NAME - DESCRIPTION
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -