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

并口编程

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. #include "s3c4510.h"
  6. #include "sst39vf160.h"
  7. #include "sst28sf040.h"
  8. int Check_SST_39VF160(void)   //历程和hy的一致,先不修改
  9. {
  10.     WORD SST_id1;
  11.     WORD SST_id2;
  12. int  ReturnStatus;
  13. write_rom2( SST39_CMD_ADDRESS1, SST39_READ_ID1);
  14.     write_rom2( SST39_CMD_ADDRESS2, SST39_READ_ID2);
  15.     write_rom2( SST39_CMD_ADDRESS1, SST39_READ_ID3);
  16. access_bus2(HOLD, SST39_CMD_ADDRESS1, SST39_READ_ID2, IP);
  17.  /*   read_rom2(0x00000000);
  18. read_rom2(0x00000002);
  19. read_rom2(0x00000004);
  20. read_rom2(0x00000006);
  21. read_rom2(0x00000008);
  22. read_rom2(0x0000000a);
  23. read_rom2(0x0000000c);
  24. read_rom2(0x0000000e);
  25. read_rom2(0x00000010);
  26.     read_rom2(0x00000012);
  27. read_rom2(0x00000014);
  28. read_rom2(0x00000016);
  29. read_rom2(0x00000018);
  30. */
  31.     SST_id1 = read_rom2(0x00000000);
  32. SST_id2 = read_rom2(0x00000002);
  33.     if ((SST_id1 == HY29_ID) && (SST_id2 == SST_39VF160))
  34.         ReturnStatus = 0;
  35.     else
  36.         ReturnStatus = -1;
  37. write_rom2( SST39_CMD_ADDRESS1, SST39_READ_ID1);
  38.     write_rom2( SST39_CMD_ADDRESS2, SST39_READ_ID2);  // exit get id
  39.     write_rom2( SST39_CMD_ADDRESS1, SST39_EXIT_READ_ID);
  40. return(ReturnStatus);
  41. }
  42. int Read_39VF160(FILE* stream, DWORD Src,DWORD size)
  43. {
  44. int i,count;
  45. DWORD SrcReadAddress = Src;
  46. WORD wTemp;     // 16 bit access
  47. if( NULL == stream )
  48. return -1;
  49. fseek( stream,0,SEEK_SET );
  50. count = 0;
  51. for( i = 1; i <= (int)(size/sizeof(WORD)); i++ ) 
  52. {
  53. wTemp = read_rom2( SrcReadAddress++ );
  54.         SrcReadAddress++ ;
  55. printf("%.4x ",wTemp);
  56. if( i%8 == 0 ) printf("n");
  57. fwrite((char*)&wTemp,sizeof(WORD),1,stream );
  58. }
  59. return 0;
  60. }
  61. int Write_39VF160(unsigned char *Src, DWORD Dst)
  62. {
  63.     WORD        *SourceBuf;
  64.     DWORD         DestBuf;
  65. DWORD        DestSector;
  66.     int             Index;
  67.     int             Count;
  68.     WORD   SourceWord,Programmedword;
  69.     unsigned char   Continue;
  70.    
  71.     
  72.     /************************************************************************************/
  73.     /*                                      ERASE OPERATION                             */
  74.     /*                                                                                  */ 
  75.     /************************************************************************************/
  76.     DestSector   = Dst;
  77. Sleep(30);
  78. if (DestSector==0x000 || DestSector == 0x4000 || DestSector== 0x6000 || DestSector== 0x4000 || DestSector== 0x8000 || DestSector&0x0000ffff ==0x0000)
  79.     { write_rom2( SST39_CMD_ADDRESS1, SST39_AUTO_PG_ERASE1);
  80.         write_rom2( SST39_CMD_ADDRESS2, SST39_AUTO_PG_ERASE2);
  81. write_rom2( SST39_CMD_ADDRESS1, SST39_AUTO_PG_ERASE3);
  82. write_rom2( SST39_CMD_ADDRESS1, SST39_AUTO_PG_ERASE4);
  83. write_rom2( SST39_CMD_ADDRESS2, SST39_AUTO_PG_ERASE5);
  84. printf("Erasing the sector 0x%.8x ....",DestSector);
  85. write_rom2( DestSector, SST39_AUTO_PG_ERASE6);
  86. printf("on doing.n");
  87. SST39Check_Toggle_Ready(DestSector);
  88.         printf("Erase Done.n");
  89. }
  90. Count=0;
  91. Continue = TRUE;
  92.     
  93.     /***********************************************************************************/
  94.     /*                        PROGRAM  OPERATION                                       */
  95.     /*                                                                                 */
  96.     /***********************************************************************************/
  97.     SourceBuf = (WORD *)Src;
  98.     DestBuf = Dst;
  99.     if ( DestBuf == 0 ) printf("write 0 Kbyte n");
  100.    for (Index = 0; Index < 4096; Index++)
  101.     {
  102.     SourceWord = *SourceBuf++;
  103.         if (SourceWord != 0xFFFF)    // If the data is 0xFF, don,t program it/
  104.     {
  105. write_rom2(SST39_CMD_ADDRESS1, SST39_AUTO_PGRM1); 
  106. write_rom2(SST39_CMD_ADDRESS2, SST39_AUTO_PGRM2);
  107. write_rom2(SST39_CMD_ADDRESS1, SST39_AUTO_PGRM3);
  108. write_rom2(DestBuf,SourceWord);  // program the data //
  109. Programmedword = SST39Check_Toggle_Ready(DestBuf); 
  110.  // wait for Toggle bit ready //
  111.           
  112.           //  Programmedword = read_rom2(DestBuf); // read back the data pragrammed /
  113.          //   printf("addr: 0x%lx read data: 0x%x .n",DestBuf, Programmedword );
  114. if (SourceWord != Programmedword)
  115.             {
  116.                 printf("data compare error 0x%x : 0x%x.n",SourceWord,Programmedword);
  117. Continue = FALSE;
  118.                 break;
  119.             }
  120. }
  121.         DestBuf++; 
  122.     DestBuf++; 
  123.       if ( DestBuf % 1024 == 0)
  124. printf("write %d Kbyte n",  DestBuf/1024); //
  125.     }
  126.  
  127.     if (!Continue)
  128.     return(TRUE);                               /* return with error */
  129.     else
  130.     return(FALSE);                              /* return with NO error */
  131. }
  132. WORD SST39Check_Toggle_Ready(DWORD Dst)
  133. {
  134.     unsigned char Loop = TRUE;
  135.     WORD PreData;
  136.     WORD CurrData;
  137. WORD ReturnData;
  138.     unsigned long TimeOut = 0;
  139.     PreData = read_rom2(Dst);
  140. access_bus2(HOLD, Dst, 0, IP);
  141.     PreData = PreData & 0x4040;
  142.     while ((TimeOut< 0x7FFFFFFF) && (Loop))
  143.     {
  144.         CurrData = read_rom2(Dst);   //操作未完成时,d6会读一次翻转一次。
  145. ReturnData = CurrData;
  146. access_bus2(HOLD, Dst, 0, IP);
  147.       //  printf("data toggle 0x%x .n",CurrData);
  148.         CurrData = CurrData & 0x4040;
  149.         if (PreData == CurrData)
  150.             Loop = FALSE;        /* ready to exit the while loop */
  151.         PreData = CurrData;
  152.         TimeOut++;
  153.     }
  154. return(ReturnData); 
  155. }