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

📄 sd.lst

📁 增强型51单片机fs7821sd卡、mmc卡读写程序
💻 LST
📖 第 1 页 / 共 3 页
字号:
 425   2              return STATUS_FLASH_ERROR;
 426   2          }
C51 COMPILER V7.02b   SD                                                                   02/01/2007 10:35:44 PAGE 8   

 427   1          else
 428   1          {
 429   2              CardType = CARD_MMC;    
 430   2              
 431   2              // Wait until card power-up operation finished
 432   2              for(RetryCount = 0; RetryCount < 500; RetryCount++)
 433   2              {
 434   3                  *((UINT32 *)(&SdCmdBuf[1])) = OPERATION_VOL_RANGE;
 435   3          
 436   3                  Status = SdSendCmd(SEND_OP_COND, SdCmdBuf);
 437   3      
 438   3                  if(Status != STATUS_SUCCESS)
 439   3                      continue;
 440   3                      
 441   3                  if(SdRespBuf[1] & CARD_PWRUP_FINISH)
 442   3                      return STATUS_SUCCESS;           
 443   3              }
 444   2              return STATUS_FLASH_ERROR;
 445   2          }
 446   1      }       
 447          
 448          
 449          //----------------------------------------------------------------------------
 450          STATUS SdGoIdentifyState()
 451          {
 452   1          STATUS Status;
 453   1          int i;
 454   1      
 455   1          for(i = 1; i <= 4; i++)
 456   1              SdCmdBuf[i] = 0;
 457   1          Status = SdSendCmd(ALL_SEND_CID, SdCmdBuf);
 458   1          return Status;
 459   1      }
 460          
 461          //--------------------------------------------------------------------
 462          STATUS SdGoStandbyState()
 463          {
 464   1          STATUS Status;
 465   1          int i;
 466   1              
 467   1          if(CardType == CARD_SD)
 468   1          {
 469   2              for(i = 1; i <= 4; i++)
 470   2                  SdCmdBuf[i] = 0;
 471   2          }
 472   1          else
 473   1          {
 474   2              RCA = 0x00010000;       
 475   2              *((UINT32 *)(&SdCmdBuf[1])) = RCA;
 476   2          }
 477   1      
 478   1          Status = SdSendCmd(SEND_RELATIVE_ADDR, SdCmdBuf);
 479   1      
 480   1          if(CardType == CARD_SD)
 481   1              RCA = *((UINT32 *)(&SdRespBuf[1])) & 0xffff0000;
 482   1      
 483   1          return STATUS_SUCCESS;
 484   1          
 485   1          // SD and MMC adopt different ways to set their relative card 
 486   1          // address. That is, they have different response to CMD3 
 487   1          // (SEND_RELATIVE_ADDR).
 488   1          //
C51 COMPILER V7.02b   SD                                                                   02/01/2007 10:35:44 PAGE 9   

 489   1          // According to SD Standard Version 1.01 page 25:
 490   1          //     the host issues CMD3 (SEND_RELATIVE_ADDR) asks the 
 491   1          //     card to publish a new relative card address (RCA).
 492   1          //
 493   1          // According to MMC Standard Version 3.1 page 24:
 494   1          //     the host issues CMD3 (SEND_RELATIVE_ADDR) to assign
 495   1          //     to this card a relative card address (RCA).
 496   1          //
 497   1          // This means : when host send CMD3 to SD, it is SD telling host
 498   1          // its new address, that is, SD is the one giving the new address
 499   1          // (and host decides whether to accept it). Meanwhile, when host 
 500   1          // sends CMD3 to MMC, it is host assigning the card a new address,
 501   1          // that is, host is the one decides the address (and MMC must 
 502   1          // accept it).
 503   1          // 
 504   1          // For SD, CMD3 has a response of type R6, which contains address
 505   1          // value returns by SD. For MMC, CMD3 has a response of type R1, 
 506   1          // which tells host if MMC accepts the address or error happens.
 507   1      }
 508          
 509          //--------------------------------------------------------------------
 510          STATUS SdReadCSD()                      
 511          {
 512   1          STATUS Status;      
 513   1          BYTE READ_BL_LEN;
 514   1          BYTE C_SIZE_MUTI;
 515   1          UINT32 C_SIZE;
 516   1           
 517   1          *((UINT32 *)(&SdCmdBuf[1])) = RCA;
 518   1          
 519   1          Status = SdSendCmd(SEND_CSD, SdCmdBuf);
 520   1          if(Status != STATUS_SUCCESS)
 521   1              return Status;  
 522   1      
 523   1          READ_BL_LEN = SdRespBuf[6] & 0xf;
 524   1          C_SIZE = SdRespBuf[7] & 0x3;
 525   1          C_SIZE <<= 8;
 526   1          C_SIZE |= SdRespBuf[8];
 527   1          C_SIZE <<= 2;
 528   1          C_SIZE |= (SdRespBuf[9] >> 6);
 529   1          C_SIZE_MUTI = SdRespBuf[10] & 0x3;
 530   1          C_SIZE_MUTI <<= 1;
 531   1          C_SIZE_MUTI |= (SdRespBuf[11] >> 7);
 532   1        
 533   1          SdAvailableBlocks = (UINT32)((C_SIZE + 1) << (C_SIZE_MUTI + 2 + 
 534   1                                   (READ_BL_LEN - BYTES_PER_SECTOR_SHIFT)));
 535   1          SdAvailableBlocks -= 1;  
 536   1      
 537   1          return STATUS_SUCCESS;
 538   1      }       
 539          
 540          //----------------------------------------------------------------------------
 541          STATUS SdGoTransferState()
 542          {
 543   1          STATUS Status;
 544   1          BYTE CurrentState , RegValue;
 545   1              
 546   1          *((UINT32 *)(&SdCmdBuf[1])) = RCA;
 547   1          Status = SdSendCmd(SELECT_CARD, SdCmdBuf);
 548   1          if(Status != STATUS_SUCCESS)
 549   1              return Status;  
 550   1      
C51 COMPILER V7.02b   SD                                                                   02/01/2007 10:35:44 PAGE 10  

 551   1          *((UINT32 *)(&SdCmdBuf[1])) = RCA;
 552   1          Status = SdSendCmd(SEND_STATUS, SdCmdBuf);
 553   1          if(Status != STATUS_SUCCESS)
 554   1              return Status;  
 555   1      
 556   1          CurrentState = (SdRespBuf[3] >> 1) & 0x0F;
 557   1          if(CurrentState == TRANSFER_STATE)
 558   1          {
 559   2              SdHiClk();
 560   2              if(CardType == CARD_SD)
 561   2              {
 562   3                  Status = SdChangeBusWidth();
 563   3                  if(Status != STATUS_SUCCESS)
 564   3                      return Status;
 565   3              }
 566   2              else // MMC 3.1
 567   2              {
 568   3                  RegValue = CSRRead(SD_BASE + SD_CLK_CTRL);
 569   3                  RegValue &= ~SD_CLK_NIBBLE;
 570   3                  CSRWrite(SD_BASE + SD_CLK_CTRL, RegValue);
 571   3              }
 572   2          }   
 573   1          else
 574   1              return STATUS_FLASH_ERROR;
 575   1          
 576   1          return STATUS_SUCCESS;
 577   1      }       
 578          
 579          //----------------------------------------------------------------------------
 580          //  Description:
 581          //    Stop the read or write command
 582          //----------------------------------------------------------------------------
 583          STATUS SdStopCmd()
 584          {
 585   1          STATUS Status;
 586   1          BYTE RegValue;
 587   1          int i;
 588   1      
 589   1          RegValue = CSRRead(SD_BASE + SD_CLK_CTRL);
 590   1          RegValue &= ~SD_CLK_AUTO_DIS;
 591   1          CSRWrite(SD_BASE + SD_CLK_CTRL, RegValue);
 592   1      
 593   1          for(i = 1; i <= 4; i++)
 594   1              SdCmdBuf[i] = 0;
 595   1          Status = SdSendCmd(STOP_TRANS, SdCmdBuf);
 596   1          if(Status != STATUS_SUCCESS)
 597   1              return Status;
 598   1       
 599   1          SdWaitCard();
 600   1            
 601   1          return STATUS_SUCCESS; 
 602   1      }
 603          //----------------------------------------------------------------------------
 604          STATUS SdWaitCmd()
 605          {
 606   1        CmdTimer = SD_CMD_TIMEOUT;    
 607   1        while ((!(CSRRead(SD_BASE + SD_IE) & SD_COMPLETE)) && CmdTimer);
 608   1        // Clear command complete INT.        
 609   1        CSRWrite(SD_BASE + SD_IE, ~SD_COMPLETE);
 610   1      
 611   1        if (CmdTimer)
 612   1          return STATUS_SUCCESS;
C51 COMPILER V7.02b   SD                                                                   02/01/2007 10:35:44 PAGE 11  

 613   1        else 
 614   1        {
 615   2          //DebugShow("<SD> Wait TimeOut! Cmd = %02bx\r\n", CmdBuf[0]);
 616   2          SdCtrlReset();
 617   2          return STATUS_FLASH_ERROR;
 618   2        }
 619   1      }       
 620          //--------------------------------------------------------------------
 621          // Read first sector of data from SD/MMC
 622          //--------------------------------------------------------------------
 623          STATUS SdReadOneSector()
 624          {
 625   1          STATUS Status;
 626   1          int RetryCount1 , RetryCount2;
 627   1          bool ReadyForData;
 628   1      
 629   1          if(SectorStart > SdAvailableBlocks)
 630   1              return STATUS_PARAM_ERROR;
 631   1      
 632   1          SdWaitCard();
 633   1        
 634   1          for(RetryCount1 = 0; RetryCount1 < 5; RetryCount1++)
 635   1          {
 636   2              ReadyForData = false;
 637   2              for(RetryCount2 = 0; RetryCount2 < 20; RetryCount2++)  //读取SD/MMC卡的状态,SD/MMC卡没准备好发送

⌨️ 快捷键说明

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