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

📄 flash16.c.bak

📁 coldfire5307的bootloader 将程序写入flash
💻 BAK
字号:
#include "mcf5307.h" 


#define FLASH_555H 	 0x555
#define FLASH_2AAH  	 0x2AA


#define CHIP_PROTECT     
#define CHIP_UNPROTECT

#define INTEL_FLASH

#define bool unsigned char
#define TRUE   1
#define True   TRUE
#define FALSE  0
#define False  FALSE

#define BlockSize 32768   // for intel TE28F800C3T , it's 32 K words at low address !

void Unlock(void);
void Lock(void);
bool VerifyBlank(long lBaseAddress,long Size);
bool WriteTest(long lBaseAddress);


bool sChipErase( long lBaseAddress );
bool sSectorErase( long lBaseAddress, long lSectorAddress );
bool sSectorUnlock( long lBaseAddress, long lSectorAddress );

bool sWriteWord( long lBaseAddress, long lAddress, short sData );
bool sVerifyEnd( volatile short *spAddress, short sVerifyData );
short SelChipManufacturerID( long lBaseAddress );
short SelChipDeviceID( long lBaseAddress );
unsigned short GetFlashStatus( long lBaseAddress );
bool WriteBlockData(int lBaseAddress, int BlockStartAddress, volatile unsigned short * SrcAddress, int HowmanyWords);	
//void Delay(unsigned int de);

unsigned char Sta[100000];


unsigned char BootImg[]={
			0
			};

unsigned char LinuxImg[]={
			0
			};

unsigned char StartAdd[]={0x00,0x30,0x00,0x00,0xff,0x01,0x00,0x00};

/*
void Delay(unsigned int de)
{
	unsigned int i,j;
	for(i = 500; i > 0; i--)
		for(j = de; j > 0; j--);
}
*/
bool SelFlashChip( long lBaseAddress )
{
	short m_ID,d_ID;
	bool FlashOp;
		
	d_ID = SelChipDeviceID( lBaseAddress );
	m_ID = SelChipManufacturerID( lBaseAddress );
	
  return 1;		
	
}


void Flash_Test()
{
  unsigned short m_ID,d_ID;
  unsigned short *ResultP;
  long baseCS0 = 0xFf000000;
  unsigned short *Protect=(unsigned short *)0x22800000;
  

  (*Protect)=0xffff;


  //while(1)
  //read  ChipDeviceID and ChipManufacturerID
  {
  
  	d_ID = SelChipDeviceID( baseCS0 );
  	if(d_ID!=0x22D7)
  		while(1)
  			{
      	  d_ID++;
  			}
  	m_ID = SelChipManufacturerID( baseCS0 );
  	if(m_ID!=0x1)
  		while(1)
  			{
       	 m_ID++;
  			}
  }

  m_ID=WriteBlockData(0xff000000, 0x00000/2, (unsigned short *)StartAdd,(4+sizeof(StartAdd)/2));
  
  m_ID=WriteBlockData(0xff000000,0x00010000/2,(unsigned short *)BootImg,(4+sizeof(BootImg)/2));
  
  m_ID=WriteBlockData(0xff000000,0x00020000/2,(unsigned short *)LinuxImg,(4+sizeof(LinuxImg)/2));

  //erase Flash Memory 1
  CHIP_UNPROTECT;
  
  //if ( !SelChipErase( 0xFe000000 ) )
  //   return;

  //read  ChipDeviceID and ChipManufacturerID
  d_ID = SelChipDeviceID( baseCS0 );
  m_ID = SelChipManufacturerID( baseCS0 );
  
  //  if ( !SelSectorErase(baseCS0,0) )
  //     return;    


  while(1)
  {
  	if ( !SelSectorErase(baseCS0,0) )
       return; 
  	SelWriteWord( baseCS0,    0x0000, 0x0000 );  
  	ResultP=(unsigned short *)baseCS0;
  	ResultP+=0x0000;
  	m_ID=*resultP;
  	if(m_ID!=0x0)
  	{
  		while(1)
  		{
       		m_ID++;
  		}
  	}

  	if ( !sSectorErase(baseCS0,0x10000) )
       return; 
  	SelWriteWord( baseCS0,    0x10000, 0x1111 ); 
  	ResultP=(unsigned short *)baseCS0;
  	ResultP+=0x10000;
  	m_ID=*resultP;
  	if(m_ID!=0x1111)
  	{
  		while(1)
  		{
        m_ID++;
  		}
  	}

  	if ( !sSectorErase(baseCS0,0x20000) )
       return; 
  	SelWriteWord( baseCS0,    0x20000, 0x2222 );  
  	ResultP=(unsigned short *)baseCS0;
  	ResultP+=0x20000;
  	m_ID=*resultP;
  	if(m_ID!=0x2222)
  	{
  		while(1)
  		{
        m_ID++;
  		}
  	}
  
  	if ( !sSectorErase(baseCS0,0x30000) )
       return; 
  	SelsWriteWord( baseCS0,    0x30000, 0x3333 );  
  	ResultP=(unsigned short *)baseCS0;
  	ResultP+=0x30000;
  	m_ID=*resultP;
  	if(m_ID!=0x3333)
  	{
  		while(1)
  		{
        m_ID++;
  		}
  	}
  
  	if ( !sSectorErase(baseCS0,0x40000) )
       return;   
  	SelWriteWord( baseCS0,    0x40000, 0x4444 );  
  	ResultP=(unsigned short *)baseCS0;
  	ResultP+=0x40000;
  	m_ID=*resultP;
  	if(m_ID!=0x4444)
  		while(1)
  		{
  	   	m_ID++;
  		}

  	if ( !sSectorErase(baseCS0,0x50000) )
       return; 
  	SelWriteWord( baseCS0,    0x50000, 0x5555 );  
  	ResultP=(unsigned short *)baseCS0;
  	ResultP+=0x50000;
  	m_ID=*resultP;
  	if(m_ID!=0x5555)
  	{
  		while(1)
  			{
        	m_ID++;
  			}
  	}
  	
  	if ( !sSectorErase(baseCS0,0x3f8000) )
       return; 
  	SelWriteWord( baseCS0,    0x3f8000, 0xABCD );  
  	ResultP=(unsigned short *)baseCS0;
  	ResultP+=0x3f8000;
  	m_ID=*resultP;
  	if(m_ID!=0xABCD)
  	{
  		while(1)
  		{
       	m_ID++;
  		}
  	}
  
  }
  
  CHIP_PROTECT;
}


