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

📄 flashmanager.c

📁 flash驱动和简单管理程序
💻 C
📖 第 1 页 / 共 4 页
字号:
	 else if( (ulOffset >= 0x208000) && ( ulOffset < 0x210000 ) )
	 {
	  nSector = 35;
	 }
	 else if( (ulOffset >= 0x280000) && ( ulOffset < 0x284000 ) )
	 {
	  nSector = 36;
	 }
	 else if( (ulOffset >= 0x284000) && ( ulOffset < 0x286000 ) )
	 {
	  nSector = 37;
	 }
	 else if( (ulOffset >= 0x286000) && ( ulOffset < 0x288000 ) )
	 {
      nSector = 38;
	 }
	 else if( (ulOffset >= 0x288000) && ( ulOffset < 0x290000 ) )
	 {
	  nSector = 35;
	 }
	 else
	 {
	  return INVALID_SECTOR;
     }
	}
	else
	{
	 nSector = ulOffset & 0xffff0000;
	 nSector = ulOffset >> 16;
	 nSector = nSector & 0x000ff;	
	}
	// if it is a valid sector, set it
	if ( (nSector >= 0) && (nSector <BF533_FLASH_SECTOR_NUM) )
		*pnSector = nSector;
	// else it is an invalid sector
	else
		return INVALID_SECTOR;

	// ok
	return NO_ERR;
}

//----------------------------------------------------------//
// ERROR_CODE EraseFlashBlock()                             //
//                                                          //
// Purpose:Erase blacks in flash.                           //
//                                                          //
//----------------------------------------------------------//
ERROR_CODE EraseFlashBlock(int nBlock)
{
	unsigned long ulSectorOff = 0x0;
	ERROR_CODE ErrorCode = NO_ERR;	// tells us if there was an error erasing flash

	// if the block is invalid just return
	if ((nBlock <0)||(nBlock>BF533_FLASH_SECTOR_NUM))
	{
	 LedIndicator(INVALID_BLOCK);
	 return INVALID_BLOCK;
	}
	// figure out the offset of the block in flash
	if ((nBlock>=0)&&(nBlock<32))
	{
	 ulSectorOff =(nBlock*BF533_FLASH_SECTOR_SIZE);
	}
	else if( nBlock == 32 )
	{
	 ulSectorOff = 0x200000;
	}
	else if( nBlock == 33 )
	{
	 ulSectorOff = 0x204000;
	}
	else if( nBlock == 34 )
	{
	 ulSectorOff = 0x206000;
	}
	else if( nBlock == 35 )
	{
	 ulSectorOff = 0x208000;
	}
	else if( nBlock == 36 )
	{
	 ulSectorOff = 0x280000;
	}
	else if( nBlock == 37 )
	{
	 ulSectorOff = 0x284000;
	}
	else if( nBlock == 38 )
	{
	 ulSectorOff = 0x286000;
	}
	else if( nBlock == 39 )
	{
	 ulSectorOff = 0x288000;
	}
	// send the erase block command to the flash
	WriteFlash( (0x0AAA | ulSectorOff), 0xaa );
	WriteFlash( (0x0554 | ulSectorOff), 0x55 );
	WriteFlash( (0x0AAA | ulSectorOff), 0x80 );
	WriteFlash( (0x0AAA | ulSectorOff), 0xaa );
	WriteFlash( (0x0554 | ulSectorOff), 0x55 );
	// the last write has to be at an address in the block
	WriteFlash(ulSectorOff,0x30);
	// poll until the command has completed
	ErrorCode=PollToggleBit(ulSectorOff);
    LedIndicator(ErrorCode);
	// block erase should be complete
	return ErrorCode;
}


//----------------------------------------------------------//
// ERROR_CODE EraseBulkFlash()                              //
//                                                          //
// Purpose:Sends an "erase all" command to the flash.       //
//                                                          //
//----------------------------------------------------------//
ERROR_CODE EraseBulkFlash(unsigned long ulOffset)
{
// FLASH A                                                                  //
// 		Type			START ADDR    	END ADDR		SECTOR NUM          //
// 		-------			----------		--------		----------          //
// 		Main Flash		0x20000000		0x200FFFFF		00 - 15             //
// 		Boot Flash		0x20200000		0x2020FFFF		32 - 35             //
// FLASH B                                                                  //
// 		Main Flash		0x20100000		0x201FFFFF		16 - 31             //
// 		Boot Flash		0x20280000		0x2028FFFF		36 - 39             //
   int WhichPartToErase;
   if(ulOffset<0x100000)
   {
	WhichPartToErase=0;//Modified:EricRao
   }
   else if(ulOffset>=0x100000&&ulOffset<0x200000)
   {
	WhichPartToErase=2;//Modified:EricRao
   }
   else if(ulOffset>=0x200000&&ulOffset<0x210000)
   {
	WhichPartToErase=1;//Modified:EricRao
   }
   else if(ulOffset>=0x280000&&ulOffset<0x290000)
   {
    WhichPartToErase=3;//Modified:EricRao
   }
   else
   {
    LedIndicator(INVALID_SECTOR);
    return INVALID_SECTOR;
   }
   ERROR_CODE ErrorCode = NO_ERR;	// tells us if there was an error erasing flash
   switch (WhichPartToErase)
   {
	 case 0://ERASE_FLASH_A_MAIN
	    //Flash A   
	    // erase contents in Main Flash Array
    	WriteFlash( 0x0AAA, 0xaa );
    	WriteFlash( 0x0554, 0x55 );
    	WriteFlash( 0x0AAA, 0x80 );
    	WriteFlash( 0x0AAA, 0xaa );
    	WriteFlash( 0x0554, 0x55 );
    	WriteFlash( 0x0AAA, 0x10 );
    	// poll until the command has completed
     	ErrorCode = PollToggleBit(0x0000);
     	break;
	 case 1://ERASE_FLASH_A_BOOT
	 	//Flash A
	    // erase contents in Boot Flash Array
		WriteFlash( 0x200AAA, 0xaa );
		WriteFlash( 0x200554, 0x55 );
		WriteFlash( 0x200AAA, 0x80 );
		WriteFlash( 0x200AAA, 0xaa );
		WriteFlash( 0x200554, 0x55 );
		WriteFlash( 0x200AAA, 0x10 );
		// poll until the command has completed
		ErrorCode = PollToggleBit(0x200000);
		break;
     case 2://ERASE_FLASH_B_MAIN
     	//Flash B
     	// erase contents in Main Flash Array
		WriteFlash( 0x100AAA, 0xaa );
		WriteFlash( 0x100554, 0x55 );
		WriteFlash( 0x100AAA, 0x80 );
		WriteFlash( 0x100AAA, 0xaa );
		WriteFlash( 0x100554, 0x55 );
		WriteFlash( 0x100AAA, 0x10 );
		// poll until the command has completed
		ErrorCode = PollToggleBit(0x100000);
	    break;
     case 3://ERASE_FLASH_B_BOOT
     	//Flash B
     	// erase contents in Boot Flash Array
		WriteFlash( 0x280AAA, 0xaa );
		WriteFlash( 0x280554, 0x55 );
		WriteFlash( 0x280AAA, 0x80 );
		WriteFlash( 0x280AAA, 0xaa );
		WriteFlash( 0x280554, 0x55 );
		WriteFlash( 0x280AAA, 0x10 );
		// poll until the command has completed
		ErrorCode = PollToggleBit(0x280000);
        break;
    default:
    	LedIndicator(INVALID_SECTOR);
        return INVALID_SECTOR;
  } 
    	// erase should be complete
    	LedIndicator(ErrorCode);
    	return ErrorCode;
}

//----------------------------------------------------------//
// ERROR_CODE EraseFlashs()                                  //
//                                                          //
// Purpose:Erase both flash A and B completely.             //
//                                                          //
//----------------------------------------------------------//

ERROR_CODE EraseFlashs()
{
	ERROR_CODE ErrorCode = NO_ERR;	// tells us if there was an error erasing flash

	// *****Flash A*****

	// erase contents in Main Flash Array
	WriteFlash( 0x0AAA, 0xaa );
	WriteFlash( 0x0554, 0x55 );
	WriteFlash( 0x0AAA, 0x80 );
	WriteFlash( 0x0AAA, 0xaa );
	WriteFlash( 0x0554, 0x55 );
	WriteFlash( 0x0AAA, 0x10 );

	// poll until the command has completed
	ErrorCode = PollToggleBit(0x0000);

	// only erase if we didn't fail the previous erase
	if( ErrorCode == NO_ERR )
	{
		// erase contents in Boot Flash Array
		WriteFlash( 0x200AAA, 0xaa );
		WriteFlash( 0x200554, 0x55 );
		WriteFlash( 0x200AAA, 0x80 );
		WriteFlash( 0x200AAA, 0xaa );
		WriteFlash( 0x200554, 0x55 );
		WriteFlash( 0x200AAA, 0x10 );

		// poll until the command has completed
		ErrorCode = PollToggleBit(0x200000);
	}

	// *****Flash B*****

	// only erase if we didn't fail the previous erase
	if( ErrorCode == NO_ERR )
	{
		// erase contents in Main Flash Array
		WriteFlash( 0x100AAA, 0xaa );
		WriteFlash( 0x100554, 0x55 );
		WriteFlash( 0x100AAA, 0x80 );
		WriteFlash( 0x100AAA, 0xaa );
		WriteFlash( 0x100554, 0x55 );
		WriteFlash( 0x100AAA, 0x10 );

		// poll until the command has completed
		ErrorCode = PollToggleBit(0x100000);
	}

	// only erase if we didn't fail the previous erase
	if( ErrorCode == NO_ERR )
	{
		// erase contents in Boot Flash Array
		WriteFlash( 0x280AAA, 0xaa );
		WriteFlash( 0x280554, 0x55 );
		WriteFlash( 0x280AAA, 0x80 );
		WriteFlash( 0x280AAA, 0xaa );
		WriteFlash( 0x280554, 0x55 );
		WriteFlash( 0x280AAA, 0x10 );

		// poll until the command has completed
		ErrorCode = PollToggleBit(0x280000);
	}

	// erase should be complete
	return ErrorCode;
}

//------------------------------------------------------------------------------//
// Function:  dmaMoveData					                                    //						
//																	    		//
// Purpose:To move data from SRAM to Flash by DMA method                     //                                							    	//
//                                                                              //
//                                                                              //
// Returns:             							                           	//
//------------------------------------------------------------------------------//
ERROR_CODE MoveData(unsigned long Source_Base_Address,unsigned long Destination_Base_Address,unsigned long nCount)
{
 
 unsigned long dma_modify=0x4;
 unsigned long dma_wakeup=MDMA0_WAKEUP;
 //Set up memory DMA
 MEM_DMA_Setup(dma_wakeup,dma_modify);
 //Put arg3,arg4 into R3,R4
 asm("R3=0x00000009;");//Source_Config
 asm("R4=0x0000008B;");//Destination_Config
 //Start up memory DMA
 MEM_DMA(Source_Base_Address,Destination_Base_Address,nCount);	
 return NO_ERR; 
}

//-------------------------------------------------------------------------//
// Function:ERROR_CODE WriteData()                                         //
//                                                                         //
// Purpose: Write a buffer to flash.                                       //
//                                                                         //
// Params:	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)
{
 int i,j;
 int wCnt=0;//times counter
 ERROR_CODE ErrorCode=NO_ERR;
 unsigned long ulOffset=ulStart;
 int iShift=0;
 int iNumWords=2;
 int lLength=TWO_BYTES;//Long length
 int nSector=0;
 // write the buffer up to BUFFER_SIZE items
 for (i=0;i<lCount;i++)
 {
  for(iShift=0,j=0;((j<iNumWords)&&(ErrorCode==NO_ERR));j++,ulOffset+=(lStride*iNumWords))
  {
   //Check to see whether it is a valid sector
   ErrorCode=SectorValityCheck(ulStart,lCount,lStride);
   if(ErrorCode!=NO_ERR)
   {
    return ErrorCode;	
   }
   // check to see that the address is within a valid sector
   ErrorCode = GetSectorNumber(ulOffset, &nSector );
   if(ErrorCode==NO_ERR)
   {
	//unlock the flash, do the write, increase shift, and wait for completion
    enProgramFlash(ulOffset);
    WriteFlash(ulOffset,(pnData[i]>>iShift));
    ErrorCode = PollToggleBit(ulOffset);
    iShift+=16;			
   }
   else
   {
   	LedIndicator(ErrorCode);
	return ErrorCode;
   }	
  }
 } 
  LedIndicator(ErrorCode);
  return ErrorCode;	
}

⌨️ 快捷键说明

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