📄 hpi.c
字号:
#include "DataType.h"
#include "HPI.H"
#include "SL811.H"
#include "MassOp.h"
extern TJHFLAGS bTJHFlags; //define from the demo.c
U8 DIRBUF[8192];
U8 INOUTBUF[INOUTBUF_LENGTH]; //#define INOUTBUF_LENGTH 65536
//////////////////////////////////////////
extern SYS_INFO_BLOCK DeviceInfo;
UART_RSP_BLOCK UartRspBlock;
extern FILE_INFO ThisFile;
extern DIR_INFO ThisDir ;
extern U8 CurFatSector[512];
extern U32 FreeClusterNum,FreeSecNum;
U32 StartSec2Write; // the number of sector will be writed into the UDisk
U8 *BufStartAddress; // the address will be writed in the UDisk
U32 OldClusterPointer; // the cluster pointer for define wether is to be write data immdeiately
//////////////////////////////////////////
// -*******************************************************
//- **** pBuffer -->filename ********
// edited by tianjh 05.04.28
//-*******************************************************
U8 CreateFile(U8 *pBuffer)
{
#define RspBlockCreateFile UartRspBlock.RspBlock.Rsp_CreateFile
//U32 sectorNum;
U16 i,j;
U8 bstop,sector,BUF[512];
U32 sectorToRead;
PDIR_STRUC pDir;
//U8 fileDir[128];
U32 dirSectorPointer,dirClusterPointer;
if(!bTJHFlags.UDiskOK)
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
ThisFile.bFileOpen=0;
////////////////////////////////////////////////
pDir=(PDIR_STRUC)pBuffer;
pDir->DIR_FstClusHI=0x0000;
pDir->DIR_FstClusLO=0x0000;
pDir->DIR_FileSize=0;
/////// Search a free space in the root dir space and build the item ///
bstop=0;
sector=0;
//clusterNum=DeviceInfo.RootClusterNum;
dirClusterPointer=ThisDir.StartCluster;
dirSectorPointer=FirstSectorofCluster(ThisDir.StartCluster);
//for(sector=0;sector<DeviceInfo.BPB_RootEntCnt;sector++)
while(1)
{
///////////////////////////////////////////////////
if(bTJHFlags.bIsFat32==0) //Is FAT16
{
if(ThisDir.bRootDir) //Is Root directory
{
if(sector>DeviceInfo.BPB_RootEntCnt>>4)
{
UartRspBlock.errcode=ERC_FILENOTFOUND;
return FALSE;
}
sectorToRead=ThisDir.StartSector+sector++;
}
else //Not Root directory
{
if(sector>DeviceInfo.BPB_SecPerClus-1)
{
dirClusterPointer=GetNextClusterNum(dirClusterPointer);
if(dirClusterPointer>0xfff8)
{
// To do:
return FALSE;
}
dirSectorPointer=FirstSectorofCluster(dirClusterPointer);
sector=0;
}
sectorToRead=dirSectorPointer+sector++;
}
}
else //Is FAT32
{
if(sector>DeviceInfo.BPB_SecPerClus-1)
{
dirClusterPointer=GetNextClusterNum(dirClusterPointer);
if(dirClusterPointer>0x0ffffff8)
{
UartRspBlock.errcode=ERC_FILENOTFOUND;
return FALSE;
}
dirSectorPointer=FirstSectorofCluster(dirClusterPointer);
sector=0;
}
///
sectorToRead=dirSectorPointer+sector++;
}
//////////////////////////////////////////////////
if(!RBC_Read(sectorToRead,1,BUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
///////////////////////////////////////////////////
///// find the empty space for the new file in the FAT table ///////
for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
{
if((BUF[i]==0x00)||(BUF[i]==0xE5)) //0xE5-->already be deleted ; 0x00--> Not Used edited by tianjh 05.04.28
{
///////////////////////////////////////
for(j=0;j<32;j++)
BUF[i+j]=*(pBuffer+j);
//copy the filename to the FAT Table ----edited by tianjh 05.03.29
///////////////////////////////////////
if(!RBC_Write(sectorToRead,1,BUF)) //sectorToRead---->0x7E0
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
bstop=1;
break;
} //end if((BUF[i]==0x00)||(BUF[i]==0xE5))
}//end for
///////////////////////////////////////////////////////
if(bstop==1)
break;
}//end while
/////////////////////////////////////////////
///// build the file inform //////////////
ThisFile.StartCluster=0x00000000; //SwapINT16(pDirInfo->startCluster);
ThisFile.LengthInByte=0;
ThisFile.ClusterPointer=0x00; //ThisFile.StartCluster;
ThisFile.SectorPointer=0x00; //FirstSectorofCluster(ThisFile.StartCluster);
ThisFile.OffsetofSector=0;
ThisFile.SectorofCluster=0;
ThisFile.bFileOpen=1;
ThisFile.pointer=0;
//ThisFile.FatSectorPointer=0;
for(j=0;j<11;j++)
{
ThisFile.FileName[j]=*(pBuffer+j);
}
//////////////////////////////////////////////
// 读入FAT表的第一个扇区 ////////////////////
FreeClusterNum=DeviceInfo.FSI_Nxt_Free;
FreeSecNum=ThisFatSecNum(DeviceInfo.FSI_Nxt_Free);
if(!RBC_Read(FreeSecNum,1,CurFatSector))
return FALSE;
///////////////////////////////////////////////
return TRUE;
#undef RspBlockCreateFile
}
/**********************************************************************
******** write the data into the file ***********
**** writeLength --->the length to be writed ****
**** pBuffer --->the pointer of the buffer to be writed ****
**** bEnd --->the end-flag of the write process ****
**** ----edited by tianjh 05.03.28 ****
************************************************************************/
U8 WriteFile(U32 writeLength,U8 *pBuffer,U8 bEnd)
{
#define RspBlockWriteFile UartRspBlock.RspBlock.Rsp_WriteFile
U16 len,i;
//PDIR_INFO pDirInfo;
U8 bSuccess,bStop,step,SecCnt2Write,bUpdate,BUF[512];
if(!bTJHFlags.UDiskOK) //U盘是否已经连接
{
UartRspBlock.errcode=ERC_NODEVICE;
return FALSE;
}
if(!ThisFile.bFileOpen) //文件是否已经打开
{
UartRspBlock.errcode=ERC_FILENOTOPENED;
return FALSE;
}
if(writeLength>65536) //#define MAX_WRITE_LENGTH 65534
{
//UartRspBlock.errcode=ERC_LENGTHEXCEED;
return FALSE;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
ThisFile.bFileOpen=0;
bSuccess=1;
bStop=0;
bUpdate=1;
UartRspBlock.len=0;
//////////////////////////////////////////////////////
//GetFreeCusterNum();
if(ThisFile.StartCluster<1)
{
ThisFile.StartCluster=CreateClusterLink(0x00000000);//GetFreeCusterNum();//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;
}
//////////////////////////////////////////////////////
if(ThisFile.OffsetofSector>0)
{
if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
{
len=DeviceInfo.BPB_BytesPerSec; //eviceInfo.BPB_BytesPerSec==512
///////////////////////////////////////////
}
else
len=writeLength+ThisFile.OffsetofSector;
//////////////////////////////////////////////////
if(!RBC_Read(ThisFile.SectorPointer,1,BUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
//ThisFile.OffsetofSector=len;
len=len-ThisFile.OffsetofSector; // get the ThisFile.OffsetofSector edited by tianjh 05.05.12
for(i=0;i<len;i++)
BUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
if(!RBC_Write(ThisFile.SectorPointer,1,BUF))
{
UartRspBlock.errcode=ERC_DEVICEERR;
return FALSE;
}
ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
step=ThisFile.OffsetofSector/DeviceInfo.BPB_BytesPerSec;
////////////////////////////////////////
writeLength-=len;
UartRspBlock.len+=len;
ThisFile.pointer+=len;
//OldCluster=;
if(step>0)
FileInfoUpdate(step);
}// end if
////////////////////////////////////////////////////
while(writeLength>0)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -