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

📄 flash39vf.c

📁 用于在SDT环境下实现交叉环境中代码写入FLASH的过程
💻 C
字号:


#include "flash39vf.h"

UINT32 g_timeOut;

void FlashSectorEraseComm()
{
	
	UINT16 *ptrTemp1,*ptrTemp2;

       /*  Issue the Sector Erase command to 39VF400A  */
	ptrTemp1 = (UINT16 *)(0x01000000+ 2 * 0x00005555); /* set up address to be 0100:5555h  */
	ptrTemp2 = (UINT16 *)(0x01000000+ 2 * 0x00002AAA); /* set up address to be 0100:2AAAh  */  

	*ptrTemp1= 0x0AAAA;
	*ptrTemp2 = 0x05555;                     /* write data 0x5555 to the address */
	*ptrTemp1= 0x08080;                      /* write data 0x8080 to the address */


}


/*************************************************************************/
/*  check whether the operation has finished                             */
/*  input: the address of flash to be checked			 */
/*  output:none			          */
/*************************************************************************/
UINT16 CheckToggleReady(UINT16 *ptrDst)
{   
	UINT16 preData;
	UINT16 currData;

	g_timeOut = 0;
	preData = *ptrDst;
    	preData = preData & 0x4040;
    	
    	while (g_timeOut< 0xffff)
    	{
		currData = *ptrDst;
        	currData = currData & 0x4040;
		g_timeOut ++;
		
        	if (preData == currData)
        		return(1);             // exit the while loop 
   
        	preData = currData;	
    	}
	return(0);
}



/*************************************************************************/
/* erase a sector of flash(2048 WORDS)					 */
/* input:Sector_Address -- the sector address of flash to be erased	 */	
/* output:none								 */
/*************************************************************************/
UINT16 FlashSectorErase (UINT16 *ptrSectorAddress)
{
	UINT16 *ptrTemp1,*ptrTemp2;
	UINT16 ret;
	UINT32 i;

	FlashSectorEraseComm();

       /*  Issue the Sector Erase command to 39VF400A  */
	ptrTemp1 = (UINT16 *)(0x01000000+ 2 * 0x00005555); /* set up address to be 0100:5555h  */
	ptrTemp2 = (UINT16 *)(0x01000000+ 2 * 0x00002AAA); /* set up address to be 0100:2AAAh  */  
	
	
	*ptrTemp1 = 0x0AAAA;                     /* write data 0xAAAA to the address */
	*ptrTemp2 = 0x05555;                     /* write data 0x5555 to the address */

	*ptrSectorAddress = 0x3030;
	
	//for(i = 0;i<100;i++)
	//	g_rdata[i] = * ptrSectorAddress;
	ret = CheckToggleReady(ptrSectorAddress);
	if(g_timeOut < 100)
		for(i = 0; i< 72000;i++);
	return(ret);	

}




/*************************************************************************/
/*  read data from flash			    		         */
/*  input:  Src -- the start address of readdata in flash	         */
/*	    Dst -- address of data be put		                 */
/*	    length -- the length of the data(half_word)	    	         */	
/*  output: 1 -- success						 */
/*          0 -- failure    					         */
/*************************************************************************/
UINT16 FlashRead(UINT16 *ptrSrc, UINT16 *ptrDst,UINT32 readLength)
{
	UINT16 temp1;
	UINT16 temp2;
	UINT16 retry;
	UINT16 i;
	
	UINT16 *ptrTemp1, *ptrTemp2;
	ptrTemp1 = (UINT16 *)(0x01000000+ 2 * 0x00005555); /* set up address to be 0100:5555h  */
	ptrTemp2 = (UINT16 *)(0x01000000+ 2 * 0x00002AAA); /* set up address to be 0100:2AAAh  */

    	*ptrTemp1= 0x0AAAA;                     /* write data 0xAAAA to the address */
    	*ptrTemp2= 0x05555;                     /* write data 0x5555 to the address */
    	*ptrTemp1= 0x0F0F0;                     /* write data 0xF0F0 to the address */

	while(readLength != 0)
	{
		retry = 4;
		again:
			retry --;
			if (retry == 0)
				return(0);

			temp1 = * ptrSrc;
			i = 0;
			temp2 = * ptrSrc;

			if(temp1 == temp2)
			{
				*ptrDst = temp1;
				ptrDst ++; 
				ptrSrc = ptrSrc + 1;
				readLength --;
			}
			else 
				goto again;

	}
	return(1);
}


