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

📄 f34x_msd_mmc.c

📁 C8051F340读写SD卡的程序
💻 C
📖 第 1 页 / 共 3 页
字号:
                                       // Issue command opcode;
  SPI0DAT = (current_command.command_byte | 0x40);
#endif
#endif
  long_arg.l = argument;              // Make argument byte addressable;
                                      // If current command changes block
                                      // length, update block length variable
                                      // to keep track;
                                      // Command byte = 16 means that a set
                                      // block length command is taking place
                                      // and block length variable must be
                                      // set;
  if(current_command.command_byte == 16) {
    current_blklen = argument;       
  }                                
                                       // Command byte = 9 or 10 means that a
                                       // 16-byte register value is being read
                                       // from the card, block length must be
                                       // set to 16 bytes, and restored at the
                                       // end of the transfer;
  if((current_command.command_byte == 9)||
     (current_command.command_byte == 10)) {
    old_blklen = current_blklen;     // Command is a GET_CSD or GET_CID,
    current_blklen = 16;             // set block length to 16-bytes;
  }

#ifndef __F326_VER__
#ifdef SEND__IN_FUNCTION
#else
  while(!SPIF){}                      // Wait for initial SPI transfer to end;
  SPIF = 0;                           // Clear SPI Interrupt flag;
#endif
#endif

                                       // If an argument is required, transmit
                                       // one, otherwise transmit 4 bytes of
                                       // 0x00;
  plast = &pchar[current_blklen];
  if(current_command.arg_required == YES) {
    counter = 0;
    while(counter <= 3) {
#ifdef __F326_VER__	
		  Write_Read_Spi_Byte(long_arg.b[counter]);
		  counter++;
#else
#ifdef SEND__IN_FUNCTION
	    Write_Read_Spi_Byte(long_arg.b[counter]);
	    counter++;
#else
      SPI0DAT = long_arg.b[counter];
      counter++;
      while(!SPIF){}
      SPIF = 0;
#endif
#endif
    }
  } else {
    counter = 0;
    while(counter <= 3) {
#ifdef __F326_VER__	
      Write_Read_Spi_Byte(0x00);
		  counter++;
#else
#ifdef SEND__IN_FUNCTION
      Write_Read_Spi_Byte(0x00);
      counter++;
#else
      SPI0DAT = 0x00;
      counter++;
      while(!SPIF){}
      SPIF = 0;
#endif
#endif
    }
  }
#ifdef __F326_VER__	
  Write_Read_Spi_Byte(current_command.CRC);
#else
#ifdef SEND__IN_FUNCTION
  Write_Read_Spi_Byte(current_command.CRC);
#else
  SPI0DAT = current_command.CRC;      // Transmit CRC byte;  In all cases
  while(!SPIF){}                      // except CMD0, this will be a dummy
  SPIF = 0;                           // character;
