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

📄 main.c

📁 Using the Blackfin Processor SPORT to Emulate a SPI Interface
💻 C
📖 第 1 页 / 共 4 页
字号:
	// Poll the status register to check the Write in Progress bit
	// Sector erase takes time
	ErrorCode = Wait_For_Status(WIP);

	// block erase should be complete
	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 )
{

	
	unsigned long ulWAddr = ulStart; 
	long lWCount = 1, lWriteCount;
	long *pnWriteCount = &lWriteCount;

	int i;
	ERROR_CODE ErrorCode = NO_ERR;

	
    for (i = 0; i < lCount; i++, ulWAddr += lStride)
	{		
		ErrorCode = WriteFlash(ulWAddr, lWCount, lStride, pnData, pnWriteCount);
	}
	
	// return the appropriate error code
	return ErrorCode;
	
}

///////////////////////////////////////////////////////////////////////////////

// ERROR_CODE WriteData()
//
// Write the contents of AFP_Buffer to SPI flash.
//
// Inputs:	unsigned long ulStart - SPI start address 
//			long lCount - number of elements write to the SPI
//			long lStride - address increment to the SPI
//			int *pnData - pointer to data buffer
//
// This function takes the input parameters and calls the function 
// WriteFlash which writes the data of AFP_Buffer to the SPI.
//
// The function WriteFlash will usually be executed multiple times because 
// the SPI needs a break after a SPI page has been filled.
// After each page transfer the addresses and counts need to be recalculated .
///////////////////////////////////////////////////////////////////////////////

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

	unsigned long ulWStart = ulStart; 
	long lWCount = lCount, lWriteCount;
	long *pnWriteCount = &lWriteCount;
	int i;
	
	
	ERROR_CODE ErrorCode = NO_ERR;

	while (lWCount != 0)	
	{
		ErrorCode = WriteFlash(ulWStart, lWCount, lStride, pnData, pnWriteCount);
		
		// After each function call of WriteFlash the counter must be adjusted
		lWCount -= *pnWriteCount;

#ifdef ATMEL_DATAFLASH
		
		ulWStart += ( *pnWriteCount + PAGE_SIZE_DIFF );


#else		
		// Also, both address pointers must be recalculated.
		ulWStart += *pnWriteCount;

#endif		

		pnData += *pnWriteCount/4;
		
	}

	// return the appropriate error code
	return ErrorCode;
	
}

//////////////////////////////////////////////////////////////
// ERROR_CODE ReadData()
//
// Read a value from flash for verify purpose
//
// Inputs:	unsigned long ulStart - holds the SPI start address
//			int pnData - pointer to store value read from flash
//			long lCount - number of elements to read
//////////////////////////////////////////////////////////////

