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

📄 flashutils.c

📁 这是ARM在Vxworks的驱动源代码
💻 C
字号:
/* flashUtils.c - embedded flash burning routine *//* Copyright 2002 Wind River Systems, Inc. *//*modification history--------------------01a,05jun02,jb  initial version...*//*DESCRIPTIONThis library provides flash interface routinesTo include flashUtils in non compressed poriton of image. BOOT_EXTRA =  flashUtils.oThis includes the flash program into the uncompressed portion of the image, and can then be called directly from the ICE.INCLUDE FILES: noneSEE ALSO:*/#include <stdio.h>#if 1#define DEBUG_LOG(a)  \	{	\		printf(a);	\	} #define DEBUG_LOG2(a,b) \	{	\		printf(a,b);	\	} #define DEBUG_LOG3(a,b,c) \	{	\		printf(a,b,c);	\	} #define DEBUG_LOG4(a,b,c,d) 	\	{	\		printf(a,b,c,d);	\	} #define DEBUG_LOG5(a,b,c,d,e) 	\	{	\		printf(a,b,c,d,e);	\	} #else#define DEBUG_LOG(a) #define DEBUG_LOG2(a,b) #define DEBUG_LOG3(a,b,c) #define DEBUG_LOG4(a,b,c,d) #define DEBUG_LOG5(a,b,c,d,e) #endif#define WAIT_CNT 5000000#define SECTOR_SIZE ( 128 * 1024)#define RETRY_CNT 100/* Function prototypes */int programFlashLoad(unsigned char* src, char* dest, int len);int programFlash(){ int rc; unsigned volatile long *ptr;#if 0    ptr = (unsigned long *) 0xc4000004; /*cs1*/    *ptr = 0xbfff3c02;    ptr = (unsigned long *) 0xc4000008; /*cs2*/    *ptr = 0xbfff3c02;#endif     rc =  programFlashLoad((unsigned char*)0x0, (char*)0x51000000,0x100000);	return 0;   }intprogramFlashLoad(unsigned char* src, char* dest, int len){    volatile short  * volatile address=(short *)dest;     volatile short *verPtr=(short *)dest;    short* originalSrc=(short *)src;        int i,retry,wait,x,y;    int currSectorNum=0;    int sectorSize=SECTOR_SIZE;    address=(short *)dest;    *address=0xff; /*Set flash to read state*/    DEBUG_LOG("Unlocking sectors\n");    do{	/*override sector size, wujianliang*/	if(address<0x51020000)		sectorSize = 32*1024;	else		sectorSize = 128*1024;	/**/	/*unlock sector*/	*address=0x60; /*unlock*/	*address=0xD0; /*confirm*/	wait=WAIT_CNT;  	while((( (*address) & 0x80)!=0x80) && wait>0){	    wait--;	}	if(wait==0){ 	    DEBUG_LOG("Error waiting for sector unlock\n");	    return -1;	}	wait=50000;	while(wait--); 		/*erase Sector*/	*address=0x20; /*erase*/	*address=0xD0; /*confirm*/	wait=WAIT_CNT;  	while((( (*address) & 0x80)!=0x80) && wait>0){	    wait--;	}        DEBUG_LOG3("Wait: %d status: %x\n",wait,*address);  	if(wait==0){ 	    DEBUG_LOG("Error waiting for sector erase\n");	    return -1;	} else {	    DEBUG_LOG2("Unlocked sector at: %p\n",address);	}	address+=( sectorSize /2 ) /* inc word pointer */; 	currSectorNum++;	/*TODO change this so we round up instead of always adding 1*/#if 0    }while(currSectorNum < (len/sectorSize));#else    }while(address < dest + len);#endif        DEBUG_LOG("Sectors have been unlocked and erased\n");    DEBUG_LOG("Check images is all ffff's \n");    address=(short *)dest;    for(i=0;i<(len/2);i++)    {         *address = 0xff;       ++address;    }    for(i=0;i<(len/2);i++)    {        unsigned short val;       val = (unsigned short)*verPtr;       if ( val != (unsigned short)0xffff )       {	  return (-3);       }       ++verPtr;    }           DEBUG_LOG("Writing images ... \n");    address=(short *)dest;    for(i=0;i<(len/2);i++)    {     for(x=0;x <= RETRY_CNT;++x)     {	*address=0xff; /*put the flash in read mode*/	*address=0x40; /*flash command write*/	*address=*(short *)src; /*word to be written*/        {         for ( y=0;y<(1 *1024);++y);        }	*address=0x70; /* Put in status read mode */	retry=WAIT_CNT; 	while((((*address) & 0x80) != 0x80) && retry--);        	if(retry<0){     	     DEBUG_LOG("Timeout waiting for programming\n");  	     return -1; 	} 	*address = 0xff; /* Put in read mode */	if ( *address == *(short *)src )	{	  address++; /* Inc word pointer */	  src++;	  src++; /* Src is a byte pointer to inc twice */	  break;	}else	{		printf("failed at 0x%x", address);	}        if ( x % 100 == 0 )	{	 int y;          for ( y=0;y<5000;++y)          {	    *address = 0xff;          }	}        if (x == RETRY_CNT -1 )        { 	     return -2; 	}      }    }    DEBUG_LOG2("\nLast address written: %p\n",address-4);    DEBUG_LOG2("Programming went ok, %d bytes written, verifying...\n",len);    address=(short *)dest;        for(i=0;i<len/2;i++)    {	*address=0xff;	if(*originalSrc!=*address)	{	    DEBUG_LOG2("Error, data compare incorrect at word# %d\n",i);	    return i;	}	address++;	originalSrc++;    }    DEBUG_LOG("Data compare passed, the load is now in flash\n");     return 0;}

⌨️ 快捷键说明

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