⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sd.i

📁 51环境下的FAT及FAT32文件系统源码
💻 I
📖 第 1 页 / 共 2 页
字号:
 typedef struct _MODE_SELECT_SPC { UINT8 OperationCode;	  UINT8 SavePage : 1 ; UINT8 Reseved0 : 3 ; UINT8 PageFormat : 1 ; UINT8 Reserved1 : 3 ; UINT8 Reserved2[2]; UINT8 ParameterLen; UINT8 Control; } MODE_SELECT_SPC, *pMODE_SELECT_SPC;  typedef struct _MBR_BLOCK { UINT8 Res[454]; unsigned long StartSector; unsigned long TotalSector; UINT8 Res1[50]; } MBR_BLOCK,*pMBR_BLOCK;  typedef struct _BPB_BLOCK { UINT8 BS_jmpBoo[3]; UINT8 BS_OEMName[8]; UINT16 BPB_BytesPerSec; UINT8 BPB_SecPerClus; UINT16 BPB_RsvdSecCnt; UINT8 BPB_NumFATs; UINT16 BPB_RootEntCnt; UINT16 BPB_TotSec16; UINT8 BPB_Media; UINT16 BPB_FATSz16; UINT16 BPB_SecPerTrk; UINT16 BPB_NumHeads; unsigned long BPB_HiddSec; unsigned long BPB_TotSec32; UINT8 BS_DrvNum; UINT8 BS_Reserved1; UINT8 BS_BootSig; UINT8 BS_VolID[4]; UINT8 BS_VolLab[11]; UINT8 BS_FilSysType[8]; UINT8 ExecutableCode[448]; UINT8 Marker[2]; } BPB_BLOCK,*pBPB_BLOCK;  typedef struct _BPB_BLOCK32 { UINT8 BS_jmpBoo[3]; UINT8 BS_OEMName[8]; UINT16 BPB_BytesPerSec; UINT8 BPB_SecPerClus; UINT16 BPB_RsvdSecCnt; UINT8 BPB_NumFATs; UINT16 BPB_RootEntCnt; UINT16 BPB_TotSec16; UINT8 BPB_Media; UINT16 BPB_FATSz16; UINT16 BPB_SecPerTrk; UINT16 BPB_NumHeads; unsigned long BPB_HiddSec; unsigned long BPB_TotSec32; UINT32 BPB_FATSz32; UINT16 BPB_ExtFlags; UINT16 BPB_FSVer; UINT32 BPB_RootClus; UINT16 BPB_FSInfo; UINT16 BPB_BkBootSec; UINT8  BPB_Reserved[12]; UINT8  BS_DrvNum; UINT8  BS_Reserved1; UINT8  BS_BootSig; UINT32 BS_VolID; UINT8  BS_VolLab[11]; UINT8  BS_FilSysType[8];  } BPB_BLOCK32,*pBPB_BLOCK32;  typedef struct _SYS_INFO_BLOCK{ unsigned long StartSector; unsigned long TotalSector;   UINT16 BPB_BytesPerSec; UINT8 BPB_SecPerClus;  UINT8 BPB_NumFATs; UINT16 BPB_RootEntCnt; UINT16 BPB_TotSec16; UINT8 BPB_Media; UINT16 BPB_FATSz16; UINT32 BPB_FATSz32; UINT16 BPB_SecPerTrk; UINT16 BPB_NumHeads; unsigned long BPB_HiddSec; unsigned long BPB_TotSec32; UINT8 BS_DrvNum;  UINT8 BS_BootSig; UINT8 BS_VolID[4]; UINT8 BS_VolLab[11]; UINT8 BS_FilSysType[8];    unsigned long FatStartSector; unsigned long RootStartSector;  unsigned long FirstDataSector;  unsigned long TotCluster; unsigned char bFatType; } SYS_INFO_BLOCK,*pSYS_INFO_BLOCK;  typedef struct _FILE_INFO{ unsigned char bFileOpen; unsigned int StartCluster;     unsigned long LengthInByte; unsigned int ClusterPointer;   unsigned long SectorPointer; unsigned int OffsetofSector;  unsigned char SectorofCluster;  unsigned long pointer;	      } FILE_INFO, *pFILE_INFO;  typedef struct _DIR_INFO{ unsigned char name[8];        unsigned char extension[3];   unsigned char attribute;      unsigned char Reserved[10];   unsigned int lastUpdateDate;   unsigned int lastUpdateTime;   unsigned int startCluster;     unsigned long length;          } DIR_INFO, *pDIR_INFO;  typedef struct _MODE_SENSE_SPC { UINT8 OperationCode;	      UINT8 Reseved0 : 3 ; UINT8 DisableBlockDescriptor : 1 ; UINT8 Reserved0 : 4 ; UINT8 PageCode:6 ; UINT8 PageControl : 2 ; UINT8 Reserved1; UINT8 ParameterLen; UINT8 Control; } MODE_SENSE_SPC, *pMODE_SENSE_SPC;      typedef struct _MEDIA_REMOVAL_SPC { UINT8 OperationCode;      UINT8 Reserved0[3]; UINT8 Prevent;   } MEDIA_REMOVAL_SPC, *pMEDIA_REMOVAL_SPC;   typedef struct _REQUEST_SENSE_SPC { UINT8 OperationCode;      UINT8 Reserved[3]; UINT8 AllocationLen; UINT8 Control; } REQUEST_SENSE_SPC, *pREQUEST_SENSE_SPC;   typedef struct _TEST_UNIT_SPC { UINT8 OperationCode;      UINT8 Reserved[4]; UINT8 Control; } TEST_UNIT_SPC, *pTEST_UNIT_SPC;    typedef struct _WRITE_BUFFER_SPC { UINT8 OperationCode;     UINT8 Mod:4 ; UINT8 Reserved0:4 ; UINT8 BufferID; UINT8 BufferOff_2; UINT8 BufferOff_1; UINT8 BufferOff_0; UINT8 ParameterLen_2; UINT8 ParameterLen_1; UINT8 ParameterLen_0; UINT8 Control; } WRITE_BUFFER_SPC, *pWRITE_BUFFER_SPC;	    typedef union _CDB_RBC { GENERIC_CDB             Cdb_Generic;   GENERIC_RBC             RbcCdb_Generic;  FORMAT_RBC              RbcCdb_Format; READ_RBC                RbcCdb_Read; READ_CAPACITY_RBC       RbcCdb_ReadCapacity; START_STOP_RBC          RbcCdb_OnOffUnit; SYNCHRONIZE_CACHE_RBC   RbcCdb_SyncCache; VERIFY_RBC              RbcCdb_Verify; WRITE_RBC               RbcCdb_Write;     INQUIRY_SPC             SpcCdb_Inquiry; MODE_SELECT_SPC         SpcCdb_ModeSelect; MODE_SENSE_SPC          SpcCdb_ModeSense; MEDIA_REMOVAL_SPC       SpcCdb_Remove; REQUEST_SENSE_SPC       SpcCdb_RequestSense; TEST_UNIT_SPC           SpcCdb_TestUnit; WRITE_BUFFER_SPC        SpcCdb_WriteBuffer;   READ_10         CmdRead10; WRITE_10        CmdWrite10; MODE_SELECT_10  CmdModeSel10; MODE_SENSE_10   CmdModeSen10;  READ_LONG_CMD	SpcCdb_ReadLong;  } CDB_RBC, *pCDB_RBC;  #line 8 "sd.c" /0    extern SYS_INFO_BLOCK xdata DeviceInfo;  extern UINT8 xdata DBUF[512];    extern UINT8 xdata FATBUF[512]; extern UINT8 xdata RDIRBUF[512]; extern UINT8 xdata respBuf[16];  unsigned int c_size, c_size_mult, read_bl_len; unsigned long drive_size, maxsect;  unsigned char GetDevInfo( ) { unsigned long FATSz; unsigned long TotSec; unsigned long RootDirSectors; unsigned long DataSec; unsigned long CountofClusters;  pMBR_BLOCK pMBR; pBPB_BLOCK pBPB; pBPB_BLOCK32 pBPB32;  pMBR=(pMBR_BLOCK)FATBUF;  DeviceInfo.BPB_BytesPerSec=512;     if(!SDReadFat(0x0)) return 0;  SendUart(FATBUF, 512);     if(FATBUF[0]==0xeb||FATBUF[0]==0xe9) { DeviceInfo.StartSector=0; } else {  DeviceInfo.StartSector=SwapINT32(pMBR->StartSector); }  DelayUs(5); pBPB=(pBPB_BLOCK)FATBUF; if(!SDReadFat(DeviceInfo.StartSector)) return 0;  SendUart(FATBUF, 512);     DeviceInfo.BPB_BytesPerSec=WordSwap(pBPB->BPB_BytesPerSec); DeviceInfo.BPB_SecPerClus=pBPB->BPB_SecPerClus; DeviceInfo.BPB_NumFATs=pBPB->BPB_NumFATs; DeviceInfo.BPB_RootEntCnt=WordSwap(pBPB->BPB_RootEntCnt); DeviceInfo.BPB_TotSec16=WordSwap(pBPB->BPB_TotSec16); DeviceInfo.BPB_FATSz16=WordSwap(pBPB->BPB_FATSz16); DeviceInfo.BPB_FATSz32=WordSwap(pBPB32->BPB_FATSz32); DeviceInfo.BPB_TotSec32=SwapINT32(pBPB->BPB_TotSec32); DeviceInfo.FatStartSector=DeviceInfo.StartSector+WordSwap(pBPB->BPB_RsvdSecCnt); DeviceInfo.RootStartSector=DeviceInfo.FatStartSector+2*DeviceInfo.BPB_FATSz16; DeviceInfo.FirstDataSector=DeviceInfo.RootStartSector+0x20;     if(DeviceInfo.BPB_FATSz16 != 0) FATSz = DeviceInfo.BPB_FATSz16; else FATSz = DeviceInfo.BPB_FATSz32;    if(DeviceInfo.BPB_TotSec16 != 0) TotSec = DeviceInfo.BPB_TotSec16; else TotSec = DeviceInfo.BPB_TotSec32;  RootDirSectors = ((DeviceInfo.BPB_RootEntCnt * 32) + (DeviceInfo.BPB_BytesPerSec - 1)) / DeviceInfo.BPB_BytesPerSec; DataSec = TotSec - (WordSwap(pBPB->BPB_RsvdSecCnt) + (DeviceInfo.BPB_NumFATs * FATSz) + RootDirSectors); CountofClusters = DataSec / DeviceInfo.BPB_SecPerClus;  if(CountofClusters < 4085)  { DeviceInfo.bFatType = 1;   }  else if(CountofClusters < 65525)  { DeviceInfo.bFatType = 2;    }  else  { DeviceInfo.bFatType = 3;   DeviceInfo.TotCluster = (DeviceInfo.BPB_TotSec32-DeviceInfo.FirstDataSector+1)/DeviceInfo.BPB_SecPerClus+1;  }  }  unsigned char SDCommand(unsigned char command, unsigned char *pAddr)  { unsigned char i, addr[4];  for (i=0;i<4;i++) { addr[i] = *pAddr; pAddr++; }   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };        { SPI_DATA=(command); while(!(SPI_STA0&0x1)); };  { SPI_DATA=(addr[0]); while(!(SPI_STA0&0x1)); };      { SPI_DATA=(addr[1]); while(!(SPI_STA0&0x1)); };  { SPI_DATA=(addr[2]); while(!(SPI_STA0&0x1)); };  { SPI_DATA=(addr[3]); while(!(SPI_STA0&0x1)); };      { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };          return (1); }  unsigned char SDMp3Read(unsigned long sector)  { unsigned int i; unsigned long startadr;       ((unsigned char volatile xdata *) 0)[0xF80C] = 0x3;                 ((unsigned char volatile xdata *) 0)[0xF80C] = 0x0;    startadr=sector * (unsigned long)512;  SDCommand(0x40 + 17, (unsigned char *)&startadr); do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while(!(SPI_STA0 & 0x2));    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while (SPI_DATA != 0x0);    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while (SPI_DATA & 0x1);    for(i=0; i<512; i++) {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };         ((unsigned char volatile xdata *) 0)[0xF80E]=SPI_DATA;    }    { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };     { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };  return 1; }  unsigned char SDReadSector(unsigned long sector)  { unsigned int i; unsigned long startadr;  if(sector>=maxsect) return 1;     startadr=sector * (unsigned long)512;   SDCommand(0x40 + 17, (unsigned char *)&startadr);  do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while(!(SPI_STA0 & 0x2));    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while (SPI_DATA != 0x0);    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while (SPI_DATA & 0x1);    for(i=0; i<512; i++) {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };       DBUF[i]=SPI_DATA;    }    { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };     { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };  return 1; }   unsigned char SDReadFat(unsigned long sector)  { unsigned int i; unsigned long startadr;  if(sector>=maxsect) return 1;     startadr=sector * (unsigned long)512;   SDCommand(0x40 + 17, (unsigned char *)&startadr);  do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while(!(SPI_STA0 & 0x2));    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while (SPI_DATA != 0x0);    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while (SPI_DATA & 0x1);    for(i=0; i<512; i++) {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };       FATBUF[i]=SPI_DATA;    }    { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };     { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };  return 1; }  unsigned char SDReadRdir(unsigned long sector)  { unsigned int i; unsigned long startadr;  if(sector>=maxsect) return 1;     startadr=sector * (unsigned long)512;   SDCommand(0x40 + 17, (unsigned char *)&startadr);  do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while(!(SPI_STA0 & 0x2));    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while (SPI_DATA != 0x0);    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while (SPI_DATA & 0x1);    for(i=0; i<512; i++) {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };       RDIRBUF[i]=SPI_DATA;    }    { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };     { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };  return 1; }  unsigned char SDWriteSector(unsigned long sector, unsigned char *pDBUF)  { unsigned int i; unsigned char by; unsigned long startadr;  if(sector>=maxsect) return 1;      startadr=sector<<9;  SDCommand(0x40 + 24,(unsigned char *)&startadr);   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };    { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };   { SPI_DATA=(0xFE); while(!(SPI_STA0&0x1)); };    for(i=0; i<512; i++) {  { SPI_DATA=(*pDBUF++); while(!(SPI_STA0&0x1)); }; }   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };    { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };   by=SPI_DATA & 0x1F; if(by != 0x05)     { return 1; }  do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; }while(SPI_DATA !=0xFF);    return 0; }   unsigned char SDIdentify(void)  { unsigned char by; unsigned int i; unsigned long addr;             SPI_CTL0 = 0x4E;   SPI_CTL1 = 0x2;     for(i=0; i<10; i++) { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };       { SPI_DATA=(0x40 + 0); while(!(SPI_STA0&0x1)); };    { SPI_DATA=(0x00); while(!(SPI_STA0&0x1)); };  { SPI_DATA=(0x00); while(!(SPI_STA0&0x1)); };  { SPI_DATA=(0x00); while(!(SPI_STA0&0x1)); };  { SPI_DATA=(0x00); while(!(SPI_STA0&0x1)); };  { SPI_DATA=(0x95); while(!(SPI_STA0&0x1)); };    { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };    while(!(SPI_STA0&0x2));  respBuf[0]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[1]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[2]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[3]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[4]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[5]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[6]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[7]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };  SendUart(respBuf, 8); DelayUs(100);  addr = 0;  do { SDCommand(0x40 + 1,(unsigned char *) &addr);  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));     respBuf[0]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[1]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[2]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[3]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[4]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[5]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[6]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));  respBuf[7]=SPI_DATA;   { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); };  SendUart(respBuf, 8);    }while(respBuf[2]!=0);    DelayUs(100);       SDCommand(0x40 + 9,(unsigned char *) &addr);   do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));     }while (SPI_DATA != 0);    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));     }while (SPI_DATA & 0x1);    SDResp(18); SendUart(respBuf,18);         c_size=respBuf[6] & 0x03;   c_size<<=10; c_size+=(unsigned int)respBuf[7]<<2; c_size+=respBuf[8]>>6;  by= respBuf[5] & 0x0F; read_bl_len=1; read_bl_len<<=by;  by=respBuf[9] & 0x03; by<<=1; by+=respBuf[10] >> 7;  c_size_mult=1; c_size_mult<<=(2+by);  drive_size=(unsigned long)(c_size+1) * (unsigned long)c_size_mult * (unsigned long)read_bl_len; maxsect= drive_size / 512;  SendUart((unsigned char *)&drive_size,4); SendUart((unsigned char *)&maxsect,4);  DelayUs(50); addr = 512; SDCommand(0x40 + 16,(unsigned char *) &addr); do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));     }while (SPI_DATA != 0);    addr = 0; SDCommand(0x40 + 13,(unsigned char *) &addr); do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));     }while (SPI_DATA != 0);    do {  { SPI_DATA=(0xFF); while(!(SPI_STA0&0x1)); }; while(!(SPI_STA0&0x2));     }while (SPI_DATA & 0x1);    SDResp(2); SendUart(respBuf,2);  return 0; }  unsigned char SDResp(unsigned char respLen) { unsigned char i;  for (i=0;i<respLen;i++) { SPI_DATA = 0xff; while(!(SPI_STA0&0x2));     respBuf[i] = SPI_DATA; }  return(1);	 }  unsigned char UartWaitCmd() { unsigned char uCmd; unsigned char uArg; unsigned char skipSong;  while (!(SCON & 0x1)); SCON = 0x50; uCmd = SBUF;  while (!(SCON & 0x1)); SCON = 0x50; uArg = SBUF;     if (!(uCmd ^ 0x1)) {   ((unsigned char volatile xdata *) 0)[0xF403] = uArg; uCmd = ((unsigned char volatile xdata *) 0)[0xF403]; SendUart(&uCmd, 1); } else if (!(uCmd ^ 0x2)) {   ((unsigned char volatile xdata *) 0)[0xF404] = uArg; uCmd = ((unsigned char volatile xdata *) 0)[0xF404]; SendUart(&uCmd, 1); } else if (!(uCmd ^ 0x3)) { skipSong = uArg; uCmd = skipSong; SendUart(&uCmd, 1); } else  { uCmd = 0xAA; SendUart(&uCmd, 1); return (0); } return (1); } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -