📄 hpi.c
字号:
}
///////////////////////////////////////////////////////////
//RspBlockRemoveFile.result=0x1;
////////////// 清除目录/////////////////////////////////////
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);
DelayMs(15);
if(!RBC_Write(DeviceInfo.RootStartSector+sector,1,DBUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
//////////////////// 清除FAT中的纪录////////////////////////
DelayMs(10);
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 int sectorNum,freesectorcnt,i;
#define RspBlockGetCapacity UartRspBlock.RspBlock.Rsp_GetCapacity
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)
{
//clusterNum++;
if((DBUF[i]==0xff)&&(DBUF[i+1]==0xff))
{
freesectorcnt++;
}
// clusterNum++;
}
}
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)
{
#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)
{
#define RspBlockSetFilePointer UartRspBlock.RspBlock.Rsp_SetFilePointer
//ThisFile.FilePointer=UartCmdBlock.CmdBlock.Cmd_SetFilePointer.pointer;
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)
{
#define RspBlockGetVersion UartRspBlock.RspBlock.Rsp_GetVersion
////////////////////////////////////////////////////////////
RspBlockGetVersion.version=0x0102;
return TRUE;
#undef RspBlockGetVersion
}
unsigned char FileList(void)
{
#define RspBlockList UartRspBlock.RspBlock.Rsp_List
unsigned int item,i,temp;
unsigned char j,k,bstop,sector;
INT32 fssector;
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
///////////////////////////////////////////////////////////
item=0;
bstop=0;
fssector = 2;
Uart_Printf("文件目录:\n");
while(1)
{
if(!RBC_Read(DeviceInfo.RootStartSector + fssector - 2,1,DBUF))//读取根目录
{
return FALSE;
}
for(i=0;i<512;i=i+32)
{
if((DBUF[i]==0x00)||(DBUF[i])==0xe5)//该目录未被使用或者被删除
continue;
else if((DBUF[i+11]==0x0f)||(DBUF[i+11]==0x02))//该目录项用于长文件名,或者隐藏文件
continue;
else
{
for(j=0;j<8;j++)
Uart_SendByte(DBUF[i+j]);
Uart_SendByte('.');Uart_SendByte(DBUF[i+8]);Uart_SendByte(DBUF[i+9]);
Uart_SendByte(DBUF[i+10]);Uart_SendByte('\n');
}
}
/*通过查找FAT表得到其他根目录*/
temp = fssector / 128;
if(!RBC_Read(DeviceInfo.FatStartSector + temp,1,DBUF))
{
return FALSE;
}
temp = fssector % 128;
temp = temp * 4;
fssector = DBUF[temp+3]<<24 | DBUF[temp+2]<<16 | DBUF[temp+1]<<8 | DBUF[temp];
if(fssector == 0x0fffffff)//根目录结束
break;
}
return TRUE;
#undef RspBlockList
}
unsigned char FileWrite(BYTE *FileName,BYTE len,BYTE *pDBUF)
{
int len_temp,fat_temp=0;
int i,j,temp1,temp,temp2,fssector=2;
Uart_Printf("创建新文件:%c\n",*FileName);
if(!bXXGFlags.bits.SLAVE_IS_ATTACHED)
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
if( len > DeviceInfo.BPB_BytesPerSec)
len_temp = DeviceInfo.BPB_BytesPerSec;
else
len_temp = len;
if(!RBC_Read(DeviceInfo.FatStartSector ,1,DBUF))
{
return FALSE;
}
if(!RBC_Read(DeviceInfo.FatStartSector + DeviceInfo.BPB_FATSz16 ,1,DBUF))
{
return FALSE;
}
temp1 = 0;
for(i = 12 ;i < 512 ;i = i + 4)
{
temp = DBUF[i+3]<<24 | DBUF[i+2]<<16 | DBUF[i+1]<<8 | DBUF[i];
if(temp == 0x00000000)
{
DBUF[i+3]=0x0f; DBUF[i+2]=0xff; DBUF[i+1]=0xff; DBUF[i]=0xff;
RBC_Write(DeviceInfo.FatStartSector , 1 ,DBUF);
RBC_Write(DeviceInfo.FatStartSector + DeviceInfo.BPB_FATSz16 ,1,DBUF);
temp1 = i/4;//记录空簇数
break;
}
}
if(temp1==0)
return FALSE;
temp2 = 512; //记录根目录地址
fssector = 2;
while(1)
{
if(!RBC_Read(DeviceInfo.RootStartSector ,1,DBUF))
{
return FALSE;
}
for(i=0;i<32;i++)
BUFTEMP[i]=DBUF[96+i];
// DBUF[448]=0xe5;
for(i=0;i<512;i=i+32)
{
if((DBUF[i]==0x00)||(DBUF[i])==0xe5)//该目录未被使用或者被删除
{
temp2 = i;
break;
}
}
if(temp2 != 512)
break;
/*通过查找FAT表得到其他根目录*/
temp = fssector / 128;
if(!RBC_Read(DeviceInfo.FatStartSector + temp,1,DBUF))
{
return FALSE;
}
temp = fssector % 128;
temp = temp * 4;
fssector = DBUF[temp+3]<<24 | DBUF[temp+2]<<16 | DBUF[temp+1]<<8 | DBUF[temp];
if(fssector == 0x0fffffff)//根目录结束
break;
}
for(i=0;i<32;i++)
DBUF[temp2+i] = BUFTEMP[i];
for(i=0;i<9;i++)
{
if(*(FileName+i)=='\0')
{
for(j=0;j<i;j++)
{
if((*(FileName+j) > 0x60) && (*(FileName+j) < 0x7B))
DBUF[temp2+j] = *(FileName+j)-0x20;
else
DBUF[temp2+j] = *(FileName+j);
}
for(;j<8;j++)
DBUF[temp2+j] = 0x20;
break;
}
}
if(i==9)
{
for(j=0;j<6;j++)
DBUF[temp2+j] = *(FileName+j);
DBUF[temp2+6] = '~';DBUF[temp2+7]='1';
}
// RBC_Write(DeviceInfo.RootStartSector ,1,DBUF);
DBUF[temp2+11]=0x20;
DBUF[temp2+21]=(temp1>>24)&0xff;DBUF[temp2+20]=(temp1>>16)&0xff;//簇起始地址
DBUF[temp2+27]=(temp1>>8) &0xff;DBUF[temp2+26]=temp1&0xff;
DBUF[temp2+31]=(len>>24)&0xff;DBUF[temp2+30]=(len>>16)&0xff;//文件长度
DBUF[temp2+29]=(len>>16)&0xff;DBUF[temp2+28]=len&0xff;
DBUF[temp2+8]='T';DBUF[temp2+9]='X';DBUF[temp2+10]='T';
DBUF[temp2+12]=0x18;DBUF[temp2+13]=0x74;DBUF[temp2+14]=0xef;DBUF[temp2+15]=0x54;
DBUF[temp2+16]=0xd6;DBUF[temp2+17]=0x36;DBUF[temp2+18]=0xd6;DBUF[temp2+19]=0x36;
DBUF[temp2+22]=0x3d;DBUF[temp2+23]=0xb0;DBUF[temp2+24]=0xd5;DBUF[temp2+25]=0x36;
RBC_Write(DeviceInfo.RootStartSector ,1,DBUF);
Uart_Printf("向文件写入数据\n");
while(1)
{
RBC_Read(DeviceInfo.DataStartSector + temp1 - 2,1,DBUF);
for(i=0;i<512;i++)
DBUF[i]=0x00;
DBUF[0]='I';DBUF[1]='L';DBUF[2]='O';DBUF[3]='V';DBUF[4]='E';DBUF[5]='U';
RBC_Write(DeviceInfo.DataStartSector + temp1 - 2,1,DBUF);
if(len>512)
{
len = len - 512;
pDBUF = pDBUF + 512;
}
else
break;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -