HPI32.C
上传用户:jcsy2001
上传日期:2013-11-29
资源大小:201k
文件大小:23k
开发平台:

C/C++

  1. #include "common.h"
  2. #include "DEVICE.H"
  3. #include "HPI.H"
  4. #include "HAL.H"
  5. #include "Fat.h"
  6. #include "Fat32.h"
  7. #include "HPI32.H"
  8. extern FLAGS bdata bFlags;
  9. extern unsigned char xdata DBUF[BUFFER_LENGTH];
  10. //////////////////////////////////////////
  11. extern unsigned char xdata UARTBUF[UARTBUF_LENGTH];
  12. extern SYS_INFO_BLOCK xdata DeviceInfo;
  13. extern FILE_INFO xdata ThisFile;
  14. extern Command_Def xdata Command;
  15. extern Response_Def xdata Response;
  16. unsigned long xdata DirStartCluster32,NowCluster32;
  17. extern unsigned long xdata NowSector;
  18. extern ShowFileName_Def xdata ShowFileName[MaxLFNum]; //long file struct
  19. /////////////////////////////////////////////
  20. unsigned char UartHandler32(void)
  21. {
  22.   Response.Result=0;Response.len=0;
  23.   switch(Command.CLass)
  24.   {
  25.     case 0x00: //List
  26.      Response.Result=List32();
  27.      break;  
  28.    case 0x01: //Open File
  29.   Response.Result=OpenFile32(Command.Parameter);
  30.       break;
  31. case 0x02: //Read File
  32. Response.Result=ReadFile32(Command.len,UARTBUF);
  33. break;   
  34.     case 0x03: //Set Pointer
  35. Response.Result=SetFilePointer32(Command.len);
  36. break;
  37. case 0x04: //Great File
  38. Response.Result=OpenFile32(Command.Parameter);
  39. if(Response.Result)
  40. {
  41. Response.Result=SetFilePointer32(ThisFile.LengthInByte);
  42. }
  43. else
  44.      Response.Result=CreateFile32(Command.len,Command.Parameter,UARTBUF);    
  45.      break; 
  46. case 0x05: //Write File
  47.      Response.Result=WriteFile32(Command.len,UARTBUF);
  48.      break; 
  49.     case 0x06: //Remove File
  50.      ThisFile.bFileOpen=0;
  51.      Response.Result=RemoveFile32(Command.Parameter);
  52.      break; 
  53. case 0x07: //Get Space    
  54.      Response.Result=GetCapacity32();
  55.      break;     
  56. case 0x08:  //DetectDisk
  57. if(bFlags.bits.SLAVE_IS_ATTACHED)
  58. Response.Result=1;
  59. break;
  60. case 0x09:  //Great Dir
  61. Response.Result=DownDir32(Command.Parameter);
  62. if(!Response.Result)
  63. Response.Result=CreateDir32(Command.len,Command.Parameter,UARTBUF);    
  64.      break; 
  65. case 0x0a: //Down Dir
  66. Response.Result=DownDir32(Command.Parameter);
  67. break;
  68. case 0x0b: //Up Dir
  69. Response.Result=UpDir32();
  70. break;
  71. case 0x0c: //Up RootDir
  72. Response.Result=UpRootDir32();
  73. break;
  74.   }
  75.   UartSendRsp();
  76.   ///////////////////////////////
  77.   return TRUE;  
  78. }
  79. unsigned char List32(void)
  80. {
  81. unsigned int item,i;
  82. unsigned char k,bstop,sector;
  83. unsigned char Lcount,Ncount,base;
  84. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  85. return FALSE;
  86. item=0;
  87. bstop=0;
  88. ////////////////////////////////////
  89. Lcount=0;
  90. for(i=0;i<MaxLFNum;i++)
  91. {
  92. ShowFileName[i].LongName[0]=0x00;
  93. ShowFileName[i].LongName[1]=0x00;
  94. }
  95. //////////////////////////////////////////////////////////////////   
  96. NowCluster32=DirStartCluster32;
  97. do
  98. {
  99. NowSector=FirstSectorofCluster32(NowCluster32);
  100. for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  101.      {   
  102. if(!SdReadSector(NowSector+sector,1,DBUF))
  103. return FALSE;
  104. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  105. {
  106. if(DBUF[i]==0x00)
  107. {bstop=1;break;}
  108. else if(DBUF[i]==0xE5)
  109. continue;
  110. else
  111. {
  112. for(k=0;k<32;k++)
  113. UARTBUF[item*32+k]=DBUF[i+k];
  114. item=item+1;
  115.  if(item>=(UARTBUF_LENGTH/32))  {bstop=1;break;}  //串口缓冲 只能有512项目录,强行结束
  116. /////////////////////////////////
  117. if(Lcount<MaxLFNum)
  118.  {
  119. if(DBUF[i+11]==0x0F)
  120. {
  121. base=((DBUF[i]&0x1F)-1)*26;
  122. if(base<=224)
  123. {
  124. Ncount=0;
  125. for(k=1;k<11;k++)
  126. {
  127. ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
  128. Ncount++;
  129. }
  130. for(k=14;k<26;k++)
  131. {
  132. ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
  133. Ncount++;
  134. }
  135. for(k=28;k<32;k++)
  136. {
  137. ShowFileName[Lcount].LongName[base+Ncount]=DBUF[i+k];
  138. Ncount++;
  139. }
  140. }
  141. }
  142. else
  143. {
  144. for(k=0;k<32;k++)
  145. ShowFileName[Lcount].item[k]=DBUF[i+k];
  146.      Lcount++;
  147. }
  148.       }
  149. /////////////////////////////////
  150. }
  151. }
  152. if(bstop==1)break;
  153.      }
  154. if(bstop==1)break;
  155. NowCluster32=GetNextClusterNum32(NowCluster32); 
  156. }while(NowCluster32<=DeviceInfo.TotCluster);
  157. Response.len=item*32;
  158. return TRUE;
  159. }
  160. unsigned char OpenFile32(unsigned char *pBuffer)
  161. {
  162. unsigned int i;
  163. unsigned char j,bstop,sector;
  164. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  165. return FALSE;
  166. ThisFile.bFileOpen=0;
  167. NowCluster32=DirStartCluster32;
  168. do
  169. {
  170. NowSector=FirstSectorofCluster32(NowCluster32);
  171. for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  172.      {   
  173. if(!SdReadSector(NowSector+sector,1,DBUF))
  174. return FALSE;
  175. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  176. {
  177. if(DBUF[i]==0x00)
  178. return FALSE;
  179. j=0;
  180. while(DBUF[i+j]==*(pBuffer+j))
  181. {
  182.  j=j+1;
  183.  if(j>10)
  184.   break;
  185. }
  186. if(j>10&&(DBUF[i+11]&0x10)!=0x10)
  187.      { 
  188. for(j=0;j<32;j++)
  189.      UARTBUF[j]=DBUF[i+j];     
  190.      bstop=1;
  191.      break;
  192. }
  193. }
  194. if(bstop==1)break;
  195.      }
  196. if(bstop==1)break;
  197. NowCluster32=GetNextClusterNum32(NowCluster32);
  198. }while(NowCluster32<=DeviceInfo.TotCluster);
  199. if(NowCluster32>DeviceInfo.TotCluster)
  200.     return FALSE;
  201. ThisFile.bFileOpen=1;
  202. ThisFile.StartCluster=LSwapINT32(UARTBUF[26],UARTBUF[27],UARTBUF[20],UARTBUF[21]);
  203. ThisFile.LengthInByte=LSwapINT32(UARTBUF[28],UARTBUF[29],UARTBUF[30],UARTBUF[31]);
  204. ThisFile.ClusterPointer=ThisFile.StartCluster;
  205. ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.StartCluster);
  206. ThisFile.OffsetofSector=0;
  207. ThisFile.SectorofCluster=0;
  208. ThisFile.FatSectorPointer=0;
  209. ThisFile.pointer=0;
  210. Response.len=32;
  211. return TRUE;
  212. }
  213. unsigned char ReadFile32(unsigned long readLength,unsigned char *pBuffer)
  214. {
  215. unsigned int len,i;
  216. unsigned int tlen;
  217. unsigned long blen;
  218. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  219. return FALSE;
  220. if(!ThisFile.bFileOpen)
  221. return FALSE;
  222. blen=readLength;
  223. tlen=0;
  224. if(readLength>MAX_READ_LENGTH)
  225. return FALSE;
  226. if(readLength+ThisFile.pointer>ThisFile.LengthInByte)
  227. return FALSE;
  228. ////////////////////////////////////////////
  229. while(readLength>0)
  230. {
  231.    if(readLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
  232.     len=DeviceInfo.BPB_BytesPerSec;
  233.    else
  234.     len=readLength+ThisFile.OffsetofSector;
  235.    
  236.    //////////////////////////////////////////////////////
  237.    if(ThisFile.OffsetofSector>0)
  238.     {
  239.     if(SdReadSector(ThisFile.SectorPointer,1,DBUF))
  240.     {
  241.    
  242.     len=len-ThisFile.OffsetofSector;
  243.     for(i=0;i<len;i++)
  244.    
  245.     *(pBuffer+i)=DBUF[ThisFile.OffsetofSector+i];
  246.     ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
  247.     }
  248.     else
  249.     return FALSE;    
  250.     }
  251.    else
  252.     {
  253.     if(!SdReadSector(ThisFile.SectorPointer,1,pBuffer+tlen))
  254.     return FALSE;
  255.     ThisFile.OffsetofSector=len;
  256.     }
  257.    ////////////////////////////////////////////////////////////
  258.    readLength-=len;
  259.    tlen+=len;
  260.   
  261.    /////////////////////////////////////////////////////////
  262.    if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
  263.    {
  264.     ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
  265.     ThisFile.SectorofCluster+=1;
  266.     if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
  267.     {
  268.     ThisFile.SectorofCluster=0;
  269.    ThisFile.ClusterPointer=GetNextClusterNum32(ThisFile.ClusterPointer);
  270.    if(ThisFile.ClusterPointer>DeviceInfo.TotCluster)
  271.        return FALSE;    
  272.    ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.ClusterPointer); 
  273.     }
  274.     else
  275.     ThisFile.SectorPointer=ThisFile.SectorPointer+1;
  276.     }
  277.    //////////////////////////////////////////////////////////////////
  278. }//end while
  279. ThisFile.bFileOpen=1;
  280. ThisFile.pointer+=tlen;
  281. //////////////////////////////////////////////
  282. Response.len=blen;
  283. return TRUE;
  284. }
  285. unsigned char SetFilePointer32(unsigned long pointer)
  286. {
  287. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  288. return FALSE;
  289. if(!ThisFile.bFileOpen)
  290. return FALSE;
  291. ///////////////////////////////////////////////////////////
  292. ThisFile.pointer=pointer;
  293. if(ThisFile.pointer>ThisFile.LengthInByte)
  294. return FALSE;
  295. if(!GoToPointer32(ThisFile.pointer))
  296. {
  297. ThisFile.bFileOpen=0;
  298. return FALSE;
  299. }
  300. //////////////////////////////////////////////
  301. return TRUE;
  302. }
  303. unsigned char CreateFile32(unsigned long len,unsigned char *pBuffer,unsigned char *pName)
  304. {
  305. unsigned int sector,i,j,DirCount;
  306. unsigned long cnum;
  307. unsigned char xdata bstop,InByte,bwrite;
  308. unsigned long ClusterPointer;
  309. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  310. return FALSE;
  311. if((len%32)!=0)
  312. return FALSE;
  313. if((len+32)>DeviceInfo.BPB_BytesPerSec)
  314. return FALSE;
  315. ThisFile.bFileOpen=0;
  316. cnum=GetFreeCusterNum32();
  317. if(cnum<0x02)
  318. return FALSE;
  319. pBuffer[21]=(unsigned char)(cnum>>24);
  320. pBuffer[20]=(unsigned char)(cnum>>16);
  321. pBuffer[27]=(unsigned char)(cnum>>8);
  322. pBuffer[26]=(unsigned char)(cnum);
  323. pBuffer[28]=0;pBuffer[29]=0;pBuffer[30]=0;pBuffer[31]=0;
  324. bstop=0;
  325. NowCluster32=DirStartCluster32;
  326. do
  327. {
  328. NowSector=FirstSectorofCluster32(NowCluster32);
  329. ClusterPointer=NowCluster32;
  330. for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  331.      {   
  332. if(!SdReadSector(NowSector+sector,1,DBUF))
  333. return FALSE;
  334. DirCount=0;bwrite=0;
  335. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  336. {
  337. if(len==0)
  338. {
  339. if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
  340. {
  341. for(j=0;j<32;j++)
  342. DBUF[i+j]=*(pBuffer+j);
  343. if(!SdWriteSector(NowSector+sector,1,DBUF))
  344.      return FALSE;     
  345. bstop=1;
  346. break;
  347. }
  348. }
  349. else
  350. {
  351. if(DirCount==0)
  352. InByte=i;
  353. if(DBUF[i]==0xE5)
  354. DirCount++;
  355. else if(DBUF[i]==0x00)
  356. {
  357. DirCount++;
  358. DBUF[i]=0xE5;
  359. bwrite=1;
  360. }
  361. else
  362. DirCount=0;
  363. if((DirCount*32)>=(len+32))
  364. {
  365. for(j=0;j<len;j++)
  366. DBUF[InByte+j]=*(pName+j);
  367. for(j=0;j<32;j++)
  368. DBUF[InByte+len+j]=*(pBuffer+j);
  369. if(!SdWriteSector(NowSector+sector,1,DBUF))
  370.      return FALSE;     
  371. bstop=1;
  372. break;
  373. }
  374.  }
  375. }
  376. if(bstop==1)break;
  377. if((len!=0)&&(bwrite==1))
  378. {
  379. if(!SdWriteSector(NowSector+sector,1,DBUF))
  380.    return FALSE;
  381.      }
  382.      }
  383. if(bstop==1)break;
  384. NowCluster32=GetNextClusterNum32(NowCluster32);
  385. if(NowCluster32>DeviceInfo.TotCluster)
  386.      {
  387. NowCluster32=CreateClusterLink32(ClusterPointer);
  388.   if(NowCluster32==0x00)
  389.    return FALSE;
  390. NowSector=FirstSectorofCluster32(NowCluster32);
  391. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i++) DBUF[i]=0x00;
  392. for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  393. {
  394. if(!SdWriteSector(NowSector+sector,1,DBUF))
  395.      return FALSE;
  396. }
  397. }
  398. }while(NowCluster32<=DeviceInfo.TotCluster);
  399. ////////////////////////////////////////////////////////////////
  400. ThisFile.StartCluster=cnum;
  401. ThisFile.LengthInByte=0;
  402. ThisFile.ClusterPointer=ThisFile.StartCluster;
  403. ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.StartCluster);
  404. ThisFile.OffsetofSector=0;
  405. ThisFile.SectorofCluster=0;
  406. ThisFile.bFileOpen=1;
  407. ThisFile.pointer=0;
  408. ThisFile.FatSectorPointer=0;
  409. return TRUE;
  410. }
  411. unsigned char WriteFile32(unsigned long writeLength,unsigned char *pBuffer)
  412. {
  413.     unsigned long cnum;
  414. unsigned int  len,sector,i,tlen;
  415. unsigned char bSuccess,bStop;
  416. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  417. return FALSE;
  418. if(!ThisFile.bFileOpen)
  419. return FALSE;
  420. ThisFile.bFileOpen=0;
  421. bSuccess=1;
  422. bStop=0;
  423. tlen=0;
  424. while(writeLength>0)
  425. {
  426. if(writeLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
  427.     len=DeviceInfo.BPB_BytesPerSec;
  428. else
  429.     len=writeLength+ThisFile.OffsetofSector;
  430.    
  431.  //////////////////////////////////////////////////////
  432.  if(ThisFile.OffsetofSector>0)
  433.   {
  434.   if(SdReadSector(ThisFile.SectorPointer,1,DBUF))
  435.     {
  436.    
  437.     len=len-ThisFile.OffsetofSector;
  438.     for(i=0;i<len;i++)    
  439.     DBUF[ThisFile.OffsetofSector+i]=*(pBuffer+i);
  440.     if(!SdWriteSector(ThisFile.SectorPointer,1,DBUF))
  441.     return FALSE;    
  442.     ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
  443.     }
  444.     else
  445.     return FALSE;    
  446.   }
  447.  else
  448.   {
  449.   if(!SdWriteSector(ThisFile.SectorPointer,1,pBuffer+tlen))
  450.     return FALSE;    
  451.     ThisFile.OffsetofSector=len;
  452.   }
  453.  /////////////////////////////////////////////////////
  454.    writeLength-=len;
  455.    tlen+=len;
  456.  /////////////更新文件指针 //////////////////////////////
  457.   if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
  458.    {
  459.     ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
  460.     ThisFile.SectorofCluster+=1;
  461.     if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
  462.     {
  463.     ThisFile.SectorofCluster=0;
  464.    ThisFile.ClusterPointer=CreateClusterLink32(ThisFile.ClusterPointer);
  465.    if(ThisFile.ClusterPointer==0x00)
  466.     return FALSE;  
  467.    ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.ClusterPointer); 
  468.     }
  469.     else
  470.     ThisFile.SectorPointer=ThisFile.SectorPointer+1;
  471.     }
  472. }//end while
  473. ThisFile.pointer+=tlen;
  474. ///////////更新文件目录信息/////////////////////////////
  475. if(bSuccess==1)
  476. {
  477. NowCluster32=DirStartCluster32;
  478. do
  479. {
  480. NowSector=FirstSectorofCluster32(NowCluster32);
  481. for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  482.       {   
  483. if(!SdReadSector(NowSector+sector,1,DBUF))
  484. return FALSE;
  485. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  486. {
  487. cnum=LSwapINT32(DBUF[i+26],DBUF[i+27],DBUF[i+20],DBUF[i+21]);
  488. if((cnum==ThisFile.StartCluster)&&(DBUF[i]!=0xe5))
  489. {
  490. if(ThisFile.pointer>ThisFile.LengthInByte)
  491. ThisFile.LengthInByte=ThisFile.pointer;
  492. DBUF[i+28]=(unsigned char)(ThisFile.LengthInByte&0xff);
  493. DBUF[i+29]=(unsigned char)((ThisFile.LengthInByte>>8)&0xff);
  494. DBUF[i+30]=(unsigned char)((ThisFile.LengthInByte>>16)&0xff);
  495. DBUF[i+31]=(unsigned char)((ThisFile.LengthInByte>>24)&0xff);
  496. if(!SdWriteSector(NowSector+sector,1,DBUF))
  497.     return FALSE;    
  498.  bStop=1;
  499.  break;
  500. }
  501.      }
  502. if(bStop==1)break;
  503. }
  504. if(bStop==1)break;
  505. NowCluster32=GetNextClusterNum32(NowCluster32);
  506. }while(NowCluster32<=DeviceInfo.TotCluster);
  507. if(NowCluster32>DeviceInfo.TotCluster)
  508.      return FALSE;
  509. }
  510. ThisFile.bFileOpen=1;
  511. //////////////////////////////////////////////
  512. return TRUE;
  513. }
  514. unsigned char RemoveFile32(unsigned char *pBuffer)
  515. {
  516. unsigned int sector,i;
  517. unsigned char bStop,j;
  518. int k;
  519. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  520. return FALSE;
  521. ////////////// 清除目录/////////////////////////////////////
  522. NowCluster32=DirStartCluster32;
  523. do
  524. {
  525. NowSector=FirstSectorofCluster32(NowCluster32);
  526. for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  527.      {   
  528. if(!SdReadSector(NowSector+sector,1,DBUF))
  529. return FALSE;
  530. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  531. {
  532. if(DBUF[i]==0x00)
  533. return FALSE;
  534. j=0;
  535. while(DBUF[i+j]==*(pBuffer+j))
  536. {
  537.  j=j+1;
  538.  if(j>10)
  539.   break;
  540. }
  541. if(j>10)
  542.      { 
  543. DBUF[i]=0xE5;
  544. ThisFile.StartCluster=LSwapINT32(DBUF[i+26],DBUF[i+27],DBUF[i+20],DBUF[i+21]);
  545.   for(k=(i-32);k>=0;k=k-32)
  546. {
  547. if(DBUF[k+11]==0x0F)
  548. DBUF[k]=0xE5;
  549. else
  550. break;
  551. }
  552.   DelayMs(15);
  553.   if(!SdWriteSector(NowSector+sector,1,DBUF))
  554. return FALSE;
  555. //////////////////// 清除FAT中的纪录////////////////////////
  556. DelayMs(10);
  557. if(!DeleteClusterLink32(ThisFile.StartCluster))
  558. return FALSE;
  559.   bStop=1;
  560.   break;
  561. }
  562. }
  563. if(bStop==1)break;
  564.      }
  565. if(bStop==1)break;
  566. NowCluster32=GetNextClusterNum32(NowCluster32);
  567. }while(NowCluster32<=DeviceInfo.TotCluster);
  568. if(NowCluster32>DeviceInfo.TotCluster)
  569.     return FALSE;
  570. return TRUE;
  571. }
  572. unsigned char GetCapacity32(void)
  573. {
  574. unsigned int sectorNum,i;
  575. unsigned long FreeSize,Freesectorcnt;
  576. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  577. return FALSE;
  578. ////////////////////////////////////////////////////////////////////////
  579. sectorNum=DeviceInfo.FatStartSector;
  580. Freesectorcnt=0;
  581. while(sectorNum<DeviceInfo.BPB_FATSz32+DeviceInfo.FatStartSector)
  582. {
  583. if(SdReadSector(sectorNum,1,DBUF))
  584. {
  585.   for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+4)
  586.    {     
  587.     if((DBUF[i]||DBUF[i+1]||DBUF[i+2]||DBUF[i+3])==0x00)
  588.      {
  589.      Freesectorcnt++;
  590.      }   
  591.    }
  592. }
  593. else
  594. return FALSE;
  595. sectorNum++;
  596. }
  597. ////////////////////////////////////////////////////////////////////////
  598. FreeSize=DeviceInfo.BPB_BytesPerSec*DeviceInfo.BPB_SecPerClus;
  599. FreeSize=Freesectorcnt*FreeSize;
  600. UARTBUF[0]=(unsigned char)((FreeSize>>24)&0xff);
  601. UARTBUF[1]=(unsigned char)((FreeSize>>16)&0xff);
  602. UARTBUF[2]=(unsigned char)((FreeSize>>8)&0xff);
  603. UARTBUF[3]=(unsigned char)(FreeSize&0xff);
  604. Response.len=4;
  605. return TRUE;
  606. }
  607. unsigned char CreateDir32(unsigned long len,unsigned char *pBuffer,unsigned char *pName)
  608. {
  609. unsigned int sector,i,j,DirCount;
  610. unsigned long cnum;
  611. unsigned char xdata bstop,InByte,bwrite;
  612. unsigned long ClusterPointer;
  613. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  614. return FALSE;
  615. if((len%32)!=0)
  616. return FALSE;
  617. if((len+32)>DeviceInfo.BPB_BytesPerSec)
  618. return FALSE;
  619. ThisFile.bFileOpen=0;
  620. ThisFile.FatSectorPointer=0;
  621. cnum=GetFreeCusterNum32();
  622. if(cnum<0x02)
  623. return FALSE;
  624. pBuffer[11]=0x10;
  625. pBuffer[21]=(unsigned char)(cnum>>24);
  626. pBuffer[20]=(unsigned char)(cnum>>16);
  627. pBuffer[27]=(unsigned char)(cnum>>8);
  628. pBuffer[26]=(unsigned char)(cnum);
  629. pBuffer[28]=0;pBuffer[29]=0;pBuffer[30]=0;pBuffer[31]=0;
  630. bstop=0;
  631. NowCluster32=DirStartCluster32;
  632. do
  633. {
  634. NowSector=FirstSectorofCluster32(NowCluster32);
  635. ClusterPointer=NowCluster32;
  636. for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  637.      {   
  638. if(!SdReadSector(NowSector+sector,1,DBUF))
  639. return FALSE;
  640. DirCount=0;bwrite=0;
  641. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  642. {
  643. if(len==0)
  644. {
  645. if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
  646. {
  647. for(j=0;j<32;j++)
  648. DBUF[i+j]=*(pBuffer+j);
  649. if(!SdWriteSector(NowSector+sector,1,DBUF))
  650.      return FALSE;     
  651. bstop=1;
  652. break;
  653. }
  654. }
  655. else
  656. {
  657. if(DirCount==0)
  658. InByte=i;
  659. if(DBUF[i]==0xE5)
  660. DirCount++;
  661. else if(DBUF[i]==0x00)
  662. {
  663. DirCount++;
  664. DBUF[i]=0xE5;
  665. bwrite=1;
  666. }
  667. else
  668. DirCount=0;
  669. if((DirCount*32)>=(len+32))
  670. {
  671. for(j=0;j<len;j++)
  672. DBUF[InByte+j]=*(pName+j);
  673. for(j=0;j<32;j++)
  674. DBUF[InByte+len+j]=*(pBuffer+j);
  675. if(!SdWriteSector(NowSector+sector,1,DBUF))
  676.      return FALSE;     
  677. bstop=1;
  678. break;
  679. }
  680.  }
  681. }
  682. if(bstop==1)break;
  683. if((len!=0)&&(bwrite==1))
  684. {
  685. if(!SdWriteSector(NowSector+sector,1,DBUF))
  686.    return FALSE;
  687.      }
  688.      }
  689. if(bstop==1)break;
  690. NowCluster32=GetNextClusterNum32(NowCluster32);
  691. if(NowCluster32>DeviceInfo.TotCluster)
  692.      {
  693. NowCluster32=CreateClusterLink32(ClusterPointer);
  694.   if(NowCluster32==0x00)
  695.    return FALSE;
  696. NowSector=FirstSectorofCluster32(NowCluster32);
  697. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i++) DBUF[i]=0x00;
  698. for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  699. {
  700. if(!SdWriteSector(NowSector+sector,1,DBUF))
  701.      return FALSE;
  702. }
  703. }
  704. }while(NowCluster32<=DeviceInfo.TotCluster);
  705. if(NowCluster32>DeviceInfo.TotCluster)
  706.     return FALSE;
  707. ////////////////////////////////////////////////////////////////
  708. for(i=64;i<DeviceInfo.BPB_BytesPerSec;i++) DBUF[i]=0x00;
  709. for(i=0;i<43;i++) DBUF[i]=0x20;
  710. DBUF[0]=0x2e;
  711. for(i=11;i<32;i++) DBUF[i]=pBuffer[i];
  712. DBUF[32]=0x2e;DBUF[33]=0x2e;
  713. for(i=43;i<64;i++) DBUF[i]=pBuffer[i-32];
  714. if(DirStartCluster32==DeviceInfo.RootStartCluster) //Root Dir
  715. {
  716. DBUF[53]=0;DBUF[52]=0;DBUF[59]=0;DBUF[58]=0;
  717. }
  718. else
  719. {
  720. DBUF[53]=(unsigned char)(DirStartCluster32>>24);
  721. DBUF[52]=(unsigned char)(DirStartCluster32>>16);
  722. DBUF[59]=(unsigned char)(DirStartCluster32>>8);
  723. DBUF[58]=(unsigned char)(DirStartCluster32);
  724. }
  725. NowSector=FirstSectorofCluster32(cnum);
  726. if(!SdWriteSector(NowSector,1,DBUF))
  727. return FALSE;
  728. DirStartCluster32=cnum;
  729. // ThisFile.ClusterPointer=DirStartCluster32;
  730. ThisFile.ClusterPointer=0;
  731. return TRUE;
  732. }
  733. unsigned char DownDir32(unsigned char *pBuffer)
  734. {
  735. unsigned int i;
  736. unsigned char j,bstop,sector;
  737. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  738. return FALSE;
  739. ThisFile.bFileOpen=0;
  740. bstop=0;
  741. NowCluster32=DirStartCluster32;
  742. do
  743. {
  744. NowSector=FirstSectorofCluster32(NowCluster32);
  745. for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
  746.      {   
  747. if(!SdReadSector(NowSector+sector,1,DBUF))
  748. return FALSE;
  749. for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
  750. {
  751. if(DBUF[i]==0x00)
  752. return FALSE;
  753. j=0;
  754. while(DBUF[i+j]==*(pBuffer+j))
  755. {
  756.  j=j+1;
  757.  if(j>10)
  758.   break;
  759. }
  760. if(j>10&&(DBUF[i+11]&0x10))
  761.      {bstop=1;break;}
  762. }
  763. if(bstop==1)break;
  764.      }
  765. if(bstop==1)break;
  766. NowCluster32=GetNextClusterNum32(NowCluster32);
  767. }while(NowCluster32<=DeviceInfo.TotCluster);
  768. if(NowCluster32>DeviceInfo.TotCluster)
  769.     return FALSE;
  770. DirStartCluster32=LSwapINT32(DBUF[i+26],DBUF[i+27],DBUF[i+20],DBUF[i+21]);
  771. // ThisFile.ClusterPointer=DirStartCluster32;
  772. ThisFile.ClusterPointer=0;
  773. return TRUE;
  774. }
  775. unsigned char UpDir32()
  776. {
  777. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  778. return FALSE;
  779. if(DirStartCluster32==DeviceInfo.RootStartCluster) //Root Dir
  780. return TRUE;
  781. ThisFile.bFileOpen=0;
  782. NowSector=FirstSectorofCluster32(DirStartCluster32);
  783. if(!SdReadSector(NowSector,1,DBUF))
  784. return FALSE;
  785. if(DBUF[32]!=0x2e&&DBUF[33]!=0x2e) //..
  786. return FALSE;
  787. DirStartCluster32=LSwapINT32(DBUF[58],DBUF[59],DBUF[52],DBUF[53]);
  788. if(DirStartCluster32==0) //Root Dir
  789. DirStartCluster32=DeviceInfo.RootStartCluster;
  790. // ThisFile.ClusterPointer=DirStartCluster32;
  791. ThisFile.ClusterPointer=0;
  792. return TRUE;
  793. }
  794. unsigned char UpRootDir32()
  795. {
  796. if(!bFlags.bits.SLAVE_IS_ATTACHED)
  797. return FALSE;
  798. ThisFile.bFileOpen=0;
  799. DirStartCluster32=DeviceInfo.RootStartCluster; //Root Dir
  800. return TRUE;
  801. }