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

📄 bf532sstflash.c

📁 blackfin bf531 flash烧写驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
				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 )
			{
					//ResetFlash();
				// unlock the flash, do the write, and wait for completion
				UnlockFlash(ulOffset);
				WriteFlash( ulOffset, pnData[0] );
				ErrorCode = PollToggleBit(ulOffset);
			}
			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 ErrorCode;
}


//////////////////////////////////////////////////////////////
// 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

	// 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, 0xf0 );

	// 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

	// *****Flash A*****
	ResetFlash();
	// erase contents in Main Flash Array
	WriteFlash( 0xaaaa, 0xaa );
	WriteFlash( 0x5554, 0x55 );
	WriteFlash( 0xaaaa, 0x80 );
	WriteFlash( 0xaaaa, 0xaa );
	WriteFlash( 0x5554, 0x55 );
	WriteFlash( 0xaaaa, 0x10 );

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

	// only erase if we didn't fail the previous erase
	if( ErrorCode == NO_ERR )
	{
		// erase contents in Boot Flash Array
/*		WriteFlash( 0x200AAA, 0xaa );
		WriteFlash( 0x200554, 0x55 );
		WriteFlash( 0x200AAA, 0x80 );
		WriteFlash( 0x200AAA, 0xaa );
		WriteFlash( 0x200554, 0x55 );
		WriteFlash( 0x200AAA, 0x10 );

		// poll until the command has completed
		ErrorCode = PollToggleBit(0x200000);
*/
		}



	// 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

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

	// figure out the offset of the block in flash
	if ( (nBlock >= 0) && (nBlock < 32) )
	{
		ulSectorOff = (nBlock * AFP_SectorSize1);
	}
//	else if( nBlock < 23 )
//	{
//		ulSectorOff =0x10000+ ((nBlock-8) * AFP_SectorSize2);

//	}
	// send the erase block command to the flash
	WriteFlash( 0xaaaa , 0xaa );
	WriteFlash( 0x5554 , 0x55 );
	WriteFlash( 0xaaaa , 0x80 );
	WriteFlash( 0xaaaa , 0xaa );
	WriteFlash( 0x5554 , 0x55 );
	WriteFlash( ulSectorOff, 0x30 );

	// poll until the command has completed
	ErrorCode = PollToggleBit(ulSectorOff);


	// 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 &= 0xFFFF0000;

	// send the unlock command to the flash
	// ORed with lOffsetAddr so we know what block we are in
	WriteFlash( 0xaaaa,0xaa );
	WriteFlash( 0x5554, 0x55 );
	WriteFlash( 0xaaaa, 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( 0xaaaa, 0xaa );
	WriteFlash( 0x5554, 0x55 );
	WriteFlash( 0xaaaa, 0x90 );
	
	// now we can read the codes
	ReadFlash( 0x000, &AFP_ManCode );

	ResetFlash();
		// send the auto select command to the flash

	// we need to issue another command to get the part out
	// of auto select mode so issue a reset which just puts
	// the device back in read mode
	ResetFlash();

	// 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;

	// sector numbers for the FLASH A boot sectors
//	if((ulOffset >= 0x10000) && (ulOffset < 0x100000))
//	{
//		nSector = ulOffset & 0xffff0000;
//		nSector = ulOffset >> 16+7;
	
//	}
	// sector numbers for the FLASH B boot sectors
	if (ulOffset < 0x40000)
	{
		nSector = ulOffset & 0xffffe000;
		nSector = ulOffset >> 13;
	}
	else
		return INVALID_SECTOR;
	// if it is a valid sector, set it
	if ( (nSector >= 0) && (nSector < AFP_NumSectors) )
		*pnSector = nSector;

	// 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 )
{
	long lSectorSize = 0;

	// block is in Boot Flash Array
	if ( (nSector >= 0) && (nSector < 32) )
	{
		*lStartOff = nSector * AFP_SectorSize1;
		*lEndOff = ( (*lStartOff) + AFP_SectorSize1 ) - 1;
	}
	// block is in main flash
//	else if ( nSector < 23 )
//	{
//		*lStartOff = ( (nSector-8) * AFP_SectorSize2)+0x10000 ;
//		*lEndOff = (long)( (*lStartOff) +AFP_SectorSize2) - 1;
//	}
	else
		return INVALID_SECTOR;


	// ok
	return NO_ERR;
}

⌨️ 快捷键说明

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