/************************************************************************************/
/*  check whether the manufactor ID and device ID is right (used when initiazation )*/
/*  input: none						  	                    */
/*  output:1 -- success	                                                            */
/*	   0 -- failure 						            */
/************************************************************************************/
UINT16 CheckFlash39vf()
{
	UINT16 *ptrTemp;
	UINT16 *ptrTemp1, *ptrTemp2;
	UINT16 sstId1;
    	UINT16 sstId2;
    	UINT16 ret;
    	/*  Issue the Software Product ID code to 39VF400A   */

	ptrTemp1 = (UINT16 *)(0x01000000+ 2 * 0x00005555); /* set up address to be 0100:5555h  */
	ptrTemp2 = (UINT16 *)(0x01000000+ 2 * 0x00002AAA); /* set up address to be 0100:2AAAh  */

    	*ptrTemp1= 0x0AAAA;                     /* write data 0xAAAA to the address */
    	*ptrTemp2= 0x05555;                     /* write data 0x5555 to the address */
    	*ptrTemp1= 0x09090;                        /* write data 0x9090 to the address */


    	/* Read the product ID from 39VF400A */
	ptrTemp  = (unsigned short *)0x01000000; /* set up address to be 0100:0000h */
    	sstId1  =  *ptrTemp;              /* get first ID unsigned int               */
	sstId1  =  sstId1 & 0x00FF;     /* mask of higher byte             */

    	ptrTemp = (unsigned short *)0x01000002; /* set up address to be 0100:0001h */
    	sstId2  =  *ptrTemp;             /* get second ID unsigned int              */

    	/* Determine whether there is a SST39VF400A installed or not */
	if ((sstId1 == SST_ID) && (sstId2 ==SST_39VF400A))
        		ret = 1;
    	else
		ret = 0;

	*ptrTemp1 = 0x0AAAA;                        /* write data 0xAAAA to the address  */
    	*ptrTemp2 = 0x05555;                        /* write data 0x5555 to the address  */
	*ptrTemp1 = 0x0F0F0; 	

    	return(ret);
}


/*************************************************************************/
/*  write one half_word to flash			                 */
/*  input: SrcWord -- a half_word to be written			         */
/*	   Dst -- the address of flash        		                 */
/*  output: 1 -- success						 */
/*	    0 -- failure					         */
/*************************************************************************/
UINT16 FlashWriteHalfWord (UINT16 srcWord,  UINT16 *ptrDst)
{
	UINT16 * ptrTemp1, * ptrTemp2;
    	UINT16 rdata,temp;
	UINT16 retry,ret,i;
	retry = 3;

again:
	retry --;
	if (retry == 0)
		return(0);
	
	ptrTemp1 = (UINT16 *)(0x01000000+ 2 * 0x00005555); /* set up address to be 0100:5555h  */
	ptrTemp2 = (UINT16 *)(0x01000000+ 2 * 0x00002AAA); /* set up address to be 0100:2AAAh  */

        *ptrTemp1 = 0x0AAAA;                                       /* write data 0xAAAA to the address */
	*ptrTemp2 = 0x05555;                       		/* write data 0x5555 to the address */
	*ptrTemp1 = 0x0A0A0;                                       /* write data 0xA0A0 to the address    */

    	*ptrDst = srcWord;                       /* transfer the byte to destination */
    	
	ret = CheckToggleReady(ptrDst);             /* wait for TOGGLE bit to get ready */
	if(g_timeOut < 3)
		for(i =0;i<60;i++);
		
	if(ret == 0)
		return(0);
	
	rdata = *ptrDst;
	temp = rdata ^ srcWord;

	if(temp == 0)
		return(1);
	else
		goto again;
}

/*************************************************************************/
/*  write data to flash							 */
/*  input : Dst -- the start address of flash to be written 	         */
/*	    Src -- the address of data to be written	                 */
/*          length --  the length of data to write  (byte)               */
/*  output: 1 -- success					         */
/*          0 -- failure				                 */
/*************************************************************************/
UINT16 FlashWrite( UINT16 *ptrDst,UINT16 *ptrSrc, UINT32 writeLength)
{	
	UINT32 temp;
	UINT16 ret;
	writeLength = (writeLength +1) /2;
    	while( writeLength != 0 )
    	{
	/*if the address is at the begin of a sector, then erase the sector at first*/
		temp = (UINT32)ptrDst & 0x0FFF;
		if(temp == 0)
		{
		ret = FlashSectorErase (ptrDst);
		if(ret == 0)
			return(0);
		}

		ret = FlashWriteHalfWord(*ptrSrc,ptrDst);
		if(ret == 1)
		{   /*add address */
			ptrDst ++;
			ptrSrc ++;
			writeLength --;
		}
		else 
			return(0);
		 
    	}
	return(1);
}	

/*************************************************************************/
/*  check whether the flash written data is right							 */
/*  input : Dst -- the start address of flash to be written 	         */
/*	    Src -- the address of data to be written	                 */
/*          length --  the length of data to write  (byte)               */
/*  output: 1 -- success					         */
/*          0 -- failure				                 */
/*************************************************************************/
UINT16 CheckImage(UINT16 *ptrFlash,UINT16 *ptrRam, UINT32 writeLength)
{
	UINT16 ram_chsum,flash_chsum;
	
	UINT16 temp1,temp2;
	UINT16 retry, i,j;
	UINT16 *ptrTemp1, *ptrTemp2;
	
	writeLength = (writeLength +1) /2;
	ram_chsum = 0x0;
	flash_chsum = 0x0;
    	for(j=0;j<writeLength;j++)
    	{
		ram_chsum = ram_chsum + * ptrRam;
		ptrRam ++;
	}
	
	ptrTemp1 = (UINT16 *)(0x01000000+ 2 * 0x00005555); /* set up address to be 0100:5555h  */
	ptrTemp2 = (UINT16 *)(0x01000000+ 2 * 0x00002AAA); /* set up address to be 0100:2AAAh  */

    	*ptrTemp1= 0x0AAAA;                     /* write data 0xAAAA to the address */
    	*ptrTemp2= 0x05555;                     /* write data 0x5555 to the address */
    	*ptrTemp1= 0x0F0F0;                     /* write data 0xF0F0 to the address */

	for(j=0;j<writeLength;j++)
	{
		retry = 4;
		again:
			retry --;
			if (retry == 0)
				return(0);

			temp1 = * ptrFlash;
			i = 0;
			temp2 = * ptrFlash;

			if(temp1 == temp2)
			{
				flash_chsum = temp1 + flash_chsum;
				ptrFlash = ptrFlash + 1;
			}
			else 
				goto again;

	}
	if(flash_chsum == ram_chsum)
		return(1);
	else
		return(0);
}


int FlashTest()
{
	UINT16 ret;
	UINT16 Src[3072];
	//UINT16 Dst[512];
	UINT16 i;
	UINT16 * temp;

	for(i = 0;i<3072;i++)
		Src[i] = i;

	ret = CheckFlash39vf();
	if(ret == 0)
		return(0);
		
	/*temp = (UINT16 *) 0x107a000;
	ret = FlashSectorErase (temp);
	if(ret == 0)
		return(0);*/

	temp = (UINT16 *)0x107c000;
	ret = FlashWrite(temp,Src,1024);
	if(ret == 0)
		return(0);
		
	temp = (UINT16 *)0x107c400;
	ret = FlashWrite(temp,Src,1024);
	if(ret == 0)
		return(0);
		
	temp = (UINT16 *)0x107c800;
	ret = FlashWrite(temp,Src,1024);
	if(ret == 0)
		return(0);
		
	return(1);
}


int WriteImage()
{
	UINT16 *Dst,*Src;
	UINT32 length;
	int ret;

	ret = CheckFlash39vf();
	if(ret == 0)
		return(0);
		
	Dst = (UINT16 *)0x1000000;
	Src = (UINT16 *)0x20000;
	/*length < 500000,以字节为单位*/
	length = 1000;
	
	//Dst = (UINT16 *)0x107a000;
	//Src = (UINT16 *)0x0;

	ret = FlashWrite(Dst,Src,length);
	if(ret == 0)
		return(0);
	ret = CheckImage(Dst,Src, length);
	if(ret == 0)
		return(0);
	return(1);

}


int Main()
{
	int ret;
	UINT32 * raddr;
	UINT32 i;
	raddr = (UINT32 *)0x3ff5000;
	*raddr = *raddr | 0xff;
	raddr = (UINT32 *)0x3ff5008;	
	
	ret = WriteImage();
	
	if(ret == 0)
	{
		*raddr = *raddr & 0xff0f; 
		return(0);
	}
		
	while(1)
	{
		*raddr = * raddr | 0x10;    
		for(i = 0;i<100000;i++);
		*raddr = * raddr & 0xffef;
		for(i = 0;i<100000;i++);	
	}
	
}

⌨️ 快捷键说明

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