📄 bf532sstflash.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"
// #defines
#define TRUE 0x1
#define FALSE 0x0
#define NULL 0x0
#define BUFFER_SIZE 0x1000
#define NUM_SECTORS 32 // number of sectors in the flash device
// 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;
// #defines for the assembler
asm ("#define FLASH_START_L 0x0000");
asm ("#define FLASH_START_H 0x2000");
// 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-BF532 AD7656 Board";
char *AFP_Description = "Fujisu";
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 = 0x2000;
int AFP_SectorSize2 = 0x10000;
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];
// flag telling us that the flash had to be reset
char reset = 0x0;
// 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 );
// 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()
{
asm("#define EBIU_AMGCTL 0xFFC00A00");
// setup EBIU_AMGCTL reg (enable all banks)
asm("p0.l = EBIU_AMGCTL & 0xFFFF;");
asm("p0.h = (EBIU_AMGCTL >> 16) & 0xFFFF;");
asm("r0 = 0x0f;");
asm("w[p0] = r0;");
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))
{
// you cannot write to this region in flash
if( ( ulOffset >= 0x20100000 ) )
return INVALID_SECTOR;
// 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 )
{
// you cannot write to this region in flash
if( ( ulOffset >= 0x20100000 ) )
return INVALID_SECTOR;
// 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))
{
// you cannot write to this region in flash
if( ( ulOffset >= 0x20270000 ) && ( ulOffset < 0x20280000 ) )
return INVALID_SECTOR;
// 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 ) )
{
// you cannot write to this region in flash
if( ( ulOffset >= 0x20100000 ) )
return INVALID_SECTOR;
// 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;
}
}
}
// return the appropriate error code
return ErrorCode;
}
//////////////////////////////////////////////////////////////
// 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 = 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 we have an odd offset we need to write a byte
// to the first location and the last
if(ulOffset%2 != 0)
{
// read the offset - 1 and OR in our value
ulNewOffset = ulOffset - 1;
ReadFlash( ulNewOffset, &nCompare );
nCompare &= 0xFF00;
nCompare |= (pnData[0] & 0x00FF);
// unlock the flash, do the write, and wait for completion
UnlockFlash(ulNewOffset);
WriteFlash( ulNewOffset, nCompare );
ErrorCode = PollToggleBit(ulNewOffset);
// 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);
// 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 )
{
//ResetFlash();
// unlock the flash, do the write, and wait for completion
UnlockFlash(ulOffset);
WriteFlash( ulOffset, pnData[0] );
ErrorCode = PollToggleBit(ulOffset);
ReadFlash( ulOffset, &nCompare );
if( nCompare != ( pnData[0] & 0x0000FFFF ) )
{
bVerifyError = TRUE;
break;
}
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -