📄 hpi32.c
字号:
#include "common.h"
#include "TPBULK.H"
#include "HPI.H"
#include "HAL.H"
#include "Fat.h"
#include "Fat32.h"
#include "HPI32.H"
extern FLAGS bdata bFlags;
extern unsigned char xdata DBUF[BUFFER_LENGTH];
//////////////////////////////////////////
extern unsigned char xdata UARTBUF[UARTBUF_LENGTH];
extern SYS_INFO_BLOCK xdata DeviceInfo;
extern FILE_INFO xdata ThisFile;
extern Command_Def xdata Command;
extern Response_Def xdata Response;
unsigned long xdata DirStartCluster32,NowCluster32;
extern unsigned long xdata NowSector;
extern ShowFileName_Def xdata ShowFileName[MaxLFNum]; //long file struct
/////////////////////////////////////////////
unsigned char UartHandler32(void)
{
Response.Result=0;Response.len=0;
switch(Command.CLass)
{
case 0x00: //List
Response.Result=List32();
break;
case 0x01: //Open File
Response.Result=OpenFile32(Command.Parameter);
break;
case 0x02: //Read File
Response.Result=ReadFile32(Command.len,UARTBUF);
break;
case 0x03: //Set Pointer
Response.Result=SetFilePointer32(Command.len);
break;
case 0x04: //Great File
Response.Result=OpenFile32(Command.Parameter);
if(Response.Result)
{
Response.Result=SetFilePointer32(ThisFile.LengthInByte);
}
else
Response.Result=CreateFile32(Command.len,Command.Parameter,UARTBUF);
break;
case 0x05: //Write File
Response.Result=WriteFile32(Command.len,UARTBUF);
break;
case 0x06: //Remove File
ThisFile.bFileOpen=0;
Response.Result=RemoveFile32(Command.Parameter);
break;
case 0x07: //Get Space
Response.Result=GetCapacity32();
break;
case 0x08: //DetectDisk
if(bFlags.bits.SLAVE_IS_ATTACHED)
Response.Result=1;
break;
case 0x09: //Great Dir
Response.Result=DownDir32(Command.Parameter);
if(!Response.Result)
Response.Result=CreateDir32(Command.len,Command.Parameter,UARTBUF);
break;
case 0x0a: //Down Dir
Response.Result=DownDir32(Command.Parameter);
break;
case 0x0b: //Up Dir
Response.Result=UpDir32();
break;
case 0x0c: //Up RootDir
Response.Result=UpRootDir32();
break;
}
UartSendRsp();
///////////////////////////////
return TRUE;
}
unsigned char List32(void)
{
unsigned int item,i;
unsigned char k,bstop,sector;
unsigned char Lcount,Ncount,base;
if(!bFlags.bits.SLAVE_IS_ATTACHED)
return FALSE;
item=0;
bstop=0;
////////////////////////////////////
Lcount=0;
for(i=0;i<MaxLFNum;i++)
{
ShowFileName[i].LongName[0]=0x00;
ShowFileName[i].LongName[1]=0x00;
}
//////////////////////////////////////////////////////////////////
NowCluster32=DirStartCluster32; //
do
{
NowSector=FirstSectorofCluster32(NowCluster32);//根据文件开始簇号计算文件的逻辑扇区号
for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
{
if(!RBC_Read(NowSector+sector,1,DBUF)) //读一个扇区数据
return FALSE;
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32) //读出本扇区的文件目录表
{
if(DBUF[i]==0x00)
{bstop=1;break;} //未使用
else if(DBUF[i]==0xE5)
continue; //该文件被删除
else
{
for(k=0;k<32;k++) //保存文件目录
UARTBUF[item*32+k]=DBUF[i+k];
item=item+1; //目录数累计
/////////////////////////////////
if(DBUF[i+11]==0x0F) //长文件名判断
{ //是长文件名
base=((DBUF[i]&0x1F)-1)*26; //计算该字符在长文件名中的长度《(DBUF[i]&0x1F)-1)》为顺序号,每段26个字符
if(base<=224) //长文件名长度不能大于256字节 224+26
{
Ncount=0; //拼接长文件名
for(k=1;k<11;k++)
{
ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
Ncount++;
}
for(k=14;k<26;k++)
{
ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
Ncount++;
}
for(k=28;k<32;k++)
{
ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
Ncount++;
}
}
}
else
{
for(k=0;k<32;k++) //段文件名
ShowFileName[Lcount].item[k]=DBUF[i+k];
Lcount++;
}
/////////////////////////////////
}
}
if(bstop==1)break; //找到未使用的目录项退出
}
if(bstop==1)break;
NowCluster32=GetNextClusterNum32(NowCluster32); //找下一个簇
}while(NowCluster32<=DeviceInfo.TotCluster);
Response.len=item*32;
return TRUE;
}
unsigned char OpenFile32(unsigned char *pBuffer)
{
unsigned int i;
unsigned char j,bstop,sector;
if(!bFlags.bits.SLAVE_IS_ATTACHED)
return FALSE;
ThisFile.bFileOpen=0; //设置文件打开标志
NowCluster32=DirStartCluster32;
do
{
NowSector=FirstSectorofCluster32(NowCluster32);
for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
{
if(!RBC_Read(NowSector+sector,1,DBUF))
return FALSE;
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
if(DBUF[i]==0x00)
return FALSE; //文件目录区未使用退出 该文件不存在 打开错误
j=0;
while(DBUF[i+j]==*(pBuffer+j))
{
j=j+1;
if(j>10)
break;
}
if(j>10&&(DBUF[i+11]&0x10)!=0x10)
{
for(j=0;j<32;j++) //文件名符合,且不是长文件名
UARTBUF[j]=DBUF[i+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; //文件没有找到,打开文件错
ThisFile.bFileOpen=1; //文件打开标志
ThisFile.StartCluster=LSwapINT32(UARTBUF[26],UARTBUF[27],UARTBUF[20],UARTBUF[21]); //开始簇号
ThisFile.LengthInByte=LSwapINT32(UARTBUF[28],UARTBUF[29],UARTBUF[30],UARTBUF[31]); //当前文件长度
ThisFile.ClusterPointer=ThisFile.StartCluster; //簇指针指向当前文件开始处
ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.StartCluster);//扇区指针指向首簇第一个扇区
ThisFile.OffsetofSector=0; //扇区中偏移量为0
ThisFile.SectorofCluster=0; //簇中偏移量为0
ThisFile.FatSectorPointer=0;
ThisFile.pointer=0;
Response.len=32;
return TRUE;
}
//***********************************************************************
//读FAT32文件
//入口参数: readLength 读数据长度(以字节为单位) *pBuffer 数据保存地址
//***********************************************************************
unsigned char ReadFile32(unsigned long readLength,unsigned char *pBuffer)
{
unsigned int len,i;
unsigned int tlen;
unsigned long blen;
if(!bFlags.bits.SLAVE_IS_ATTACHED)
return FALSE;
if(!ThisFile.bFileOpen)
return FALSE; //文件没有打开,读文件错
blen=readLength; //保存读文件长度
tlen=0;
if(readLength>MAX_READ_LENGTH)
return FALSE; //一次读的数据长度不能超过16K
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(RBC_Read(ThisFile.SectorPointer,1,DBUF)) //读一个扇区
{
len=len-ThisFile.OffsetofSector;
for(i=0;i<len;i++)
*(pBuffer+i)=DBUF[ThisFile.OffsetofSector+i]; //从ThisFlie.OffSetofSector处开始保存文件
ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
}
else
return FALSE;
}
else
{
if(!RBC_Read(ThisFile.SectorPointer,1,pBuffer+tlen))//直接读一个扇区数据并保存到数据缓冲区中
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; //累加文件读写点
//////////////////////////////////////////////
Response.len=blen;
return TRUE;
}
//设置文件读写位置
//
unsigned char SetFilePointer32(unsigned long pointer)
{
if(!bFlags.bits.SLAVE_IS_ATTACHED)
return FALSE;
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; //设置文件读写位置成功
}
//*****************************************************************************************
//创建文件
//
//
//*****************************************************************************************
unsigned char CreateFile32(unsigned long len,unsigned char *pBuffer,unsigned char *pName)
{
unsigned int sector,i,j,DirCount;
unsigned long cnum;
unsigned char xdata bstop,InByte,bwrite;
unsigned long ClusterPointer;
if(!bFlags.bits.SLAVE_IS_ATTACHED)
return FALSE;
if((len%32)!=0)
return FALSE; //长文件名字节数不是32的整数倍退出
if((len+32)>DeviceInfo.BPB_BytesPerSec)
return FALSE;
ThisFile.bFileOpen=0; //设置文件关闭标志
cnum=GetFreeCusterNum32(); //
if(cnum<0x02)
return FALSE;
pBuffer[21]=(unsigned char)(cnum>>24);
pBuffer[20]=(unsigned char)(cnum>>16);
pBuffer[27]=(unsigned char)(cnum>>8);
pBuffer[26]=(unsigned char)(cnum);
pBuffer[28]=0;pBuffer[29]=0;pBuffer[30]=0;pBuffer[31]=0;
bstop=0;
NowCluster32=DirStartCluster32;
do
{
NowSector=FirstSectorofCluster32(NowCluster32);
ClusterPointer=NowCluster32;
for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
{
if(!RBC_Read(NowSector+sector,1,DBUF))
return FALSE;
DirCount=0;bwrite=0;
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
if(len==0)
{
if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
{
for(j=0;j<32;j++)
DBUF[i+j]=*(pBuffer+j);
if(!RBC_Write(NowSector+sector,1,DBUF))
return FALSE;
bstop=1;
break;
}
}
else
{
if(DirCount==0)
InByte=i;
if(DBUF[i]==0xE5)
DirCount++;
else if(DBUF[i]==0x00)
{
DirCount++;
DBUF[i]=0xE5;
bwrite=1;
}
else
DirCount=0;
if((DirCount*32)>=(len+32))
{
for(j=0;j<len;j++)
DBUF[InByte+j]=*(pName+j);
for(j=0;j<32;j++)
DBUF[InByte+len+j]=*(pBuffer+j);
if(!RBC_Write(NowSector+sector,1,DBUF))
return FALSE;
bstop=1;
break;
}
}
}
if(bstop==1)break;
if((len!=0)&&(bwrite==1))
{
if(!RBC_Write(NowSector+sector,1,DBUF))
return FALSE;
}
}
if(bstop==1)break;
NowCluster32=GetNextClusterNum32(NowCluster32);
if(NowCluster32>DeviceInfo.TotCluster)
{
NowCluster32=CreateClusterLink32(ClusterPointer);
if(NowCluster32==0x00)
return FALSE;
NowSector=FirstSectorofCluster32(NowCluster32);
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i++) DBUF[i]=0x00;
for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
{
if(!RBC_Write(NowSector+sector,1,DBUF))
return FALSE;
}
}
}while(NowCluster32<=DeviceInfo.TotCluster);
////////////////////////////////////////////////////////////////
ThisFile.StartCluster=cnum;
ThisFile.LengthInByte=0;
ThisFile.ClusterPointer=ThisFile.StartCluster;
ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.StartCluster);
ThisFile.OffsetofSector=0;
ThisFile.SectorofCluster=0;
ThisFile.bFileOpen=1;
ThisFile.pointer=0;
ThisFile.FatSectorPointer=0;
return TRUE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -