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

📄 sd.lst

📁 SdCard_V2.1TinyFatFs.rar是单片机实现SD卡功能的FAT文件系统
💻 LST
📖 第 1 页 / 共 5 页
字号:
1019   2                  return STATUS_SUCCESS;
1020   2          }
1021   1          return STATUS_FLASH_ERROR;
1022   1      
1023   1      }
1024          
1025          /*
1026          //----------------------------------------------------------------------------
1027          //  Description:
1028          //    Read one of the other sectors,except the first sector, 
1029          //        when execute multi-read command.
1030          //----------------------------------------------------------------------------
1031          #pragma OPTIMIZE(4)
1032          STATUS SdReadNextSectorData(void)
1033          {
1034            STATUS Status;
1035            bit Stop;
1036          
1037            Stop = SectorCount == 1;//
1038                  if (!SdCardExist())
1039                          return STATUS_FLASH_ERROR;//如果卡拔出,直接返回!!
1040           
1041          //      SDMI_BUFBASE = (FIFO_ADDRESS_IN>>8);
1042          
1043                  SDMI_CMD=SD_CMD_RX_DATA_ONLY;   
1044                  SDMI_CTL|=bmSD_AUTO_CLK_EN;     
1045                  if ((Status = SdWaitCmd()) != STATUS_SUCCESS)
1046                          return Status;           
C51 COMPILER V7.02b   SD                                                                   03/24/2008 15:52:29 PAGE 18  

1047          
1048                  if(SDMI_ST&bmSD_CRC16_ERR) // CRC7 Error
1049                  {
1050                          SDMI_ST&=~bmSD_CRC16_ERR; // Clear INT
1051                          return STATUS_DATA_ERROR;
1052                  }
1053          
1054                  if (Stop) 
1055                          return SdStopCmd();//所有block的数据已传输完毕,停止命令
1056          
1057                  return STATUS_SUCCESS;
1058          }       */
1059          
1060          /*
1061          
1062          STATUS SdReadNextSector(void)
1063          {
1064              BYTE        RetryCount;
1065                  STATUS Status;
1066                  Status = SdReadNextSectorData();
1067              if(Status != STATUS_SUCCESS)
1068                  {
1069                          for(RetryCount = 0 ; RetryCount < 15; RetryCount ++)
1070                          {
1071                                  Status = SdStopCmd();
1072                                  if(Status != STATUS_SUCCESS)
1073                                          continue;
1074                                  Status = SdReadOneSector();
1075                                          break;
1076                          }       
1077                  }
1078              SectorStart ++;
1079              return Status;
1080          }       */
1081          
1082          
1083          //--------------------------------------------------------------------
1084          // Write single sector of data to SD/MMC,
1085          // or write the first sector when execute multi-write command.
1086          //--------------------------------------------------------------------
1087          STATUS SdWriteOneSector(BYTE*buff, DWORD sector)
1088          {
1089   1          STATUS Status;
1090   1          BYTE CrcSts;
1091   1          int RetryCount1 , RetryCount2;
1092   1          bool ReadyForData;
1093   1              BYTE MMCerror1;//09.12
1094   1      
1095   1               DWORD       SectorStart,SectorCount;
1096   1               SectorStart= sector;
1097   1               SectorCount =1;
1098   1              if(SdCardExist()==0)
1099   1                      return STATUS_NO_MEDIA; //Derek 2007.03.14
1100   1          if(SDMI_ST & bmSD_WR_PROTECT)
1101   1              return STATUS_WRITE_PROTECT;
1102   1        
1103   1          SdWaitCard();  //保证上次操作完成!
1104   1      
1105   1          for(RetryCount1 = 0; RetryCount1 < 10; RetryCount1++)
1106   1          {
1107   2              ReadyForData = false;
1108   2              for(RetryCount2 = 0; RetryCount2 < 20; RetryCount2++)
C51 COMPILER V7.02b   SD                                                                   03/24/2008 15:52:29 PAGE 19  

1109   2              {
1110   3                  *((UINT32 *)(&SdCmdBuf[1])) = RCA;
1111   3                  Status = SdSendCmd(SEND_STATUS, SdCmdBuf);
1112   3                  if(Status != STATUS_SUCCESS)
1113   3                      {
1114   4                                      //MMCerror1++;
1115   4                      continue;}  
1116   3                  if(SdRespBuf[3] & 0x01) // READY_FOR_DATA in Card Status
1117   3                  {
1118   4                      ReadyForData = true;
1119   4                      break;
1120   4                  }
1121   3              }        
1122   2              if(!ReadyForData)
1123   2                  {
1124   3                              CmdTimer=2;
1125   3                              while(CmdTimer);
1126   3                  continue;}
1127   2      
1128   2               memcpy((BYTE xdata *)DMA_FIFOB0,buff, 512);    
1129   2                 SDMI_BUFBASE=(DMA_FIFOB0>>8);
1130   2      if(SdHcFlag) {     //Derek 2007.07.02  对于HC SD,以sector为单位。
1131   3                      *((UINT32 *)(&SdCmdBuf[1])) = SectorStart;
1132   3      } else                     //对于standard,以字节为单位。
1133   2              *((UINT32 *)(&SdCmdBuf[1])) = 
1134   2                  SectorStart << BYTES_PER_SECTOR_SHIFT;       //
1135   2      //***********************
1136   2          if (SectorCount > 1) // Muti-Write.
1137   2              {   
1138   3              Status = SdSendCmd(WRITE_MUL_BLOCK, SdCmdBuf);
1139   3              if(Status != STATUS_SUCCESS)
1140   3                  continue;
1141   3              }
1142   2           else
1143   2              {
1144   3      
1145   3              Status = SdSendCmd(WRITE_BLOCK, SdCmdBuf);
1146   3              if(Status != STATUS_SUCCESS)
1147   3                  continue;
1148   3      
1149   3             }
1150   2      //*******************************   
1151   2              
1152   2                      CrcSts = (SDMI_ST&0x38) >> 3;
1153   2                      if(CrcSts != GOOD_CRC16_STS) {
1154   3                              MMCerror1 ++;  
1155   3                              continue;
1156   3                              }
1157   2              else
1158   2                  return STATUS_SUCCESS;
1159   2          }
1160   1          
1161   1          
1162   1          return STATUS_FLASH_ERROR;
1163   1      }  
1164           /*
1165          STATUS SdWriteSector()
1166          {
1167              STATUS Status = SdWriteOneSector();
1168          
1169              SectorStart ++;
1170              return Status;
C51 COMPILER V7.02b   SD                                                                   03/24/2008 15:52:29 PAGE 20  

1171          }
1172          
1173          //----------------------------------------------------------------------------
1174          //  Description:
1175          //    Write one sector of data from the SD device 
1176          //----------------------------------------------------------------------------
1177          STATUS SdWriteNextSector()
1178          {
1179                  STATUS Status;
1180                  BYTE CrcSts;
1181                  bit Stop;
1182          
1183          
1184                  Stop = SectorCount == 1;
1185                  if(SdCardExist()==0)
1186                          return STATUS_NO_MEDIA; //Derek 2007.03.14
1187                  SdWaitCard();
1188                  CmdTimer = SD_CMD_TIMEOUT;
1189                  if (CmdTimer == 0) 
1190                  {
1191                          return STATUS_FLASH_ERROR;
1192                  }
1193          
1194          //      SDMI_BUFBASE=(FIFO_ADDRESS_OUT>>8); 
1195          
1196                  SDMI_CMD=SD_CMD_TX_DATA_ONLY;    
1197                  if ((Status = SdWaitCmd()) != STATUS_SUCCESS)
1198                          return Status;                           
1199                  CrcSts = (SDMI_ST&0x38) >> 3;
1200                  if (CrcSts != GOOD_CRC16_STS)
1201                  {
1202                          return STATUS_DATA_ERROR;
1203                  }
1204          
1205                  if (Stop)
1206                          return SdStopCmd();
1207          
1208                  return STATUS_SUCCESS;
1209          }  */
1210          #if 0
              //--------------用于SD1.10,2.0卡function 切换----------------------
              //主要是为了切换到high speed mode。
              STATUS SDCMD6Test()
              {
                      STATUS Status;
                      int i=0;
               
                      SDMI_BUFBASE=0xF2; //FIFOB1
                      SDMI_LENH = 0;
                      SDMI_LENL = 64;
                  *((UINT32 *)(&SdCmdBuf[1])) = 0x00FFFFF1; //check switchable function(mode0)
                      Status = SdSendCmd(CMD6, SdCmdBuf);               //CMD6-adtc command type
                      SDMI_LENH = 0X02;
                      SDMI_LENL = 0;
                      if(Status != STATUS_SUCCESS)
                              return Status;
              
                      SDMI_LENH = 0;
                      SDMI_LENL = 64;
                  *((UINT32 *)(&SdCmdBuf[1])) = 0x80FFFFF1; //switch card function(mode1)
                      Status = SdSendCmd(CMD6, SdCmdBuf);
                      SDMI_LENH = 0X02;
C51 COMPILER V7.02b   SD                                                                   03/24/2008 15:52:29 PAGE 21  

                      SDMI_LENL = 0;
                      if(Status != STATUS_SUCCESS)
                              return Status;
                      return Status;
              }
              #endif
