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

📄 filesys.c

📁 keil lpc2138 epson6610 lcd source
💻 C
字号:

#include "filesys.h"



 unsigned long cluster;
 unsigned long sektor;
 unsigned long fatStart;
 unsigned long rootStart;
 unsigned long dataStart; 
 unsigned char fatSectorsPerCluster;
 unsigned long fileSize;
 unsigned char dirLevel = 0;
 unsigned long freeSector = 0;	 
 unsigned char currentFileName[12];
 #define MAX_NUMBER_SUBDIRECTORIES 5
 
 struct directoryStack {
   unsigned long sector; 
   unsigned char entry;  
 } dirStack[MAX_NUMBER_SUBDIRECTORIES];



 unsigned char LoadNextSector(void){  
	sectorAddress.l++ ; 			   	  
  if (sectorAddress.b.b1 & 0x80){
    return 0x0d; /*EOF*/
   }		  	 
   if (ReadDiskSector(sectorAddress.l)){
     return 0x0d; /*EOF*/
   }	
   return 0; /* All OK return */
 }
   

 unsigned char FGetChar(){
 		 
 if (!fileSize) return 0; //return 0 for end-of-file
 
  if (dataBufPtr==0){         		 
     ReadDiskSector(sectorAddress.l);
     dataBufPtr = diskSect.raw.buf;
   }
   if (dataBufPtr>diskSect.raw.buf+511){
    /* An end of sector has been reached, read the next sector */  
  LoadNextSector();
    // if (LoadNextSector()){
       /* Error, end-of-file according to FAT records */
     // return 0; /* must return something */
    //}
     dataBufPtr=diskSect.raw.buf;	  
   }  
   /* Everything should now be ok for reading a byte. */  
  fileSize--;
   return (*dataBufPtr++);
 }
 		
  
unsigned char OpenFile(unsigned int fileNumber){
   char tempc;
		  
   /* Start at the start of root directory. */
   dirLevel = 0; /* At root directory */
   dirStack[dirLevel].sector=rootStart;
   dirStack[dirLevel].entry=0;
 	 
 	   
   if (fileNumber==0){
     fileNumber = 32766; //use max-1 value for scanning for free entry
   }
  
   while (fileNumber){
     if (dirStack[dirLevel].entry==0){
       /* At the start of new dir, load first disk sector */
       while (ReadDiskSector(dirStack[dirLevel].sector)){
         InitMMC();
       }
     }
     tempo.c = dirStack[dirLevel].entry;	      

    /* Does current entry point to  a regular file? */
     /* Attributes: NO directory, NO volume id, NO system, NO hidden */
     if (((diskSect.dir[tempo.c].entry.Attr & 222) == 0) 
        && (diskSect.dir[tempo.c].entry.Name[0] != 0xe5) 
         && (diskSect.dir[tempo.c].entry.Name[0] != 0)  ){
      
       /* It is a regular file. */
      if (!(--fileNumber)){
 
         /* ------------ FILE FOUND ------------- */
 		
          sectorAddress.l =  ((unsigned long) diskSect.dir[tempo.c].entry.FstClusHi<<16) 
          + diskSect.dir[tempo.c].entry.FstClusLo;
         sectorAddress.l *= fatSectorsPerCluster;
        sectorAddress.l += dataStart; 
        fileSize = diskSect.dir[tempo.c].entry.FileSize;
         dataBufPtr = 0; /* Reset data buffer ptr for FGetChar */        				    
         
         for (tempc=0; tempc<8; tempc++){
           currentFileName[tempc]=diskSect.dir[tempo.c].entry.Name[tempc];
         }
         currentFileName[8]='.';
         currentFileName[9]=diskSect.dir[tempo.c].entry.Name[8];
         currentFileName[10]=diskSect.dir[tempo.c].entry.Name[9];
         currentFileName[11]=diskSect.dir[tempo.c].entry.Name[10];  
       
        return 0; /* File found, All OK return */
      }
    } /* it was a regular file */
     

 
     /* Is it a subdirectory? */
    if (((diskSect.dir[tempo.c].entry.Attr & 16) != 0) 
         && (diskSect.dir[tempo.c].entry.Name[0] != '.') /* skip . and .. */ 
        && (diskSect.dir[tempo.c].entry.Name[0] != 0xe5) 
       && (diskSect.dir[tempo.c].entry.Name[0] != 0)  ){
 
      /* It is a subdirectory. */
      if (dirLevel<MAX_NUMBER_SUBDIRECTORIES-1){
         /* Yes, we have room in dirStack to traverse deeper. */
         dirLevel++; /* Advance to next directory level */       
         sectorAddress.l = 
           ((unsigned long)diskSect.dir[tempo.c].entry.FstClusHi<<16) 
           + diskSect.dir[tempo.c].entry.FstClusLo;
         sectorAddress.l *= fatSectorsPerCluster;
         sectorAddress.l += dataStart;
       
         /* Prepare for loading. */
         dirStack[dirLevel].sector = sectorAddress.l;
         dirStack[dirLevel].entry = 255; /* Magic number */
                 
       } /* we had room in dirStack */      
     } /* it was a subdirectory */
       

     /* Have we reached the end of the directory? */
     if (diskSect.dir[tempo.c].entry.Name[0] == 0){
       /* It's the end of directory. */

      /* Is it the end of root directory? */
       if (dirLevel == 0){
         /* End of root directory, end of all files in volume */
         //ConsoleWrite("File not found.\r");
         sectorAddress.l = dataStart; 
         /* when in error point to start of data */
         return 0x0c; /* File Not Found return */
       } 
 
      /* End of subdirectory, return from subdirectory */
       dirLevel--;
    ReadDiskSector(dirStack[dirLevel].sector);
    /* restore temp entry pointer */
      tempo.c = dirStack[dirLevel].entry;

  } /* it was end of directory */


     /* Advance to next entry */
     tempo.c++;
  
     /* If we just went to a subdir, set temp entry pointer to 0 */
     if (dirStack[dirLevel].entry == 255){
       /* Magic Number 255: we have gone to a subdirectory */
     tempo.c=0;
   }
     
 if (tempo.c==16){ /* End of sector */
     /* Prepare to load next sector */
     //dirStack[dirLevel].sector = GetNextSector (dirStack[dirLevel].sector);
     tempo.c=0;
    }
 
    dirStack[dirLevel].entry = tempo.c;
   }

  /* Control should never reach this far, end of root directory should
      occur first. */

sectorAddress.l = dataStart; /* when in error point to start of data */
  return 0x0c; /* File Not Found return */
 }



 unsigned char FatInitGlobals(void){	
  
 
   	 cluster = diskSect.fat.BPB_BytsPerSec;   
 		
   /* Determine FAT Type (16/32) */
   /* This should be done better, but it'll do for now. */
  IS_FAT_32 = 1;
   if (diskSect.fat.BPB_RootEntCnt) 
     IS_FAT_32 = 0; /* FAT32 does not have separate root entries. */ 
   						 			 				  
   diskSect.fat.BPB_BytsPerSec /= 512;
 			    
   fatSectorsPerCluster =
     diskSect.fat.BPB_SecPerClus *= diskSect.fat.BPB_BytsPerSec;
   /* Note: BPB_BytsPerSec has already been divided by 512 */  
 		   	
   
   
   fatStart = (unsigned long)sectorAddress.l
     + (unsigned long)diskSect.fat.BPB_RsvdSecCnt 
     * (unsigned long)diskSect.fat.BPB_BytsPerSec;  
  
 				
   rootStart = diskSect.fat.BPB_FATSz16;
   if (rootStart==0){
     if (!IS_FAT_32) 
       return 0x0b; /* should be FAT32; can not find root directory */      
	 rootStart = diskSect.fat.ext._32.BPB_FATSz32;
   }	  
  	
   rootStart *= diskSect.fat.BPB_NumFATs;
   rootStart *= diskSect.fat.BPB_BytsPerSec; /* ADJUSTED BytsPerSec! */
   rootStart += fatStart;	 
  
   dataStart = diskSect.fat.BPB_RootEntCnt >> 4;
   dataStart += rootStart;
   dataStart -= (fatSectorsPerCluster*2); /*first cluster is cluster 2*/  	
   
 	 diskSect.fat.BPB_BytsPerSec *= 512;   		  
			   		 
   return 0;
 }
 
 unsigned char InitFileSystem(){
   unsigned char c;
   
   /* Initialize variables to sane values in case of error exit. */
   fatStart = 0;
   rootStart = 0;
   dataStart = 0;
   freeSector = 0;	  
  	   c=InitStorage();
   if (c){     
     return c; /* Error in InitStorage */
   }	  
 
   /* Load MBR */
   sectorAddress.l = 0; /* the first sector on disk */
   ReadDiskSector(0);     
   /* Ok, it should be a MBR sector. Let's verify */
   if (diskSect.raw.buf[510] != 0x55)
     return 8; /* sector 0 is not MBR. */
   if (diskSect.raw.buf[511] != 0xaa)
     return 8; /* sector 0 is not MBR. */     
 
   /* This checks that partition 1 is active. Alter code to allow
    * other partition configurations. */
	   if ((diskSect.raw.buf[0x00]== 0xEB) | (diskSect.raw.buf[0x00]== 0xE9))     	 
  			sectorAddress.l =0x00;
	   else {

   if (diskSect.raw.buf[0x1b0] == 0x80){
     sectorAddress.b.b0 = diskSect.raw.buf[0x1c6];
     sectorAddress.b.b1 = diskSect.raw.buf[0x1c7];
     sectorAddress.b.b2 = diskSect.raw.buf[0x1c8];
     sectorAddress.b.b3 = diskSect.raw.buf[0x1c9];
     
   }else{
     
     sectorAddress.b.b0 = diskSect.raw.buf[0x1c6];
     sectorAddress.b.b1 = diskSect.raw.buf[0x1c7];
     sectorAddress.b.b2 = diskSect.raw.buf[0x1c8];
     sectorAddress.b.b3 = diskSect.raw.buf[0x1c9];
 
   }    

ReadDiskSector(sectorAddress.l);
		  }
 if (FatInitGlobals()){
    return 0x0a; /* FAT init failed */
   }

  return 0; /* All ok return */

 }

⌨️ 快捷键说明

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