bool SelChipErase( long lBaseAddress )
{
  volatile short *sp;
  bool retval;
  sp = (short*)lBaseAddress;

  *( sp + FLASH_555H ) = 0xAA;
  *( sp + FLASH_2AAH ) = 0x55;
  *( sp + FLASH_555H ) = 0x80;
  *( sp + FLASH_555H ) = 0xAA;
  *( sp + FLASH_2AAH ) = 0x55;
  *( sp + FLASH_555H ) = 0x10;

  retval = SelVerifyEnd( sp, 0x80 );   //if operation is finished, DQ7 = 1
  return retval;
}

/************************************************************************
Name:     SelSectorErase
Function: erase data of a sector
input:    lBaseAddress, the base address of Flash memory to be erased
          lSectorAddress, the base address the sector to be erased
return:   True: erase is successful; False: erase is fail
*************************************************************************/

bool SelSectorErase( long lBaseAddress, long lSectorAddress )
{
  volatile short *sp;
  bool retval;
  unsigned short ID;
  sp = (short*)lBaseAddress;

  #ifndef INTEL_FLASH


	*( sp + FLASH_555H ) = 0xAA;
	*( sp + FLASH_2AAH ) = 0x55;
  *( sp + FLASH_555H ) = 0x80;
  *( sp + FLASH_555H ) = 0xAA;
  *( sp + FLASH_2AAH ) = 0x55;
  *( sp + lSectorAddress ) = 0x30;   //??? lSectorAddress = 0x3000

  retval = sVerifyEnd( sp+lSectorAddress, 0x80 );   //if operation is finished, DQ7 = 1
  return retval;

  #else

	Unlock();
 
 	*sp=0x50;								//clear flag
  *sp=0x60;								//set config
  *( sp + lSectorAddress ) = 0xd0;		//unlock it!
  *sp=0x90;								//read config
  ID=*(sp+lSectorAddress+2);								//is unlock ?
  while(0x00!=(ID&0x01))
 	{
		ID=*(sp+lSectorAddress+2);		
  }

  *sp=0x50;							  //clear flag	
  *( sp + lSectorAddress ) = 0x20;  	//setup erase
  *( sp + lSectorAddress ) = 0xd0;   //confirm it! 

	ID=*sp;								//ready ?
  while(0x00==(ID&0x80))
 	{
  	 ID=*sp;		
 	}

  ID=*(sp);							 //all ok?
  if(0x00==(ID&0x3a))
 	{
   	*sp=0xff;						//set it in read mode
   	return True;
 	}
  else
 	{
   	*sp=0xff;						
   	return False;
 	}

  #endif

}


