📄 bsl_flash.c
字号:
FLASH_WRITE16( 0xAAA, 0xAA );
FLASH_WRITE16( 0x555, 0x55 );
FLASH_WRITE16( FLASH_getSectorStartOffset(sectorIndex), 0x30 );
}
//==============================================================================
// Tests the lock state of the flash resource (device/controller), and if it is
// not locked, the function call sets it to locked.
// It is an atomic (uninterruptable) "test and set" function.
//
// return:
// FLASH_OK If the flash was not locked. The flash is locked now.
// FLASH_BUSY If the flash was locked, and it is still locked.
//==============================================================================
Uint32 FLASH_testAndLock()
{
Uint32 gie;
Uint32 ret;
gie = IRQ_globalDisable();
if( FLASH_lock == FLASH_UNLOCKED )
{
FLASH_lock = FLASH_LOCKED;
ret = FLASH_OK;
}
else
{
ret = FLASH_BUSY;
}
IRQ_globalRestore(gie);
return ret;
}
//==============================================================================
// Releases (unlocks) the flash resource
//==============================================================================
void FLASH_release()
{
FLASH_lock = FLASH_UNLOCKED;
}
//==============================================================================
// Checking address ranges
//
// parameters:
// flashOffset Starting byte offset within the flash address space
// dspAddress Starting byte address within the DSP address space
// (must be an 16-bit aligned address)
// numOf16bWs Number of 16-bit words to be transferred
//
// return:
// FLASH_CHKPRM_SIZE too many 16-bit words should be transferred
// FLASH_CHKPRM_FLASH flash offset is too big
// FLASH_CHKPRM_DSP DSP address (range) falls into flash memory address region
// FLASH_CHKPRM_OK all right
//==============================================================================
Uint32 FLASH_checkParams(
Uint32 flashOffset, Uint32 dspAddress, Uint32 numOf16bWs)
{
Uint32 numOfBytes;
if( (numOf16bWs == 0) || (numOf16bWs > (FLASH_sizeInBytes >> 1)) )
{
// either the size of buffer to be transferred is larger than the flash
// memory's whole size, or it is zero length
return FLASH_CHKPRM_SIZE;
}
if( (flashOffset & 1) || (flashOffset >= FLASH_sizeInBytes) )
{
// either not 16-bit word aligned address (offset),
// or too big flash offset (over the flash size)
return FLASH_CHKPRM_FLASH;
}
// create transfer block size in bytes
// calculation requirement: max flash device size less than 2 GBytes (not MB!)
numOfBytes = numOf16bWs << 1;
if( (flashOffset + numOfBytes) > FLASH_sizeInBytes )
{
// the end of the flash buffer to be transferred is over the end of the flash
return FLASH_CHKPRM_SIZE;
}
if( dspAddress < FLASH_STARTING_ADDRESS )
{
if( (dspAddress + numOfBytes) > FLASH_STARTING_ADDRESS )
{
// the end of the DSP buffer to be transferred falls into the flash memory
return FLASH_CHKPRM_SIZE;
}
}
else if( (dspAddress & 1) || (dspAddress <= FLASH_lastAddress) )
{
// either not 16-bit word aligned address,
// or the DSP starting address range falls into the flash memory range
return FLASH_CHKPRM_DSP;
}
return FLASH_CHKPRM_OK;
}
//==============================================================================
// Checking address ranges
//
// parameters:
// startSI Index of the first sector in the selected sector block
// Counter from 0.
// endSI Index of the last sector in the selected sector block
// Counter from 0.
//
// return:
// FLASH_CHKPRM_START_SI Too big sector index.
// FLASH_CHKPRM_END_SI endSI less then startSI
// FLASH_CHKPRM_OK all right
//==============================================================================
Uint32 FLASH_checkSiParams( Uint32 startSI, Uint32 endSI )
{
if( startSI > FLASH_getLastSectorIndex() ) return FLASH_CHKPRM_START_SI;
if( endSI < startSI ) return FLASH_CHKPRM_END_SI;
return FLASH_CHKPRM_OK;
}
/******************************************************************************\
* G L O B A L S E C T I O N
\******************************************************************************/
/******************************************************************************\
* global variable definitions
\******************************************************************************/
/******************************************************************************\
* global function definitions
\******************************************************************************/
//==============================================================================
// Returns the flash byte offset of the beginning of the given sector
//
// parameters:
// sectorIndex Selects the desired sector. Counted from 0.
// The 0-index sector is at the beginning of the flash
// memory.
// return:
// The starting flash byte offset of the given sector.
//==============================================================================
Uint32 FLASH_getSectorStartOffset(Uint32 sectorIndex)
{
if( FLASH_sizeInBytes == 0x00400000 )
{
// there is an Am29LV320DB flash device installed on the board
if( sectorIndex > 7 ) return ((sectorIndex - 7) << 16);
return (sectorIndex << 13);
}
else // if( FLASH_sizeInBytes == 0x00200000 )
{
// there is an Am29LV160DB flash device installed on the board
if( sectorIndex > 3 ) return ((sectorIndex - 3) << 16);
else if( sectorIndex == 0 ) return 0;
else if( sectorIndex != 3 ) return 0x004000 + ((sectorIndex - 1) << 13);
else return 0x008000;
}
}
//==============================================================================
// Returns the length of the selected sector in bytes
//
// parameters:
// sectorIndex Selects the desired sector. Counted from 0.
// The 0-index sector is at the beginning of the flash
// memory.
// return:
// The length of the selected sector in bytes.
//==============================================================================
Uint32 FLASH_getSectorLength(Uint32 sectorIndex)
{
if( FLASH_sizeInBytes == 0x00400000 )
{
// there is an Am29LV320DB flash device installed on the board
if( sectorIndex > 7 ) return 0x10000;
return 0x02000;
}
else // if( FLASH_sizeInBytes == 0x00200000 )
{
// there is an Am29LV160DB flash device installed on the board
if( sectorIndex > 3 ) return 0x10000;
else if( sectorIndex == 0 ) return 0x04000;
else if( sectorIndex != 3 ) return 0x02000;
else return 0x08000;
}
}
//==============================================================================
// Returns the last sector index of the flash memory
//
// return:
// The last sector index of the installed flash device
//==============================================================================
Uint32 FLASH_getLastSectorIndex()
{
if( FLASH_sizeInBytes == 0x00400000 )
{
// there is an Am29LV320DB flash device installed on the board
return 70;
}
else // if( FLASH_sizeInBytes == 0x00200000 )
{
// there is an Am29LV160DB flash device installed on the board
return 34;
}
}
//==============================================================================
// Returns the size of the flash memory in bytes
//
// return:
// flash memory size in bytes
//==============================================================================
Uint32 FLASH_getFlashSizeInBytes()
{
return FLASH_sizeInBytes;
}
//==============================================================================
// Reads a memory block from the flash memory
// (it can cross sector boundaries)
//
// parameters:
// flashOffset Starting byte offset within the flash address space
// dspAddress Starting byte address within the DSP address space
// (must be an 16-bit aligned address)
// numOf16bWs Number of 16-bit words to be transferred
// pReturn Pointer to the user's FLASH_Return-type
// variable, which will be set after the operation
//
// return:
// FLASH_ERROR The error code is in the pReturn-pointed variable
// FLASH_OK All right. The block has been copied from the flash
// to the DSP.
// <other error codes belong to function parameter checking>
//==============================================================================
Uint32 FLASH_blockRead16(
Uint32 flashOffset, Uint32 dspAddress, Uint32 numOf16bWs,
FLASH_PReturn pReturn)
{
Uint32 chkprm;
//Uint16 *pBuf16InDSP;
//Uint16 *pBuf16InFlash;
Uint32 cnt;
Uint32 xfrId;
if( FLASH_testAndLock() != FLASH_OK )
{
// there is another flash operation in process
pReturn->errorCode = FLASH_RETERR_BUSY;
return FLASH_ERROR;
}
// The flash was unlocked. Now, it is locked.
// check parameters of block transfer
chkprm = FLASH_checkParams(flashOffset,dspAddress,numOf16bWs);
if( chkprm != FLASH_CHKPRM_OK )
{
// parameter check error
pReturn->errorCode = chkprm << 4;
FLASH_release();
return FLASH_ERROR;
}
// transfer block from flash to DSP
// a DAT channel has been able to be opened to use it for data transfer
cnt = numOf16bWs << 1;
while( cnt >= 0xFFF8 )
{
xfrId = DAT_copy(
(void *)(FLASH_STARTING_ADDRESS + flashOffset), // flash offset
(void *)(dspAddress), // DSP address
0xFFF8 // transfer byte count
);
cnt -= 0xFFF8;
dspAddress += 0xFFF8;
flashOffset += 0xFFF8;
DAT_wait( xfrId );
}
if( cnt != 0 )
{
xfrId = DAT_copy(
(void *)(FLASH_STARTING_ADDRESS + flashOffset), // flash offset
(void *)(dspAddress), // DSP address
(Uint16)(cnt) // transfer byte count
);
DAT_wait( xfrId );
}
// data transfer with no DAT //pBuf16InDSP = (Uint16 *)(dspAddress);
//pBuf16InFlash = (Uint16 *)(FLASH_STARTING_ADDRESS + flashOffset);
//for( cnt = numOf16bWs; cnt != 0; cnt-- )
//{
// *pBuf16InDSP++ = *pBuf16InFlash++;
//}
// set the return values
pReturn->isCompleted = FLASH_COMPLETED;
pReturn->errorCode = FLASH_RETERR_NO_ERROR;
// unlock the flash resource
FLASH_release();
// After a call of the FLASH_release() function, the FLASH_accessReqObj can be
// overwritten, therfore, do not touch the FLASH_accessReqObj more!
// all right, the block has been copied
return FLASH_OK;
}
//==============================================================================
// Writes a memory block into the flash memory (without erase)
// (it can cross sector boundaries)
//
// parameters:
// flashOffset Starting byte offset within the flash address space
// dspAddress Starting byte address within the DSP address space
// (must be an 16-bit aligned address)
// numOf16bWs Number of 16-bit words to be transferred
// pReturn Pointer to the user's FLASH_Return-type
// variable, which will be set after the operation
//
// return:
// FLASH_ERROR The error code is in the pReturn-pointed variable
// FLASH_OK All right. The flash is under programming.
// (pReturn->isCompleted = FLASH_BUSY)
//
// pReturn.errorCode can be the followings after the operation is completed
// FLASH_RETERR_BUSY Another flash operation is in process.
// FLASH_RETERR_PROGRAMMING_ERROR The programming failed. The read-back
// check failed.
// FLASH_RETERR_SUCCESSFUL Programming was successful at all the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -