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

📄 api32.c

📁 ISP1362 Fat32 coldfire523x
💻 C
📖 第 1 页 / 共 2 页
字号:
      {
        return FALSE;		   		
      }
    }
    else
    {
      if(! WriteSector((pBuffer + tlen), ThisFile.SectorPointer, 1))
      {
        return FALSE;		   		
      }

      ThisFile.OffsetofSector = len;
    }

    writeLength -= len;
    tlen += len;
		
    /*******************************更新文件指针*********************************/
    if(ThisFile.OffsetofSector > (DeviceInfo.BPB_BytesPerSec - 1))
    {	
      ThisFile.OffsetofSector -= DeviceInfo.BPB_BytesPerSec;
      ThisFile.SectorofCluster += 1;

      if(ThisFile.SectorofCluster > (DeviceInfo.BPB_SecPerClus-1))
      {
        ThisFile.SectorofCluster = 0;
        ThisFile.ClusterPointer = CreateClusterLink32(ThisFile.ClusterPointer);
		 		
        if(ThisFile.ClusterPointer==0x00)
        {
          return FALSE;		 		
        }
        ThisFile.SectorPointer = FirstSectorofCluster32(ThisFile.ClusterPointer); 	
      }
      else
      {
        ThisFile.SectorPointer = (ThisFile.SectorPointer + 1);
      }
    }	
  }/*end while*/
	
  ThisFile.pointer += tlen;
  bSuccess = 1;

  /**********************************更新文件目录信息*******************************/
  if(bSuccess)
  {
    NowCluster32 = ThisFile.FDTClusterPointer;		
    //NowSector = FirstSectorofCluster32(NowCluster32) + ThisFile.FDTSectorPointer;
    NowSector = ThisFile.FDTSectorPointer;
    InSector = ThisFile.FDTOffsetOfSector;
    ThisFile.LengthInByte = ThisFile.pointer;
	
    if(! ReadSector(DBUF, NowSector, 1))
    {
      return FALSE;
    }
		
		//更新文件大小
    DBUF[InSector + 28] = (uint8)(ThisFile.LengthInByte & 0xff);
    DBUF[InSector + 29] = (uint8)((ThisFile.LengthInByte >> 8) & 0xff);
    DBUF[InSector + 30] = (uint8)((ThisFile.LengthInByte >> 16) & 0xff);
    DBUF[InSector + 31] = (uint8)((ThisFile.LengthInByte >> 24) & 0xff);
    
    //更新文件时间
    DirCreatTime = ((uint16)RiliShizhong[3] << 11) | ((uint16)RiliShizhong[4] << 5) | (((uint16)RiliShizhong[5]+1) >> 1);
    DirCreatDate = ((uint16)(RiliShizhong[0]+20) << 9 ) | ((uint16)RiliShizhong[1] << 5) | ((uint16)RiliShizhong[2]);
    
    DBUF[InSector + 22]	= (DirCreatTime & 0x00ff);
    DBUF[InSector + 23] = (DirCreatTime >>8);
    DBUF[InSector + 24] = (DirCreatDate & 0x00ff);
    DBUF[InSector + 25] = (DirCreatDate >>8);
		
    if(! WriteSector(DBUF, NowSector, 1))
    {
      return FALSE;
    }
	
    ThisFile.bFileOpen = 1;
  }
  return TRUE;
}