ERROR_CODE ReadData(  unsigned long ulStart, long lCount, long lStride, int *pnData  )
{
	char *cnData;	
	char ShiftValue [3];	

	cnData = (char *)pnData; // Pointer cast to be able to increment byte wise

	// Send the highest byte of the 24 bit address at first
	ShiftValue[0] = ulStart >> 16;
	// Send the middle byte of the 24 bit address or highest in case or 16 bit addresssing at second
	ShiftValue[1] = ulStart >> 8;
	// Send the lowest byte finally.
	ShiftValue[2] = ulStart;
	// After the SPI device address has been placed on the MOSI pin the data can be
	// received on the MISO pin.
	sm_spi_read_multiple_8bits(SPI_READ, (u8* )&ShiftValue, (u8 *)cnData, lCount);

	return NO_ERR;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE WriteFlash()
//
// Write the maximum of n bytes to flash "limited to the SPI page size".
//
// Inputs:	unsigned long StartAddr - address to write to
//			int iDataSource - pointer to the AFP_Buffer
//			long lTransferCount - number of elements to send
//////////////////////////////////////////////////////////////

ERROR_CODE WriteFlash ( unsigned long ulStartAddr, long lTransferCount, long lModify, int *iDataSource, long *lWriteCount )
{	
	long lWTransferCount = 0;
	int i;
	char iData;
	char dummycommand;
	int dummyread;
	ERROR_CODE ErrorCode = NO_ERR;
	char myShiftRegister[3];

// In case of taking Atmel Data flash the routine below must not be executed.
#ifndef ATMEL_DATAFLASH	

	// First, a Write Enable Command must be sent to the SPI.
	SendSingleCommand(SPI_WREN);
	
	// Second, the SPI Status Register will be tested whether the 
	//         Write Enable Bit has been set. 
	ErrorCode = Wait_For_nStatus();

	if( POLL_TIMEOUT == ErrorCode )
		return ErrorCode;
	else	
#endif	
		
	// Third, the address will be shifted out the SPI MOSI bytewise.
	// Fourth, maximum number of n bytes will be taken from the Buffer
	// and sent to the SPI device. "SPI_PAGE_SIZE" can be found in the file "SPI_Device_Settings.h"

	myShiftRegister[0] = ulStartAddr >> 16;
	myShiftRegister[1] = ulStartAddr >> 8;
	myShiftRegister[2] = ulStartAddr;
	if (lTransferCount <= SPI_PAGE_SIZE) {
		sm_spi_write_8bits(SPI_PP, (u8 *) &myShiftRegister[0], (u8 *) iDataSource, lTransferCount, lModify);	
		*lWriteCount = lTransferCount;
	}
	else {
		sm_spi_write_8bits(SPI_PP, (u8 *) &myShiftRegister[0], (u8 *) iDataSource, SPI_PAGE_SIZE, lModify);
		*lWriteCount = SPI_PAGE_SIZE;
	}    
	//dummycommand = *((char *)iDataSource);
	//((char *)iDataSource)++;
	//sm_spi_write_8bits(dummycommand, (u8* )iDataSource, lTransferCount-1);
	// Sixth, the SPI Write in Progress Bit must be toggled to ensure the 
	// programming is done before start of next transfer.
	
#ifdef ATMEL_DATAFLASH
	ErrorCode = Wait_For_nStatus(WIP);	
#else
	ErrorCode = Wait_For_Status(WIP);
#endif
		
	if( POLL_TIMEOUT == ErrorCode )
		return ErrorCode;
	else
	
	return ErrorCode;
}


//////////////////////////////////////////////////////////////
// ERROR_CODE GetSectorNumber()
//
// Gets a sector number based on the offset.
//
// In the flash programmer plug-in a sector can be chosen
// and than be erased.
//////////////////////////////////////////////////////////////

ERROR_CODE GetSectorNumber( unsigned long ulOffset, int *pnSector )
{
	int nSector = 0;
	int i=0;
	ERROR_CODE ErrorCode = NO_ERR;

	// This function makes use of the settings in the file "SPI_Device_Settings.h".

	// determine the sector
	
	if(ulOffset > SPI_SECTOR_SIZE * SPI_SECTORS - 1)
	{
		ErrorCode = INVALID_SECTOR;
		return ErrorCode;
	}
	
	
	for (i=SPI_SECTORS-1;i>=0;i--) {
		if (ulOffset >= SECTORBASE + i * SPI_SECTOR_SIZE) {
			nSector = i;
			break;
		}
	}
					
	*pnSector = nSector;
	
	// ok
	return ErrorCode;
}



//////////////////////////////////////////////////////////////
// Wait_For_nStatus(void)
//
// Polls the WEL (Write Enable Latch) bit of the Flash's status
// register.
// Inputs - none
// returns- none
//
//////////////////////////////////////////////////////////////

ERROR_CODE Wait_For_nStatus(void)
{
	int n, i;
	char status_register = 0;
	ERROR_CODE ErrorCode = NO_ERR;	// tells us if there was an error erasing flash
	
		for(i = 0; i < 500; i++)
		{
			status_register = ReadStatusRegister();
			if( (status_register & WEL) )
			{
				ErrorCode = NO_ERR;
				return ErrorCode;
			}
			ErrorCode = POLL_TIMEOUT;	// Time out error
		};
							
	return ErrorCode;
}
//////////////////////////////////////////////////////////////
// Wait_For_Status(void)
//
// Polls the Status Register of the Flash's status
// register until the Flash is finished with its access. Accesses
// that are affected by a latency are Page_Program, Sector_Erase,
// and Block_Erase.
// Inputs - Statusbit
// returns- none
//
//////////////////////////////////////////////////////////////

ERROR_CODE Wait_For_Status( char Statusbit )
{
	int n, i;
	char status_register = 0xFF;
	ERROR_CODE ErrorCode = NO_ERR;	// tells us if there was an error erasing flash
	
		for(i = 0; i < 350000; i++) // polls for more than 3 seconds
		{
			status_register = ReadStatusRegister();
			if( !(status_register & Statusbit) )
			{
				ErrorCode = NO_ERR;	// tells us if there was an error erasing flash
				break;
			}

			ErrorCode = POLL_TIMEOUT;	// Time out error
		};
		
	
	return ErrorCode;
	
}

//////////////////////////////////////////////////////////////
// int ReadStatusRegister(void)
//
// Returns the 8-bit value of the status register.
// Inputs - none
// returns- second location of status_register[2],
//         first location is garbage.
// Core sends the command
//
//////////////////////////////////////////////////////////////
char ReadStatusRegister(void)
{
	static char data;
	sm_spi_read_8bits(SPI_RDSR, (u8 *)&data, sizeof(data));
	return data;
}

//Juan's code
void SectorErase(u8 opcode, u8* addr)
{
	static u8 opcode_addr;
	sm_sport_desc_t *spi_se;	

⌨️ 快捷键说明

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