DEVICE.C
上传用户:jcsy2001
上传日期:2013-11-29
资源大小:201k
文件大小:4k
开发平台:

C/C++

  1. #include "common.h"
  2. #include "DEVICE.H"
  3. #include "HAL.H"
  4. #include "HPI.H"
  5. #include "HPI32.H"
  6. //////////////////////////////////
  7. extern unsigned char xdata DBUF[BUFFER_LENGTH];
  8. extern SYS_INFO_BLOCK xdata DeviceInfo;
  9. extern FILE_INFO xdata ThisFile;
  10. extern unsigned int xdata DirStartCluster;
  11. extern unsigned long xdata DirStartCluster32;
  12. unsigned char xdata DBUF[BUFFER_LENGTH];
  13. ///////////////////////////////////////////////////////////////////////////
  14. unsigned char InitFileSystem(void)
  15. {
  16. unsigned int ReservedSectorsNum;
  17. ////////////////////////////////////////////////////
  18. DeviceInfo.BPB_BytesPerSec=512; //暂假设为512
  19. ////////////////////////////////////////////////////
  20. if(!SdReadSector(0x0,1,DBUF))
  21. return FALSE;
  22.     if(DBUF[510] != 0x55 || DBUF[511] != 0xaa) return FALSE;
  23. //////////////////////////////////
  24. if(DBUF[0]==0xeb||DBUF[0]==0xe9)
  25. {
  26. DeviceInfo.StartSector=0;
  27. }
  28. else
  29. {
  30.  if(DBUF[446] != 0x80 && DBUF[446] != 0)  return FALSE;
  31.  DeviceInfo.StartSector=LSwapINT32(DBUF[454],DBUF[455],DBUF[456],DBUF[457]);
  32. }
  33. ///////////////////////////////////////////////////////
  34. if(!SdReadSector(DeviceInfo.StartSector,1,DBUF))
  35. return FALSE;
  36. if(DBUF[510] != 0x55 || DBUF[511] != 0xaa) return FALSE;
  37. DeviceInfo.BPB_BytesPerSec=LSwapINT16(DBUF[11],DBUF[12]);
  38. DeviceInfo.BPB_SecPerClus=DBUF[13];
  39. ReservedSectorsNum=LSwapINT16(DBUF[14],DBUF[15]);
  40. DeviceInfo.BPB_NumFATs=DBUF[16];
  41. if(DBUF[82]=='F'&&DBUF[83]=='A'&&DBUF[84]=='T'&&DBUF[85]=='3'&&DBUF[86]=='2')
  42. {
  43. DeviceInfo.BPB_RootEntCnt=LSwapINT16(DBUF[17],DBUF[18]);
  44. DeviceInfo.BPB_RootEntCnt=(DeviceInfo.BPB_RootEntCnt)*32/DeviceInfo.BPB_BytesPerSec;
  45. DeviceInfo.BPB_TotSec32=LSwapINT32(DBUF[32],DBUF[33],DBUF[34],DBUF[35]);
  46. DeviceInfo.BPB_FATSz32=LSwapINT32(DBUF[36],DBUF[37],DBUF[38],DBUF[39]);
  47. DeviceInfo.RootStartCluster=LSwapINT32(DBUF[44],DBUF[45],DBUF[46],DBUF[47]);
  48. DeviceInfo.FatStartSector=DeviceInfo.StartSector+ReservedSectorsNum;
  49. DeviceInfo.FirstDataSector=DeviceInfo.FatStartSector+DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz32;
  50. //DeviceInfo.TotCluster=(DeviceInfo.BPB_TotSec32-DeviceInfo.FirstDataSector+1)/DeviceInfo.BPB_SecPerClus+1;
  51. DeviceInfo.TotCluster=(DeviceInfo.BPB_TotSec32-ReservedSectorsNum-DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz32-DeviceInfo.BPB_RootEntCnt)/DeviceInfo.BPB_SecPerClus;
  52. DirStartCluster32=DeviceInfo.RootStartCluster;
  53. DeviceInfo.FAT=1; //FAT16=0,FAT32=1;
  54. }
  55. else
  56. {
  57. DeviceInfo.BPB_RootEntCnt=LSwapINT16(DBUF[17],DBUF[18]);
  58. DeviceInfo.BPB_RootEntCnt=(DeviceInfo.BPB_RootEntCnt)*32/DeviceInfo.BPB_BytesPerSec;
  59. DeviceInfo.BPB_TotSec16=LSwapINT16(DBUF[19],DBUF[20]);
  60. if(DeviceInfo.BPB_TotSec16==0)
  61.   DeviceInfo.BPB_TotSec16=LSwapINT32(DBUF[32],DBUF[33],DBUF[34],DBUF[35]);
  62. DeviceInfo.BPB_FATSz16=LSwapINT16(DBUF[22],DBUF[23]);
  63. DeviceInfo.FatStartSector=DeviceInfo.StartSector+ReservedSectorsNum;
  64. DeviceInfo.RootStartSector=DeviceInfo.StartSector+DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz16+ReservedSectorsNum;
  65. DeviceInfo.FirstDataSector=DeviceInfo.FatStartSector+DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz16+DeviceInfo.BPB_RootEntCnt;
  66. DeviceInfo.TotCluster=(DeviceInfo.BPB_TotSec16-DeviceInfo.BPB_RootEntCnt-DeviceInfo.BPB_NumFATs*DeviceInfo.BPB_FATSz16-1)/DeviceInfo.BPB_SecPerClus;
  67.         if(DeviceInfo.TotCluster<4085) return FALSE; //FAT12 不被支持
  68. DeviceInfo.FAT=0;
  69. }
  70. ///////////////////////////////////////////////////////
  71. ThisFile.bFileOpen=0;
  72. ///////////////////////////////////////////////////////
  73. return TRUE;
  74. }
  75. unsigned char SdReadSector(unsigned long sector,unsigned char len,unsigned char *pBuffer)
  76. {
  77. while(len--)
  78.  {
  79.   if(SdReadBlock(pBuffer,sector<<9)==0) 
  80.              return 0;
  81.       pBuffer+=512;
  82.  }
  83.   return 1;
  84. }
  85. unsigned char SdWriteSector(unsigned long sector,unsigned char len,unsigned char *pBuffer)
  86. {
  87. while(len--)
  88.  {
  89.   if(SdWriteBlock(pBuffer,sector<<9)==0) return 0;
  90.       pBuffer+=512;
  91.  }
  92.  return 1;  
  93. }