⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hpi32.lst

📁 51模拟SPI读写SD卡(包括Fat和Fat32文件系统)源代码
💻 LST
📖 第 1 页 / 共 4 页
字号:
 222   1              ThisFile.FatSectorPointer=0;    
 223   1              ThisFile.pointer=0;
 224   1              
 225   1              Response.len=32;
 226   1              return TRUE;    
 227   1      }
 228          
 229          
 230          unsigned char ReadFile32(unsigned long readLength,unsigned char *pBuffer)
 231          {
 232   1      
 233   1              unsigned int len,i;
 234   1              unsigned int tlen;      
 235   1              unsigned long blen;
 236   1      
 237   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 238   1                      return FALSE;           
 239   1              if(!ThisFile.bFileOpen)
 240   1                      return FALSE;
 241   1      
C51 COMPILER V8.02   HPI32                                                                 04/29/2007 12:48:53 PAGE 5   

 242   1              blen=readLength;
 243   1              tlen=0;
 244   1              if(readLength>MAX_READ_LENGTH)
 245   1                      return FALSE;   
 246   1              
 247   1              if(readLength+ThisFile.pointer>ThisFile.LengthInByte)
 248   1                      return FALSE;   
 249   1                      
 250   1              ////////////////////////////////////////////            
 251   1                      while(readLength>0)
 252   1                      {
 253   2                         if(readLength+ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec)
 254   2                              len=DeviceInfo.BPB_BytesPerSec;
 255   2                         else
 256   2                              len=readLength+ThisFile.OffsetofSector;
 257   2                         
 258   2                         //////////////////////////////////////////////////////
 259   2                         if(ThisFile.OffsetofSector>0)
 260   2                              {
 261   3                              if(SdReadSector(ThisFile.SectorPointer,1,DBUF))
 262   3                                      {
 263   4                                      
 264   4                                      len=len-ThisFile.OffsetofSector;
 265   4                                      for(i=0;i<len;i++)
 266   4                                              
 267   4                                              *(pBuffer+i)=DBUF[ThisFile.OffsetofSector+i];
 268   4                                      ThisFile.OffsetofSector=ThisFile.OffsetofSector+len;
 269   4                                      }
 270   3                              else
 271   3                                      return FALSE;                           
 272   3                              }
 273   2                         else
 274   2                              {
 275   3                                      if(!SdReadSector(ThisFile.SectorPointer,1,pBuffer+tlen))
 276   3                                                      return FALSE;   
 277   3                                      ThisFile.OffsetofSector=len;
 278   3                              }
 279   2                         ////////////////////////////////////////////////////////////
 280   2                         readLength-=len;
 281   2                         tlen+=len;
 282   2                        
 283   2                         /////////////////////////////////////////////////////////
 284   2                         if(ThisFile.OffsetofSector>DeviceInfo.BPB_BytesPerSec-1)
 285   2                         {    
 286   3                              ThisFile.OffsetofSector-=DeviceInfo.BPB_BytesPerSec;
 287   3                              ThisFile.SectorofCluster+=1;
 288   3                              if(ThisFile.SectorofCluster>DeviceInfo.BPB_SecPerClus-1)
 289   3                              {
 290   4                                      ThisFile.SectorofCluster=0;
 291   4                                       ThisFile.ClusterPointer=GetNextClusterNum32(ThisFile.ClusterPointer);
 292   4                                       if(ThisFile.ClusterPointer>DeviceInfo.TotCluster)
 293   4                                                 return FALSE;                                                
 294   4                                       ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.ClusterPointer);        
 295   4                              }
 296   3                              else
 297   3                                      ThisFile.SectorPointer=ThisFile.SectorPointer+1;
 298   3                          }
 299   2                         //////////////////////////////////////////////////////////////////
 300   2                      }//end while
 301   1                      
 302   1              ThisFile.bFileOpen=1;
 303   1              ThisFile.pointer+=tlen;
