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

📄 bf533ezflash.c

📁 SST39VF160 NOR Flash驱动 VDSP环境
💻 C
📖 第 1 页 / 共 2 页
字号:
		WriteFlash( ulNewOffset, nCompare );
		//ErrorCode = PollToggleBit(ulNewOffset);
		ErrorCode = Delay( DELAY_WRITE );

		// move to the last offset
		ulNewOffset = (ulOffset - 1) + (lCount * (lStride * 2) );

		// read the value and OR in our value
		ReadFlash( ulNewOffset, &nCompare );
		nCompare &= 0x00FF;
		nCompare |= (pnData[0] & 0xFF00);

		// unlock the flash, do the write, and wait for completion
		UnlockFlash(ulNewOffset);
		WriteFlash( ulNewOffset, nCompare );
		//ErrorCode = PollToggleBit(ulNewOffset);
		ErrorCode = Delay( DELAY_WRITE );

		// increment the offset and count
		ulOffset = ( (ulOffset - 1) + (lStride * 2) );
		lCount--;
	}

	// verify writes if the user wants to
	if( AFP_Verify == TRUE )
	{
		// fill the value
		for (i = 0; ( ( i < lCount ) && ( ErrorCode == NO_ERR ) ); i++, ulOffset += (lStride * 2) )
		{

			// check to see that the address is within a valid sector
			ErrorCode = GetSectorNumber( ulOffset, &nSector );
			if( NO_ERR == ErrorCode )
			{
				// unlock the flash, do the write, and wait for completion
				UnlockFlash(ulOffset);
				WriteFlash( ulOffset, pnData[0] );
				//ErrorCode = PollToggleBit(ulOffset);
				ErrorCode = Delay( DELAY_WRITE );
				ReadFlash( ulOffset, &nCompare );
				if( nCompare != ( pnData[0] & 0x0000FFFF ) )
				{
					bVerifyError = TRUE;
					break;
				}
			}
			else
			{
				return ErrorCode;
			}

		}

		// return appropriate error code if there was a verification error
		if( bVerifyError == TRUE )
			return VERIFY_WRITE;
	}
	// user did not want to verify writes
	else
	{
		// fill the value
		for (i = 0; ( ( i < lCount ) && ( ErrorCode == NO_ERR ) ); i++, ulOffset += (lStride * 2) )
		{

			// check to see that the address is within a valid sector
			ErrorCode = GetSectorNumber( ulOffset, &nSector );
			if( NO_ERR == ErrorCode )
			{
				// unlock the flash, do the write, and wait for completion
				UnlockFlash(ulOffset);
				WriteFlash( ulOffset, pnData[0] );
				//ErrorCode = PollToggleBit(ulOffset);
				ErrorCode = Delay( DELAY_WRITE );
			}
			else
			{
				return ErrorCode;
			}
		}
	}

	// return the appropriate error code
	return ErrorCode;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE ReadData()
//
// Read a buffer from flash.
//
// Inputs:	unsigned long ulStart - offset in flash to start the reads at
//			int nCount - number of elements to read, in this case bytes
//			int nStride - number of locations to skip between reads
//			int *pnData - pointer to data buffer to fill
//
//////////////////////////////////////////////////////////////

