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

📄 mmc_spi_fat32.c

📁 MMC_SPI_FAT32 一个国外网友编写的程序
💻 C
📖 第 1 页 / 共 4 页
字号:
   } 
   changeList = TRUE; 
   return MMC_OK; 
} 

MMCResponse CloseList(char f) 
{ 
   if (f > (MAXFILES-1)) 
      return MMC_INVALID_FILE; 
   gFiles[f].Free = TRUE; 
   return MMC_OK; 
} 

void FreeList() 
{ 
   int i; 
   for(i=0;i<MAX_FILE_LIST;i++) 
   { 
      if(FileList[i].isLong)           // If it is a long filename, name and short name are different string 
         free(FileList[i].shortName); // then free both 
      else 
         FileList[i].shortName = NULL; 
      free(FileList[i].name);          // else free ONLY name(they point to same string..) 
   } 
} 
#endif// ENABLE_FILELISTNG 

// Function: Creates a file 
MMCResponse fcreate(char f,char *fname) 
{ 
   DIR *pDir; 
   int32 actsector,actcl; 
   int16 i; 

   if (f > (MAXFILES-1)) 
   { 
      return MMC_INVALID_FILE; 
   } 
   if (gFAT32Vars.gFirstDirEntryCluster == 0x0FFFFFFF) 
   { 
      // extend the directory file !!! 
      gFAT32Vars.gFirstDirEntryCluster = FindFirstFreeCluster(); 
      gFAT32Vars.gFirstEmptyDirEntry = 0; 
      SetClusterEntry(gFiles[f].CurrentCluster,gFAT32Vars.gFirstDirEntryCluster); 
      SetClusterEntry(gFAT32Vars.gFirstDirEntryCluster,0x0FFFFFFF); 
      actsector = gFAT32Vars.gFirstDirEntryCluster + gFAT32Vars.gFirstDataSector; 
      for (i=0;i<512;i++) 
         gFiles[f].IOpuffer[i] = 0; 
      WriteSector(actsector,gFiles[f].IOpuffer); 
   } 
   actsector = gFAT32Vars.gFirstDirEntryCluster + gFAT32Vars.gFirstDataSector; 
   ReadSector(actsector,gFiles[f].IOpuffer); 
   pDir = (DIR*)(&(gFiles[f].IOpuffer[32*(int16)gFAT32Vars.gFirstEmptyDirEntry])); 
   gFiles[f].dirSector = actsector; 
   gFiles[f].dirIdx = gFAT32Vars.gFirstEmptyDirEntry; 
   GetDOSName(pDir,fname); 
   pDir->bAttr = 0; 
   actcl = FindFirstFreeCluster(); 
   pDir->hCluster = actcl & 0xFFFF; 
   pDir->hClusterH = actcl >> 16; 
   SetClusterEntry(actcl,0x0FFFFFFF); 
   pDir->wSize = 0; 
   gFiles[f].position = 0; 
   pDir->hDate = GetCurrentDOSDate(); 
   pDir->hTime = GetCurrentDOSTime(); 
   WriteSector(actsector,gFiles[f].IOpuffer); 
   memcpy(&(gFiles[f].DirEntry),pDir,32); 
   return MMC_OK; 
} 

int32 ComposeCluster(char f) 
{ 
   int32 retval; 

   retval = gFiles[f].DirEntry.hClusterH; 
   retval <<= 16; 
   retval |= gFiles[f].DirEntry.hCluster; 
   return retval; 
} 

// Function: Opens a file with the specified mode 
// Returns : A file handle or error code 
MMCResponse fopen(char *fname, char mode) 
{ 
   char found; 
   char f; 
   int32 actsector,actcluster,nextcluster; 
   char *filename; 

   if (!CardInserted()) 
      return MMC_NO_CARD_INSERTED; 

   filename = TryFile(fname,&f); 
   if (filename == 0) 
   { 
      return MMC_NOT_FOUND; // probebly invalid directory 
   } 
   found = FALSE; 
   found = FindDirEntry(filename,f); 

   if (!found) 
   { 
      if (mode == 'r') 
      { 
         gFiles[f].Free = TRUE; 
         return MMC_NOT_FOUND; 
      } 
      else 
      { 
         if (fcreate(f,filename) != MMC_OK) 
            return MMC_NOT_FOUND; 
         found = TRUE; 
      } 
   } 
   if (found) 
   { 
      gFiles[f].Free = FALSE; 
      gFiles[f].mode = mode; 
      if  (mode == 'a') 
      { 
         gFiles[f].position = gFiles[f].DirEntry.wSize; 
         actcluster = ComposeCluster(f); 
         while (actcluster != 0x0FFFFFFF && nextcluster != 0) 
         { 
            nextcluster = GetNextCluster(actcluster); 
            if (nextcluster == 0x0FFFFFFF || nextcluster == 0) 
               break; 
            actcluster = nextcluster; 
         } 
         actsector = actcluster + gFAT32Vars.gFirstDataSector; 
         ReadSector(actsector,gFiles[f].IOpuffer); 
         gFiles[f].CurrentCluster = actcluster; 
         gFiles[f].posinsector = gFiles[f].position & 0x01FF; 
         if (gFiles[f].posinsector == 0 && gFiles[f].position != 0) 
            gFiles[f].posinsector = 512; 
      } 
      else 
      { 
         gFiles[f].position = 0; 
         actsector = ComposeCluster(f); 
         actsector += gFAT32Vars.gFirstDataSector; 
         ReadSector(actsector,gFiles[f].IOpuffer); 
         gFiles[f].CurrentCluster = ComposeCluster(f); 
         gFiles[f].posinsector = 0; 
      } 
   } 
   return f; 
} 

