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

📄 bf561ezflash.c

📁 基于BF561的JS28F128 FLASH驱动
💻 C
字号:
///////////////////////////////////////////////////////////////
//
// BF533EzFlash.c
//
// Analog Devices, Inc. - 2001
//
//
// Change Log
//
//		1.00.1
//			- made changes so that the driver will work with
//			  the revised GUI
//
//		1.00.0
//			- initial release
//
// VisualDSP++ "Flash Programmer" flash driver for use with the
// ADSP-BF533 EZ-KIT Lite containing the STMicroelectronics PSD4256G
// flash device.
//
///////////////////////////////////////////////////////////////

// error enum
#include "Errors.h"
#include "define.h"
#include "SetupDSP.h"
#include "smallflash.h"


#define BUFFER_SIZE		0x600
#define	NUM_SECTORS 	(FLASH_SIZE/FLASH_BLOCK_SIZE)
#define FLASH_WIDTH		16 //8	//Flash is 16bits width   

// 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
	ERS_DATA_SEC,   //0x0a
	ERS_PROGRAM_SEC,//0x0b
	ERS_CONFIG_SEC,//0x0c
}enProgCmds;

// #defines for the assembler
asm ("#define FLASH_START_L 0x0000");
asm ("#define FLASH_START_H 0x2000");
bool EraseConfigSector();
bool EraseDataSector();
bool EraseProgramSector();
// 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 = "ADSP-BF561 EZ-KIT Lite";//"ADSP-BF533 EZ-KIT Lite"; 
char 			*AFP_Description = "28F128J3A";//"SST39VF040";  
enProgCmds 		AFP_Command = NO_COMMAND;
int 			AFP_ManCode = -1;				// 0x20 = STMicroelectronics
int 			AFP_DevCode = -1;				// 0xE9 = PSD4256G
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 = 0x10000;
int 			AFP_SectorSize2 = 0x1000;
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;       // 0x08;// width of the flash device    
int 			*AFP_SectorInfo;
SECTORLOCATION SectorInfo[NUM_SECTORS];



// flag telling us that the flash had to be reset
char reset = 0x0;

// exit flag
bool bExit = FALSE;

// *********************
// Map of flash sectors
// *********************

// FLASH A

// 		Type			START ADDR    	END ADDR		SECTOR NUM
// 		-------			----------		--------		----------
// 		Main Flash		0x20000000		0x200FFFFF		00 - 15
// 		Boot Flash		0x20200000		0x2020FFFF		32 - 35

// FLASH B

// 		Main Flash		0x20100000		0x201FFFFF		16 - 31
// 		Boot Flash		0x20280000		0x2028FFFF		36 - 39




// FLASH bf561  

// 		Type			START ADDR    	END ADDR		SECTOR NUM
// 		-------			----------		--------		----------
// 		Flash		    0x20000000		0x200FFFFF		00 - 31

//     During Block-Erase, A19-A15 address line will select the block.
 
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
	SetupDSP();
	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;
			case ERS_DATA_SEC:
				{
					EraseDataSector(); //跟 case ERASE_SECT	 功能一样 		
				}
				break;	
			case ERS_PROGRAM_SEC:
				{
					EraseProgramSector();					
				}
				break;
			case ERS_CONFIG_SEC:
				{
					EraseConfigSector();
				}
				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 );


	// 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()///????
{
	//*pEBIU_AMBCTL0	= 0x0000FFC0;
	//*pEBIU_AMGCTL	= 0x00F2;

	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 )
{
	FlashWriteData(ulStart,lCount,lStride,pnData);
	return NO_ERR;	
}


//////////////////////////////////////////////////////////////
// ERROR_CODE FillData()
//
// Fill flash with a value.
//
// 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 FillData( unsigned long ulStart, long lCount, long lStride, int *pnData )
{
	long i;	
	for(i=0;i<lCount/2;i++)
	{      
		FlashWriteData(ulStart, 2, lStride, pnData);  //zhanzy modify
		ulStart += sizeof(UWORD);
	}
	// return the appropriate error code
	return NO_ERR;
}


//////////////////////////////////////////////////////////////
// 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 )
{
	FlashReadData(ulStart,lCount,lStride,pnData);
	// 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 )
{
	FlashWriteCell(ulOffset, nValue);
	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 )
{
	FlashReadCell(ulOffset, pnValue);
	// 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)
{
	FlashPollToggleBit(ulOffset);
	return NO_ERR;
}



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

ERROR_CODE ResetFlash()
{
	// send the reset command to the flash
	FlashReset();

	// reset should be complete
	return NO_ERR;
}


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

ERROR_CODE EraseFlash()
{
	FlashEraseChip();
	return NO_ERR;
}

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

ERROR_CODE EraseBlock( int  nBlock)
{
	FlashEraseBlock(nBlock);
	return NO_ERR;
}


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

ERROR_CODE UnlockFlash(unsigned long ulOffset)
{
	FlashUnlock(ulOffset);
	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
	UDWORD wdID = FlashGetID();
	
	AFP_DevCode = wdID & 0x00FF;

	// there is no ManCode so just hard code it to 0x0
	AFP_ManCode = ((wdID>>8)&0xff);

	// 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
	FlashReset();

	// 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 = ulOffset/FLASH_BLOCK_SIZE;
	*pnSector   = nSector;

	// 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 )
{
	if(nSector >= NUM_SECTORS)
		return INVALID_SECTOR;
	*lStartOff  = nSector * FLASH_BLOCK_SIZE;
	*lEndOff    = nSector * FLASH_BLOCK_SIZE + FLASH_BLOCK_SIZE - 1;

	// ok
	return NO_ERR;
}

//擦除数据区
bool EraseDataSector()
{
	int nEraseStart = FLASH_DATA_OFFSET / FLASH_BLOCK_SIZE;
	int nEraseEnd   = FLASH_CFG_OFFSET  / FLASH_BLOCK_SIZE;
	int i,j;
	for(i=nEraseStart;i<nEraseEnd;i++)
	{
		EraseBlock(i);			
	}
	return TRUE;
}

bool EraseProgramSector()
{
	int nEraseStart = 0;
	int nEraseEnd   = FLASH_DATA_OFFSET / FLASH_BLOCK_SIZE;
	int i,j;
	for(i=nEraseStart;i<nEraseEnd;i++)
	{
		EraseBlock(i);		
	}
	return TRUE;
}

bool EraseConfigSector()
{
	int nEraseStart = FLASH_CFG_OFFSET / FLASH_BLOCK_SIZE;
	int nEraseEnd   = FLASH_SIZE / FLASH_BLOCK_SIZE;
	int i,j;
	for(i=nEraseStart;i<nEraseEnd;i++)
	{
		EraseBlock(i);		
	}
	return TRUE;
}

⌨️ 快捷键说明

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