uint32 OpenFile32(uint8 *str)
{
  SFILE_ENTRY_INFO FileEntryInfo;
  uint32 i = 0, tmp = 0, FileInfoAddr = 0;
  uint8 TMPBUF[32] = {0}; /*存储文件名属性*/
  uint8 pName[11] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};/**/
  uint32 j, bstop = 0, sector;
  /*ShortEntSearchAddr(str, pName, 0);*/
  
  UpRootDir32();
  str2Name(pName,str);
 
  ThisFile.bFileOpen = 0;			
  NowCluster32 = DirStartCluster32;		
  do
  {
    NowSector = FirstSectorofCluster32(NowCluster32);
    for(sector = 0; sector < DeviceInfo.BPB_SecPerClus; sector++)
    {   
      if(! ReadSector(DBUF, NowSector + sector, 1))
      {
        return FALSE;				
      }
			
      for(FileInfoAddr = 0; FileInfoAddr < DeviceInfo.BPB_BytesPerSec; FileInfoAddr += 32)
      {
        /*if(DBUF[i] == 0x00)
        {
          return FALSE;
        }*/

        j = 0;

        while(DBUF[FileInfoAddr + j] == *(pName + j))
        {
          j++;
          if(j > 10)
            break;
        }

        if(j > 10 && (DBUF[i + 11] & 0x10) != 0x10)  
        { 
          for(j = 0; j < 32; j++)
          {
            TMPBUF[j] = DBUF[FileInfoAddr + j];			    
          }
					
          bstop = 1;
			    	
          break;
        }
      }
      if(bstop == 1)
        break;		
    }
    if(bstop == 1)
      break;	
    NowCluster32 = GetNextClusterNum32(NowCluster32);	
				
  }while(NowCluster32 <= DeviceInfo.TotCluster);
		
  if(NowCluster32 > DeviceInfo.TotCluster)
  {
    return FALSE;
  }
	  
  if(bstop == 1)
  {
    FileEntryInfo.FileSizeInByte = LSwapuint32(TMPBUF[28],TMPBUF[29],TMPBUF[30],TMPBUF[31]);
    tmp = LSwapuint16(TMPBUF[16], TMPBUF[17]);
    FileEntryInfo.CreateDate.year  = (tmp >> 9);
    FileEntryInfo.CreateDate.month = ((tmp >> 5) & 0x0F);
    FileEntryInfo.CreateDate.day   = (tmp & 0x1F);
    tmp = LSwapuint16(TMPBUF[14], TMPBUF[15]);
    FileEntryInfo.CreateTime.hour = (tmp >> 11);
    FileEntryInfo.CreateTime.min  = ((tmp >> 5) & 0x3F);
    FileEntryInfo.CreateTime.sec  = ((tmp & 0x1F) * 2);
    FileEntryInfo.CreateTime.milsec = TMPBUF[13];/**/
    for(i = 0; i < 11; i ++)
    {
      if(i < 8)
      {
        if(TMPBUF[i] != ' ')
        {
          FileEntryInfo.FileName[i] = TMPBUF[i];
        }
        else
        {
          FileEntryInfo.FileName[i] = '.';
          tmp = i;
          i = 7;
        }
      }
      else
      {
        if(tmp != 0)
        {
          FileEntryInfo.FileName[++tmp] = TMPBUF[i];
        }
        else
        {
          FileEntryInfo.FileName[8] = '.';
          FileEntryInfo.FileName[i+1] = TMPBUF[i];
        }
      }
    }	
  }
  
  ThisFile.StartCluster = LSwapuint32(TMPBUF[26],TMPBUF[27],TMPBUF[20],TMPBUF[21]);
  ThisFile.LengthInByte = LSwapuint32(TMPBUF[28],TMPBUF[29],TMPBUF[30],TMPBUF[31]);
  ThisFile.ClusterPointer = ThisFile.StartCluster;	
  ThisFile.SectorPointer = FirstSectorofCluster32(ThisFile.StartCluster);
  ThisFile.OffsetofSector = 0;
  ThisFile.SectorofCluster = 0;	
  ThisFile.FatSectorPointer = 0;	
  ThisFile.pointer = 0;
  ThisFile.FDTClusterPointer = NowCluster32;
  ThisFile.FDTSectorPointer = NowSector + sector;
  ThisFile.FDTOffsetOfSector = FileInfoAddr;
	
  ThisFile.bFileOpen = 1;

  return FileEntryInfo.FileSizeInByte;	
}


uint32 ReadFile32(uint32 pointer, uint32 readLength, uint8 *pBuffer, uint8 Mode)
{
  uint32 tlen, len, i;/**/
	
  if(!ThisFile.bFileOpen)
  return FALSE;

  if((Mode == 1) || (pointer == 0))
  {
    if(! SetFilePointer32(pointer))
    {
      return FALSE;
    }
  }
  
  tlen = 0;

  if(readLength > MAX_READ_LENGTH)
  {
    return FALSE;	
  }

  if((readLength + ThisFile.pointer) > ThisFile.LengthInByte)
  {
    return FALSE;	
  }
		
  while(readLength > 0)
  {
    if((readLength + ThisFile.OffsetofSector) > DeviceInfo.BPB_BytesPerSec)
    {
      len = DeviceInfo.BPB_BytesPerSec;
    }
    else
    {
      len = (readLength + ThisFile.OffsetofSector);
    }

    if(ThisFile.OffsetofSector > 0)
    {
      if(ReadSector(DBUF, ThisFile.SectorPointer, 1))
      {
        len = (len - ThisFile.OffsetofSector);

        for(i = 0; i < len; i ++)
        {	
          *(pBuffer + i) = DBUF[ThisFile.OffsetofSector + i];
        }

        ThisFile.OffsetofSector = (ThisFile.OffsetofSector + len);
      }
      else
      {
        return FALSE;		   		
      }
    }
    else
    {
      if(! ReadSector((pBuffer + tlen), ThisFile.SectorPointer, 1))
      {
        return FALSE;	
      }
			
      ThisFile.OffsetofSector = len;
    }

    readLength -= len;
    tlen += len;

    if((ThisFile.OffsetofSector > (DeviceInfo.BPB_BytesPerSec - 1)) && ((tlen + ThisFile.pointer) < ThisFile.LengthInByte))
    {	
      ThisFile.OffsetofSector -= DeviceInfo.BPB_BytesPerSec;
      ThisFile.SectorofCluster += 1;

      if(ThisFile.SectorofCluster > (DeviceInfo.BPB_SecPerClus - 1))
      {
        ThisFile.SectorofCluster = 0;
        ThisFile.ClusterPointer = GetNextClusterNum32(ThisFile.ClusterPointer);

        if(ThisFile.ClusterPointer > DeviceInfo.TotCluster)
        {
          return FALSE;			 		 	
        }

        ThisFile.SectorPointer = FirstSectorofCluster32(ThisFile.ClusterPointer); 	
      }
      else
      {
        ThisFile.SectorPointer = (ThisFile.SectorPointer + 1);
      }
    }

  }/*end while*/
		
  ThisFile.bFileOpen = 1;
  ThisFile.pointer += tlen;

  return tlen;
  /*return TRUE;*/
}

