复件 flash16.c
来自「coldfire5307的bootloader 将程序写入flash」· C语言 代码 · 共 791 行 · 第 1/2 页
C
791 行
*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: sVerifyEndFunction: Verify if the operation to Flash mem is finishedinput: spAddress: the address of Flash mem to be verified sVerifyData: Verify Datareturn: True: writing passed; False: writing is fail ***************************************************************************/bool sVerifyEnd( 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: sWriteWordFunction: Write a word to Flash memoryinput: lBaseAddress: The first address of Flash memory lAddress: Address of the memory to be written sData: Data to be writtenreturn: True: writing is successful; False: writing is failure*****************************************************************************/bool sWriteWord( 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; //return 1; //kang 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 sChipManufacturerID( 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 sChipDeviceID( 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 okbool 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; delay(1000);//kang for(i=0;i<HowmanyWords;) { Temp=*SrcAddress; //Temp=(unsigned short)(0xffff&(*SrcAddress)); if(False==sWriteWord(lBaseAddress,BlockStartAddress+BlockSize*BlockIndex+(i%BlockSize),Temp) ) return False; //delay(1);//kang SrcAddress++; i++; if( (0x0==(i%BlockSize)) && (i!=HowmanyWords)) { BlockIndex++; //delay(10);//kang if( False==sSectorErase(lBaseAddress,BlockStartAddress+BlockSize*BlockIndex) ) return False; delay(1000);//kang } } //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; }// 延时void delay(int t){ int i=0,j=0; for(i=0;i<t;i++) for(j=0;j<10000;j++) ; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?