bool SelSectorUnlock( long lBaseAddress, long lSectorAddress )
{
  volatile short *sp;
  bool retval;
  unsigned short ID;
  int i=0;
  sp = (short*)lBaseAddress;

  #ifndef INTEL_FLASH
	  return 0x01;
  #else
	Unlock();

	*sp=0x50;								//clear flag
  *sp=0x60;								//set config
  *( sp + lSectorAddress ) = 0xd0;		//unlock it!
  *sp=0x90;								//read config
  ID=*(sp+lSectorAddress+2);								//is unlock ?
  i=0;
  while(0x00!=(ID&0x01))
 	{
  	 ID=*(sp+lSectorAddress+2);
   	 i++;
   	 if(i>10000)						//over time ?
 	 	return False;
 	}

  *sp=0x50;							  //clear flag	
 	*sp=0xff;						
 	return True;
   
  #endif
 	
}

/***************************************************************************
Name:    sVerifyEnd
Function: Verify if the operation to Flash mem is finished
input:    spAddress: the address of Flash mem to be verified
	  sVerifyData: Verify Data
return:   True: writing passed;     False: writing is fail	        
***************************************************************************/

bool SelVerifyEnd( volatile short *spAddress, short sVerifyData )
{
  unsigned short sb;
  unsigned  i;
  sb = * spAddress;

  #ifndef INTEL_FLASH

  while( (sb & 0x80 ) != ( sVerifyData & 0x80 ) )
  {
    if( (sb & 0x20 ) == 0x20 )            //if DQ5 == 1, exceed timing limits
    {
      sb = *spAddress;                    //read data again
      if( (sb & 0x80) != (sVerifyData & 0x80) )   //if DQ7!=Data, writing is failure
				return False;
      else
        return True;
    }
    sb = *spAddress;                      //read data again
  }
  	return True;
	#else

  sb=GetFlashStatus((long)spAddress);
  while( (sb & 0x80 ) != 0x80 )       //  1 means ready
  {
    if( 0x00!= ((sb)&sVerifyData) )  // 0 means ok!
			return False;
    else
      return True;		
    sb=GetFlashStatus((long) spAddress); 
 	}

  if( 0x00!= ((sb)&sVerifyData) )  // 0 means ok!
			return False;
  else
      return True;		
       
	#endif

}

/*****************************************************************************
Name:     SelWriteWord
Function: Write a word to Flash memory
input:    lBaseAddress: The first address of Flash memory
	  lAddress: Address of the memory to be written
	  sData: Data to be written
return:   True: writing is successful;  False: writing is failure
*****************************************************************************/

bool SelWriteWord( long lBaseAddress, long lAddress, short sData )
{
  volatile short *sp;
  bool retval;
  unsigned short ID;

  sp = (short*)lBaseAddress;
  #ifndef INTEL_FLASH
  	*( sp + FLASH_555H ) = 0xAA;
    *( sp + FLASH_2AAH ) = 0x55;
    *( sp + FLASH_555H ) = 0xA0;
    sp += lAddress;
    *sp = sData;
    retval = sVerifyEnd( sp, sData );
    return retval;
  #else
 		Unlock();
    *sp=0x50;													 //clear flag
    *sp=0x40;												    //program command 
	  *( sp + lAddress ) = (unsigned short)(sData);		//send data
    while( 0x80!=(0x80&(*sp)) )							//wait ready
		{	;
		}
		
		ID=*(sp);							 //all ok?
  	if(0x00==(ID&0x1a))
    {
    	*sp=0xff;						//set it in read mode
    	return True;
    }
    else
    {
    	*sp=0xff;						
    	return False;
    }
	
	#endif

}


short SelChipManufacturerID( long lBaseAddress )
{

	#ifndef INTEL_FLASH
	  volatile short *sp, M_ID;
  	sp = (short*)lBaseAddress;
		*( sp + FLASH_555H ) = 0xAA;
  	*( sp + FLASH_2AAH ) = 0x55;
  	*( sp + FLASH_555H ) = 0x90;
  	M_ID = * sp ;
  	*sp=0xf0;
  	return M_ID;
	
	#else
		volatile short *sp,M_ID;
  	sp = (short*)lBaseAddress; 
  	*sp=0x90;				// first cycle, 
		M_ID = *(sp+0x0);
  	*sp=0xff;
    return M_ID;  		//intel should be 0x0089
 	#endif
  
}

short SelChipDeviceID( long lBaseAddress )
{
  volatile short *sp,D_ID;
  sp = (short*)lBaseAddress;

	#ifndef INTEL_FLASH
	  *( sp + FLASH_555H ) = 0xAA;
  	*( sp + FLASH_2AAH ) = 0x55;
  	*( sp + FLASH_555H ) = 0x90;
  	D_ID = * ( sp + 0x01 );
  	*sp=0xf0;
  	return D_ID;
	
	#else
	  sp = (short*)lBaseAddress; 
		*sp=0x90;				// first cycle, 
		D_ID = *(sp+0x1);
  	*sp=0xff;
  	return D_ID;  		//
	
	#endif
  
}


unsigned short GetFlashStatus( long lBaseAddress )
{
	#ifdef INTEL_FLASH
		volatile unsigned short * sp;
		unsigned short Status;
		sp = (unsigned short *)lBaseAddress;
		*sp=0x70;
		Status=*sp;
		return (Status);

	#endif
}

void Unlock(void)
{
//	unsigned char * p;
//	p=(unsigned char *)0x33400000;
//	*p=0x20;
 	
}
void Lock(void)
{
//	unsigned char * p;
//	p=(unsigned char *)0x33400000;
//	*p=0x00;
}

bool VerifyBlank(long lBaseAddress,long Size)
{
	unsigned short int Blank;
	volatile unsigned short int *BlankPtr;
 	long Temp;
 
 	Temp=0;
 	BlankPtr=(unsigned short int *)lBaseAddress;
 	while(Temp<Size)
 	{
  	Blank=*BlankPtr;
  	BlankPtr++;
  	Temp++;
  	if(Blank!=0xffff)  return FALSE;
 	}
 	
 return TRUE;
  	
}

bool WriteTest(long lBaseAddress)
{
 volatile unsigned  short  int *Ptr;
 
 if(sSectorErase(lBaseAddress,0x1f8000)==0x00)           //erase the bottom
    return 0x00;
 
 if(sWriteWord(lBaseAddress,0x1f8000,0x1234)==0x00)    // write the bottom  
    return 0x00;
    
 Ptr=(unsigned  short  int *)(lBaseAddress+2*0x1f8000);

 if( (*Ptr)!=0x1234  )                                  //read th result 
    return 0x00;    
 else
    return 0x01;
    
 
 if(sSectorErase(lBaseAddress,0x0)==0x00)         //erase the top
    return 0x00;
 
 if(sWriteWord(lBaseAddress,0x0,0x1234)==0x00)    // write the top  
    return 0x00;
    
 Ptr=(unsigned  short  int *)(lBaseAddress+2*0x0);

 if( (*Ptr)!=0x1234  )                                  //read th result 
    return 0x00;    
 else
    return 0x01;   
    	
}

//for example 
// 0xfe000000
// 0x00010000
// 0x2000
//macro BlockSize needed!
// return 0x01 if ok
bool WriteBlockData(int lBaseAddress, int BlockStartAddress, volatile unsigned short * SrcAddress, int HowmanyWords)	
{

 int BlockIndex;
 int SrcCheckSum;
 int DestCheckSum;
 unsigned short Temp;

 volatile unsigned short * Src;
 volatile unsigned short * Dest;
 
 
 int i;

 BlockIndex=0;


 Src=(unsigned short *)SrcAddress;
 Dest=(unsigned short *)(lBaseAddress+2*BlockStartAddress);

 //erase the first sector
 if( False==sSectorErase(lBaseAddress,BlockStartAddress+BlockSize*BlockIndex) )
 	return False;
 
 for(i=0;i<HowmanyWords;)
 	{
     Temp=*SrcAddress;
     
     //Temp=(unsigned short)(0xffff&(unsigned short)SrcAddress);
     	
     if(False==sWriteWord(lBaseAddress,BlockStartAddress+BlockSize*BlockIndex+(i%BlockSize),Temp) )
     	return False;
     
     SrcAddress++;
     i++;
     
     if( (0x0==(i%BlockSize)) && (i!=HowmanyWords))
     	{
     	BlockIndex++;
     	if( False==sSectorErase(lBaseAddress,BlockStartAddress+BlockSize*BlockIndex) )
		 	return False;
     	}
 	
 	}


 //now we will check it!

 
 for(i=0;i<HowmanyWords;i++)
 	{
 	
 	
	if(*Src!=*Dest)
		return False;
	
    /*
	if( *Dest!=((unsigned short)(0xffff&(unsigned short)Dest)) )
		return False;
    */
    
	
	Src++;
	Dest++;
 	}

 return True;	


}

⌨️ 快捷键说明

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