C51 COMPILER V8.02   HPI32                                                                 04/29/2007 12:48:53 PAGE 6   

 304   1              //////////////////////////////////////////////
 305   1              Response.len=blen;
 306   1      
 307   1              return TRUE;
 308   1      }
 309          
 310          unsigned char SetFilePointer32(unsigned long pointer)
 311          {
 312   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 313   1                      return FALSE;           
 314   1              if(!ThisFile.bFileOpen)
 315   1                      return FALSE;           
 316   1              ///////////////////////////////////////////////////////////
 317   1              ThisFile.pointer=pointer;
 318   1              if(ThisFile.pointer>ThisFile.LengthInByte)
 319   1                      return FALSE;   
 320   1      
 321   1              if(!GoToPointer32(ThisFile.pointer))
 322   1              {
 323   2              ThisFile.bFileOpen=0;
 324   2              return FALSE;   
 325   2              }
 326   1              //////////////////////////////////////////////
 327   1              return TRUE;
 328   1      }
 329          
 330          unsigned char CreateFile32(unsigned long len,unsigned char *pBuffer,unsigned char *pName)
 331          {
 332   1              unsigned int sector,i,j,DirCount;
 333   1              unsigned long cnum;
 334   1              unsigned char xdata bstop,InByte,bwrite;
 335   1              unsigned long ClusterPointer;
 336   1              
 337   1              if(!bFlags.bits.SLAVE_IS_ATTACHED)
 338   1                      return FALSE;
 339   1              if((len%32)!=0)
 340   1                      return FALSE;
 341   1              if((len+32)>DeviceInfo.BPB_BytesPerSec)
 342   1                      return FALSE;
 343   1      
 344   1              ThisFile.bFileOpen=0;   
 345   1      
 346   1              cnum=GetFreeCusterNum32();
 347   1              if(cnum<0x02)
 348   1                      return FALSE;   
 349   1      
 350   1              pBuffer[21]=(unsigned char)(cnum>>24);
 351   1              pBuffer[20]=(unsigned char)(cnum>>16);
 352   1              pBuffer[27]=(unsigned char)(cnum>>8);
 353   1              pBuffer[26]=(unsigned char)(cnum);
 354   1      
 355   1              pBuffer[28]=0;pBuffer[29]=0;pBuffer[30]=0;pBuffer[31]=0;
 356   1              bstop=0;
 357   1      
 358   1              NowCluster32=DirStartCluster32;         
 359   1                      do
 360   1                      {
 361   2                              NowSector=FirstSectorofCluster32(NowCluster32);
 362   2                              ClusterPointer=NowCluster32;
 363   2                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
 364   2                      {   
 365   3                                      if(!SdReadSector(NowSector+sector,1,DBUF))
C51 COMPILER V8.02   HPI32                                                                 04/29/2007 12:48:53 PAGE 7   

 366   3                                              return FALSE;
 367   3                                      DirCount=0;bwrite=0;
 368   3      
 369   3                                      for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+32)
 370   3                                      {
 371   4                                      if(len==0)
 372   4                                              {
 373   5                                              if((DBUF[i]==0x00)||(DBUF[i]==0xE5))
 374   5                                              {
 375   6                                              for(j=0;j<32;j++)
 376   6                                                      DBUF[i+j]=*(pBuffer+j);
 377   6                                              if(!SdWriteSector(NowSector+sector,1,DBUF))
 378   6                                                      return FALSE;                                   
 379   6                                              bstop=1;
 380   6                                              break;
 381   6                                              }               
 382   5                                              }
 383   4                                      else
 384   4                                      {
 385   5                                      if(DirCount==0)
 386   5                                              InByte=i;
 387   5                                      if(DBUF[i]==0xE5)                               
 388   5                                              DirCount++;                             
 389   5                                      else if(DBUF[i]==0x00)
 390   5                                              {       
 391   6                                              DirCount++;     
 392   6                                              DBUF[i]=0xE5;   
 393   6                                              bwrite=1;                               
 394   6                                              }
 395   5                                      else
 396   5                                              DirCount=0;
 397   5      
 398   5                                      if((DirCount*32)>=(len+32))
 399   5                                              {
 400   6                                              for(j=0;j<len;j++)
 401   6                                                      DBUF[InByte+j]=*(pName+j);
 402   6                                              for(j=0;j<32;j++)
 403   6                                                      DBUF[InByte+len+j]=*(pBuffer+j);
 404   6                                              if(!SdWriteSector(NowSector+sector,1,DBUF))
 405   6                                                      return FALSE;                                   
 406   6                                              bstop=1;
 407   6                                              break;
 408   6                                              }
 409   5                                       }
 410   4                                      }
 411   3                                      if(bstop==1)break;
 412   3                                      
 413   3                                      if((len!=0)&&(bwrite==1))
 414   3                                      {
 415   4                                      if(!SdWriteSector(NowSector+sector,1,DBUF))
 416   4                                              return FALSE;
 417   4                              }
 418   3                      }
 419   2                              if(bstop==1)break;
 420   2              
 421   2                              NowCluster32=GetNextClusterNum32(NowCluster32);
 422   2                              if(NowCluster32>DeviceInfo.TotCluster)
 423   2                      {
 424   3                              NowCluster32=CreateClusterLink32(ClusterPointer);
 425   3                              if(NowCluster32==0x00)
 426   3                                       return FALSE;
 427   3                              NowSector=FirstSectorofCluster32(NowCluster32);
C51 COMPILER V8.02   HPI32                                                                 04/29/2007 12:48:53 PAGE 8   

 428   3                              for(i=0;i<DeviceInfo.BPB_BytesPerSec;i++) DBUF[i]=0x00;
 429   3                              for(sector=0;sector<DeviceInfo.BPB_SecPerClus;sector++)
 430   3                                      {
 431   4                                      if(!SdWriteSector(NowSector+sector,1,DBUF))
 432   4                                              return FALSE;
 433   4                                      }
 434   3                              }
 435   2                      }while(NowCluster32<=DeviceInfo.TotCluster);    
 436   1      ////////////////////////////////////////////////////////////////
 437   1              
 438   1              ThisFile.StartCluster=cnum;
 439   1              ThisFile.LengthInByte=0;
 440   1              ThisFile.ClusterPointer=ThisFile.StartCluster;
 441   1              ThisFile.SectorPointer=FirstSectorofCluster32(ThisFile.StartCluster);
 442   1              ThisFile.OffsetofSector=0;
 443   1              ThisFile.SectorofCluster=0;
 444   1              ThisFile.bFileOpen=1;
 445   1              ThisFile.pointer=0;
 446   1              ThisFile.FatSectorPointer=0;
 447   1              
 448   1              return TRUE;
 449   1      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -