📄 bf533_sst_flash_driver.c
字号:
{
gpDeviceInfo = &DeviceInfo[i];
break;
}
}
if (gpDeviceInfo == NULL)
{
return PROCESS_COMMAND_ERR; // device not supported
}
//initiate sector information structures
AFP_NumSectors = gpDeviceInfo->NumSectors;
for(i=0; i < AFP_NumSectors; i++)
{
GetSectorStartEnd(&SectorInfo[i].lStartOff, &SectorInfo[i].lEndOff, i);
}
AFP_SectorInfo = (int*)&SectorInfo[0];
AFP_Description = gpDeviceInfo->DeviceName;
// ok
return NO_ERR;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE ResetFlash()
//
// Sends a "reset" command to the flash.
//
//////////////////////////////////////////////////////////////
ERROR_CODE ResetFlash(void)
{
return SST_Reset_Flash();
}
//////////////////////////////////////////////////////////////
// ERROR_CODE WriteData( unsigned long ulStart, long lCount, long lStride, int *pnData )
//
// 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 )
{
ERROR_CODE ErrorCode = NO_ERR; // tells whether we had an error while filling
U32 i = 0; // loop counter
U8 Data;
#if DEBUG == 1
sprintf(cDebug, "ulStart = %d, lCount = %d, lStride = %d, pnData = 0x%x.\r\n", ulStart, lCount, lStride, pnData);
UART_TX(cDebug, strlen(cDebug));
return NO_ERR;
#endif
U32 iSize = gpDeviceInfo->Size;
// check for flash size
if (ulStart >= iSize || (ulStart + lCount > iSize))
{
return INVALID_BLOCK;
}
// write memory
if (lStride == 1)
{
for (i = 0; (i < lCount) && (i < BUFFER_SIZE); i++)
{
ErrorCode = SST_Write_Byte(ulStart+i, ((U8 *)pnData)[i]);
if( ErrorCode != NO_ERR)
{
return ErrorCode;
}
}
}
else
{
return INVALID_BLOCK; // we don't support non-sequential write at the moment
}
// if the user wants to verify then do it
if( AFP_Verify == TRUE )
{
for (i = 0; (i < lCount) && (i < BUFFER_SIZE); i++)
{
ErrorCode = SST_Read_Byte(ulStart+i, &Data);
if (Data != ((U8 *)pnData)[i])
{
return VERIFY_WRITE;
}
}
}
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 )
{
ERROR_CODE ErrorCode = NO_ERR; // tells whether we had an error while filling
U32 i = 0; // loop counter
U8 Data;
#if DEBUG == 1
sprintf(cDebug, "ulStart = %d, lCount = %d, lStride = %d, pnData = 0x%x.\r\n", ulStart, lCount, lStride, pnData);
UART_TX(cDebug, strlen(cDebug));
return NO_ERR;
#endif
U32 iSize = gpDeviceInfo->Size;
// check for flash size
if (ulStart >= iSize || (ulStart + lCount > iSize))
{
return INVALID_BLOCK;
}
// write memory
if (lStride == 1)
{
for (i = 0; (i < lCount) && (i < BUFFER_SIZE); i++)
{
ErrorCode = SST_Write_Byte(ulStart+i, (U8)pnData[0]);
if( ErrorCode != NO_ERR)
{
return ErrorCode;
}
}
}
else
{
return INVALID_BLOCK; // we don't support non-sequential write at the moment
}
// if the user wants to verify then do it
if( AFP_Verify == TRUE )
{
for (i = 0; (i < lCount) && (i < BUFFER_SIZE); i++)
{
ErrorCode = SST_Read_Byte(ulStart+i, &Data);
if (Data != (U8)pnData[0])
{
return VERIFY_WRITE;
}
}
}
// return the appropriate error code
return ErrorCode;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE EraseFlash()
//
// Sends an "erase all" command to the flash.
//
//////////////////////////////////////////////////////////////
ERROR_CODE EraseFlash(void)
{
return SST_Chip_Erase();
}
//////////////////////////////////////////////////////////////
// ERROR_CODE EraseBlock()
//
// Sends an "erase block" command to the flash.
//
//////////////////////////////////////////////////////////////
ERROR_CODE EraseBlock( int nBlock )
{
ERROR_CODE ErrorCode = NO_ERR; // tells us if there was an error erasing flash
#if DEBUG == 1
sprintf(cDebug, "nBlock = %d.\r\n", nBlock);
UART_TX(cDebug, strlen(cDebug));
return NO_ERR;
#endif
// if the block is invalid just return
if (nBlock >= gpDeviceInfo->NumSectors )
{
return INVALID_BLOCK;
}
ErrorCode = SST_Sector_Erase(nBlock);
// block erase should be complete
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 )
{
U32 i = 0; // loop counter
#if DEBUG == 1
sprintf(cDebug, "ulStart = %d, lCount = %d, lStride = %d, pnData = 0x%x.\r\n", ulStart, lCount, lStride, pnData);
UART_TX(cDebug, strlen(cDebug));
return NO_ERR;
#endif
U32 iSize = gpDeviceInfo->Size;
// check for flash size
if (ulStart >= iSize || (ulStart + lCount > iSize))
{
return INVALID_BLOCK;
}
// read memory
if (lStride == 1)
{
for (i = 0; (i < lCount) && (i < BUFFER_SIZE); i++)
{
SST_Read_Byte(ulStart+i, &(((U8 *)pnData)[i]));
}
}
else
{
return INVALID_BLOCK; // we don't support non-sequential write at the moment
}
return NO_ERR;
}
//////////////////////////////////////////////////////////////
// ERROR_CODE GetSectorNumber()
//
// Gets a sector number based on the offset.
//
//////////////////////////////////////////////////////////////
ERROR_CODE GetSectorNumber( unsigned long ulOffset, int *pnSector )
{
if (ulOffset > gpDeviceInfo->Size)
{
return INVALID_SECTOR;
}
*pnSector = ulOffset / gpDeviceInfo->SectorSize;
// 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 < gpDeviceInfo->NumSectors)
{
*lStartOff = nSector * gpDeviceInfo->SectorSize;
*lEndOff = *lStartOff + gpDeviceInfo->SectorSize - 1;
}
else
{
return INVALID_SECTOR;
}
// ok
return NO_ERR;
}
#endif // __BF533_SST_FLASH_DRIVER_C__
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -