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

📄 spi_flashdriver.c

📁 ADI Blackfin DSP 通过SPI接口读写Flash的程序.Flash芯片型号为:M25P80MW
💻 C
📖 第 1 页 / 共 2 页
字号:
///////////////////////////////////////////////////////////////
//
// SPI_FLASHDRIVER.c
//
//	TO USE:
//	In VisualDSP select Tools->Flash Programmer...
//	Select this program as the driver file.   Load the driver.
//
//	Build option for the program LDR file: SPI, Intel Hex, 8-bit
//
//
//
//
//
//  This flash driver is targeted for the REV B Hardware
//	This board uses a M25P80MW Serial SPI flash chip.
///////////////////////////////////////////////////////////////
//  SPI interface Tools
#include "spi_memory.h"

// error enum
#include "Errors.h"

// #defines
#define TRUE		0x1
#define FALSE		0x0
#define NULL		0x0

#define BUFFER_SIZE	0x600
#define NUM_SECTORS 	8				   // number of sectors in the flash device
#define SECTOR_SIZE	(256*256)
#define FLASH_SIZE	(NUM_SECTORS*SECTOR_SIZE)

// structure for flash sector information
typedef struct _SECTORLOCATION{
	long lStartOff;
	long lEndOff;
}SECTORLOCATION;


// Flash Programmer commands
typedef enum
{
	NO_COMMAND,		// 0
	GET_CODES,		// 1
	RESET,			// 2
	WRITE,			// 3
	FILL,			// 4
	ERASE_ALL,		// 5
	ERASE_SECT,		// 6
	READ,			// 7
	GET_SECTNUM,		// 8
	GET_SECSTARTEND		// 9
}enProgCmds;

// function prototypes
ERROR_CODE SetupForFlash();
ERROR_CODE GetCodes();
ERROR_CODE PollToggleBit(unsigned long ulOffset);
ERROR_CODE ResetFlash();
ERROR_CODE EraseFlash();
ERROR_CODE EraseBlock( int nBlock );
ERROR_CODE UnlockFlash(unsigned long ulOffset);
ERROR_CODE WriteData( unsigned long ulStart, long lCount, long lStride, int *pnData );
ERROR_CODE FillData( unsigned long ulStart, long lCount, long lStride, int *pnData );
ERROR_CODE ReadData( unsigned long ulStart, long lCount, long lStride, int *pnData );
ERROR_CODE ReadFlash( unsigned long ulOffset, int *pnValue );
ERROR_CODE WriteFlash( unsigned long ulOffset, int nValue );
ERROR_CODE GetSectorNumber( unsigned long ulOffset, int *pnSector );
ERROR_CODE GetSectorStartEnd( long *lStartOff, long *lEndOff, int nSector );

// global data for use with the VisualDSP++ plug-in
char 		*AFP_Title		 = "Radiant INC. 2004 PRW";
char 		*AFP_Description	 = "SPI EEPROM M25P80";
enProgCmds 	AFP_Command		 = NO_COMMAND;
int 		AFP_ManCode		 = -1;
int 		AFP_DevCode		 = -1;
unsigned long 	AFP_Offset		 = 0x0;
int 		*AFP_Buffer;
long 		AFP_Size		 = BUFFER_SIZE;
long 		AFP_Count		 = -1;
long 		AFP_Stride		 = -1;
int 		AFP_NumSectors		 = NUM_SECTORS;
long 		AFP_SectorSize1		 = SECTOR_SIZE;
int 		AFP_SectorSize2		 = SECTOR_SIZE;
int 		AFP_Sector		 = -1;
int 		AFP_Error 		 = 0;			// contains last error encountered
bool 		AFP_Verify 		 = FALSE;		// verify writes or not
long 		AFP_StartOff 		 = 0x0;			// sector start offset
long 		AFP_EndOff 		 = 0x0;			// sector end offset
int		AFP_FlashWidth		 = 0x10;		// width of the flash device
int 		*AFP_SectorInfo;

SECTORLOCATION SectorInfo[NUM_SECTORS];

// exit flag
bool bExit = FALSE;

