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

📄 bsl_flash.c

📁 BSL64xx是一dsp的程序代码
💻 C
📖 第 1 页 / 共 3 页
字号:
//										16-bit words in the block.
//		<other error codes belong to function parameter checking>
//==============================================================================
Uint32 FLASH_blockProg16(
	Uint32 flashOffset, Uint32 dspAddress, Uint32 numOf16bWs,
	FLASH_PReturn pReturn)
{
	Uint32	chkprm;

	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;
	}

	// set the Flash RARO
	FLASH_accessReqObj.operation = FLASH_OP_TRANSFER_TO_FLASH;
	FLASH_accessReqObj.param1 = flashOffset;
	FLASH_accessReqObj.param2 = dspAddress;
	FLASH_accessReqObj.numOf16bWs = numOf16bWs;
	FLASH_accessReqObj.pReturn = pReturn;
	FLASH_accessReqObj.pReturn->isCompleted = FLASH_BUSY;
	FLASH_accessReqObj.pReturn->errorCode = FLASH_RETERR_BUSY;

	// setup the working variable for flash controller's ISR
	FLASH_OP_param1 = FLASH_accessReqObj.param1;
	FLASH_OP_param2 = FLASH_accessReqObj.param2;
	FLASH_OP_count = FLASH_accessReqObj.numOf16bWs;

	// enter Unlock Bypass Programming mode
	FLASH_cmdUnlockBypass();

	// start the programming in unlock bypass mode
	// by programming the first 16-bit word
	FLASH_cmdUnlockBypassProg16( flashOffset, DSP_READ16(dspAddress) );
	// after the physical programming, a Busy#-to-Ready low-to-high rising edge
	// will be generated on the pin Ready/Busy# of the flash device, which can
	// generate an interrupt (... and the FLASH_operation() function will be
	// called)

	// all right, the flash controller started to work for us
	return FLASH_OK;
}

//==============================================================================
// Erases the entire flash memory
//
// parameters:
//		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 erasing.
//							(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_ERASE_FAILED		The erasing failed.
//		FLASH_RETERR_SUCCESSFUL			Erasing was successful.
//==============================================================================
Uint32 FLASH_chipErase(FLASH_PReturn pReturn)
{
	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.

	// set the Flash RARO
	FLASH_accessReqObj.operation = FLASH_OP_ERASE_CHIP;
	FLASH_accessReqObj.pReturn = pReturn;
	FLASH_accessReqObj.pReturn->isCompleted = FLASH_BUSY;
	FLASH_accessReqObj.pReturn->errorCode = FLASH_RETERR_BUSY;

	// start the chip erasing
	FLASH_cmdChipErase();

	// all right, the flash controller started to work for us
	return FLASH_OK;
}

//==============================================================================
// Erases some sectors of the flash
//
// parameters:
//		startSI				Index of the first sector to be erased.
//							Counted from 0.
//		endSI				Index of the last sector to be erased.
//							Counted from 0.
//		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 erasing.
//							(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_ERASE_FAILED		The erasing failed.
//		FLASH_RETERR_SUCCESSFUL			Erasing was successful.
//==============================================================================
Uint32 FLASH_sectorErase(Uint32 startSI, Uint32 endSI,
	FLASH_PReturn pReturn)
{
	Uint32	chkprm;

	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
	chkprm = FLASH_checkSiParams(startSI,endSI);
	if( chkprm != FLASH_CHKPRM_OK )
	{
		// parameter check error
		pReturn->errorCode = chkprm << 4;
		FLASH_release();
		return FLASH_ERROR;
	}

	// set the Flash RARO
	FLASH_accessReqObj.operation = FLASH_OP_ERASE_SECTORS;
	FLASH_accessReqObj.param1 = startSI;
	if( endSI <= FLASH_getLastSectorIndex() )
	{
		FLASH_accessReqObj.param2 = endSI;
	}
	else
	{
		FLASH_accessReqObj.param2 = FLASH_getLastSectorIndex();
	}
	FLASH_accessReqObj.pReturn = pReturn;
	FLASH_accessReqObj.pReturn->isCompleted = FLASH_BUSY;
	FLASH_accessReqObj.pReturn->errorCode = FLASH_RETERR_BUSY;

	// setup the working variable for flash controller's ISR
	FLASH_OP_param1 = FLASH_accessReqObj.param1;
	FLASH_OP_param2 = FLASH_accessReqObj.param2;
	FLASH_OP_count = FLASH_OP_param1;

	// start to erase the first given sector
	FLASH_cmdSectorErase( FLASH_OP_count );

	// all right, the flash controller started to work for us
	return FLASH_OK;
}