ERROR_CODE ReadData( unsigned long ulStart, long lCount, long lStride, int *pnData )
{

	long i = 0;						// loop counter
	int j = 0;						// inner loop counter
	unsigned long ulOffset = ulStart;			// current offset to write
	int iShift = 0;					// shift value by iShift bits
	int iNumWords = 2;				// number of words in a long
	int nLeftover = lCount % 4;		// how much if any do we have leftover to write
	int nHi,nLow;
	int nSector = 0;
	ERROR_CODE ErrorCode = NO_ERR;		// tells whether we had an error while filling

	// write the buffer up to BUFFER_SIZE items
	for (i = 0; (i < lCount/4) && (i < BUFFER_SIZE); i++)
	{
		for ( iShift = 0, j = 0; j < iNumWords ; j+=2 )
		{
			// check to see that the address is within a valid sector
			ErrorCode = GetSectorNumber( ulOffset, &nSector );
			if( NO_ERR == ErrorCode )
			{
				// Read flash
				ReadFlash( ulOffset, &nLow );
				ulOffset += (lStride * 2);
				ReadFlash( ulOffset, &nHi );
				ulOffset += (lStride * 2);
				pnData[i] = (nHi << 16) | nLow;
			}
			else
			{
				return ErrorCode;
			}
		}
	}

	// because of the way our ldr file is built, we will always have
	// 2 bytes leftover if there is leftover, because the flash is 16 bit
	// that will mean that there is only one write left to do.
	if( nLeftover > 0 )
	{
		// check to see that the address is within a valid sector
		ErrorCode = GetSectorNumber( ulOffset, &nSector );
		if( NO_ERR == ErrorCode )
		{
			ReadFlash( ulOffset, &pnData[i] );
		}
		else
		{
			return ErrorCode;
		}
	}
	
	// return the appropriate error code
	return NO_ERR;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE WriteFlash()
//
// Write a value to an offset in flash.
//
// Inputs:	unsigned long ulOffset - offset to write to
//			int nValue - value to write
//
//////////////////////////////////////////////////////////////

ERROR_CODE WriteFlash( unsigned long ulOffset, int nValue )
{
	// get the offset from the start of flash and set the P register
	asm ("p2.l = 0x0000;");
	asm ("p2.h = 0x2000;");
	asm ("r3 = %0;": : "d" (ulOffset) );
	asm ("r2 = p2;");
	asm ("r2 = r2 + r3;");
	asm ("p2 = r2;");


	// write the value to the flash
	asm ("SSYNC;");
	asm ("w[p2] = %0;": : "d" (nValue) );
	asm ("SSYNC;");

	// ok
	return NO_ERR;
}

//////////////////////////////////////////////////////////////
// ERROR_CODE ReadFlash()
//
// Read a value from an offset in flash.
//
// Inputs:	unsigned long ulOffset - offset to read from
//			int pnValue - pointer to store value read from flash
//
//////////////////////////////////////////////////////////////

ERROR_CODE ReadFlash( unsigned long ulOffset, int *pnValue )
{
	// temp holder
	int nValue = 0x0;

	// get the offset from the start of flash and put it in a P register
	asm ("p2.l = 0x0000;");
	asm ("p2.h = 0x2000;");
	asm ("r3 = %0;": : "d" (ulOffset) );
	asm ("r2 = p2;");
	asm ("r2 = r2 + r3;");
	asm ("p2 = r2;");

	// now place it into memory
	asm ("SSYNC;");
	asm ("%0 = w[p2] (Z);": "=d" (nValue) : );
	asm ("SSYNC;");

	// put the value at the location passed in
	*pnValue = nValue;

	// ok
	return NO_ERR;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE PollToggleBit()
//
// Polls the toggle bit in the flash to see when the operation
// is complete.
//
// Inputs:	unsigned long ulOffset - offset in flash
//
//////////////////////////////////////////////////////////////

ERROR_CODE PollToggleBit(unsigned long ulOffset)
{
	ERROR_CODE ErrorCode = NO_ERR;	// flag to indicate error

	
	unsigned char Loop = TRUE;
    unsigned short PreData;
    unsigned short CurrData;
    unsigned long TimeOut = 0;

    unsigned short * Dst = 0x20000000+ulOffset;
    PreData = *Dst;
    PreData = PreData & 0x4040;
    while ((TimeOut< 0x07FFFFFF) && (Loop))
    {
         CurrData = *Dst;
         CurrData = CurrData & 0x4040;
         if (PreData == CurrData)
               Loop = FALSE;   /* ready to exit the while loop */
         PreData = CurrData;
         TimeOut++;
    }
	// read from the flash twice and check the toggle bit
/*
	asm ("POLL_TOGGLE_BIT:");
	asm ("p2.l = 0x0000;");
	asm ("p2.h = 0x2000;");
	asm ("r2 = r0;");		// ulOffset is passed into PollTogglebit in R0
	asm ("r1 = p2;");
	asm ("r1 = r1 + r2;");
	asm ("p2 = r1;");
	asm ("r1 = w[p2] (Z);");
	asm ("SSYNC;");
	asm ("r2 = w[p2] (Z);");
	asm ("SSYNC;");

	// set bits in r0 where data toggled
	asm("r1 = r1 ^ r2;");

	// see if we are still toggling, if not we are done
	asm ("cc = bittst(r1, 6);");
	asm ("if !cc jump DONE_TOGGLE_BIT;");

	// see if there was an error
//	asm ("cc = bittst(r2, 5);");
//	asm ("if !cc jump POLL_TOGGLE_BIT;");

	// if we get here we detected the error bit set
	asm ("r1 = w[p2] (Z);");
	asm ("SSYNC;");
	asm ("r2 = w[p2] (Z);");
	asm ("SSYNC;");

	// set bits in r0 where data toggled
	asm("r1 = r1 ^ r2;");

	// see if we are still toggling, if not we are done
	asm ("cc = bittst(r1, 6);");
	asm ("if !cc jump DONE_TOGGLE_BIT;");

	// else we have failed, restore page, set error flag, and reset
	ErrorCode = POLL_TIMEOUT;
	reset = 0x1;
	ResetFlash();

	// we are done toggling
	asm ("DONE_TOGGLE_BIT:");
*/
	// we can return
	return ErrorCode;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE ResetFlash()
//
// Sends a "reset" command to the flash.
//
//////////////////////////////////////////////////////////////

ERROR_CODE ResetFlash()
{
	// send the reset command to the flash
//	WriteFlash( 0x0AAA*2, 0xf0 );

	EraseFlash();
	
	// reset should be complete
	return NO_ERR;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE EraseFlash()
//
// Sends an "erase all" command to the flash.
//
//////////////////////////////////////////////////////////////

ERROR_CODE EraseFlash()
{
	ERROR_CODE ErrorCode = NO_ERR;	// tells us if there was an error erasing flash

	// erase contents in Main Flash Array
	WriteFlash( 0x5555*2, 0xaa );
	WriteFlash( 0x2aaa*2, 0x55 );
	WriteFlash( 0x5555*2, 0x80 );
	WriteFlash( 0x5555*2, 0xaa );
	WriteFlash( 0x2aaa*2, 0x55 );
	WriteFlash( 0x5555*2, 0x10 );

	// poll until the command has completed
	//ErrorCode = PollToggleBit(0x0000, 0x80);

	ErrorCode = Delay( DELAY_WRITE*10000 );

	// erase should be complete
	return ErrorCode;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE EraseBlock()
//
// Sends an "erase block" command to the flash.
//
//////////////////////////////////////////////////////////////

ERROR_CODE EraseBlock( int nBlock )
{
	unsigned long ulSectorOff = 0x0;
	ERROR_CODE ErrorCode = NO_ERR;	// tells us if there was an error erasing flash

	
	// if the block is invalid just return
	if ( (nBlock < 0) || (nBlock >= AFP_NumSectors) )
		return INVALID_BLOCK;

	// block is in Main Flash Array
	if ( 1 )
	{
		// send the erase block command to the flash
		WriteFlash( 0x5555*2, 0xaa );
		WriteFlash( 0x2aaa*2, 0x55 );
		WriteFlash( 0x5555*2, 0x80 );
		WriteFlash( 0x5555*2, 0xaa );
		WriteFlash( 0x2aaa*2, 0x55 );

		// the last write has to be at an address in the block
		// we want to erase
		ulSectorOff = (nBlock * AFP_SectorSize);
		WriteFlash( ulSectorOff, 0x50 );
		ErrorCode = Delay( DELAY_WRITE*5000 );
		// poll until the command has completed
		//ErrorCode = PollToggleBit(lSectorOff, 0x80);
	}

	// block erase should be complete
	return ErrorCode;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE UnlockFlash()
//
// Sends an "unlock" command to the flash to allow the flash
// to be programmed.
//
//////////////////////////////////////////////////////////////

ERROR_CODE UnlockFlash(unsigned long ulOffset)
{
	unsigned long ulOffsetAddr = ulOffset;
	ulOffsetAddr &= 0x000F0000;

	// send the unlock command to the flash
	// ORed with lOffsetAddr so we know what block we are in
	WriteFlash( (0x5555 | ulOffsetAddr)*2, 0xaa );
	WriteFlash( (0x2aaa | ulOffsetAddr)*2, 0x55 );
	WriteFlash( (0x5555 | ulOffsetAddr)*2, 0xa0 );

	// ok
	return NO_ERR;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE GetCodes()
//
// Sends an "auto select" command to the flash which will allow
// us to get the manufacturer and device codes.
//
//////////////////////////////////////////////////////////////

ERROR_CODE GetCodes()
{

	// send the auto select command to the flash
	WriteFlash( 0x5555*2, 0xaaaa );
	WriteFlash( 0x2aaa*2, 0x5555 );
	WriteFlash( 0x5555*2, 0x9090 );

	// now we can read the codes
	ReadFlash( 0x0001*2, &AFP_DevCode );
	
	ReadFlash( 0x0000*2, &AFP_ManCode );
/*	for(i=0;i<0x35;i++)
	{
		//ReadFlash( 0x0002*i, &data[i] );
		ReadFlash( i*2, &data[i] );
	}*/
	Delay( DELAY_WRITE *100);
	
	WriteFlash( 0x5555*2, 0xaaaa );
	WriteFlash( 0x2aaa*2, 0x5555 );
	WriteFlash( 0x5555*2, 0xf0f0 );
	Delay( DELAY_WRITE *1000);

	//WriteFlash( 0x5555*2, 0xf0f0 );

	// ok
	return NO_ERR;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE GetSectorNumber()
//
// Gets a sector number based on the offset.
//
//////////////////////////////////////////////////////////////

ERROR_CODE GetSectorNumber( unsigned long ulOffset, int *pnSector )
{
	int nSector = 0;

	if(ulOffset < 0xFFFFF)
	{  
		nSector = ulOffset & 0x000f1000;
		nSector = ulOffset >> 15;
		nSector = nSector & 0x001f;

	//nSector += 0x3;
	}
	// else it is an invalid sector
	else
		return INVALID_SECTOR;

	// ok
	return NO_ERR;
}

//////////////////////////////////////////////////////////////
// ERROR_CODE GetSectorStartEnd()
//
// Gets a sector number based on the offset.
//
// Inputs:	long *lStartOff - pointer to the start offset
//			long *lEndOff - pointer to the end offset
//			int nSector - sector number
//
//////////////////////////////////////////////////////////////

ERROR_CODE GetSectorStartEnd( long *lStartOff, long *lEndOff, int nSector )
{

	// block is in Boot Flash Array
	if( nSector < NUM_SECTORS )
	{
		
		if ((nSector >= 0) && (nSector <= 31))
		{
			*lStartOff =  nSector * AFP_SectorSize ;
			*lEndOff = ( (*lStartOff) + AFP_SectorSize ) - 1;
		}

	}	
	// no such sector
	else
		return INVALID_SECTOR;


	// ok
	return NO_ERR;
}

⌨️ 快捷键说明

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