sst28sf040.c
上传用户:fsjiahua
上传日期:2013-03-07
资源大小:89k
文件大小:6k
源码类别:

并口编程

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. #include "s3c4510.h"
  6. #include "sst28sf040.h"
  7. void Disable_Chip_Data_Protection(void)
  8. {
  9.     access_bus(RS, 0x1823, 0, IP);
  10.     read_rom(0x00001823);   // read data from 0x1823
  11.     access_bus(RS, 0x1820, 0, IP);
  12.     read_rom(0x00001820);   // read data from 0x1820
  13.     access_bus(RS, 0x1822, 0, IP);
  14.     read_rom(0x00001822);   // read data from 0x1822
  15.     access_bus(RS, 0x0418, 0, IP);
  16.     read_rom(0x00000418);   // read data from 0x0418
  17.     access_bus(RS, 0x041b, 0, IP);
  18.     read_rom(0x0000041B);   // read data from 0x041B
  19.     access_bus(RS, 0x0419, 0, IP);
  20.     read_rom(0x00000419);   // read data from 0x0419
  21.     access_bus(RS, 0x041a, 0, IP);
  22.     read_rom(0x0000041A);   // read data from 0x041A
  23.     access_bus(RS, 0x041a, 0, IP);
  24. }
  25. void Enable_Chip_Data_Protection()
  26. {
  27.     access_bus(RS, 0x1823, 0, IP);
  28.     read_rom(0x00001823);   // read data from 0x1823
  29.     access_bus(RS, 0x1820, 0, IP);
  30.     read_rom(0x00001820);   // read data from 0x1820
  31.     access_bus(RS, 0x1822, 0, IP);
  32.     read_rom(0x00001822);   // read data from 0x1822
  33.     access_bus(RS, 0x0418, 0, IP);
  34.     read_rom(0x00000418);   // read data from 0x0418
  35.     access_bus(RS, 0x041b, 0, IP);
  36.     read_rom(0x0000041B);   // read data from 0x041B
  37.     access_bus(RS, 0x0419, 0, IP);
  38.     read_rom(0x00000419);   // read data from 0x0419
  39.     access_bus(RS, 0x040a, 0, IP);
  40.     read_rom(0x0000040A);   // read data from 0x040A
  41.     access_bus(RS, 0x040a, 0, IP);
  42. }
  43. int Check_SST_28SF040(void)
  44. {
  45.   
  46.     BYTE SST_id1;
  47.     BYTE SST_id2;
  48.     int  ReturnStatus;
  49.     BYTE OriginalByte;
  50.     OriginalByte = read_rom(0x00000000);/* save the original memory contents  */
  51.     write_rom(0x0000, RESET);           /* reset tje 28SF040 chip first       */
  52.     write_rom(0x0000, READ_ID);         /* issue the READ_ID command          */
  53.     SST_id1 = read_rom(0x00000000);     /* get first ID byte                  */
  54.     SST_id2 = read_rom(0x00000001);     /* get second ID byte                 */
  55.     if ((SST_id1 == SST_ID) && (SST_id2 == SST_28SF040))
  56.         ReturnStatus = 0;
  57.     else
  58.         ReturnStatus = -1;
  59.     write_rom(0x00000000, RESET);       /* issue RESET command to 28SF040      */
  60.     if (ReturnStatus ==-1)  /* if not 28SF040, restore original contents  */
  61.         write_rom(0x00000000, OriginalByte);
  62.     return(ReturnStatus);
  63. }
  64. void Check_Toggle_Ready(DWORD Dst)
  65. {
  66.     unsigned char Loop = TRUE;
  67.     unsigned char PreData;
  68.     unsigned char CurrData;
  69.     unsigned long TimeOut = 0;
  70.     PreData = read_rom(Dst);
  71.     PreData = PreData & 0x40;
  72.     while ((TimeOut< 0x7FFFFFFF) && (Loop))
  73.     {
  74.         CurrData = read_rom(Dst);
  75.         CurrData = CurrData & 0x40;
  76.         if (PreData == CurrData)
  77.             Loop = FALSE;        /* ready to exit the while loop */
  78.         PreData = CurrData;
  79.         TimeOut++;
  80.     }
  81. }
  82. int Read_28SF040(FILE* stream, DWORD Src,DWORD size)
  83. {
  84. int i,count;
  85. DWORD SrcReadAddress = Src;
  86. unsigned char cTemp;
  87. if( NULL == stream )
  88. return -1;
  89. fseek( stream,0,SEEK_SET );
  90. count = 0;
  91. for( i = 1; i <= (int)size; i++ ) 
  92. {
  93. cTemp = read_rom( SrcReadAddress++ );
  94. printf("%.2x ",cTemp);
  95. if( i%16 == 0 ) printf("n");
  96. fwrite((char*)&cTemp,sizeof(char),1,stream );
  97. }
  98. return 0;
  99. }
  100. int Write_28SF040(unsigned char *Src, DWORD Dst)
  101. {
  102.     unsigned char   *SourceBuf;
  103.     DWORD           DestBuf;
  104.     int             Index;
  105.     int             Count;
  106.     unsigned char   SourceByte;
  107.     unsigned char   Continue;
  108.     
  109.     Disable_Chip_Data_Protection();
  110.     SourceBuf = Src;
  111.     DestBuf   = Dst;
  112.     /************************************************************************************/
  113.     /*                                      ERASE OPERATION                             */
  114.     /*                                                                                  */ 
  115.     /************************************************************************************/
  116.     write_rom(DestBuf, AUTO_PG_ERASE1); /* erase the page before programming */
  117.     write_rom(DestBuf, AUTO_PG_ERASE2);
  118.     Check_Toggle_Ready(Dst);            /* wait for Toggle bit ready         */
  119.     Count = 0;
  120.     Continue = TRUE;
  121. /*    while ((Count < ROW_SIZE) && (Continue))
  122.     {
  123.         SourceByte = read_rom(DestBuf++);
  124.         if (SourceByte == 0xFF)
  125.             Count++;
  126.         else
  127.             Continue =  FALSE;
  128.     }
  129.     if (!Continue)
  130.     {     
  131.         Enable_Chip_Data_Protection();             
  132.         return (TRUE);               
  133.     }
  134. */
  135.     /***********************************************************************************/
  136.     /*                        PROGRAM  OPERATION                                       */
  137.     /*                                                                                 */
  138.     /***********************************************************************************/
  139.     SourceBuf = Src;
  140.     DestBuf = Dst;
  141.     for (Index = 0; Index < ROW_SIZE; Index++)
  142.     {
  143.     SourceByte = *SourceBuf++;
  144.         if (SourceByte != 0xFF)             /* If the data is 0xFF, don,t program it*/
  145.     {
  146.             write_rom(DestBuf,AUTO_PGRM);   /*issue AUTO PROGRAM command*/
  147.             write_rom(DestBuf,SourceByte);  /* program the data */
  148.             //Check_Toggle_Ready(Dst);        /* wait for Toggle bit ready */
  149.             //ProgrammedByte = read_rom(DestBuf); /* read back the data pragrammed */
  150.             //if (SourceByte != ProgrammedByte)
  151.             //{
  152.             //    Continue = FALSE;
  153.             //    break;
  154.             //}
  155.         }
  156.         DestBuf++;
  157.     }
  158.     Enable_Chip_Data_Protection();
  159.     if (!Continue)
  160.     return(TRUE);                               /* return with error */
  161.     else
  162.     return(FALSE);                              /* return with NO error */
  163. }