// Function: closes a open file and makes it avavible for a new file 
MMCResponse fclose(char f) 
{ 
   if (f > (MAXFILES-1)) 
      return MMC_INVALID_FILE; 
   if ((gFiles[f].mode == 'a') || (gFiles[f].mode == 'w')) 
      fflush(f); 
   gFiles[f].Free = TRUE; 
   return MMC_OK; 
} 

// Function: writes the chach to the MMC 
MMCResponse fflush(char f) 
{ 
   int32 actsector; 
   DIR *pDir; 

   if (f > (MAXFILES-1)) 
      return MMC_INVALID_FILE; 
   actsector = gFiles[f].CurrentCluster + gFAT32Vars.gFirstDataSector; 
   WriteSector(actsector,gFiles[f].IOpuffer); 
   ReadSector(gFiles[f].dirSector,gFiles[f].IOpuffer); 
   pDir = (DIR*)(&(gFiles[f].IOpuffer[32*gFiles[f].dirIdx])); 
   if (gFiles[f].DirEntry.bAttr & 0x10)  // if it is a directory 
      pDir->wSize = 0; 
   else 
      pDir->wSize = gFiles[f].position; 
   pDir->hDate = GetCurrentDOSDate(); 
   pDir->hTime = GetCurrentDOSTime(); 
   WriteSector(gFiles[f].dirSector,gFiles[f].IOpuffer); 
   ReadSector(actsector,gFiles[f].IOpuffer); 
   return MMC_OK; 
} 

// Function: Enter a specified directory 
char cwd(char *fname, char f) 
{ 
   int32 actsector; 
   if (f > (MAXFILES-1)) 
   { 
      return FALSE; // just in case of overaddressing 
   } 
   if (IsSelfDir(fname)) 
   { 
      return TRUE; // already in Root dir 
   } 
   if (!FindDirEntry(fname,f)) 
   { 
      return FALSE; // not found 
   } 

   actsector = ComposeCluster(f); 
   actsector += gFAT32Vars.gFirstDataSector; // read current dir 
   ReadSector(actsector,gFiles[f].IOpuffer); 
   gFAT32Vars.gDirEntrySector = actsector; 
   gFiles[f].dirSector = actsector; 
   gFiles[f].CurrentCluster = ComposeCluster(f); 

   return TRUE; 
} 

// Function: Put a char to the open file 
MMCResponse fputch(char be, char f) 
{ 
   int32 nextcluster,actsector; 

   if (f > (MAXFILES-1)) 
      return MMC_INVALID_FILE; 
   if (gFiles[f].posinsector == 512) 
   { 
      actsector = gFiles[f].CurrentCluster + gFAT32Vars.gFirstDataSector; 
      WriteSector(actsector,gFiles[f].IOpuffer); 
      nextcluster = FindFirstFreeCluster(); 
      if (nextcluster != 0x0FFFFFFF && nextcluster != 0) 
      { 
         SetClusterEntry(gFiles[f].CurrentCluster,nextcluster); 
         SetClusterEntry(nextcluster,0x0FFFFFFF); 
         actsector = nextcluster + gFAT32Vars.gFirstDataSector; 
         ReadSector(actsector,gFiles[f].IOpuffer); 
         gFiles[f].CurrentCluster = nextcluster; 
         gFiles[f].posinsector = 0; 
      } 
   } 
   gFiles[f].IOpuffer[gFiles[f].posinsector] = be; 
   gFiles[f].posinsector++; 
   gFiles[f].position++; 
   return MMC_OK; 
} 

