⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eepromctrlex0205.c

📁 如何使用flash来模拟EEPROM
💻 C
📖 第 1 页 / 共 3 页
字号:
		if ( (USHORT)pSearch <  usTopAdr ){							/* search finish? */
			break;
		}
		/* check delimiter */
		if ( *(pSearch + (WRITE_DATA_LENGTH-1)) != 0xFF ){			/* FFh? */
			break;
		}
		/* save address */
		pWrite = pSearch;
	}
	return ( (USHORT)pWrite );
}

/*-------------------------------------------------------------------
 Function name : usEEPROMDataSearchEx
 Arguments	   : ucData:Read data No.
 		       : ucBlockTBLNo:Currently used block table number(block number of block set for
 		       : EEPROM use by user definition)
 Return value  : Address of latest data(0x0001 to 0xffff), Abnormal completion(0x0000)
 Summary       : Reads storage address of latest data at specified number.
-------------------------------------------------------------------*/
USHORT usEEPROMDataSearchEx( UCHAR ucDataNo, UCHAR ucBlockTBLNo )	/* Processing to read latest specified data from EEPROM blocks */
{
	USHORT	usTopAdr;
	UCHAR	*pSearch;
	
	/* Bank set */
	SET_BANK( ucEEPROM_BANK[ ucBlockTBLNo ] );
	/* bank top address */
	usTopAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ ucBlockTBLNo ] );	/* Obtains EEPROM write address */
	/* search address */
	pSearch = (UCHAR *)( usTopAdr + DATA_END_OFFSET );
	/* data address */
	usTopAdr += DATATOP;
	
	while ( 1 ){
		/* search address - data length */
		pSearch -= WRITE_DATA_LENGTH+1;
		if ( (USHORT)pSearch < usTopAdr ){							/* finish? */
			pSearch = 0;											/* set 0 */
			break;
		}
		/* check delimiter */
		if ( *(pSearch + (WRITE_DATA_LENGTH-1)) == 0x00 ){			/* 00h? */
			/* check data No. */
			if ( *pSearch == ucDataNo ){							/* coincidence? */
				break;
			}
		}
	}
	return ( (USHORT)pSearch );
}

/*-------------------------------------------------------------------
 Function name : ucEEPROMUnableEx
 Arguments	   : ucBlockTBLNo:Block table number(0x00 to 0xFE)
 Return value  : Normal completion(TRUE:0x00) / Abnormal completion(FALSE:0xff)
 Summary       : make a block the ban on use
-------------------------------------------------------------------*/
UCHAR ucEEPROMUnableEx( UCHAR ucBlockTBLNo )		/* EEPROM latest data recover processing */
{
	USHORT	usSetAdr;
	UCHAR	ucCount;
	UCHAR	ucResult;
	UCHAR	i;
	
	ucResult = FALSE;
	SET_BANK( ucEEPROM_BANK[ ucBlockTBLNo ] );
	usSetAdr = BLOCK_TO_ADDRESS( ucEEPROM_BLOCK[ ucBlockTBLNo ] );
	/* effective/Invalid flag */
	for ( i = 0 ; i < 4 ; i++ ){
		ucDataBuf[ i ] = 0x00;
	}
	/* effective flag update */
	for ( ucCount = (WRITENUM + 1); ucCount > 0; ucCount-- ){
		if( ucSelfFlashWordWrite( ucEEPROM_BANK[ ucBlockTBLNo ], usSetAdr, (USHORT)4 ) == TRUE ) {
			ucResult = TRUE;
			break;
		}
	}
	usSetAdr += 4;
	/* Invalid flag update */
	for ( ucCount = (WRITENUM + 1); ucCount > 0; ucCount-- ){
		if( ucSelfFlashWordWrite( ucEEPROM_BANK[ ucBlockTBLNo ], usSetAdr, (USHORT)4 ) == TRUE ) {
			ucResult = TRUE;
			break;
		}
	}
	return( ucResult );
}

/*-------------------------------------------------------------------

/*-------------------------------------------------------------------
 Function name : SelfFlashModeOn
 Arguments	   : None
 Return value  : None
 Summary       : Sets self rewriting mode(A1).
-------------------------------------------------------------------*/
void SelfFlashModeOn( void )										/* Processing to switch from normal mode to self programming mode */
{
	DI();
	FlashStart();													/* Self programing start library call */
	EI();
	return;
}