#endif
#endif
                                       // The command table entry will indicate
                                       // what type of response to expect for
                                       // a given command;  The following 
                                       // conditional handles the MMC response;
  if(current_command.response == R1) { // Read the R1 response from the card;
    loopguard=0;
    do {
#ifdef __F326_VER__
      card_response.b[0] = Write_Read_Spi_Byte(0xFF);
#else

#ifdef SEND__IN_FUNCTION
		  card_response.b[0] = Write_Read_Spi_Byte(0xFF);
#else
      SPI0DAT = 0xFF;               // Write dummy value to SPI so that 
      while(!SPIF){}                // the response byte will be shifted in;
      SPIF = 0;
      card_response.b[0] = SPI0DAT; // Save the response;
#endif
#endif

      if(!++loopguard) break;
      if(card_response.b[0] & BUSY_BIT) { 
		 //	printf("R1 response 0x%02bX\r\n",card_response.b[0]);
        Wait_ns(700);
		  }
    } while((card_response.b[0] & BUSY_BIT));
	  if(!loopguard) { 
    /*printf("R1 response 0x%02bX\r\n",card_response.b[0]);*/
      return card_response.b[0];}//{ return 0; }
    }                                     // Read the R1b response;
    else if(current_command.response == R1b) {
      loopguard = 0;	
      do {
#ifdef __F326_VER__
        card_response.b[0] = Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
        card_response.b[0] = Write_Read_Spi_Byte(0xFF);
#else
        SPI0DAT = 0xFF;               // Start SPI transfer;
        while(!SPIF){}
        SPIF = 0;
        card_response.b[0] = SPI0DAT; // Save card response
#endif
#endif
        if(!++loopguard) break;
      }
      while((card_response.b[0] & BUSY_BIT));
#ifdef __F326_VER__
      while(Write_Read_Spi_Byte(0xff) == 0x00);
#else
#ifdef SEND__IN_FUNCTION
	    while(Write_Read_Spi_Byte(0xff) == 0x00);
#else
      loopguard = 0;
      do {                              // Wait for busy signal to end;
        SPI0DAT = 0xFF;               
        while(!SPIF){}
        SPIF = 0;
  		  if(!++loopguard) break;
      } while(SPI0DAT == 0x00);          // When byte from card is non-zero,
	    if(!loopguard) {BACK_FROM_ERROR;}
#endif
#endif
    }                                   // card is no longer busy;
                                       // Read R2 response
    else if(current_command.response == R2) {
      loopguard=0;
      do {
#ifdef __F326_VER__
		    card_response.b[0] = Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
        card_response.b[0] = Write_Read_Spi_Byte(0xFF);
#else
        SPI0DAT = 0xFF;               // Start SPI transfer;
        while(!SPIF){}
        SPIF = 0;
        card_response.b[0] = SPI0DAT; // Read first byte of response;
#endif
#endif
        if(!++loopguard) break;
      } while((card_response.b[0] & BUSY_BIT));

	    if(!loopguard) { BACK_FROM_ERROR; }
#ifdef __F326_VER__
	    card_response.b[1] = Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
      card_response.b[1] = Write_Read_Spi_Byte(0xFF);
#else
      SPI0DAT = 0xFF;
      while(!SPIF){}
      SPIF = 0;
      card_response.b[1] = SPI0DAT;    // Read second byte of response;
#endif
#endif
    } else {                               // Read R3 response;
      loopguard=0;
      do {
#ifdef __F326_VER__
        card_response.b[0] = Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
        card_response.b[0] = Write_Read_Spi_Byte(0xFF);
#else
        SPI0DAT = 0xFF;               // Start SPI transfer;
        while(!SPIF){}
        SPIF = 0;
        card_response.b[0] = SPI0DAT; // Read first byte of response;
#endif
#endif
		    if(!++loopguard) break;
      } while((card_response.b[0] & BUSY_BIT));
  
  	  if(!loopguard) { BACK_FROM_ERROR; }
      counter = 0;
      while(counter <= 3)              // Read next three bytes and store them
      {                                // in local memory;  These bytes make up
        counter++;                    // the Operating Conditions Register
#ifdef __F326_VER__
	      *pchar++ = Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
        *pchar++ = Write_Read_Spi_Byte(0xFF);
#else
        SPI0DAT = 0xFF;               // (OCR);
        while(!SPIF){}
        SPIF = 0;
        *pchar++ = SPI0DAT;
#endif
#endif
      }
    }
    switch(current_command.trans_type)  // This conditional handles all data 
    {                                   // operations;  The command entry
                                       // determines what type, if any, data
                                       // operations need to occur;
      case RD:                         // Read data from the MMC;
  		  loopguard = 0;
#ifdef __F326_VER__
	      while(Write_Read_Spi_Byte(0xFF)!=START_SBR) {
    	  	if(!++loopguard) {BACK_FROM_ERROR;}
	      }	
#else
#ifdef SEND__IN_FUNCTION
	      while((Write_Read_Spi_Byte(0xFF))!=START_SBR) {
          Wait_ns(700);
	  	    if(!++loopguard) { 
          /*printf("RD Start response not set ");*/ 
            BACK_FROM_ERROR;}
	        }	
#else
          do                            // Wait for a start read Token from
          {                             // the MMC;
									   // Start a SPI transfer;
			      SPI0DAT = 0xFF;            
            while(!SPIF){}
            SPIF = 0;
			      if(!++loopguard) break;
          } while(SPI0DAT != START_SBR);  // Check for a start read Token;
		      if(!loopguard) { BACK_FROM_ERROR; }
#endif
#endif
		
          counter = 0;                  // Reset byte counter;
                                       // Read <current_blklen> bytes;
	//	 START_SPI_TIMEOUT;
		      READ_BYTES(pchar,current_blklen);
	//	 STOP_SPI_TIME_OUT;
		
#ifdef __F326_VER__
		      dummy_CRC = Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
		      dummy_CRC = Write_Read_Spi_Byte(0xFF);
#else
          SPI0DAT = 0xFF;               // After all data is read, read the two
          while(!SPIF){}                // CRC bytes;  These bytes are not used
          SPIF = 0;                     // in this mode, but the placeholders 
          dummy_CRC = SPI0DAT;          // must be read anyway;
          SPI0DAT = 0xFF;
          while(!SPIF){}
          SPIF = 0;
          dummy_CRC = SPI0DAT;
#endif
#endif
          break;
        case WR: 
	  
#ifdef __F326_VER__
          Write_Read_Spi_Byte(0xFF);
          Write_Read_Spi_Byte(START_SBW);
#else

#ifdef SEND__IN_FUNCTION
          Write_Read_Spi_Byte(0xFF);
          Write_Read_Spi_Byte(START_SBW);
#else										// Write data to the MMC;
          SPI0DAT = 0xFF;               // Start by sending 8 SPI clocks so
          while(!SPIF){}                // the MMC can prepare for the write;
          SPIF = 0;
          SPI0DAT = START_SBW;          // Send the start write block Token;
          while(!SPIF){}
          SPIF = 0;
         					            // Reset byte counter;
                                       // Write <current_blklen> bytes to MMC;
#endif
          Wait_ns(700);
#endif

		 //START_SPI_TIMEOUT;
        WRITE_BYTES(pchar,current_blklen);
 	    // STOP_SPI_TIME_OUT;
	
#ifdef __F326_VER__
        Write_Read_Spi_Byte(0xFF);
        Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
        Write_Read_Spi_Byte(0xFF);
        Write_Read_Spi_Byte(0xFF);
#else
        SPI0DAT = 0xFF;               // Write CRC bytes (don't cares);
        while(!SPIF){}
        SPIF = 0;
        SPI0DAT = 0xFF;
        while(!SPIF){}
        SPIF = 0;
#endif
#endif
        loopguard = 0;
        do                            // Read Data Response from card;
        {  
#ifdef __F326_VER__
          data_resp = Write_Read_Spi_Byte(0xFF);
          if(!++loopguard) break;
#else
#ifdef SEND__IN_FUNCTION
          data_resp = Write_Read_Spi_Byte(0xFF);
          if(!++loopguard) break;
#else
          SPI0DAT = 0xFF;
          while(!SPIF){}
          SPIF = 0;
          data_resp = SPI0DAT;
          if(!++loopguard) break;
#endif
#endif
        }                             // When bit 0 of the MMC response
                                       // is clear, a valid data response
                                       // has been received;
        while((data_resp & DATA_RESP_MASK) != 0x01);
        if(!loopguard) { BACK_FROM_ERROR; }

#ifdef __F326_VER__
        while(Write_Read_Spi_Byte(0xFF)==0x00);
        Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
        while(Write_Read_Spi_Byte(0xFF)==0x00);
        Write_Read_Spi_Byte(0xFF);
#else
        do                            // Wait for end of busy signal;
        {
          SPI0DAT = 0xFF;            // Start SPI transfer to receive
          while(!SPIF){}             // busy tokens;
          SPIF = 0;
        } while(SPI0DAT == 0x00);       // When a non-zero Token is returned,
                                       // card is no longer busy;

        SPI0DAT = 0xFF;               // Issue 8 SPI clocks so that all card
        while(!SPIF){}                // operations can complete;
        SPIF = 0;
#endif
#endif

⌨️ 快捷键说明

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