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

📄 sd.lst

📁 FS7805 使用ATA硬盘的demo
💻 LST
📖 第 1 页 / 共 5 页
字号:
 561   2              return STATUS_FLASH_ERROR;
 562   2          }
 563   1          else
 564   1          {
 565   2              CardType = CARD_MMC;
 566   2      
 567   2              // Wait until card power-up operation finished
 568   2              for(RetryCount = 0; RetryCount < 500; RetryCount++)
 569   2              {
 570   3      
 571   3                  *((UINT32 *)(&SdCmdBuf[1])) = OPERATION_VOL_RANGE;
 572   3      
 573   3                  Status = SdSendCmd(SEND_OP_COND, SdCmdBuf);
 574   3      
 575   3                  if(Status != STATUS_SUCCESS)
 576   3                      continue;
 577   3      
 578   3                  if(SdRespBuf[1] & CARD_PWRUP_FINISH)
 579   3                      return STATUS_SUCCESS;
 580   3              }
 581   2              return STATUS_FLASH_ERROR;
 582   2          }
 583   1      }
 584          
 585          
 586          //----------------------------------------------------------------------------
 587          STATUS SdGoIdentifyState()
 588          {
 589   1          STATUS Status;
 590   1          int i;
 591   1      
 592   1          for(i = 1; i <= 4; i++)
 593   1              SdCmdBuf[i] = 0;
 594   1          Status = SdSendCmd(ALL_SEND_CID, SdCmdBuf);
 595   1          return Status;
 596   1      }
 597          
 598          //--------------------------------------------------------------------
 599          STATUS SdGoStandbyState()
 600          {
 601   1          STATUS Status;
 602   1          int i;
 603   1      
 604   1          if(CardType == CARD_SD)
 605   1          {
 606   2              for(i = 1; i <= 4; i++)
 607   2                  SdCmdBuf[i] = 0;
 608   2          }
 609   1          else
 610   1          {
 611   2              RCA = 0x00010000;
 612   2              *((UINT32 *)(&SdCmdBuf[1])) = RCA;
C51 COMPILER V7.02b   SD                                                                   07/18/2006 15:32:57 PAGE 11  

 613   2          }
 614   1      
 615   1          Status = SdSendCmd(SEND_RELATIVE_ADDR, SdCmdBuf);
 616   1      
 617   1          if(CardType == CARD_SD)
 618   1              RCA = *((UINT32 *)(&SdRespBuf[1])) & 0xffff0000;
 619   1      
 620   1          return STATUS_SUCCESS;
 621   1      
 622   1          // SD and MMC adopt different ways to set their relative card
 623   1          // address. That is, they have different response to CMD3
 624   1          // (SEND_RELATIVE_ADDR).
 625   1          //
 626   1          // According to SD Standard Version 1.01 page 25:
 627   1          //     the host issues CMD3 (SEND_RELATIVE_ADDR) asks the
 628   1          //     card to publish a new relative card address (RCA).
 629   1          //
 630   1          // According to MMC Standard Version 3.1 page 24:
 631   1          //     the host issues CMD3 (SEND_RELATIVE_ADDR) to assign
 632   1          //     to this card a relative card address (RCA).
 633   1          //
 634   1          // This means : when host send CMD3 to SD, it is SD telling host
 635   1          // its new address, that is, SD is the one giving the new address
 636   1          // (and host decides whether to accept it). Meanwhile, when host
 637   1          // sends CMD3 to MMC, it is host assigning the card a new address,
 638   1          // that is, host is the one decides the address (and MMC must
 639   1          // accept it).
 640   1          //
 641   1          // For SD, CMD3 has a response of type R6, which contains address
 642   1          // value returns by SD. For MMC, CMD3 has a response of type R1,
 643   1          // which tells host if MMC accepts the address or error happens.
 644   1      }
 645          
 646          //--------------------------------------------------------------------
 647          STATUS SdReadCSD()
 648          {
 649   1          STATUS Status;
 650   1          BYTE READ_BL_LEN;
 651   1          BYTE C_SIZE_MUTI;
 652   1          UINT32 C_SIZE;
 653   1      
 654   1          *((UINT32 *)(&SdCmdBuf[1])) = RCA;
 655   1      
 656   1          Status = SdSendCmd(SEND_CSD, SdCmdBuf);
 657   1          if(Status != STATUS_SUCCESS)
 658   1              return Status;
 659   1      
 660   1          READ_BL_LEN = SdRespBuf[6] & 0xf;
 661   1          C_SIZE = SdRespBuf[7] & 0x3;
 662   1          C_SIZE <<= 8;
 663   1          C_SIZE |= SdRespBuf[8];
 664   1          C_SIZE <<= 2;
 665   1          C_SIZE |= (SdRespBuf[9] >> 6);
 666   1          C_SIZE_MUTI = SdRespBuf[10] & 0x3;
 667   1          C_SIZE_MUTI <<= 1;
 668   1          C_SIZE_MUTI |= (SdRespBuf[11] >> 7);
 669   1          //DebugPrint("READ_BL_LEN = %bx\r\n", READ_BL_LEN);
 670   1          //DebugPrint("C_SIZE_MUTI = %bx\r\n", C_SIZE_MUTI);
 671   1          //DebugPrint("C_SIZE = 0x%08lx\r\n", C_SIZE);
 672   1      
 673   1          SdAvailableBlocks = (UINT32)((C_SIZE + 1) << (C_SIZE_MUTI + 2 +
 674   1                                   (READ_BL_LEN - BYTES_PER_SECTOR_SHIFT)));
C51 COMPILER V7.02b   SD                                                                   07/18/2006 15:32:57 PAGE 12  

 675   1          SdAvailableBlocks -= 1;
 676   1      
 677   1          return STATUS_SUCCESS;
 678   1      }
 679          
 680          //----------------------------------------------------------------------------
 681          STATUS SdGoTransferState()
 682          {
 683   1          STATUS Status;
 684   1          BYTE CurrentState;
 685   1      
 686   1          *((INT32U *)(&SdCmdBuf[1])) = RCA;
 687   1          Status = SdSendCmd(SELECT_CARD, SdCmdBuf);
 688   1          if(Status != STATUS_SUCCESS)
 689   1              return Status;
 690   1      
 691   1          *((INT32U *)(&SdCmdBuf[1])) = RCA;
 692   1          Status = SdSendCmd(SEND_STATUS, SdCmdBuf);
 693   1          if(Status != STATUS_SUCCESS)
 694   1              return Status;
 695   1      
 696   1          CurrentState = (SdRespBuf[3] >> 1) & 0x0F;
 697   1          if(CurrentState == TRANSFER_STATE)
 698   1          {
 699   2              SdHiClk();
 700   2              //if(CardType == CARD_SD)
 701   2              //{
 702   2                  Status = SdChangeBusWidth(4);
 703   2                  if(Status != STATUS_SUCCESS)
 704   2                      return Status;
 705   2              //}
 706   2              /*else // MMC 3.1,1bit Mode
 707   2              {
 708   2                  SDMI_CTL&=~bmSD_BUS_4BIT_EN;
 709   2                  SDMI_CTL&=~bmSD_BUS_8BIT_EN;
 710   2              }*/
 711   2          }
 712   1          else
 713   1              return STATUS_FLASH_ERROR;
 714   1      
 715   1          return STATUS_SUCCESS;
 716   1      }
 717          
 718          //----------------------------------------------------------------------------
 719          //  Description:
 720          //    Stop the read or write command
 721          //----------------------------------------------------------------------------
 722          STATUS SdStopCmd()
 723          {
 724   1          STATUS Status;
 725   1          //BYTE RegValue;
 726   1          int i;
 727   1      
 728   1          SDMI_CTL&=~bmSD_AUTO_CLK_EN;
 729   1      
 730   1          for(i = 1; i <= 4; i++)
 731   1              SdCmdBuf[i] = 0;
 732   1          Status = SdSendCmd(STOP_TRANS, SdCmdBuf);
 733   1          if(Status != STATUS_SUCCESS)
 734   1              return Status;
 735   1      
 736   1          SdWaitCard();
C51 COMPILER V7.02b   SD                                                                   07/18/2006 15:32:57 PAGE 13  

 737   1      
 738   1          return STATUS_SUCCESS;
 739   1      }
 740          //----------------------------------------------------------------------------
 741          STATUS SdWaitCmd()
 742          {
 743   1          CmdTimer = SD_CMD_TIMEOUT;
 744   1          while((!(SDMI_INT&bmSD_COMPLETE_INT))&&CmdTimer); //等待命令执行完毕
 745   1          SDMI_INT&=~bmSD_COMPLETE_INT; //清除中断
 746   1      
 747   1          if (CmdTimer)
 748   1              return STATUS_SUCCESS;
 749   1          else
 750   1          {
 751   2              //DebugShow("<SD> Wait TimeOut! Cmd = %02bx\r\n", CmdBuf[0]);
 752   2              SdCtrlReset();
 753   2              return STATUS_FLASH_ERROR;
 754   2          }
 755   1      }
 756          //--------------------------------------------------------------------
 757          // Read first sector of data from SD/MMC
 758          //--------------------------------------------------------------------
 759          STATUS SdReadOneSector()
 760          {
 761   1          STATUS Status;
 762   1          int RetryCount1 , RetryCount2;
 763   1          bool ReadyForData;
 764   1      
 765   1      
 766   1          if (!SdCardExist())
 767   1              return STATUS_FLASH_ERROR;
 768   1      
 769   1          if(SectorStart > SdAvailableBlocks)
 770   1              return STATUS_PARAM_ERROR;
 771   1      
 772   1          SdWaitCard();
 773   1      
 774   1          for(RetryCount1 = 0; RetryCount1 < 5; RetryCount1++)
 775   1          {
 776   2              ReadyForData = false;
 777   2              for(RetryCount2 = 0; RetryCount2 < 20; RetryCount2++)
 778   2              {
 779   3                  *((UINT32 *)(&SdCmdBuf[1])) = RCA;
 780   3                  Status = SdSendCmd(SEND_STATUS, SdCmdBuf);
 781   3                  if(Status != STATUS_SUCCESS)
 782   3                      continue;
 783   3                  if(SdRespBuf[3] & 0x01) // READY_FOR_DATA in Card Status
 784   3                  {
 785   4                      ReadyForData = true;
 786   4                      break;
 787   4                  }
 788   3              }
 789   2              if(!ReadyForData)
 790   2                  continue;
 791   2      
 792   2              //CSRWrite(SD_BASE + SD_BUFFER_BASE,((TDataCast *)&CurrentBuffer)->ucByte[0]);
 793   2              SDMI_BUFBASE=0xF2; //FIFOB0
 794   2      
 795   2              *((UINT32 *)(&SdCmdBuf[1])) =
 796   2                  SectorStart << BYTES_PER_SECTOR_SHIFT;
 797   2      
 798   2      //***********************8
C51 COMPILER V7.02b   SD                                                                   07/18/2006 15:32:57 PAGE 14  

 799   2              if (SectorCount > 1) // Muti-Read.
 800   2              {
 801   3                  SDMI_CTL|=bmSD_AUTO_CLK_EN;
 802   3                  Status = SdSendCmd(READ_MUL_BLOCK, SdCmdBuf);
 803   3                  if(Status != STATUS_SUCCESS)
 804   3                      continue;
 805   3      #ifdef PREFETCH
                          {
                              UINT16 NextBuffer = GetNextBuffer(CurrentBuffer);
                              CSRWrite(SD_BASE + SD_BUFFER_BASE, ((TDataCast *)&NextBuffer)->ucByte[0]);
                          }
                          CSRWrite(SD_BASE + SD_COMMAND, SD_CMD_RX_DATA_ONLY);
                          CSRWrite(SD_BASE + SD_CLK_CTRL, CSRRead(SD_BASE + SD_CLK_CTRL) | SD_CLK_AUTO_DIS);
              #endif
 813   3              }
 814   2              else
 815   2              {
 816   3      
 817   3                  Status = SdSendCmd(READ_BLOCK, SdCmdBuf);
 818   3                  if(Status != STATUS_SUCCESS)
 819   3                      continue;
 820   3              }
 821   2      //*******************************
 822   2              if(SDMI_ST&bmSD_CRC16_ERR)
 823   2              {
 824   3                  SDMI_ST &=~bmSD_CRC16_ERR;
 825   3                  continue;
 826   3              }
 827   2              else
 828   2                  return STATUS_SUCCESS;
 829   2          }
 830   1          return STATUS_FLASH_ERROR;
 831   1      }
 832          
 833          STATUS SdReadSector()
 834          {
 835   1          STATUS Status = SdReadOneSector();
 836   1          SectorStart ++;
 837   1          return Status;
 838   1      }
 839          //----------------------------------------------------------------------------
 840          //  Description:
 841          //    Read one sector of data from the SD device
 842          //----------------------------------------------------------------------------
 843          STATUS SdReadNextSectorData()
 844          {
 845   1        STATUS Status;
 846   1        bit Stop;

⌨️ 快捷键说明

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