//==============================================================================
// Checks the given sectors of the flash for blank
//
// parameters:
//		startSI				Index of the first sector to be checked.
//							Counted from 0.
//		endSI				Index of the last sector to be checked.
//							Counted from 0.
//		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 reading/checking.
//							(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_CHECK_ERROR		The selected sectors are not blank.
//		FLASH_RETERR_SUCCESSFUL			The selected sectors are blank.
//==============================================================================
Uint32 FLASH_checkSectorBlank(Uint32 startSI, Uint32 endSI,
	FLASH_PReturn pReturn)
{
	Uint32	chkprm;
	Uint32	*pB;
	Uint32	d0,d1,d2,d3;
	Uint32	cnt;

	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
	chkprm = FLASH_checkSiParams(startSI,endSI);
	if( chkprm != FLASH_CHKPRM_OK )
	{
		// parameter check error
		pReturn->errorCode = chkprm << 4;
		FLASH_release();
		return FLASH_ERROR;
	}

	// start address
	cnt = FLASH_STARTING_ADDRESS + FLASH_getSectorStartOffset(startSI);
	pB = (Uint32 *)(cnt);

	// number of bytes to be checked
	cnt = FLASH_STARTING_ADDRESS + FLASH_getSectorStartOffset(endSI) +
		FLASH_getSectorLength(endSI) - cnt;

	d0 = *pB++;
	d1 = *pB++;
	d2 = *pB++;
	d3 = *pB++;
	for( ; cnt != 0; cnt -= 16 )
	{
		if( d0 != 0xFFFFFFFF ) break;
		d0 = *pB++;
		if( d1 != 0xFFFFFFFF ) break;
		d1 = *pB++;
		if( d2 != 0xFFFFFFFF ) break;
		d2 = *pB++;
		if( d3 != 0xFFFFFFFF ) break;
		d3 = *pB++;
	}
	if( cnt != 0 )
	{
		// the checking failed
		pReturn->errorCode = FLASH_RETERR_CHECK_ERROR;
	}
	else
	{
		// checking was successful
		pReturn->errorCode = FLASH_RETERR_NO_ERROR;
	}

	// set the return values
	pReturn->isCompleted = FLASH_COMPLETED;

	// 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;
}

//==============================================================================
// Reads the given block of the flash memory to compare its content with the
// given memory block located in the DSP's memory.
//
// 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 reading/checking.
//							(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_CHECK_ERROR		Comparing failed.
//		FLASH_RETERR_SUCCESSFUL			Comparing was successful.
//==============================================================================
Uint32 FLASH_blockComp16(
	Uint32 flashOffset, Uint32 dspAddress, Uint32 numOf16bWs,
	FLASH_PReturn pReturn)
{
	Uint32	chkprm;
	Uint16	*pBuf16InDSP;
	Uint16	*pBuf16InFlash;
	Uint32	cnt16;

	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 selections
	chkprm = FLASH_checkParams(flashOffset,dspAddress,numOf16bWs);
	if( chkprm != FLASH_CHKPRM_OK )
	{
		// parameter check error
		pReturn->errorCode = chkprm << 4;
		FLASH_release();
		return FLASH_ERROR;
	}

	// start addresses
	pBuf16InDSP		= (Uint16 *)(dspAddress);
	pBuf16InFlash	= (Uint16 *)(FLASH_STARTING_ADDRESS + flashOffset);

	for( cnt16 = numOf16bWs; cnt16 != 0; cnt16 -- )
	{
		if( *pBuf16InDSP++ != *pBuf16InFlash++ ) break;
	}
	if( cnt16 != 0 )
	{
		// the checking failed
		// the two blocks are different
		pReturn->errorCode = FLASH_RETERR_CHECK_ERROR;
	}
	else
	{
		// checking was successful
		// the two blocks are the same
		pReturn->errorCode = FLASH_RETERR_NO_ERROR;
	}

	// set the return values
	pReturn->isCompleted = FLASH_COMPLETED;

	// 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!

	return FLASH_OK;
}


#endif /* BSL_FLASH_SUPPORT */
/******************************************************************************\
* End of bsl_flash.c
\******************************************************************************/

⌨️ 快捷键说明

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