FlashBadBlockProcess.c
上传用户:xhjmsc
上传日期:2019-09-13
资源大小:389k
文件大小:9k
源码类别:

传真(Fax)编程

开发平台:

C/C++

  1. /***************************************
  2. Program:     FlashBadBlockProcess
  3. Author:      Bryan Cai
  4. Date:        2006.12.02
  5. ***************************************/
  6. #include "includeH.h"
  7. void CheckInvalidBlock(void)
  8. {
  9.   unsigned int wPageAddress=0, wBlockAddress=1,
  10.                wByteCnt=0;
  11. unsigned char ucFlashWriteData=0,ucFlashReadData=0,
  12.               ucRxIndex=0,ucGeneralCnt=0,
  13.               ucBadBlockCnt=0,ucRxBufFullCnt=0;
  14.   
  15. ZERO_ADDRESS();
  16. ERASE_ONE_BLOCK();   // Erase block 0
  17. for (wBlockAddress=1;wBlockAddress<1024;wBlockAddress++)
  18. {
  19.    wPageAddress=wBlockAddress<<6;
  20.    ROW_ADDRESS_LOW=wPageAddress & 0XFF;
  21.    ROW_ADDRESS_HIGH=(wPageAddress>>8) & 0XFF; 
  22.    ERASE_ONE_BLOCK();
  23.   for (ucGeneralCnt=0;ucGeneralCnt<64;ucGeneralCnt++,wPageAddress++)
  24.   {
  25.    ROW_ADDRESS_LOW=wPageAddress & 0XFF;
  26.    ROW_ADDRESS_HIGH=(wPageAddress>>8) & 0XFF;
  27.    ucFlashWriteData=0xff;
  28.    //PROGRAM_ONE_PAGE(ucFlashWriteData);
  29. //COMMAND
  30. COMMAND_VAL=0x00; //#0X00
  31. COMMAND_LATCH_INTO();
  32. //ADDRESS
  33. ADDRESS_VALUE=COLUMN_ADDRESS_LOW; //#0X00
  34. ADDRESS_LATCH_INTO();
  35. ADDRESS_VALUE=COLUMN_ADDRESS_HIGH; //#0X00
  36. ADDRESS_LATCH_INTO();
  37. ADDRESS_VALUE=ROW_ADDRESS_LOW; //#0X00
  38. ADDRESS_LATCH_INTO();
  39. ADDRESS_VALUE=ROW_ADDRESS_HIGH; //#0X00
  40. ADDRESS_LATCH_INTO();
  41. FLASH_CON_PORT_OUT &= ~ALE;
  42. COMMAND_VAL=0X30;
  43. COMMAND_LATCH_INTO();
  44. //ATcmdDelay();
  45. //for(wGeneralCnt=0;wGeneralCnt<0xff;wGeneralCnt++); //delay
  46. while((FLASH_CON_PORT_IN & RB)==0);
  47.  DATA_PORT_IN_INITIAL;
  48.  for(wByteCnt=0;wByteCnt<2112;wByteCnt++)
  49.  {
  50.       FLASH_CON_PORT_OUT &=~ RE; //RE IS LOW,ENABLE READ DATA
  51.     ucFlashReadData=DATA_PORT_IN;
  52.     FLASH_CON_PORT_OUT |= RE;
  53.       if (ucFlashReadData!=ucFlashWriteData)
  54.       {
  55.   
  56.      if (ucBadBlockCnt++<128)
  57.      {
  58.       aucUartRxBuf[ucRxIndex++]=wBlockAddress & 0xff;      //low 8 byte
  59.         aucUartRxBuf[ucRxIndex++]=(wBlockAddress>>8) & 0xff; //high 8 byte
  60.      }//if
  61.      else
  62.      {
  63.          COLUMN_ADDRESS_LOW=0X00;
  64.               COLUMN_ADDRESS_HIGH=0X00;
  65.               ROW_ADDRESS_LOW=ucRxBufFullCnt;
  66.               ROW_ADDRESS_HIGH=0X00;
  67.          PROGRAM_256_BYTE();
  68.          ucRxBufFullCnt++;
  69.      }//else
  70.      break;   
  71.      }//if unequal
  72.   }//for next byte
  73. if (ucFlashReadData!=ucFlashWriteData){break;}//next block
  74.   
  75.    ucFlashWriteData=0x00;
  76.    PROGRAM_ONE_PAGE(ucFlashWriteData);
  77. //COMMAND
  78. COMMAND_VAL=0x00; //#0X00
  79. COMMAND_LATCH_INTO();
  80. //ADDRESS
  81. ADDRESS_VALUE=COLUMN_ADDRESS_LOW; //#0X00
  82. ADDRESS_LATCH_INTO();
  83. ADDRESS_VALUE=COLUMN_ADDRESS_HIGH; //#0X00
  84. ADDRESS_LATCH_INTO();
  85. ADDRESS_VALUE=ROW_ADDRESS_LOW; //#0X00
  86. ADDRESS_LATCH_INTO();
  87. ADDRESS_VALUE=ROW_ADDRESS_HIGH; //#0X00
  88. ADDRESS_LATCH_INTO();
  89. FLASH_CON_PORT_OUT &=~ ALE;
  90. COMMAND_VAL=0X30;
  91. COMMAND_LATCH_INTO();
  92. //ATcmdDelay();
  93. //for(wGeneralCnt=0;wGeneralCnt<0xff;wGeneralCnt++); //delay
  94. while((FLASH_CON_PORT_IN & RB)==0);
  95.  DATA_PORT_IN_INITIAL; //MCU INPUT,MCU READ A DATA
  96.  for(wByteCnt=0;wByteCnt<2112;wByteCnt++)
  97.  {
  98.         FLASH_CON_PORT_OUT &=~ RE; //RE IS LOW,ENABLE READ DATA
  99. ucFlashReadData=DATA_PORT_IN;
  100. FLASH_CON_PORT_OUT |= RE;
  101.      
  102.       if (ucFlashReadData!=ucFlashWriteData)
  103.       {
  104.      if (ucBadBlockCnt++<128)
  105.      {
  106.       aucUartRxBuf[ucRxIndex++]=wBlockAddress & 0xff;      //low 8 byte
  107.         aucUartRxBuf[ucRxIndex++]=(wBlockAddress>>8) & 0xff; //high 8 byte
  108.      }//if
  109.      else
  110.      {
  111.          COLUMN_ADDRESS_LOW=0X00;
  112.               COLUMN_ADDRESS_HIGH=0X00;
  113.               ROW_ADDRESS_LOW=ucRxBufFullCnt;
  114.               ROW_ADDRESS_HIGH=0X00;
  115.          PROGRAM_256_BYTE();
  116.          ucRxBufFullCnt++;
  117.      }//else 
  118.      break;  
  119.      }//if unequal
  120.  }//for next byte
  121.  if (ucFlashReadData!=ucFlashWriteData){break;}//next block
  122. }//for next page 
  123.  _NOP();
  124.  }//for next block
  125. if (ucBadBlockCnt!=0)
  126. {
  127.   COLUMN_ADDRESS_LOW=0X00;
  128.   COLUMN_ADDRESS_HIGH=0X00;
  129.   ROW_ADDRESS_LOW=ucRxBufFullCnt;//first block
  130.   ROW_ADDRESS_HIGH=0X00;
  131.   PROGRAM_256_BYTE();
  132.   ucRxBufFullCnt++;
  133. }
  134. }
  135. /*
  136. void CheckInvalidBlockForNewFlash(void) //64*256Bytes=2^14 bytes
  137. {
  138. unsigned int wPageAddress=0, wBlockAddress=0;
  139. unsigned char ucBadBlockCnt=0,ucRxIndex=0,
  140.               ucRxBufFullCnt=0,ucFlashReadData,
  141.               ucGeneralCnt;
  142. COLUMN_ADDRESS_LOW=0X00;   //check "FF" at the column address 2048 of 
  143. COLUMN_ADDRESS_HIGH=0X08;  //the 1st and 2nd page in the block
  144. while (!(wBlockAddress++==1023))
  145. {
  146. wPageAddress=wBlockAddress<<6;
  147. ROW_ADDRESS_LOW=wPageAddress & 0XFF;
  148. ROW_ADDRESS_HIGH=(wPageAddress>>8) & 0XFF;
  149. for (ucGeneralCnt=0;ucGeneralCnt<2;ucGeneralCnt++,wPageAddress++)
  150. {
  151. //COMMAND
  152.   COMMAND_VAL=0x00; //#0X00 //00H READ1 0-2111BYTE
  153.   COMMAND_LATCH_INTO();
  154. //ADDRESS
  155.   ADDRESS_VALUE=COLUMN_ADDRESS_LOW;
  156.   ADDRESS_LATCH_INTO();
  157.   ADDRESS_VALUE=COLUMN_ADDRESS_HIGH;
  158.   ADDRESS_LATCH_INTO();
  159.   ADDRESS_VALUE=wPageAddress & 0xff; //ROW_ADDRESS_LOW; a bug of Dec. 07,2006
  160.   ADDRESS_LATCH_INTO();
  161.   ADDRESS_VALUE=(wPageAddress>>8) & 0xff;
  162.   ADDRESS_LATCH_INTO();
  163.   FLASH_CON_PORT_OUT &=~ ALE;
  164.   COMMAND_VAL=0X30;
  165.   COMMAND_LATCH_INTO();
  166.   //ATcmdDelay();
  167.   while((FLASH_CON_PORT_IN & RB)==0);
  168.   DATA_PORT_IN_INITIAL;
  169.   FLASH_CON_PORT_OUT &=~ RE; //RE IS LOW,ENABLE READ DATA
  170.   ucFlashReadData=DATA_PORT_IN;
  171.   FLASH_CON_PORT_OUT |= RE;
  172.     if (0xff != ucFlashReadData)
  173.      {
  174.      if (ucBadBlockCnt++<128)
  175.      {
  176.       aucUartRxBuf[ucRxIndex++]=wBlockAddress & 0xff;      //low 8 byte
  177.         aucUartRxBuf[ucRxIndex++]=(wBlockAddress>>8) & 0xff; //high 8 byte
  178.      }//if
  179.      else
  180.      {
  181.             COLUMN_ADDRESS_LOW=0X00;
  182.               COLUMN_ADDRESS_HIGH=0X00;
  183.               ROW_ADDRESS_LOW=ucRxBufFullCnt;
  184.               ROW_ADDRESS_HIGH=0X00;
  185.          PROGRAM_256_BYTE();
  186.          ucRxBufFullCnt++;
  187.      }//else
  188.      break;  //first page or second page   
  189.      }//if
  190.     //else
  191.      //ROW_ADDRESS_LOW++;  //next page-->2nd page in the block //a bug of Dec. 07,2006
  192.   }//for
  193. }//while
  194. if (ucBadBlockCnt!=0)
  195. {
  196.   COLUMN_ADDRESS_LOW=0X00;
  197.   COLUMN_ADDRESS_HIGH=0X00;
  198.   ROW_ADDRESS_LOW=ucRxBufFullCnt;//first block
  199.   ROW_ADDRESS_HIGH=0X00;
  200.   PROGRAM_256_BYTE();
  201.   ucRxBufFullCnt++;
  202. }
  203. FlashFormat();
  204. */
  205. unsigned int CalNextPageAddress(unsigned int wCurrentPageAddress)
  206. {
  207.     unsigned char ucGeneralCnt=0,ucRxIndex=0,ucBadBlockCnt=0;
  208.     unsigned int  wBlockAddress=0,wNextPageAddress=0;
  209.               
  210.     wNextPageAddress=wCurrentPageAddress;
  211.     wBlockAddress=wCurrentPageAddress>>6;              
  212.     if (wCurrentPageAddress%64==0)
  213.     {
  214.        for (ucGeneralCnt=0;ucGeneralCnt<64;ucGeneralCnt++,ROW_ADDRESS_LOW++)
  215.      {
  216.         READ_256_BYTE();
  217.         for (ucRxIndex=0,ucBadBlockCnt=0;ucBadBlockCnt<128;ucBadBlockCnt++)
  218.         {
  219.           if (aucUartRxBuf[ucRxIndex]==(wBlockAddress & 0xff) 
  220.                && aucUartRxBuf[ucRxIndex+1]==((wBlockAddress>>8) & 0xff))
  221.               {wNextPageAddress=wCurrentPageAddress+64; break;}
  222.           if (aucUartRxBuf[ucRxIndex]==0xFF && aucUartRxBuf[ucRxIndex+1]==0xFF && 
  223.              aucUartRxBuf[ucRxIndex+2]==0xFF &&aucUartRxBuf[ucRxIndex+3]==0xFF)
  224.              {break;}
  225.           ucRxIndex+=2;
  226.         }//for
  227.         if (aucUartRxBuf[ucRxIndex++]==(wBlockAddress & 0xff) 
  228.                && aucUartRxBuf[ucRxIndex++]==((wBlockAddress>>8) & 0xff))
  229.               {wNextPageAddress=wCurrentPageAddress+64; break;}
  230.         if (aucUartRxBuf[ucRxIndex]==0xFF && aucUartRxBuf[ucRxIndex+1]==0xFF && 
  231.              aucUartRxBuf[ucRxIndex+2]==0xFF &&aucUartRxBuf[ucRxIndex+3]==0xFF)
  232.              {break;}
  233.      }//for
  234.     }//if
  235.     return wNextPageAddress;
  236. }
  237. /*******************
  238. unsigned int CalNextBlockAddress(unsigned int wCurrentBlockAddress)
  239. {
  240.     unsigned char ucGeneralCnt=0,ucRxIndex=0,ucBadBlockCnt=0;
  241.     unsigned int  wBlockAddress=0,wNextBlockAddress=0;
  242.               
  243.     wNextBlockAddress=wCurrentBlockAddress;              
  244.     for (ucGeneralCnt=0;ucGeneralCnt<64;ucGeneralCnt++,ROW_ADDRESS_LOW++)
  245.      {
  246.         
  247.         READ_256_BYTE();
  248.         for (ucRxIndex=0,ucBadBlockCnt=0;ucBadBlockCnt<128;ucBadBlockCnt++)
  249.         {
  250.           if (aucUartRxBuf[ucRxIndex++]==(wBlockAddress & 0xff) 
  251.                && aucUartRxBuf[ucRxIndex++]==((wBlockAddress>>8) & 0xff))
  252.               {wNextBlockAddress=wCurrentBlockAddress+1; break;}
  253.           if (aucUartRxBuf[ucRxIndex]==0xFF && aucUartRxBuf[ucRxIndex+1]==0xFF && 
  254.              aucUartRxBuf[ucRxIndex+2]==0xFF &&aucUartRxBuf[ucRxIndex+3]==0xFF)
  255.              {break;}
  256.         }//for
  257.         if (aucUartRxBuf[ucRxIndex]==0xFF && aucUartRxBuf[ucRxIndex+1]==0xFF && 
  258.              aucUartRxBuf[ucRxIndex+2]==0xFF &&aucUartRxBuf[ucRxIndex+3]==0xFF)
  259.              {break;}
  260.      }//for
  261.      return wNextBlockAddress;
  262. *******************************/ 
  263. void FlashFormat(void)
  264. {
  265.           unsigned int wBlockAddress=0,wPageAddress=0;
  266.           for (wBlockAddress=1;wBlockAddress<1024;wBlockAddress++)
  267.          {
  268.                  wPageAddress=wBlockAddress<<6;
  269.           ROW_ADDRESS_LOW=wPageAddress & 0XFF;
  270.           ROW_ADDRESS_HIGH=(wPageAddress>>8) & 0XFF; 
  271.           ERASE_ONE_BLOCK();
  272.          }