uint8 SetFilePointer32(uint32 pointer)
{	
  if(! ThisFile.bFileOpen)
  {
    return FALSE;		
  }

  ThisFile.pointer = pointer;

  if(ThisFile.pointer > ThisFile.LengthInByte)
  {
    return FALSE;	
  }

  if(!GoToPointer32(ThisFile.pointer))
  {
    ThisFile.bFileOpen=0;
    return FALSE;	
  }

  return TRUE;
}

uint32 GetFreeCapacity32(void)
{
	uint32 sectorNum, i;	
	uint32 FreeSize, Freesectorcnt;

	sectorNum = DeviceInfo.FatStartSector;
	Freesectorcnt = 0;

	while(sectorNum < (DeviceInfo.BPB_FATSz32 + DeviceInfo.FatStartSector))
	{
		if(ReadSector(DBUF, sectorNum, 1))
		{
		    for(i = 0; i < DeviceInfo.BPB_BytesPerSec; i += 4)
		  	{		  	 
			  	if((DBUF[i]||DBUF[i+1]||DBUF[i+2]||DBUF[i+3])==0x00)
			  	{	
			  	 	Freesectorcnt++;
			    }	  
		  	}	
		}
		else			
		{
			return FALSE;			
		}

		sectorNum++;
	}
	
	FreeSize = (DeviceInfo.BPB_BytesPerSec * DeviceInfo.BPB_SecPerClus);
	FreeSize = (Freesectorcnt * FreeSize);
	
	return FreeSize;
}

uint8 RemoveFile32(uint8 *str)
{
  uint16 sector,i;
  uint8 bStop = 0, j, n;
  int k;
  uint8 pName[11]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
  uint32 FileStartCluster;
		
  str2Name(pName,str);
	
  NowCluster32 = DirStartCluster32;		
  do
  {
    NowSector = FirstSectorofCluster32(NowCluster32);
    for(sector = 0; sector < DeviceInfo.BPB_SecPerClus; sector++)
    {   
      if(! ReadSector(DBUF, NowSector + sector, 1))
        return FALSE;				
      for(i = 0; i < DeviceInfo.BPB_BytesPerSec; i = i + 32)
      {
//        if(DBUF[i] == 0x00)
//          return FALSE;
        j = 0;
        while(DBUF[i + j] == *(pName + j))
        {
          j++;
          if(j > 10)
            break;
        }
        if(j > 10 && (DBUF[i + 11] & 0x10) != 0x10)
        { 
          FileStartCluster = LSwapuint32(DBUF[i + 26],DBUF[i + 27],DBUF[i + 20],DBUF[i + 21]);
          DBUF[i] = 0xE5;
          for(k = (i - 32); k >= 0; k = k - 32)
          {
            if(DBUF[k + 11] == 0x0F)
              DBUF[k] = 0xE5;
            else
              break;
          }
          if(! WriteSector(DBUF, NowSector + sector, 1))
            return FALSE;					
          if(! DeleteClusterLink32(FileStartCluster))
            return FALSE;					
          bStop = 1;
          break;
        }
      }
      if(bStop == 1)
        break;		
    }
    if(bStop == 1)
      break;	
    NowCluster32 = GetNextClusterNum32(NowCluster32);			
  }while(NowCluster32 <= DeviceInfo.TotCluster);	
	
  if(NowCluster32 > DeviceInfo.TotCluster)
  {
    return FALSE;
  }

  return TRUE;
}



uint8 UpRootDir32(void)
{
	ThisFile.bFileOpen = 0;
	DirStartCluster32 = DeviceInfo.RootStartCluster;	/*Root Dir*/
			
	return TRUE;
}

⌨️ 快捷键说明

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