1239          //--------------------------------------------------------------------
1240          // Initialization sequence of SD/MMC
1241          //--------------------------------------------------------------------
1242          STATUS SdReadFlashInfo(void)
1243          {
1244   1          STATUS Status;
1245   1          int RetryCount;
1246   1          
1247   1          for(RetryCount = 0; RetryCount < 5; RetryCount++)
1248   1          {
1249   2              Status = SdReset();
1250   2              if(Status != STATUS_SUCCESS)
1251   2                  continue;
1252   2      ////////////////SD 2.00 card detect.
1253   2                      Status = SdSendIFCon();    //SD2.0 only,
1254   2                      if(Status == STATUS_SUCCESS)
1255   2                              SdVer2Flag = 2;                 //SD 2.00 card
1256   2                      else
1257   2                              SdVer2Flag = 0;                 //non-Ver2.00 card.
1258   2      /////////////////////
1259   2              Status = SdDetect();
1260   2              if(Status != STATUS_SUCCESS)
1261   2                  continue;
1262   2      
1263   2              Status = SdGoIdentifyState();
1264   2              if(Status != STATUS_SUCCESS)
1265   2                  continue;
1266   2      
1267   2              Status = SdGoStandbyState();
1268   2              if(Status != STATUS_SUCCESS)
1269   2                  continue;
1270   2      
1271   2              Status = SdReadCSD();
1272   2        

⌨️ 快捷键说明

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