main()
{
	int i = 0;
	// by making AFP_Buffer as big as possible the plug-in can send and
	// receive more data at a time making the data transfer quicker
	//
	// by allocating it on the heap the compiler does not create an
	// initialized array therefore making the driver image smaller
	// and faster to load
	//
	// we have modified the linker description file (LDF) so that the heap
	// is large enough to store BUFFER_SIZE elements at this point
	AFP_Buffer = (int *)malloc(BUFFER_SIZE);

	// AFP_Buffer will be NULL if we could not allocate storage for the
	// buffer
	if ( AFP_Buffer == NULL )
	{
		// tell GUI that our buffer was not initialized
		AFP_Error = BUFFER_IS_NULL;
	}
	
	//initiate sector information structures
	for(i=0;i<AFP_NumSectors; i++)
	{
		GetSectorStartEnd(&SectorInfo[i].lStartOff, &SectorInfo[i].lEndOff, i);
	}

	AFP_SectorInfo = (int*)&SectorInfo[0];

	// setup the flash so the DSP can access it
	SetupForFlash();

	//setup code so that flash programmer can just read memory instead of call GetCodes().
	GetCodes();

	// command processing loop
	while ( !bExit )
	{
		// the plug-in will set a breakpoint at "AFP_BreakReady" so it knows
		// when we are ready for a new command because the DSP will halt
		//
		// the jump is used so that the label will be part of the debug
		// information in the driver image otherwise it may be left out
		// since the label is not referenced anywhere
		asm("AFP_BreakReady:");
		if ( FALSE )
			asm("jump AFP_BreakReady;");

		// switch on the command
		switch ( AFP_Command )
		{
			// get manufacturer and device codes
			case GET_CODES:
				AFP_Error = GetCodes();
				break;

			// reset
			case RESET:
				AFP_Error = ResetFlash();
				break;

			// write
			case WRITE:
				AFP_Error = WriteData( AFP_Offset, AFP_Count, AFP_Stride, AFP_Buffer );
				break;

			// fill
			case FILL:
				AFP_Error = FillData( AFP_Offset, AFP_Count, AFP_Stride, AFP_Buffer );
				break;

			// erase all
			case ERASE_ALL:
				AFP_Error = EraseFlash();
				break;

			// erase sector
			case ERASE_SECT:
				AFP_Error = EraseBlock( AFP_Sector );
				break;

			// read
			case READ:
				AFP_Error = ReadData( AFP_Offset, AFP_Count, AFP_Stride, AFP_Buffer );
				break;

			// get sector number based on address
			case GET_SECTNUM:
				AFP_Error = GetSectorNumber( AFP_Offset, &AFP_Sector );
				break;

			// get sector number start and end offset
			case GET_SECSTARTEND:
				AFP_Error = GetSectorStartEnd( &AFP_StartOff, &AFP_EndOff, AFP_Sector );
				break;

			// no command or unknown command do nothing
			case NO_COMMAND:
			default:
				// set our error
				AFP_Error = UNKNOWN_COMMAND;
				break;
		}

		// clear the command
		AFP_Command = NO_COMMAND;
	}

	// free the buffer if we were able to allocate one
	if ( AFP_Buffer )
		free( AFP_Buffer );


	spi_close_memory();
	
	// all done
	return 0;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE SetupForFlash()
//
// Perform necessary setup for the processor to talk to the
// flash such as external memory interface registers, etc.
//
//////////////////////////////////////////////////////////////

ERROR_CODE SetupForFlash()
{
//  Call the memory open routine
  spi_open_memory ();

  return NO_ERR;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE WriteData()
//
// Write a buffer to flash.
//
// Inputs:	unsigned long ulStart - offset in flash to start the writes at
//			long lCount - number of elements to write, in this case bytes
//			long lStride - number of locations to skip between writes
//			int *pnData - pointer to data buffer
//
//////////////////////////////////////////////////////////////

ERROR_CODE WriteData( 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
	ERROR_CODE ErrorCode = NO_ERR;	// tells whether there was an error trying to write
	int nCompare = 0;				// value that we use to verify flash
	bool bVerifyError = FALSE;		// lets us know if there was a verify error
	int nSector = 0;

	// if the user wants to verify then do it
	if( AFP_Verify == TRUE )
	{
		// 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 ) && ( ErrorCode == NO_ERR ) ); j++, 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, increase shift, and wait for completion
					UnlockFlash(ulOffset);
					WriteFlash( ulOffset, (pnData[i] >> iShift) );
					ErrorCode = PollToggleBit(ulOffset);
					ReadFlash( ulOffset, &nCompare );
					if( nCompare != ( (pnData[i]  >> iShift) & 0x0000FFFF ) )
					{
						bVerifyError = TRUE;
						break;
					}
					iShift += 16;
				}
				else
				{	// handle error
					bVerifyError = TRUE;
					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 only one write to do.
		if( ( nLeftover > 0 ) && ( ErrorCode == NO_ERR ) && bVerifyError == FALSE )
		{
			// 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, increase shift, and wait for completion
				UnlockFlash(ulOffset);
				WriteFlash( ulOffset, pnData[i] );
				ErrorCode = PollToggleBit(ulOffset);
				ReadFlash( ulOffset, &nCompare );
				if( nCompare != ( pnData[i] & 0x0000FFFF ) )
				{
					bVerifyError = TRUE;
				}
			}
			else
			{
				return ErrorCode;
			}
		}

		// return appropriate error code if there was a verification error
		if( bVerifyError == TRUE )
			return VERIFY_WRITE;
	}
	// the user does not want to verify
	else
	{
		// 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 ) && ( ErrorCode == NO_ERR ) ); j++, 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, increase shift, and wait for completion
					UnlockFlash(ulOffset);
					WriteFlash( ulOffset, (pnData[i] >> iShift) );
					ErrorCode = PollToggleBit(ulOffset);
					iShift += 16;
				}
				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 only one write to do.
		if( ( nLeftover > 0 ) && ( ErrorCode == NO_ERR ) )
		{
			// 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, increase shift, and wait for completion
				UnlockFlash(ulOffset);
				WriteFlash( ulOffset, pnData[i] );
				ErrorCode = PollToggleBit(ulOffset);
			}
			else
			{
				return ErrorCode;
			}
		}

⌨️ 快捷键说明

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