// Function: Puts a string to the open file 
MMCResponse fputstring(char *be, char f) 
{ 
   int16 leng,i; 

   if (f > (MAXFILES-1)) 
      return MMC_INVALID_FILE; 
   leng = strlen(be); 
   for (i=0;i<leng;i++) 
      fputch(be[i],f); 
   return MMC_OK; 
} 

// Function: Read a buffer from the open file 
int16 fread(char *buffer, int16 leng, char f) 
{ 
   int16 i,retv; 
   char c,v; 

   if (f > (MAXFILES-1)) 
      return 0; 
   retv = 0; 
   for (i=0;i<leng;i++) 
   { 
      v = fgetch(&c,f); 
      if (v == MMC_OK) 
      { 
         buffer[i] = c; 
         retv++; 
      } 
      else 
         break; 
   } 
   return retv; 
} 

// Function: Write a buffer to the open file 
MMCResponse fwrite(char *buffer, int16 leng, char f) 
{ 
   int16 i; 

   if (f > (MAXFILES-1)) 
      return MMC_INVALID_FILE; 
   for (i=0;i<leng;i++) 
      fputch(buffer[i],f); 
   return MMC_OK; 
} 

// Function: Read a char from the open file 
MMCResponse fgetch(char *ki,char f) 
{ 
   int32 nextcluster,actsector; 

   if (f > (MAXFILES-1)) 
      return MMC_INVALID_FILE; 
   if (gFiles[f].position >= gFiles[f].DirEntry.wSize) 
      return MMC_INVALID_POSITION; 
   *ki = gFiles[f].IOpuffer[gFiles[f].posinsector]; 
   gFiles[f].posinsector++; 
   gFiles[f].position++; 
   if (gFiles[f].posinsector == 512) 
   { 
      nextcluster = GetNextCluster(gFiles[f].CurrentCluster); 
      if (nextcluster != 0x0FFFFFFF && nextcluster != 0) 
      { 
         actsector = nextcluster + gFAT32Vars.gFirstDataSector; 
         ReadSector(actsector,gFiles[f].IOpuffer); 
         gFiles[f].CurrentCluster = nextcluster; 
         gFiles[f].posinsector = 0; 
      } 
   } 
   return MMC_OK; 
} 

// Function: Removes a file 
MMCResponse remove(char *fname) 
{ 
   char i,found; 
   char f; 
   DIR *pDir; 
   int32 nextcluster,currentcluster; 
   char *filename; 

   filename = TryFile(fname,&f); 
   if (filename == 0) 
      return MMC_NOT_FOUND; 
   found = FindDirEntry(filename,f); 
   if (!found) 
   { 
      gFiles[f].Free = TRUE; 
      return MMC_NOT_FOUND; 
   } 

   pDir = (DIR*)(&(gFiles[f].IOpuffer[32*(int16)gFAT32Vars.gDirEntryIdx])); 
   pDir->sName[0] = 0xE5; 
   for (i=1;i<8;i++) 
      pDir->sName[i] = ' '; 
   for (i=0;i<3;i++) 
      pDir->spam[i] = ' '; 
   WriteSector(gFAT32Vars.gDirEntrySector,gFiles[f].IOpuffer); 
   currentcluster = ComposeCluster(f); 
   while (currentcluster != 0x0FFFFFFF && nextcluster != 0) 
   { 
      nextcluster = GetNextCluster(currentcluster); 
      ClearClusterEntry(currentcluster); 
      currentcluster = nextcluster; 
   } 
   ClearClusterEntry(currentcluster); 
   SetClusterEntry(currentcluster,0); 
   currentcluster = gFAT32Vars.gStartSector+DiskInfo.Reserved1 + gFAT32Vars.FATstartidx; 
   WriteSector(currentcluster,FATTable); 
   currentcluster += DiskInfo.hSectorsPerFat; 
   WriteSector(currentcluster,FATTable); 
   gFiles[f].Free = TRUE; 

   return MMC_OK; 
} 


// Function: Gets the size of a file 
MMCResponse getfsize(char *fname, int32 *fsiz) 
{ 
   char found; 
   char f; 
   DIR *pDir; 
   char *filename; 

   filename = TryFile(fname,&f); 
   if (filename == 0) 
      return MMC_NOT_FOUND; 
   found = FindDirEntry(filename,f); 
   if (!found) 
   { 
      gFiles[f].Free = TRUE; 
      return MMC_NOT_FOUND; 
   } 
   pDir = (DIR*)(&(gFiles[f].IOpuffer[32*(Int16)gFAT32Vars.gDirEntryIdx])); 
   gFiles[f].Free = TRUE; 
   *fsiz = pDir->wSize; 
   return MMC_OK; 
} 

⌨️ 快捷键说明

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