/*-------------------------------------------------------------------
 Function name : SelfFlashModeOff
 Arguments	   : None
 Return value  : None
 Summary       : Releases self rewriting mode.
-------------------------------------------------------------------*/
void SelfFlashModeOff( void )										/* Processing to switch from self programming mode to normal mode */
{
	DI();
	FlashEnd();														/* Self programing end library call */
	EI();
	return;
}

/*-------------------------------------------------------------------
 Function name : ucSelfFlashInitialize
 Arguments	   : None
 Return value  : Normal completion(TRUE:0x00) / Abnormal completion(FALSE:0xff)
 Summary       : Initializes self programming firmware for EEPROM emulation.
			   : Initializes flash firmware (sets operating frequency)
			   : Checks FLMD pin [because programming may be disabled depending on the FLMD pin's status]
-------------------------------------------------------------------*/
UCHAR ucSelfFlashInitialize( void )									/* Flash memory self programming initialization processing */
{
	FlashEnv( (USHORT)&ucEntryRAM );								/* Initializes internal flash firmware */
	
	if( CheckFLMD() != LIB_NORMAL_END ){							/* Obtains FLMD pin information */
		return( FALSE );
	}
	return( TRUE );
}

/*-------------------------------------------------------------------
 Function name : ucSelfFlashBlockErase
 Arguments	   : ucEraseStartBlock:Erase block number
 Return value  : Normal completion(TRUE:0x00) / Abnormal completion(FALSE:0xFF)
 Summary       : Erases specified block.
-------------------------------------------------------------------*/
UCHAR ucSelfFlashBlockErase( UCHAR ucBank, UCHAR ucBlock )			/* Flash memory block erase processing */
{
	UCHAR	ucStatus;
	
	while(1){
		DI();
		ucStatus = FlashBlockBlankCheck( ucBank, ucBlock );			/* Flash memory blank check library call */
		EI();
		if ( ucStatus == LIB_BLKCHK_ERR ){							/* Blank check error (Blank check error) */
			DI();
			ucStatus = FlashBlockErase( ucBank, ucBlock );			/* Flash memorry erase library call */
			EI();
			if (( ucStatus != LIB_NORMAL_END )&&					/* States other than discontinuation error and nomarl error */
				( ucStatus != LIB_STATE_INT )){						/* (parameter/protect/erase error) */
				return( FALSE );
			}
		}
		else if ( ucStatus == LIB_NORMAL_END ){						/* Blank check normal end */
			FlashEnv( (USHORT)&ucEntryRAM );
			return( TRUE );
		}
		else if ( ucStatus != LIB_STATE_INT ){						/* States other than discontinuation error (parameter error)*/
			return( FALSE );
		}
	}
}

/*-------------------------------------------------------------------
 Function name : ucSelfFlashEEPROMWrite
 Arguments     : ucBank:Bank number
               : ucNum:Data number
               : usAddr:Writes start address
 Return value  : Normal completion(TRUE:0x00) / Abnormal completion(FALSE:0xFF)
 Summary       : write data to EEPROM
-------------------------------------------------------------------*/
UCHAR ucSelfFlashEEPROMWrite( UCHAR ucBank, USHORT usAddr, UCHAR ucNum )	/* Flash memory data write processing */
{
	UCHAR	ucStatus;
	
	WordAddr.WriteAddress = usAddr;
	WordAddr.WriteBank = ucBank;
	
	while(1){
		DI();
		ucStatus = EEPROMWrite( &WordAddr, (UCHAR)((ucNum/4)+1), (USHORT)( &ucDataBuf[ 0 ] ));	/* Flash memory EEPROM write library call */
		EI();
		if ( ucStatus == LIB_NORMAL_END ){							/* Word write normal end */
			return( TRUE );
		}
		else if ( ucStatus != LIB_STATE_INT ){						/* States other than discontinuation error (parameter/protect/write error) */
			return( ucStatus );
		}
	}
}

/*-------------------------------------------------------------------
 Function name : ucSelfFlashWordWrite
 Arguments     : usSetAdr:
               : usDataAdr:
 Return value  : Normal completion(TRUE:0x00) / Abnormal completion(FALSE:0xFF)
 Summary       : write word data to EEPROM
-------------------------------------------------------------------*/
UCHAR ucSelfFlashWordWrite( UCHAR ucBank, USHORT usSetAdr, UCHAR ucNum )	/* Flash memory word data write processing */
{
	UCHAR	ucStatus;
	
	WordAddr.WriteAddress = usSetAdr;
	WordAddr.WriteBank = ucBank;

	while(1){
		DI();
		ucStatus = FlashWordWrite( &WordAddr, (UCHAR)((((USHORT)ucNum-1)/4)+1), (USHORT)(&ucDataBuf[0]) );	/* Word write library call */
		EI();
		if ( ucStatus == LIB_NORMAL_END ){							/* Word write normal end */
			return( TRUE );
		}
		else if ( ucStatus != LIB_STATE_INT ){						/* States other than discontinuation error (parameter/protect/write error) */
			return( FALSE );
		}
	}
}

/*-------------------------------------------------------------------

/*-------------------------------------------------------------------
 Function name : usBlockToAddress
 Arguments	   : ucBlock:Block number
 Return value  : Address Data
 Summary       : A block number is changed into an address.
-------------------------------------------------------------------*/
USHORT usBlockToAddress( USHORT usBlock )
{
	return ( (usBlock << 2) * 0x0100 );
}

/*-------------------------------------------------------------------
 Function name : ucUseProhibitionBlock
 Arguments	   : usFlagAdr:Flag Address
 Return value  : 00h(TRUE) / FFh(FALSE)
 Summary       : examine it whether it is a ban on use block
-------------------------------------------------------------------*/
UCHAR ucUseProhibitionBlock( UCHAR *p )
{
	if (((p[0]==0x00)&&(p[1]==0x00)&&(p[2]==0x00)&&(p[3]==0x00))||
		((p[4]==0x00)&&(p[5]==0x00)&&(p[6]==0x00)&&(p[7]==0x00))){
		return ( TRUE );
	}
	return ( FALSE );
}

/*-------------------------------------------------------------------
 Function name : ucEffectiveBlock
 Arguments	   : usFlagAdr:Flag Address
 Return value  : 00h(TRUE) / FFh(FALSE)
 Summary       : examine whether I block it effectively
-------------------------------------------------------------------*/
UCHAR ucEffectiveBlock( UCHAR *p )
{
	if (((p[0]==0x55)&&(p[1]==0x55)&&(p[2]==0x55)&&(p[3]==0x55))&&
		((p[4]==0xFF)&&(p[5]==0xFF)&&(p[6]==0xFF)&&(p[7]==0xFF))){
		return ( TRUE );
	}
	return ( FALSE );
}

/*-------------------------------------------------------------------
 Function name : ucBlankBlock
 Arguments	   : usFlagAdr:Flag Address
 Return value  : 00h(TRUE) / FFh(FALSE)
 Summary       : examine it whether it is a blank block
-------------------------------------------------------------------*/
UCHAR ucBlankBlock( UCHAR *p )
{
	if (((p[0]==0xFF)&&(p[1]==0xFF)&&(p[2]==0xFF)&&(p[3]==0xFF))&&
		((p[4]==0xFF)&&(p[5]==0xFF)&&(p[6]==0xFF)&&(p[7]==0xFF))){
		return ( TRUE );
	}
	return ( FALSE );
}

/*-------------------------------------------------------------------
 Function name : ucParamCheck
 Arguments	   : none
 Return value  : 00h(TRUE) / FFh(FALSE)
 Summary       : check parameteres
-------------------------------------------------------------------*/
UCHAR ucParamCheck( void )
{
	if(( DATALENGTH < 1) || (DATALENGTH > 254)){
		return( FALSE );
	}
	if(DATANOMAX > 253){
		return( FALSE );
	}
	if(WRITENUM > 253){
		return( FALSE );
	}
	if(ERASENUM > 254){
		return( FALSE );
	}
	return( TRUE );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -