📄 eepromctrlex0205.c
字号:
/* Parameter Check */
if( PARAM_OK == FALSE ){ /* parameter error? */
return( ERRPRM ); /* then rerun */
}
/* Flag initialization */
ucFlags = 0x00;
/* Searches for valid blocks being used as EEPROM */
ucOldBlockTBLNo = ucEEPROMUseBlockSearchEx();
if ( ucOldBlockTBLNo == FALSE ){
return ( ERRNOUSE ); /* With no effective block */
}
/* Bank set */
SET_BANK( ucEEPROM_BANK[ ucOldBlockTBLNo ] );
/* bank top address(flag address) get */
usOldAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ ucOldBlockTBLNo ] );
/* processing block no. set */
ucNewBlockTBLNo = ucOldBlockTBLNo;
/* block change process */
for ( i = 0 ; i < EEPROM_BLOCK_NO ; i++ ) { /* loop */
/* block no. update */
if ( ucNewBlockTBLNo >= (EEPROM_BLOCK_NO-1) ){ /* last block? */
ucNewBlockTBLNo = 0; /* returns to a head */
}
else {
ucNewBlockTBLNo++; /* block No+1 */
}
/* Bank set */
SET_BANK( ucEEPROM_BANK[ ucNewBlockTBLNo ] );
/* bank top address(flag address) get */
usNewAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ ucNewBlockTBLNo ] );
/* effective/Invalid flag check */
if( BLOCK_IS_PROHIBIT( (UCHAR*)usNewAdr ) == FALSE ){ /* block亖Except prohibition of use? */
#ifdef AUTOERASE
SelfFlashModeOn(); /* changes to a self-programming mode */
if ( ucSelfFlashInitialize() != TRUE ){ /* self-programming Initialization */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRINIT ); /* initialization error */
}
for ( ucCount = ERASENUM + 1; ucCount > 0; ucCount-- ){ /* loop */
/* Erases data in the block next used as EEPROM */
if ( ucSelfFlashBlockErase( ucEEPROM_BANK[ ucNewBlockTBLNo ], ucEEPROM_BLOCK[ ucNewBlockTBLNo ] ) == TRUE ){ /* Normal end */
break;
}
}
if ( ucCount <= 0 ){ /* counter <= 0 (retry over) */
/* block is made prohibition of use */
if ( ucEEPROMUnableEx( ucNewBlockTBLNo ) == FALSE ){/* abnormal end */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRDEV ); /* device error */
}
ucFlags |= FL_BLKERR; /* Block error flag On */
continue;
}
SelfFlashModeOff(); /* changes to a normal mode */
#endif /* AUTOERASE */
if( BLOCK_IS_BLANK( (UCHAR*)usNewAdr ) == TRUE ){ /* block亖state of eliminating? */
/* Sets write start address for next block's data */
pSetAdr = (UCHAR *)( usNewAdr + DATATOP ) ;
/* Transfers latest EEPROM data from current block to next block */
for ( j = 0 ; j < DATA_NO_MAX ; j++ ){
/* The newest data search */
pGetAdr = (UCHAR *)usEEPROMDataSearchEx( j, ucOldBlockTBLNo );
if ( (USHORT)pGetAdr != 0 ){ /* Those with data? */
for ( k=0 ; k < (WRITE_DATA_LENGTH+1) ; k++ ){
ucDataBuf[k] = *pGetAdr++;
}
SET_BANK( ucEEPROM_BANK[ ucNewBlockTBLNo ] );/* Bank set */
SelfFlashModeOn(); /* changes to a self-programming mode */
if ( ucSelfFlashInitialize() != TRUE ){ /* self-programming Initialization */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRINIT ); /* initialization error */
}
for ( ucCount = (WRITENUM + 1); ucCount > 0; ucCount--){
/* write to new block */
if ( ucSelfFlashEEPROMWrite( ucEEPROM_BANK[ ucNewBlockTBLNo ], (USHORT)pSetAdr, WRITE_DATA_LENGTH ) == TRUE ){
break;
}
/* Address update */
pSetAdr += WRITE_DATA_LENGTH+1;
}
if ( ucCount <= 0 ) { /* write error */
/* Except prohibition of use */
if ( ucEEPROMUnableEx( ucNewBlockTBLNo ) == FALSE ){ /* abnormal end */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRDEV ); /* device error */
}
SelfFlashModeOff(); /* changes to a normal mode */
ucFlags |= FL_BLKERR; /* Block error flag On */
break;
}
SelfFlashModeOff(); /* changes to a normal mode */
/* 嵟戝僨乕僞挿暘Address update */
pSetAdr += WRITE_DATA_LENGTH+1;
}
}
/* Renewal processing of a flag */
if( j >= DATA_NO_MAX ) { /* data copy Normal end? */
/* write 55555555h */
ucDataBuf[ 0 ] = 0x55;
ucDataBuf[ 1 ] = 0x55;
ucDataBuf[ 2 ] = 0x55;
ucDataBuf[ 3 ] = 0x55;
SelfFlashModeOn(); /* changes to a self-programming mode */
if ( ucSelfFlashInitialize() != TRUE ){ /* self-programming Initialization */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRINIT ); /* initialization error */
}
/* new block is effective */
if ( ucSelfFlashEEPROMWrite( ucEEPROM_BANK[ ucNewBlockTBLNo ], usNewAdr, 4-1 ) == TRUE ) { /* Normal end */
/* block is Invalid */
if ( ucSelfFlashEEPROMWrite( ucEEPROM_BANK[ ucOldBlockTBLNo ], (usOldAdr+4), 4-1 ) != TRUE ) { /* abnormal end */
/* write error仺make a block the ban on use */
if ( ucEEPROMUnableEx( ucOldBlockTBLNo ) == FALSE ){ /* abnormal end */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRDEV ); /* device error */
}
ucFlags |= FL_BLKERR; /* Block error flag On */
}
SelfFlashModeOff(); /* changes to a normal mode */
break;
}
else { /* abnormal end */
/* write error仺make a block the ban on use */
if ( ucEEPROMUnableEx( ucNewBlockTBLNo ) == FALSE ){ /* abnormal end */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRDEV ); /* device error */
}
ucFlags |= FL_BLKERR; /* Block error flag On */
}
SelfFlashModeOff(); /* changes to a normal mode */
}
}
}
}
/* With no eliminated block */
if ( i >= EEPROM_BLOCK_NO ) {
return ( ERRNOBLA ); /* With no eliminated block */
}
if ( ucFlags & FL_BLKERR ) { /* Those with a block error? */
return ( NMLBLK ); /* Normal end(Block error generating) */
}
return ( TRUE ); /* Normal end */
}
/*-------------------------------------------------------------------
Function name : ucEEPROMEraseEx (user access function)
Arguments : None
Return value : Normal completion(TRUE:00H) / With no eliminable block(ERRNOERS:F9H) /
With no effective block(ERRNOUSE:FAH) / Block eror(ERRBLK:FCH) /
Device error(ERRDEV:FDH) / Initialization error(ERRINIT:FEH)
Summary : Erases data in the block to which data is written to next in advance
-------------------------------------------------------------------*/
UCHAR ucEEPROMEraseEx( void ) /* EEPROM erase processing */
{
UCHAR ucBlockTBLNo; /* erase block no */
UCHAR ucCount; /* erase Counter */
UCHAR ucResult; /* Processing result */
USHORT usFlagAdr; /* flag address */
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 */
}
/* Processing result initial value */
ucResult = TRUE;
/* The loop for several block minutes is performed(Except an effective block) */
for ( i = 0 ; i < (EEPROM_BLOCK_NO - 1) ; i++ ) {
/* blockNo.+侾 */
if ( ucBlockTBLNo <= 0 ){
ucBlockTBLNo = EEPROM_BLOCK_NO - 1; /* last block No. */
}
else {
ucBlockTBLNo--; /* blockNo.-1 */
}
/* Bank set */
SET_BANK( ucEEPROM_BANK[ ucBlockTBLNo ] );
/* bank top address(flag address) get */
usFlagAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ ucBlockTBLNo ] );
/* effective/Invalid flag check */
if( BLOCK_IS_PROHIBIT( (UCHAR*)usFlagAdr ) == FALSE ){ /* Except prohibition of use? */
/* erase process */
SelfFlashModeOn(); /* changes to a self-programming mode */
if ( ucSelfFlashInitialize() != TRUE ){ /* self-programming Initialization */
SelfFlashModeOff(); /* changes to a normal mode */
return ( ERRINIT ); /* initialization error */
}
for ( ucCount = (ERASENUM + 1); ucCount > 0; ucCount-- ){ /* loop */
/* Erases data in the block next used as EEPROM */
if ( ucSelfFlashBlockErase( ucEEPROM_BANK[ ucBlockTBLNo ], ucEEPROM_BLOCK[ ucBlockTBLNo ] ) == TRUE ){ /* Normal end */
break;
}
}
if ( ucCount <= 0 ){ /* counter <= 0 (retry over) */
/* Except prohibition of use */
if ( ucEEPROMUnableEx( ucBlockTBLNo ) == FALSE ){ /* abnormal end */
ucResult = ERRDEV; /* device error */
}
else {
ucResult = ERRBLK; /* block error */
}
}
SelfFlashModeOff(); /* changes to a normal mode */
break;
}
}
/* Processing result */
if ( i >= (EEPROM_BLOCK_NO - 1) ) { /* With no eliminable block */
ucResult = ERRNOERS; /* return value亖With no eliminable block */
}
return ( ucResult );
}
/*-------------------------------------------------------------------
Function name : ucEEPROMGetRemainEx (user access function)
Arguments : None
Return value : The number of data which can be written in
Summary : examine the number of the writable data
-------------------------------------------------------------------*/
UCHAR ucEEPROMGetRemainEx( void )
{
UCHAR ucBlockTBLNo; /* erase blockNo. */
UCHAR ucResult; /* Processing result */
USHORT usWriteAdr; /* write top address */
USHORT usBlockAdr; /* block愭摢address */
/* Parameter Check */
if( PARAM_OK == FALSE ){ /* parameter error? */
return( FALSE ); /* then rerun */
}
ucResult = 0; /* initial value */
/* Searches for valid blocks being used as EEPROM */
ucBlockTBLNo = ucEEPROMUseBlockSearchEx();
if ( ucBlockTBLNo == FALSE ){ /* With no effective block? */
return ( FALSE ); /* With no effective block */
}
/* Bank set */
SET_BANK( ucEEPROM_BANK[ ucBlockTBLNo ] );
/* top address */
usBlockAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ ucBlockTBLNo ] ); /* Obtains EEPROM write address */
/* write address */
usWriteAdr = usEEPROMWriteTopSearchEx( ucBlockTBLNo );
/* The number calculation of data */
ucResult = (UCHAR)((DATA_END_OFFSET - (usWriteAdr - usBlockAdr)) / (WRITE_DATA_LENGTH+1));
return ( ucResult );
}
/*-------------------------------------------------------------------
/*-------------------------------------------------------------------
Function name : ucEEPROMUseBlockSearchEx
Arguments : None
Return value : Block table number(0x00 to 0xFE), Abnormal completion(0xFF)
Summary : Searches for currently used blocks in flash memory that are allocated to EEPROM.
-------------------------------------------------------------------*/
UCHAR ucEEPROMUseBlockSearchEx( void ) /* EEPROM block search processing */
{
UCHAR i;
USHORT usFlagAdr;
/* Searches for currently used block (block with a valid "valid/invalid" flag) */
for ( i = 0 ; i < EEPROM_BLOCK_NO ; i++ ){
/* Bank set */
SET_BANK( ucEEPROM_BANK[ i ] );
/* abnk top address */
usFlagAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ i ] );
if ( BLOCK_IS_EFFECTIVE( (UCHAR*)usFlagAdr ) == TRUE ){ /* block亖effective? */
return( i ); /* the block is being used */
}
}
return( FALSE );
}
/*-------------------------------------------------------------------
Function name : usEEPROMWriteTopSearchEx
Arguments : ucBlockTBLNo:Currently used block table number(block number of block set for
: EEPROM use by user definition)
Return value : Write address(0x0001 to 0xffff), Abnormal completion(0x0000)
Summary : Searches for specified block's write address.
-------------------------------------------------------------------*/
USHORT usEEPROMWriteTopSearchEx( UCHAR ucBlockTBLNo ) /* EEPROM block data write top search processing */
{
USHORT usTopAdr;
UCHAR *pSearch;
UCHAR *pWrite;
/* Bank set */
SET_BANK( ucEEPROM_BANK[ ucBlockTBLNo ] );
/* bank top address */
usTopAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ ucBlockTBLNo ] ); /* Obtains EEPROM write address */
/* address search & set write address */
pSearch = pWrite = (UCHAR *)( usTopAdr + DATA_END_OFFSET );
/* set a data address */
usTopAdr += DATATOP;
while ( 1 ){
/* search address - data length */
pSearch -= WRITE_DATA_LENGTH+1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -