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

📄 sst39vf800a.c

📁 一款常用FLASH芯片SST39VF800A的驱动程序源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
{
	long i = 0;							// loop counter
	unsigned long ulOffset = ulStart;	// current offset to write
	ERROR_CODE ErrorCode = NO_ERR;		// tells whether we had an error while filling
	int nCompare = 0;					// value that we use to verify flash
	bool bVerifyError = FALSE;			// lets us know if there was a verify error
	unsigned long ulNewOffset = 0;		// used if we have an odd address
	int nSector = 0;

	// 不允许地址为奇数
	if(ulOffset%2 != 0)
	{
       return INVALID_SECTOR;
	}

	// 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] );         //16位填充
				ErrorCode = PollToggleBit(ulOffset);
				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);
			}
			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);       //lStride=1,读低16位
				ReadFlash( ulOffset, &nHi );
				ulOffset += (lStride * 2);       //lStride=1,读高16位
				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
//
// 32位寄存器的低16位数据写入16位Flash
//////////////////////////////////////////////////////////////

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
//
// 读取16位数据放入32位寄存器
//////////////////////////////////////////////////////////////

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;");
    asm ("jump POLL_TOGGLE_BIT;");

	// 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(void)
{
    //5555H AAH 2AAAH 55H 5555H F0H
	// send the reset command to the flash
	WriteFlash( 0x5555<<1, 0xAA );
	WriteFlash( 0x2AAA<<1, 0x55 );
	WriteFlash( 0x5555<<1, 0xF0 );
	// reset should be complete
	return NO_ERR;
}


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

ERROR_CODE EraseFlash(void)
{
    //chiperase
    //             5555H AAH 2AAAH 55H 5555H 80H 5555H AAH 2AAAH 55H 5555H 10H
	ERROR_CODE ErrorCode = NO_ERR;	// tells us if there was an error erasing flash

	// *****Flash *****

	// erase contents in Main Flash Array
	WriteFlash( 0x5555<<1, 0xaa );
	WriteFlash( 0x2AAA<<1, 0x55 );
	WriteFlash( 0x5555<<1, 0x80 );
	WriteFlash( 0x5555<<1, 0xaa );
	WriteFlash( 0x2AAA<<1, 0x55 );
	WriteFlash( 0x5555<<1, 0x10 );

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


	// erase should be complete
	return ErrorCode;
}


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

ERROR_CODE EraseBlock( int nBlock )
{
    //1.EraseBlock
    //            5555H AAH 2AAAH 55H 5555H 80H 5555H AAH 2AAAH 55H BAX 50H
    //2.Sector-Erase
    //            5555H AAH 2AAAH 55H 5555H 80H 5555H AAH 2AAAH 55H SAX 30H
    //
    //SAX for Sector-Erase; uses A18-A11 address lines;    4Kbytes
    //BAX for Block-Erase; uses  A18-A15 address lines;    64Kbytes

    //now we use Sector-Erase
	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;

    ulSectorOff = (nBlock * AFP_SectorSize);

	// send the erase block command to the flash
	WriteFlash( 0x5555<<1, 0xaa );
	WriteFlash( 0x2AAA<<1, 0x55 );
	WriteFlash( 0x5555<<1, 0x80 );
	WriteFlash( 0x5555<<1, 0xaa );
	WriteFlash( 0x2AAA<<1, 0x55 );
	WriteFlash( ulSectorOff<<1, 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)
{
    // Word-Program 5555H AAH 2AAAH 55H 5555H A0H //WA3 Data
	// send the unlock command to the flash
	// ORed with lOffsetAddr so we know what block we are in
	WriteFlash( 0x5555<<1, 0xAA );
	WriteFlash( 0x2AAA<<1, 0x55 );
	WriteFlash( 0x5555<<1, 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(void)
{
    //5555H AAH 2AAAH 55H 5555H 90H
	// send the auto select command to the flash
	WriteFlash( 0x5555<<1, 0xaa );
	WriteFlash( 0x2AAA<<1, 0x55 );
	WriteFlash( 0x5555<<1, 0x90 );

	// now we can read the codes
	ReadFlash( 0x0001<<1, &AFP_DevCode );
	AFP_DevCode &= 0xFFFF;

	// there is no ManCode so just hard code it to 0x0
	ReadFlash( 0x0000<<1, &AFP_ManCode );
	AFP_ManCode &= 0x00FF;

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

    nSector=(ulOffset>>12);         //4K bytes =2**12;
    if(nSector<256)                 //256 sectors
    {
         *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 < 256) )
    {
    	*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 + -