📄 eepromctrlex0205.c
字号:
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
System : EEPROM emulation
Module name : EEPROM emulation control processing
File name : EEPROMCtrlEx.c
Version name : 1.00
Target CPU : uPD78F0547(78K0/KF2)
Last updated : 2005/09/08
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma section @@CODE SLF_CTRL AT 7400H
#include "EEPROMCtrlEx0205.h"
/*-------------------------------------------------------------------
Function name : ucEEPROMInitEx(user access function)
Arguments : None
Return value : Normal(TRUE:00H) / Noemal end(Block error)(NMLBLK:01H) /
Prohibition of all block use(ERRALL:FBH) / Device error(ERRDEV:FDH) /
Initialization error(ERRINIT:FEH)
Summary : initialize EEPROM emulation
------------------------------------------------------------------*/
UCHAR ucEEPROMInitEx( void ) /* EEPROM emulation Initialize processing */
{
USHORT usFlagAdr; /* Effective/invalid flag address */
UCHAR ucFindBlock; /* Discovery block num ber */
UCHAR ucCount; /* Elimination counter */
UCHAR ucFlags; /* Various flags */
/* D7:Block error */
/* D6:Block elimination */
/* D5:not use */
/* D4:not use */
/* D3:not use */
/* D2:not use */
/* D1:not use */
/* D0:not use */
UCHAR ucResult; /* Processing result */
UCHAR i;
/* Parameter Check */
if( PARAM_OK == FALSE ){ /* parameter error? */
return( ERRPRM ); /* then rerun */
}
/* Variable initialization */
ucFindBlock = 0xFF; /* Discovery block number initialization */
ucFlags = 0x00; /* Various flag initialization */
/* Block elimination and effective block setting processing */
for ( i = 0 ; i < EEPROM_BLOCK_NO ; i++ ){ /* loop for use block several minutes */
/* Elimination flag Off */
ucFlags &= ~FL_DOERS;
/* Bank setting */
SET_BANK( ucEEPROM_BANK[ i ] );
/* The bank top address (a flag storage address) acquisition */
usFlagAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ i ] );
/* An elimination condition judgment */
if( BLOCK_IS_PROHIBIT( (UCHAR*)usFlagAdr ) == FALSE ){ /* A block is a ban on use state? */
if ( BLOCK_IS_EFFECTIVE( (UCHAR*)usFlagAdr ) == TRUE ){ /* A block is effective? */
if ( ucFindBlock == 0xFF ){ /* Effective block non-discovery */
ucFindBlock = i; /* Block number setting */
}
else{ /* finished with effective block discovery */
ucFlags |= FL_DOERS; /* Elimination flag On */
}
}
else{ /* block=An invalid state (an abnormal state) */
ucFlags |= FL_DOERS; /* Elimination flag On */
}
}
/* Processing to erase a block */
if ( ucFlags & FL_DOERS ){ /* Elimination flag On? */
SelfFlashModeOn(); /* changes to a self-programming mode */
if ( ucSelfFlashInitialize() == FALSE ){ /* self-programming Initialization */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRINIT ); /* An initialization error */
}
/* An elimination counter minute loop */
for ( ucCount = ERASENUM + 1; ucCount > 0; ucCount-- ) {
/* Erases data in the block next used as EEPROM */
if ( ucSelfFlashBlockErase( ucEEPROM_BANK[ i ], ucEEPROM_BLOCK[ i ] ) == TRUE ){ /* 惓忢廔椆 */
break; /* loop end */
}
}
/* An elimination result judgment */
if ( ucCount <= 0 ){ /* Count<=0 (re-try over) */
/* make a block the ban on use */
if ( ucEEPROMUnableEx( i ) == FALSE ){ /* Abnormal termination */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRDEV ); /* device error */
}
ucFlags |= FL_BLKERR; /* Block error flag On */
}
SelfFlashModeOff(); /* changes to a normal mode */
}
}
/* Setting handling of effective blocking */
ucResult = TRUE; /* Return value setting */
if ( ucFindBlock == 0xFF ){ /* Effective block non-discovery */
for ( i = 0 ; i < EEPROM_BLOCK_NO ; i++ ){ /* loop for use block several minutes */
/* Bank setting */
SET_BANK( ucEEPROM_BANK[ i ] );
/* The bank top address (a flag storage address) acquisition */
usFlagAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ i ] );
/* A flag state judgment */
if( BLOCK_IS_BLANK( (UCHAR*)usFlagAdr ) == TRUE ){ /* A block is a state finished with elimination? */
ucDataBuf[ 0 ] = 0x55;
ucDataBuf[ 1 ] = 0x55;
ucDataBuf[ 2 ] = 0x55;
ucDataBuf[ 3 ] = 0x55; /* Provids block validity setting data */
SelfFlashModeOn(); /* changes to a self-programming mode */
if ( ucSelfFlashInitialize() == FALSE ){ /* self-programming Initialization */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRINIT ); /* initialization error */
}
if ( ucSelfFlashEEPROMWrite( ucEEPROM_BANK[ i ], usFlagAdr, 4-1 ) == TRUE ){ /* Sets block 0 as valid block */
SelfFlashModeOff(); /* changes to a normal mode */
break;
}
else {
/* make a block the ban on use */
if ( ucEEPROMUnableEx( i ) == FALSE ){ /* abnormal end */
SelfFlashModeOff(); /* changes to a normal mode */
ucResult = ERRDEV; /* device error */
break;
}
ucFlags |= FL_BLKERR; /* Block error flag On */
}
SelfFlashModeOff(); /* changes to a normal mode */
}
}
if ( i >= EEPROM_BLOCK_NO ){ /* not able to set a block effectively */
ucResult = ERRALL; /* Prohibition of all block use */
}
}
if ( ucResult == TRUE ){ /* Normal end */
if ( ucFlags & FL_BLKERR ){ /* Those with a block error */
ucResult = NMLBLK; /* Normal end(Block error generating) */
}
}
return ( ucResult );
}
/*-------------------------------------------------------------------
Function name : ucEEPROMReadEx(user access function)
Arguments : ucDataNo:Read data No.
: *pDataAddress:Read data storage address
Return value : Normal completion(TRUE:00H) / Reading data nothing(ERRNODAT:F7H) /
With no effective block(ERRNOUSE:FAH)
Summary : Data with the specified number is read from EEPROM to the storage address.
: 1.Searches for blocks being used as EEPROM.
: 2.Searches for latest data's address among valid blocks.
: 3.Reads latest data from searched address.
-------------------------------------------------------------------*/
UCHAR ucEEPROMReadEx( UCHAR ucDataNo, UCHAR *pDataAddress ) /* EEPROM data read processing */
{
USHORT usDataSrcAdr;
UCHAR ucBlockTBLNo;
UCHAR *pReadAdr;
UCHAR i;
/* Parameter Check */
if( PARAM_OK == FALSE ){ /* parameter error? */
return( ERRPRM ); /* then rerun */
}
/* Searches for valid blocks currently used as EEPROM */
ucBlockTBLNo = ucEEPROMUseBlockSearchEx();
if ( ucBlockTBLNo == FALSE ){ /* abnormal end? */
return ( ERRNOUSE ); /* With no effective block */
}
/* Obtains address of latest data for specified number */
usDataSrcAdr = usEEPROMDataSearchEx( ucDataNo, ucBlockTBLNo );
if ( usDataSrcAdr == 0x00 ){
return( ERRNODAT ); /* no data */
}
/* Reads latest data from obtained address */
pReadAdr = (UCHAR*)( usDataSrcAdr +1 );
for ( i=0 ; i < DATALENGTH ; i++ ){
*pDataAddress++ = *pReadAdr++;
}
if ( ( (USHORT)pReadAdr & 0x0003 ) == 0x0003 ){ /* Offset is +3 located (a 4byte unit) */
/* Since the delimiter is read, it rewrites by the last byte data */
pDataAddress--; /* One pointer is returned */
*pDataAddress = *pReadAdr;
}
return( TRUE );
}
/*-------------------------------------------------------------------
Function name : ucEEPROMWriteEx(user access function)
Arguments : ucDataNo:Write data number
: *pDataAddress:Write data storage address
Return value : Normal completion(TRUE:00H) / Data full(ERRFULL:F5H) / Write error(ERRWRITE:F6H) /
With no effective block(ERRNOUSE:FAH) / Initialization error(ERRINIT:FEH)
Summary : Writes data with specified number from the storage address to EEPROM.
: 1.Searches for blocks being used as EEPROM.
: 2:Searches for addresses to enable writing to valid block.
: 3:Creates write data.
: 4:Writes to valid blocks.
-------------------------------------------------------------------*/
UCHAR ucEEPROMWriteEx( UCHAR ucDataNo, UCHAR *pDataAddress ) /* EEPROM data write processing */
{
USHORT usBlockAdr;
USHORT usWriteAdr;
UCHAR ucBlockTBLNo;
UCHAR ucCount;
UCHAR ucResult;
UCHAR i;
/* Parameter Check */
if( PARAM_OK == FALSE ){ /* parameter error? */
return( ERRPRM ); /* then rerun */
}
/* Searches for valid blocks being used as EEPROM */
ucBlockTBLNo = ucEEPROMUseBlockSearchEx();
if ( ucBlockTBLNo == FALSE ){ /* abnormal end */
return( ERRNOUSE ); /* With no effective block */
}
/* Bank set */
SET_BANK( ucEEPROM_BANK[ ucBlockTBLNo ] );
/* Bank final address acquisition */
usBlockAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ ucBlockTBLNo ] );
usBlockAdr += DATA_END_OFFSET;
/* Obtains EEPROM write address */
usWriteAdr = usEEPROMWriteTopSearchEx( ucBlockTBLNo );
/* Uses data specified by DataNo to create data to write to EEPROM */
ucDataBuf[ WRITE_DATA_LENGTH ] = 0xFF; /* Last field: Delimiter */
ucDataBuf[ WRITE_DATA_LENGTH -1 ] = 0xFF;
ucDataBuf[ WRITE_DATA_LENGTH -2 ] = 0xFF;
ucDataBuf[ WRITE_DATA_LENGTH -3 ] = 0xFF; /* FFH setting of empty data */
ucDataBuf[ 0 ] = ucDataNo; /* Top: Data number */
for ( i = 1 ; i <= DATALENGTH ; i++ ){
ucDataBuf[ i ] = *pDataAddress++; /* Setting of writing data */
}
/* A delimiter is set */
ucDataBuf[ WRITE_DATA_LENGTH ] = ucDataBuf[ WRITE_DATA_LENGTH-1 ]; /* The last data is moved to the last byte */
ucDataBuf[ WRITE_DATA_LENGTH-1 ] = 0x00;
SelfFlashModeOn(); /* changes to a self-programming mode */
if ( ucSelfFlashInitialize() == FALSE ){ /* self-programming Initialization */
SelfFlashModeOff(); /* changes to a normal mode */
return( ERRINIT ); /* initialization error */
}
/* Write-in counter initialization */
ucCount = WRITENUM + 1;
while( 1 ) { /* loop */
/* An empty check */
if ( usWriteAdr >= usBlockAdr ) { /* With no opening */
ucResult = ERRFULL; /* Data full */
break;
}
/* Writes write data to EEPROM */
ucResult = ucSelfFlashEEPROMWrite( ucEEPROM_BANK[ ucBlockTBLNo ], usWriteAdr, WRITE_DATA_LENGTH );
if ( ucResult == TRUE ){ /* Normal end? */
break;
}
else if ( ucResult != LIB_BLANK_ERR ){ /* Except a blank error */
ucCount--; /* Counter-1 */
if ( ucCount <= 0 ) { /* retry over */
ucResult = ERRWRITE; /* write error */
break;
}
}
/* Address update */
usWriteAdr += WRITE_DATA_LENGTH+1;
}
/* changes to a normal mode */
SelfFlashModeOff();
return ( ucResult );
}
/*-------------------------------------------------------------------
Function name : ucEEPROMChangeEx
Arguments : None
Return value : Normal completion(TRUE:00H) / Noemal end(Block eror)(NMLBLK:01H) /
Block nothing finished with elimination(ERRNOBLA:F8H) /
With no effective block(ERRNOUSE:FAH) / Device error(ERRDEV:FDH) /
Initialization error(ERRINIT:FEH)
Summary : If the currently used blocks are full of data, this function searches for the next
: block to be used and copies data to that block.
: 1.Sets block to be used next.
: 2.Erases block to be used next.
: 3.Transfers the latest data from a valid block to the next block.
: 4.Sets the next block to be used as valid.
: 5.Sets currently valid blocks as invalid.
-------------------------------------------------------------------*/
UCHAR ucEEPROMChangeEx( void ) /* EEPROM block change processing */
{
UCHAR ucFlags; /* Various flags */
/* D7:block error */
/* D6:Block elimination */
/* D5:not use */
/* D4:not use */
/* D3:not use */
/* D2:not use */
/* D1:not use */
/* D0:not use */
UCHAR ucCount; /* erase counter */
USHORT usOldAdr; /* old block address */
USHORT usNewAdr; /* new block address */
UCHAR *pGetAdr; /* Read address */
UCHAR *pSetAdr; /* Write address */
UCHAR ucOldBlockTBLNo; /* old block no. */
UCHAR ucNewBlockTBLNo; /* new block no. */
UCHAR i;
UCHAR j;
USHORT k;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -