/************************************************************************* CMD TYPE之格式 0x00 讀取所送ADDRESS之FLASH 0x01 ERASE所送ADDRESS之FLASH 0x02 寫入所送ADDRESS之FLASH *************************************************************************/ void DoUSBCMD(){ unsigned char check_sum=0; Uart.CMDtodo=0; switch(Uart.CMD_Type){ case 0: FRead_Data(Uart.CMD_Address,Uart.DATABuf); for(int i=0;i<Uart.Datalength;i++){ SendByteToUart(Uart.DATABuf[i]); check_sum^=Uart.DATABuf[i]; } SendByteToUart(check_sum); SendByteToUart(Uart.CheckSum); Uart.CMDMode=0; break; case 1: BlockErase(Uart.CMD_Address,0); SendTextToUart("ACKOK",5); SendByteToUart(Uart.CheckSum); Uart.CMDMode=0; break; case 2: if(Uart.CMD_Address%32==0)BlockErase(Uart.CMD_Address,0); SendTextToUart("ACKOK",5); SendByteToUart(Uart.CheckSum); break; case 4: ShowTimeInFlash(); SendTextToUart("ACKOK",5); SendByteToUart(Uart.CheckSum); Uart.CMDMode=0; break; } Uart.CMDIndex=0; }
int Find_Start(char User){ unsigned int Page_offset; unsigned int i; UserDataStruct.Start_page=UserStartPage[User]; UserDataStruct.End_page=UserEndPage[User]; FRead_Data(UserDataStruct.End_page,RING_BUFF); // FRead_Data(Start_page,RING_BUFF); for (i=0;i<512;i+=16){ if ((RING_BUFF[i+21]==0xFF && RING_BUFF[i+22]==0xFF) || (RING_BUFF[i+21]==0x00 && RING_BUFF[i+22]==0x00)) { // 因為NAND Flash裡面沒寫資料,另外規則中已經把巧合的情形排除。 }else{ if (RING_BUFF[i+7]==RING_BUFF[i+21] && RING_BUFF[i+8]==RING_BUFF[i+22]){ Page_offset = RING_BUFF[i+7] * 256 + RING_BUFF[i+8]; UserDataStruct.Index_page = (i / 16) + 1; } } } //如果存滿了,就清空記憶體並從頭開始存 if (Page_offset > 1536 || UserDataStruct.Index_page > 20){ BlockErase(UserDataStruct.End_page,1); Page_offset = 0; UserDataStruct.Index_page = 0; } return Page_offset; }
void Clear_UserDataCount(unsigned char UserID){ unsigned char UserDataCountLIST[6]={0,0,0,0,0,0}; unsigned char UserDataCount=0; for(int i=0;i<6;i++){ FRead_Data(UserIDPAGEList[i],USB); UserDataCount=USB[0]; if (UserDataCount==0xff){UserDataCount=0;} UserDataCountLIST[i]=UserDataCount; } UserDataCountLIST[UserID]=0; //刪除該使用者記錄的數量 BlockErase(UserIDPAGEList[0],1); for(int i=0;i<6;i++){ //依序儲存 USB[0]=UserDataCountLIST[i]; FSave_Data(UserIDPAGEList[i],USB); } };
void Save_UserDataCount(unsigned char UserID){ unsigned char UserDataCountLIST[6]={0,0,0,0,0,0}; unsigned char UserDataCount=0; for(int i=0;i<6;i++){ FRead_Data(UserIDPAGEList[i],USB); UserDataCount=USB[0]; if (UserDataCount==0xff){UserDataCount=0;} UserDataCountLIST[i]=UserDataCount; } UserDataCountLIST[UserID]++; //代表量測完成..多一筆紀錄 BlockErase(UserIDPAGEList[0],1); for(int i=0;i<6;i++){ //依序儲存 USB[0]=UserDataCountLIST[i]; FSave_Data(UserIDPAGEList[i],USB); } };
void DataFlash_Block::EraseAll() { for (uint16_t j = 1; j <= (df_NumPages+1)/8; j++) { BlockErase(j); if (j%6 == 0) { hal.scheduler->delay(6); } } // write the logging format in the last page hal.scheduler->delay(100); StartWrite(df_NumPages+1); uint32_t version = DF_LOGGING_FORMAT; log_write_started = true; WriteBlock(&version, sizeof(version)); log_write_started = false; FinishWrite(); hal.scheduler->delay(100); }
int main( int ArgC, char *ArgV[] ) { int BDMHandle; unsigned int Base; unsigned int Chips; unsigned int Bytes; char *Operation; FlashError_t Error; if (ArgC < 6) Usage( ArgV[0] ); if((BDMHandle = bdm_init(ArgV[1]))<0) { fprintf( stderr, "Problem opening bdm device %s, error code %d.\n", ArgV[1], BDMHandle ); return (EXIT_FAILURE); } Base = (unsigned int) strtoul( ArgV[2], NULL, 0 ); Chips = (unsigned int) strtoul( ArgV[3], NULL, 0 ); Bytes = (unsigned int) strtoul( ArgV[4], NULL, 0 ); Operation = ArgV[5]; Error = BDMFlashConfigSet( BDMHandle, Base, Chips, Bytes ); if (Error != FlashErrorOkay_c) { fprintf( stderr, "Problem configuring flash; error code = %d '%s'.\n", (int) Error, FlashErrorDescriptionEnglish[Error] ); } if (strcasecmp( Operation, "erase" ) == 0) { Erase( ArgC, ArgV ); } else if (strcasecmp( Operation, "SectorErase" ) == 0) { SectorErase( ArgC, ArgV ); } else if (strcasecmp( Operation, "BlockErase" ) == 0) { BlockErase( ArgC, ArgV ); } else if (strcasecmp( Operation, "write" ) == 0) { Write( ArgC, ArgV ); } else if (strcasecmp( Operation, "read" ) == 0) { Read( BDMHandle, ArgC, ArgV ); } else if (strcasecmp( Operation, "probe" ) == 0) { Probe( ArgC, ArgV ); } else if (strcasecmp( Operation, "detect" ) == 0) { Detect( ArgC, ArgV ); } else Usage( ArgV[0] ); bdm_release( BDMHandle ); return (EXIT_SUCCESS); }