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

📄 main.c

📁 使用ADSP-BF533的例子源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	AFP_DeviceCompany = CmdStruct.SGetDesc.pFlashCompany;

	return(ErrorCode);

}


//----------- F i l l D a t a  ( ) ----------//
//
//  PURPOSE
//  	Fill flash device with a value.
//
// 	INPUTS
//	 	unsigned long ulStart - address 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
//
// 	RETURN VALUE
// 		ERROR_CODE - value if any error occurs during fill
// 		NO_ERR     - otherwise
//
// 	CHANGES
//  	9-28-2005 Created

ERROR_CODE FillData( unsigned long ulStart, long lCount, long lStride, int *pnData )
{
	long i = 0;							// loop counter
	ERROR_CODE ErrorCode = NO_ERR;		// tells whether we had an error while filling
	bool bVerifyError = FALSE;			// lets us know if there was a verify error
	unsigned long ulNewOffset = 0;		// used if we have an odd address
	bool bByteSwapMid = FALSE;			// if writing to odd address the middle needs byte swapping
	unsigned long ulStartAddr;   		// current address to fill
	unsigned long ulSector = 0;			// sector number to verify address
	int nCompare = 0;					// value that we use to verify flash

	ADI_DEV_1D_BUFFER		WriteBuff;	// buffer pointer
	ADI_DEV_1D_BUFFER		ReadBuff;	// buffer pointer

	ulStartAddr = FLASH_START_ADDR + ulStart;
	COMMAND_STRUCT	CmdStruct;	//structure for GetSectStartEnd

	// if we have an odd offset we need to write a byte
	// to the first location and the last
	if(ulStartAddr%2 != 0)
	{
		// read the offset - 1 and OR in our value
		ulNewOffset = ulStartAddr - 1;

		ReadBuff.Data 				= (void *)&nCompare;
		ReadBuff.pAdditionalInfo 	= (void *)&ulNewOffset ;
		ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Read(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&ReadBuff);

		nCompare &= 0x00FF;
		nCompare |= ( (pnData[0] << 8) & 0xFF00 );

		// unlock the flash, do the write, and wait for completion
		WriteBuff.Data 				= (void *)&nCompare;
		WriteBuff.pAdditionalInfo 	= (void *)&ulNewOffset ;
		ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Write(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&WriteBuff);

		// move to the last offset
		ulNewOffset = ( (ulStartAddr - 1) + (lCount * ( lStride * 2 ) ) );

		// read the value and OR in our value
		ReadBuff.Data 				= (void *)&nCompare;
		ReadBuff.pAdditionalInfo 	= (void *)&ulNewOffset ;
		ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Read(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&ReadBuff);

		nCompare &= 0xFF00;
		nCompare |= ( ( pnData[0] >> 8) & 0x00FF );

		// unlock the flash, do the write, and wait for completion
		WriteBuff.Data 				= (void *)&nCompare;
		WriteBuff.pAdditionalInfo 	= (void *)&ulNewOffset ;
		ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Write(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&WriteBuff);

		// increment the offset and count
		ulStartAddr = ( (ulStartAddr - 1) + ( lStride * 2 ) );
		lCount--;

		bByteSwapMid = TRUE;
	}

	if( bByteSwapMid == TRUE )
	{
		int nTemp = (char)pnData[0];
		pnData[0] &= 0xFF00;
		pnData[0] >>= 8;
		pnData[0] |= (nTemp << 8);
	}

	// verify writes if the user wants to
	if( AFP_Verify == TRUE )
	{
		// fill the value
		for (i = 0; ( ( i < lCount ) && ( ErrorCode == NO_ERR ) ); i++, ulStartAddr += ( lStride * 2 ) )
		{

			// check to see that the address is within a valid sector
			CmdStruct.SGetSectNum.ulOffset = ulStartAddr;
			CmdStruct.SGetSectNum.pSectorNum = &ulSector;
			ErrorCode = (ERROR_CODE)ADIPSD4256GEntryPoint.adi_pdd_Control(NULL, CNTRL_GET_SECTNUM, &CmdStruct  );

			if( NO_ERR == ErrorCode )
			{
				// unlock the flash, do the write, and wait for completion
				WriteBuff.Data 				= (void *)&pnData[0];
				WriteBuff.pAdditionalInfo 	= (void *)&ulStartAddr;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Write(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&WriteBuff);

				ReadBuff.Data 				= (void *)&nCompare;
				ReadBuff.pAdditionalInfo 	= (void *)&ulStartAddr ;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Read(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&ReadBuff);

				if( nCompare != ( pnData[0] & 0x0000FFFF ) )
				{
					bVerifyError = TRUE;
					break;
				}
			}
			else
			{
				return ErrorCode;
			}

		}

		// return appropriate error code if there was a verification error
		if( bVerifyError == TRUE )
			return VERIFY_WRITE;
	}
	// user did not want to verify writes
	else
	{
		// fill the value
		for (i = 0; ( ( i < lCount ) && ( ErrorCode == NO_ERR ) ); i++, ulStartAddr += ( lStride * 2 ))
		{

			// check to see that the address is within a valid sector
			CmdStruct.SGetSectNum.ulOffset = ulStartAddr;
			CmdStruct.SGetSectNum.pSectorNum = &ulSector;
			ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Control(NULL, CNTRL_GET_SECTNUM, &CmdStruct  );

			if( NO_ERR == ErrorCode )
			{			// unlock the flash, do the write, and wait for completion
				WriteBuff.Data 				= (void *)&pnData[0];
				WriteBuff.pAdditionalInfo 	= (void *)&ulStartAddr;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Write(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&WriteBuff);
			}
			else
			{
				return ErrorCode;
			}
		}
	}

	// return the appropriate error code
	return ErrorCode;
}


//----------- W r i t e D a t a  ( ) ----------//
//
//  PURPOSE
//  	Write a buffer to flash device.
//
// 	INPUTS
// 		unsigned long ulStart - address 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
//
//  RETURN VALUE
//  	ERROR_CODE - value if any error occurs during writing
//  	NO_ERR     - otherwise
//
//  CHANGES
//  	9-28-2005 Created

ERROR_CODE WriteData( unsigned long ulStart, long lCount, long lStride, int *pnData )
{
	long i = 0;						// loop counter
	int j = 0;						// inner loop counter
	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 *pnValue=0;					// value to write
	unsigned long ulAbsoluteAddr;   // current address to write
	unsigned long ulSector = 0;		// sector number to verify address

	ADI_DEV_1D_BUFFER		WriteBuff;	// buffer pointer
	ADI_DEV_1D_BUFFER		ReadBuff;	// buffer pointer

	ulAbsoluteAddr = FLASH_START_ADDR + ulStart;

	COMMAND_STRUCT	CmdStruct;	//structure for GetSectStartEnd

	// 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++, ulAbsoluteAddr += ( lStride * 2 ) )
			{

				// check to see that the address is within a valid sector
				CmdStruct.SGetSectNum.ulOffset = ulAbsoluteAddr;
				CmdStruct.SGetSectNum.pSectorNum = &ulSector;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Control(NULL, CNTRL_GET_SECTNUM, &CmdStruct  );

				if( NO_ERR == ErrorCode )
				{
					// unlock the flash, do the write, increase shift, and wait for completion
					*pnValue = pnData[i] >> iShift;
					WriteBuff.Data 				= (void *)pnValue;
					WriteBuff.pAdditionalInfo 	= (void *)&ulAbsoluteAddr ;
					ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Write(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&WriteBuff);

					ReadBuff.Data 				= (void *)&nCompare;
					ReadBuff.pAdditionalInfo 	= (void *)&ulAbsoluteAddr ;
					ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Read(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&ReadBuff);

					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 )
		{

			// check to see that the address is within a valid sector
			CmdStruct.SGetSectNum.ulOffset = ulAbsoluteAddr;
			CmdStruct.SGetSectNum.pSectorNum = &ulSector;
			ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Control(NULL, CNTRL_GET_SECTNUM, &CmdStruct  );

			if( NO_ERR == ErrorCode )
		 	{
				// unlock the flash, do the write, increase shift, and wait for completion
				WriteBuff.Data 				= (void *)&pnData[i];
				WriteBuff.pAdditionalInfo 	= (void *)&ulAbsoluteAddr ;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Write(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&WriteBuff);

				ReadBuff.Data 				= (void *)&nCompare;
				ReadBuff.pAdditionalInfo 	= (void *)&ulAbsoluteAddr ;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Read(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&ReadBuff);

				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++, ulAbsoluteAddr += ( lStride * 2 ))
			{
				// check to see that the address is within a valid sector
				CmdStruct.SGetSectNum.ulOffset = ulAbsoluteAddr;
				CmdStruct.SGetSectNum.pSectorNum = &ulSector;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Control(NULL, CNTRL_GET_SECTNUM, &CmdStruct  );

				if( NO_ERR == ErrorCode )
				{
					// unlock the flash, do the write, increase shift, and wait for completion
					*pnValue = pnData[i] >> iShift;
					WriteBuff.Data 				= (void *)pnValue;
					WriteBuff.pAdditionalInfo 	= (void *)&ulAbsoluteAddr ;
					ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Write(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&WriteBuff);

					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 ) )
		{
			// check to see that the address is within a valid sector
			CmdStruct.SGetSectNum.ulOffset = ulAbsoluteAddr;
			CmdStruct.SGetSectNum.pSectorNum = &ulSector;
			ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Control(NULL, CNTRL_GET_SECTNUM, &CmdStruct  );

			if( NO_ERR == ErrorCode )
			{
				// unlock the flash, do the write, increase shift, and wait for completion
				WriteBuff.Data 				= (void *)&pnData[i] ;
				WriteBuff.pAdditionalInfo 	= (void *)&ulAbsoluteAddr ;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Write(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&WriteBuff);
			}
			else
			{
				return ErrorCode;
			}
		}

	}

	// return the appropriate error code
	return ErrorCode;
}


//----------- R e a d D a t a  ( ) ----------//
//
//  PURPOSE
//  	Read a buffer from flash device.
//
// 	INPUTS
//		unsigned long ulStart - address in flash to start the reads at
//		long lCount - number of elements to read, in this case bytes
//		long lStride - number of locations to skip between reads
//		int *pnData - pointer to data buffer to fill
//
//	RETURN VALUE
//  	ERROR_CODE - value if any error occurs during reading
//  	NO_ERR     - otherwise
//
//  CHANGES
//  	9-28-2005 Created

ERROR_CODE ReadData( unsigned long ulStart, long lCount, long lStride, int *pnData )
{

	long i = 0;						// loop counter
	int j = 0;						// inner loop counter
	int iShift = 0;					// shift value by iShift bits
	unsigned long ulOffset = ulStart;			// current offset to read
	int iNumWords = 2;				// number of words in a long
	int nLeftover = lCount % 4;		// how much if any do we have leftover to read
	int nHi,nLow;
	ERROR_CODE ErrorCode = NO_ERR;	// tells whether there was an error trying to read
	unsigned long ulAbsoluteAddr;   // current address to read
	unsigned long ulSector = 0;		// sector number to verify address
	unsigned long ulMask =0xffff;

	ADI_DEV_1D_BUFFER		ReadBuff;	// buffer pointer

	COMMAND_STRUCT	CmdStruct;	//structure for GetSectStartEnd

	ulAbsoluteAddr = FLASH_START_ADDR + ulStart;

	// read the buffer up to BUFFER_SIZE items
	for (i = 0; (i < lCount/4) && (i < BUFFER_SIZE); i++)
	{
		for ( iShift = 0, j = 0; j < iNumWords ; j+=2 )
		{

			// check to see that the address is within a valid sector
			CmdStruct.SGetSectNum.ulOffset = ulAbsoluteAddr;
			CmdStruct.SGetSectNum.pSectorNum = &ulSector;
	 		ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Control(NULL, CNTRL_GET_SECTNUM, &CmdStruct  );

			if( NO_ERR == ErrorCode )
			{
				ReadBuff.Data 				= (void *)&nLow;
				ReadBuff.pAdditionalInfo 	= (void *)&ulAbsoluteAddr ;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Read(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&ReadBuff);

				ulAbsoluteAddr += (lStride*2);

				ReadBuff.Data 				= (void *)&nHi;
				ReadBuff.pAdditionalInfo 	= (void *)&ulAbsoluteAddr ;
				ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Read(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&ReadBuff);

				ulAbsoluteAddr += (lStride*2);
				//mask nLow to get the exact lsb bits
				pnData[i] = (nHi << 16) | (nLow & ulMask);

			}
			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 that there is only one read left to do.
	if( nLeftover > 0 )
	{
		// check to see that the address is within a valid sector
		CmdStruct.SGetSectNum.ulOffset = ulAbsoluteAddr;
		CmdStruct.SGetSectNum.pSectorNum = &ulSector;
		ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Control(NULL, CNTRL_GET_SECTNUM, &CmdStruct  );

		if( NO_ERR == ErrorCode )
		{
			ReadBuff.Data 				= (void *)&pnData[i];
			ReadBuff.pAdditionalInfo 	= (void *)&ulAbsoluteAddr ;
			ErrorCode = (ERROR_CODE) ADIPSD4256GEntryPoint.adi_pdd_Read(NULL, ADI_DEV_1D, (ADI_DEV_BUFFER *)&ReadBuff);
		}
		else
		{
			return ErrorCode;
		}
	}

	// return the appropriate error code
	return ErrorCode;
}

⌨️ 快捷键说明

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