?? hpi.c
字號:
if(pDirInfo->startCluster<0x2)
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
pDirInfo->length=0;
ThisFile.bFileOpen=0;
bstop=0;
for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
{
if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
{
for(j=0;j<32;j++)
DBUF[i+j]=*(pBuffer+j);
if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
bstop=1;
break;
}
}
if(bstop==1) break;
}
ThisFile.StartCluster=SwapINT16(pDirInfo->startCluster);
ThisFile.LengthInByte=0;
ThisFile.ClusterPointer=ThisFile.StartCluster;
ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.StartCluster);
ThisFile.OffsetofSector=0;
ThisFile.SectorofCluster=0;
ThisFile.bFileOpen=1;
ThisFile.pointer=0;
ThisFile.FatSectorPointer=0;
FreeFat.SectorNum=DeviceInfo.FatStartSector;
FreeFat.OldSectorNum=DeviceInfo.FatStartSector;
FreeFat.OffsetofSector=2;
return TRUE;
#undef RspBlockCreateFile
}
/*-----------------------------------------------------------------------------------
名稱: unsigned char WriteFile(unsigned int writeLength,unsigned char *pBuffer)
功能: 寫入數(shù)據(jù)到文件.
-----------------------------------------------------------------------------------*/
unsigned char WriteFile(unsigned int writeLength,unsigned char *pBuffer)
{
#define RspBlockWriteFile UartRspBlock.RspBlock.Rsp_WriteFile
unsigned int len,sector,i;
PDIR_INFO pDirInfo;
unsigned char bSuccess,bStop,step;
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED) //U盤是否已經(jīng)連接
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
if(!ThisFile.bFileOpen) //文件是否已經(jīng)打開
{
UartRspBlock.errcode=ERC_FILENOTOPENED;
return FALSE;
}
ThisFile.bFileOpen=0;
bSuccess=1;
bStop=0;
UartRspBlock.len=0;
while(writeLength>0)
{
if(ThisFile.OffsetofSector>0)
{
if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
len=DeviceInfo.BPB_BytesPerSec;
else
len=writeLength+ThisFile.OffsetofSector;
if(!RBC_Read(ThisFile.SectorPointer,1,DBUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
len=len-ThisFile.OffsetofSector;
for(i=0;i<len;i++)
DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
if(!RBC_Write(ThisFile.SectorPointer,1,DBUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
}
else
{
if(writeLength>DeviceInfo.BPB_BytesPerSec)
{
step=writeLength/DeviceInfo.BPB_BytesPerSec;
if(step>DeviceInfo.BPB_SecPerClus)
{
step=DeviceInfo.BPB_SecPerClus-ThisFile.SectorofCluster;
len=step*DeviceInfo.BPB_BytesPerSec;
}
else
{
step=step-ThisFile.SectorofCluster;
len=step*DeviceInfo.BPB_BytesPerSec;
}
}
else
{
step=1;
len=writeLength;
}
if(!RBC_Write(ThisFile.SectorPointer,step,pBuffer+UartRspBlock.len))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
ThisFile.OffsetofSector=len;
}
writeLength-=len;
UartRspBlock.len+=len;
/////////////更新文件指針 //////////////////////////////
//改變參數(shù):OffsetofSector,SectorofCluster,SectorPointer,ClusterPointer
step=ThisFile.OffsetofSector/DeviceInfo.BPB_BytesPerSec;
if(step>0)
{
ThisFile.OffsetofSector-=step*DeviceInfo.BPB_BytesPerSec;
ThisFile.SectorofCluster+=step;
if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
{
ThisFile.SectorofCluster=0;
if(ThisFile.pointer+UartRspBlock.len<ThisFile.LengthInByte)
ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
else
ThisFile.ClusterPointer=CreateClusterLink(ThisFile.ClusterPointer);//GetNextClusterNum(ThisFile.ClusterPointer);
if(ThisFile.ClusterPointer==0x00)
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer);
}
else
ThisFile.SectorPointer=ThisFile.SectorPointer+step;
}
}//end while
ThisFile.pointer+=UartRspBlock.len;
UpdateFat(FreeFat.SectorNum);
/*更新文件目錄信息*/
if(bSuccess==1)
{
for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
{
if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
pDirInfo=(PDIR_INFO)(DBUF+i);
if(pDirInfo->startCluster==SwapINT16(ThisFile.StartCluster))
{
if(ThisFile.pointer>ThisFile.LengthInByte)
ThisFile.LengthInByte=ThisFile.pointer;
pDirInfo->length=SwapINT32(ThisFile.LengthInByte);
if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
bStop=1;
break;
}
}
if(bStop==1) break;
}
}
UartRspBlock.len=0;
ThisFile.bFileOpen=1;
return TRUE;
#undef RspBlockWriteFile
}
/*-----------------------------------------------------------------------------------
名稱: unsigned char RemoveFile(unsigned char *pBuffer)
功能: 刪除文件.
-----------------------------------------------------------------------------------*/
unsigned char RemoveFile(unsigned char *pBuffer)
{
#define RspBlockRemoveFile UartRspBlock.RspBlock.Rsp_RemoveFile
unsigned int sector,i;
unsigned char bStop,j;
PDIR_INFO pDirInfo;
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
/*清除目錄*/
for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
{
if(!RBC_Read(DeviceInfo.RootStartSector+sector,1,DBUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
if(DBUF[i]==0x00)
{
UartRspBlock.errcode=ERC_FILENOTFOUND;
return FALSE;
}
j=0;
while(DBUF[i+j]==*(pBuffer+j))
{
j=j+1;
if(j>10) break;
}//end while
if(j>10)
{
DBUF[i]=0xE5;
pDirInfo=(PDIR_INFO)(DBUF+i);
ThisFile.StartCluster=SwapINT16(pDirInfo->startCluster);
if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
/*清除FAT中的紀(jì)錄*/
if(!DeleteClusterLink(ThisFile.StartCluster))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
bStop=1;
break;
}
}//end for
if(bStop==1) break;
}//end search
if(sector>=DeviceInfo.BPB_RootEntCnt)
{
UartRspBlock.errcode=ERC_FILENOTFOUND;
return FALSE;
}
return TRUE;
#undef RspBlockRemoveFile
}
/*-----------------------------------------------------------------------------------
名稱: unsigned char GetCapacity(void)
功能: 獲取磁盤空間.
-----------------------------------------------------------------------------------*/
unsigned char GetCapacity(void)
{
#define RspBlockGetCapacity UartRspBlock.RspBlock.Rsp_GetCapacity
unsigned int sectorNum,freesectorcnt,i;
PREAD_CAPACITY_RSP pBuf;
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
if(!RBC_ReadCapacity())
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
pBuf=(PREAD_CAPACITY_RSP)DBUF;
RspBlockGetCapacity.disksize=SwapINT32((pBuf->LastLBA+1)*pBuf->BlockSize);
sectorNum=DeviceInfo.FatStartSector;
freesectorcnt=0;
while(sectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
{
if(RBC_Read(sectorNum,1,DBUF))
{
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+2)
{
if((DBUF[i]==0xff)&&(DBUF[i+1]==0xff))
{
freesectorcnt++;
}
}
}
else
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
sectorNum++;
}
RspBlockGetCapacity.freedisksize=DeviceInfo.BPB_BytesPerSec*DeviceInfo.BPB_SecPerClus;
RspBlockGetCapacity.freedisksize=freesectorcnt*RspBlockGetCapacity.freedisksize;
RspBlockGetCapacity.freedisksize=SwapINT32(RspBlockGetCapacity.disksize)-RspBlockGetCapacity.freedisksize;
RspBlockGetCapacity.freedisksize=SwapINT32(RspBlockGetCapacity.freedisksize);
return TRUE;
#undef RspBlockGetCapacity
}
/*-----------------------------------------------------------------------------------
名稱: unsigned char GetFreeCapacity(void)
功能: 獲取磁盤剩余空間.
-----------------------------------------------------------------------------------*/
unsigned char GetFreeCapacity(void)
{
#define RspBlockGetCapacity UartRspBlock.RspBlock.Rsp_GetFreeCapacity
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
return TRUE;
#undef RspBlockGetFreeCapacity
}
/*-----------------------------------------------------------------------------------
名稱: unsigned char SetFilePointer(unsigned long pointer)
功能: 將文件指針移動到指定位置.
-----------------------------------------------------------------------------------*/
unsigned char SetFilePointer(unsigned long pointer)
{
#define RspBlockSetFilePointer UartRspBlock.RspBlock.Rsp_SetFilePointer
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
if(!ThisFile.bFileOpen)
{
UartRspBlock.errcode=ERC_FILENOTOPENED;
return FALSE;
}
ThisFile.pointer=pointer;
if(ThisFile.pointer>ThisFile.LengthInByte)
{
UartRspBlock.errcode=ERC_LENGTHEXCEED;
return FALSE;
}
if(!GoToPointer(ThisFile.pointer))
{
ThisFile.bFileOpen=0;
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
return TRUE;
#undef RspBlockSetFilePointer
}
/*-----------------------------------------------------------------------------------
名稱: unsigned char GetFirmwareVersion(void)
功能: 獲取軟件版本.
-----------------------------------------------------------------------------------*/
unsigned char GetFirmwareVersion(void)
{
#define RspBlockGetVersion UartRspBlock.RspBlock.Rsp_GetVersion
RspBlockGetVersion.version=0x0102;
return TRUE;
#undef RspBlockGetVersion
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -