FlashBadBlockProcess.c
上传用户:xhjmsc
上传日期:2019-09-13
资源大小:389k
文件大小:9k
- /***************************************
- Program: FlashBadBlockProcess
- Author: Bryan Cai
- Date: 2006.12.02
- ***************************************/
- #include "includeH.h"
- void CheckInvalidBlock(void)
- {
- unsigned int wPageAddress=0, wBlockAddress=1,
- wByteCnt=0;
- unsigned char ucFlashWriteData=0,ucFlashReadData=0,
- ucRxIndex=0,ucGeneralCnt=0,
- ucBadBlockCnt=0,ucRxBufFullCnt=0;
-
- ZERO_ADDRESS();
- ERASE_ONE_BLOCK(); // Erase block 0
- for (wBlockAddress=1;wBlockAddress<1024;wBlockAddress++)
- {
- wPageAddress=wBlockAddress<<6;
- ROW_ADDRESS_LOW=wPageAddress & 0XFF;
- ROW_ADDRESS_HIGH=(wPageAddress>>8) & 0XFF;
- ERASE_ONE_BLOCK();
- for (ucGeneralCnt=0;ucGeneralCnt<64;ucGeneralCnt++,wPageAddress++)
- {
- ROW_ADDRESS_LOW=wPageAddress & 0XFF;
- ROW_ADDRESS_HIGH=(wPageAddress>>8) & 0XFF;
- ucFlashWriteData=0xff;
- //PROGRAM_ONE_PAGE(ucFlashWriteData);
- //COMMAND
- COMMAND_VAL=0x00; //#0X00
- COMMAND_LATCH_INTO();
- //ADDRESS
- ADDRESS_VALUE=COLUMN_ADDRESS_LOW; //#0X00
- ADDRESS_LATCH_INTO();
-
- ADDRESS_VALUE=COLUMN_ADDRESS_HIGH; //#0X00
- ADDRESS_LATCH_INTO();
-
- ADDRESS_VALUE=ROW_ADDRESS_LOW; //#0X00
- ADDRESS_LATCH_INTO();
-
- ADDRESS_VALUE=ROW_ADDRESS_HIGH; //#0X00
- ADDRESS_LATCH_INTO();
- FLASH_CON_PORT_OUT &= ~ALE;
-
- COMMAND_VAL=0X30;
- COMMAND_LATCH_INTO();
- //ATcmdDelay();
- //for(wGeneralCnt=0;wGeneralCnt<0xff;wGeneralCnt++); //delay
- while((FLASH_CON_PORT_IN & RB)==0);
- DATA_PORT_IN_INITIAL;
- for(wByteCnt=0;wByteCnt<2112;wByteCnt++)
- {
- FLASH_CON_PORT_OUT &=~ RE; //RE IS LOW,ENABLE READ DATA
- ucFlashReadData=DATA_PORT_IN;
- FLASH_CON_PORT_OUT |= RE;
- if (ucFlashReadData!=ucFlashWriteData)
- {
-
- if (ucBadBlockCnt++<128)
- {
- aucUartRxBuf[ucRxIndex++]=wBlockAddress & 0xff; //low 8 byte
- aucUartRxBuf[ucRxIndex++]=(wBlockAddress>>8) & 0xff; //high 8 byte
- }//if
- else
- {
- COLUMN_ADDRESS_LOW=0X00;
- COLUMN_ADDRESS_HIGH=0X00;
- ROW_ADDRESS_LOW=ucRxBufFullCnt;
- ROW_ADDRESS_HIGH=0X00;
- PROGRAM_256_BYTE();
- ucRxBufFullCnt++;
- }//else
- break;
- }//if unequal
- }//for next byte
- if (ucFlashReadData!=ucFlashWriteData){break;}//next block
-
- ucFlashWriteData=0x00;
- PROGRAM_ONE_PAGE(ucFlashWriteData);
- //COMMAND
- COMMAND_VAL=0x00; //#0X00
- COMMAND_LATCH_INTO();
- //ADDRESS
- ADDRESS_VALUE=COLUMN_ADDRESS_LOW; //#0X00
- ADDRESS_LATCH_INTO();
-
- ADDRESS_VALUE=COLUMN_ADDRESS_HIGH; //#0X00
- ADDRESS_LATCH_INTO();
-
- ADDRESS_VALUE=ROW_ADDRESS_LOW; //#0X00
- ADDRESS_LATCH_INTO();
-
- ADDRESS_VALUE=ROW_ADDRESS_HIGH; //#0X00
- ADDRESS_LATCH_INTO();
- FLASH_CON_PORT_OUT &=~ ALE;
-
- COMMAND_VAL=0X30;
- COMMAND_LATCH_INTO();
- //ATcmdDelay();
- //for(wGeneralCnt=0;wGeneralCnt<0xff;wGeneralCnt++); //delay
- while((FLASH_CON_PORT_IN & RB)==0);
- DATA_PORT_IN_INITIAL; //MCU INPUT,MCU READ A DATA
- for(wByteCnt=0;wByteCnt<2112;wByteCnt++)
- {
- FLASH_CON_PORT_OUT &=~ RE; //RE IS LOW,ENABLE READ DATA
- ucFlashReadData=DATA_PORT_IN;
- FLASH_CON_PORT_OUT |= RE;
-
- if (ucFlashReadData!=ucFlashWriteData)
- {
- if (ucBadBlockCnt++<128)
- {
- aucUartRxBuf[ucRxIndex++]=wBlockAddress & 0xff; //low 8 byte
- aucUartRxBuf[ucRxIndex++]=(wBlockAddress>>8) & 0xff; //high 8 byte
- }//if
- else
- {
- COLUMN_ADDRESS_LOW=0X00;
- COLUMN_ADDRESS_HIGH=0X00;
- ROW_ADDRESS_LOW=ucRxBufFullCnt;
- ROW_ADDRESS_HIGH=0X00;
- PROGRAM_256_BYTE();
- ucRxBufFullCnt++;
- }//else
- break;
- }//if unequal
- }//for next byte
- if (ucFlashReadData!=ucFlashWriteData){break;}//next block
- }//for next page
- _NOP();
- }//for next block
- if (ucBadBlockCnt!=0)
- {
- COLUMN_ADDRESS_LOW=0X00;
- COLUMN_ADDRESS_HIGH=0X00;
- ROW_ADDRESS_LOW=ucRxBufFullCnt;//first block
- ROW_ADDRESS_HIGH=0X00;
- PROGRAM_256_BYTE();
- ucRxBufFullCnt++;
- }
- }
- /*
- void CheckInvalidBlockForNewFlash(void) //64*256Bytes=2^14 bytes
- {
- unsigned int wPageAddress=0, wBlockAddress=0;
- unsigned char ucBadBlockCnt=0,ucRxIndex=0,
- ucRxBufFullCnt=0,ucFlashReadData,
- ucGeneralCnt;
-
- COLUMN_ADDRESS_LOW=0X00; //check "FF" at the column address 2048 of
- COLUMN_ADDRESS_HIGH=0X08; //the 1st and 2nd page in the block
- while (!(wBlockAddress++==1023))
- {
- wPageAddress=wBlockAddress<<6;
- ROW_ADDRESS_LOW=wPageAddress & 0XFF;
- ROW_ADDRESS_HIGH=(wPageAddress>>8) & 0XFF;
- for (ucGeneralCnt=0;ucGeneralCnt<2;ucGeneralCnt++,wPageAddress++)
- {
- //COMMAND
- COMMAND_VAL=0x00; //#0X00 //00H READ1 0-2111BYTE
- COMMAND_LATCH_INTO();
- //ADDRESS
- ADDRESS_VALUE=COLUMN_ADDRESS_LOW;
- ADDRESS_LATCH_INTO();
-
- ADDRESS_VALUE=COLUMN_ADDRESS_HIGH;
- ADDRESS_LATCH_INTO();
-
- ADDRESS_VALUE=wPageAddress & 0xff; //ROW_ADDRESS_LOW; a bug of Dec. 07,2006
- ADDRESS_LATCH_INTO();
-
- ADDRESS_VALUE=(wPageAddress>>8) & 0xff;
- ADDRESS_LATCH_INTO();
- FLASH_CON_PORT_OUT &=~ ALE;
-
- COMMAND_VAL=0X30;
- COMMAND_LATCH_INTO();
- //ATcmdDelay();
- while((FLASH_CON_PORT_IN & RB)==0);
- DATA_PORT_IN_INITIAL;
- FLASH_CON_PORT_OUT &=~ RE; //RE IS LOW,ENABLE READ DATA
- ucFlashReadData=DATA_PORT_IN;
- FLASH_CON_PORT_OUT |= RE;
- if (0xff != ucFlashReadData)
- {
- if (ucBadBlockCnt++<128)
- {
- aucUartRxBuf[ucRxIndex++]=wBlockAddress & 0xff; //low 8 byte
- aucUartRxBuf[ucRxIndex++]=(wBlockAddress>>8) & 0xff; //high 8 byte
- }//if
- else
- {
- COLUMN_ADDRESS_LOW=0X00;
- COLUMN_ADDRESS_HIGH=0X00;
- ROW_ADDRESS_LOW=ucRxBufFullCnt;
- ROW_ADDRESS_HIGH=0X00;
- PROGRAM_256_BYTE();
- ucRxBufFullCnt++;
- }//else
- break; //first page or second page
- }//if
- //else
- //ROW_ADDRESS_LOW++; //next page-->2nd page in the block //a bug of Dec. 07,2006
- }//for
- }//while
-
- if (ucBadBlockCnt!=0)
- {
- COLUMN_ADDRESS_LOW=0X00;
- COLUMN_ADDRESS_HIGH=0X00;
- ROW_ADDRESS_LOW=ucRxBufFullCnt;//first block
- ROW_ADDRESS_HIGH=0X00;
- PROGRAM_256_BYTE();
- ucRxBufFullCnt++;
- }
-
- FlashFormat();
-
- }
- */
- unsigned int CalNextPageAddress(unsigned int wCurrentPageAddress)
- {
- unsigned char ucGeneralCnt=0,ucRxIndex=0,ucBadBlockCnt=0;
- unsigned int wBlockAddress=0,wNextPageAddress=0;
-
- wNextPageAddress=wCurrentPageAddress;
- wBlockAddress=wCurrentPageAddress>>6;
- if (wCurrentPageAddress%64==0)
- {
- for (ucGeneralCnt=0;ucGeneralCnt<64;ucGeneralCnt++,ROW_ADDRESS_LOW++)
- {
- READ_256_BYTE();
- for (ucRxIndex=0,ucBadBlockCnt=0;ucBadBlockCnt<128;ucBadBlockCnt++)
- {
- if (aucUartRxBuf[ucRxIndex]==(wBlockAddress & 0xff)
- && aucUartRxBuf[ucRxIndex+1]==((wBlockAddress>>8) & 0xff))
- {wNextPageAddress=wCurrentPageAddress+64; break;}
- if (aucUartRxBuf[ucRxIndex]==0xFF && aucUartRxBuf[ucRxIndex+1]==0xFF &&
- aucUartRxBuf[ucRxIndex+2]==0xFF &&aucUartRxBuf[ucRxIndex+3]==0xFF)
- {break;}
- ucRxIndex+=2;
- }//for
- if (aucUartRxBuf[ucRxIndex++]==(wBlockAddress & 0xff)
- && aucUartRxBuf[ucRxIndex++]==((wBlockAddress>>8) & 0xff))
- {wNextPageAddress=wCurrentPageAddress+64; break;}
- if (aucUartRxBuf[ucRxIndex]==0xFF && aucUartRxBuf[ucRxIndex+1]==0xFF &&
- aucUartRxBuf[ucRxIndex+2]==0xFF &&aucUartRxBuf[ucRxIndex+3]==0xFF)
- {break;}
- }//for
- }//if
- return wNextPageAddress;
- }
- /*******************
- unsigned int CalNextBlockAddress(unsigned int wCurrentBlockAddress)
- {
- unsigned char ucGeneralCnt=0,ucRxIndex=0,ucBadBlockCnt=0;
- unsigned int wBlockAddress=0,wNextBlockAddress=0;
-
- wNextBlockAddress=wCurrentBlockAddress;
- for (ucGeneralCnt=0;ucGeneralCnt<64;ucGeneralCnt++,ROW_ADDRESS_LOW++)
- {
-
- READ_256_BYTE();
- for (ucRxIndex=0,ucBadBlockCnt=0;ucBadBlockCnt<128;ucBadBlockCnt++)
- {
- if (aucUartRxBuf[ucRxIndex++]==(wBlockAddress & 0xff)
- && aucUartRxBuf[ucRxIndex++]==((wBlockAddress>>8) & 0xff))
- {wNextBlockAddress=wCurrentBlockAddress+1; break;}
- if (aucUartRxBuf[ucRxIndex]==0xFF && aucUartRxBuf[ucRxIndex+1]==0xFF &&
- aucUartRxBuf[ucRxIndex+2]==0xFF &&aucUartRxBuf[ucRxIndex+3]==0xFF)
- {break;}
- }//for
- if (aucUartRxBuf[ucRxIndex]==0xFF && aucUartRxBuf[ucRxIndex+1]==0xFF &&
- aucUartRxBuf[ucRxIndex+2]==0xFF &&aucUartRxBuf[ucRxIndex+3]==0xFF)
- {break;}
- }//for
- return wNextBlockAddress;
- }
- *******************************/
- void FlashFormat(void)
- {
- unsigned int wBlockAddress=0,wPageAddress=0;
- for (wBlockAddress=1;wBlockAddress<1024;wBlockAddress++)
- {
- wPageAddress=wBlockAddress<<6;
- ROW_ADDRESS_LOW=wPageAddress & 0XFF;
- ROW_ADDRESS_HIGH=(wPageAddress>>8) & 0XFF;
- ERASE_ONE_BLOCK();
- }
- }