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

📄 f34x_msd_mmc.c

📁 C8051F340读写SD卡的程序
💻 C
📖 第 1 页 / 共 3 页
字号:
        break;
      default: break;
    }
#ifdef __F326_VER__
    Write_Read_Spi_Byte(0xFF);
    SCS = 1;
    Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
    Write_Read_Spi_Byte(0xFF);
    NSSMD0 = 1;
    Write_Read_Spi_Byte(0xFF);
#else
    SPI0DAT = 0xFF;
    while(!SPIF){}
    SPIF = 0;

    NSSMD0 = 1;                         // Deselect memory card;
    SPI0DAT = 0xFF;                     // Send 8 more SPI clocks to ensure
    while(!SPIF){}                      // the card has finished all necessary
    SPIF = 0;                           // operations;
                                       // Restore old block length if needed;
#endif
#endif
    if((current_command.command_byte == 9)||
      (current_command.command_byte == 10)) {
      current_blklen = old_blklen;
    }
    return card_response.i;
}


//-----------------------------------------------------------------------------
// MMC_FLASH_Init
//-----------------------------------------------------------------------------
//
// This function initializes the flash card, configures it to operate in SPI
// mode, and reads the operating conditions register to ensure that the device
// has initialized correctly.  It also determines the size of the card by 
// reading the Card Specific Data Register (CSD).

void MMC_FLASH_Init(void)
{
  xdata unsigned loopguard;
  xdata int i;
  xdata UINT card_status;             // Stores card status returned from 
                                       // MMC function calls(MMC_Command_Exec);
  xdata unsigned char counter = 0;    // SPI byte counter;
  unsigned char xdata *pchar;         // Xdata pointer for storing MMC 
                                       // register values;
                                       // Transmit at least 64 SPI clocks
                                       // before any bus comm occurs.

  unsigned int c_size,bl_len;
  unsigned char c_mult;
//	PHYSICAL_SIZE=0;
//	PHYSICAL_BLOCKS=0;

  SPI_Init();
  Wait_ms(100);
  pchar = (unsigned char xdata*)LOCAL_BLOCK;
  for(counter = 0; counter < 8; counter++) {
#ifdef __F326_VER__
    Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
    Write_Read_Spi_Byte(0xFF);
#else
    SPI0DAT = 0xFF;
    while(!SPIF){}
    SPIF = 0;
#endif
#endif
  }
  for(counter = 0; counter < 2; counter++) {
#ifdef __F326_VER__
    Write_Read_Spi_Byte(0xFF);
#else
#ifdef SEND__IN_FUNCTION
    Write_Read_Spi_Byte(0xFF);
#else
    SPI0DAT = 0xFF;
    while(!SPIF){}
    SPIF = 0;
#endif
#endif
  }
  
#ifdef __F326_VER__
  SCS = 0;
#else
  NSSMD0 = 0;                         // Select the MMC with the CS pin;
                                       // Send 16 more SPI clocks to 
                                       // ensure proper startup;
#endif
                                       // Send the GO_IDLE_STATE command with
                                       // CS driven low;  This causes the MMC
                                       // to enter SPI mode; 	
  Wait_ms(1);
  card_status.i = MMC_Command_Exec(GO_IDLE_STATE,EMPTY,EMPTY);

  loopguard=0;

                                       // Send the SEND_OP_COND command
  do                                  // until the MMC indicates that it is
  {         

    Wait_ms(1);
    card_status.i = MMC_Command_Exec(SEND_OP_COND,EMPTY,EMPTY);
    if(!++loopguard) break;
  } while ((card_status.b[0] & 0x01));
  printf("count SEND_OP_COND: %d\n",loopguard);

  if(!loopguard) return;

#ifdef __F326_VER__
  Write_Read_Spi_Byte(0xFF);
#else

#ifdef SEND__IN_FUNCTION
  Write_Read_Spi_Byte(0xFF);
#else
  SPI0DAT = 0xFF;                     // Send 8 more SPI clocks to complete
  while(!SPIF){}                      // the initialization sequence;
  SPIF = 0;
#endif
#endif
  loopguard=0;

  do                                  // Read the Operating Conditions 
  {                                   // Register (OCR);
    card_status.i = MMC_Command_Exec(READ_OCR,EMPTY,pchar);
    if(!++loopguard) break;
  } while(!(*pchar&0x80));              // Check the card busy bit of the OCR;
  if(!loopguard) return;

  card_status.i = MMC_Command_Exec(SEND_STATUS,EMPTY,EMPTY);
                                       // Get the Card Specific Data (CSD)
                                       // register to determine the size of the
                                       // MMC;
  for(i=0;i<4;i++) {
    printf("0x%02bX ",pchar[i]);
  }

  card_status.i = MMC_Command_Exec(SEND_CSD,EMPTY,pchar);
	
  if(card_status.i==0) {
    printf("Change speed");
    for(i=0;i<16;i++) {
      printf("0x%02bX ",pchar[i]);
    }
	#ifdef F340_M24
    SPI0CKR = 0;
	#else
    SPI0CKR = 1;
	#endif
    Wait_ms(1);
  } else {
    printf("CARD STATUS 0x%02X:\n",card_status.i);
  	for(i=0;i<16;i++) {
      printf("0x%02bX ",pchar[i]);
    }
    PHYSICAL_BLOCKS = 0;
    PHYSICAL_SIZE = PHYSICAL_BLOCKS * bl_len;
    return;
  }

  card_status.i = MMC_Command_Exec(SET_BLOCKLEN,(unsigned long)PHYSICAL_BLOCK_SIZE,EMPTY);

  bl_len = 1 << (pchar[5] & 0x0f) ;
  c_size = ((pchar[6] & 0x03) << 10) | 
   			(pchar[7] << 2) | ((pchar[8] &0xc0) >> 6);
  c_mult = (((pchar[9] & 0x03) << 1) | ((pchar[10] & 0x80) >> 7));
  
                                       // Determine the number of MMC sectors;
  PHYSICAL_BLOCKS = (unsigned long)(c_size+1)*(1 << (c_mult+2));
  PHYSICAL_SIZE = PHYSICAL_BLOCKS * bl_len;

  loopguard = 0;

  while((MMC_FLASH_Block_Read(0,Scratch)!=0)) {
    if(!++loopguard) break;
  } 
  //printf("Wrong reads %d\n",loopguard);

  Is_Initialized = 1;
	
  Led1=0;Led2=0;
}

//-----------------------------------------------------------------------------
// MMC_FLASH_Block_Read
//-----------------------------------------------------------------------------
//
// If you know beforehand that you'll read an entire 512-byte block, then
// this function has a smaller ROM footprint than MMC_FLASH_Read.
//
// Parameters   : address - address of block
//				  pchar - pointer to byte 
// Return Value : card status
//----------------------------------------------------------------------------

unsigned int MMC_FLASH_Block_Read(unsigned long address, unsigned char *pchar) 
{
  xdata unsigned int card_status;     // Stores MMC status after each MMC command;
  address *= PHYSICAL_BLOCK_SIZE;
  Led1=1;
/*	card_status = MMC_Command_Exec(SET_BLOCKLEN,(unsigned long)PHYSICAL_BLOCK_SIZE,EMPTY);
  if(card_status!=0) {
    printf("Read Address 0x%02LX\r\n",address);
    printf("Reading ERROR during set blocklen 0x%02BX\n",card_status);
    return card_status;
  }*/
  card_status = MMC_Command_Exec(READ_SINGLE_BLOCK,address,pchar);
	/*if(card_status!=0) {
    printf("Read Address 0x%02LX\r\n",address);
    printf("Reading ERROR during reading data 0x%02BX\n",card_status);
	}*/
	Led1=0;
	return card_status;
}

//-----------------------------------------------------------------------------
// MMC_FLASH_Block_Write
//-----------------------------------------------------------------------------
//
// If you know beforehand that you'll write an entire 512-byte block, then
// this function is more RAM-efficient than MMC_FLASH_Write because it 
// doesn't require a 512-byte Scratch buffer (and it's faster too, it doesn't
// require a read operation first). And it has a smaller ROM footprint too.
//
// Parameters   : address - address of block
//				  wdata - pointer to data 
// Return Value : card status
//----------------------------------------------------------------------------

unsigned char MMC_FLASH_Block_Write(unsigned long address,unsigned char *wdata) 
{
  xdata unsigned int card_status;     // Stores status returned from MMC;

  address *= PHYSICAL_BLOCK_SIZE;
  Led2=1;
  card_status = MMC_Command_Exec(WRITE_BLOCK,address ,wdata);
  Led2=0;
  return card_status;
}


#ifdef __F326_VER__

//----------------------------------------------------------------------------
// Write_Read_Spi_Byte
//----------------------------------------------------------------------------
//
// Function sends one byte to spi and reads ony byte from spi
// it will be written with SYSCLK as clk
//
// Parameters   : byte - value to write
// Return Value : SPI byte
//----------------------------------------------------------------------------


unsigned char Write_Read_Spi_Byte(unsigned char byte)
{
  unsigned char i,ret = 0;
  for(i=0;i<8;i++) {
    MOSI = (byte & 0x80) ? 1 : 0;
    SCLK = 0;	
    byte <<= 1;
    ret <<= 1;
    SCLK = 1;
    ret |= MISO;
  }
  return ret;
}

#endif


#ifdef SEND__IN_FUNCTION

//----------------------------------------------------------------------------
// Write_Read_Spi_Byte
//----------------------------------------------------------------------------
//
// Function sends one byte to spi and reads ony byte from spi
// it will be written with SYSCLK as clk
//
// Parameters   : byte - value to write
// Return Value : SPI byte
//----------------------------------------------------------------------------

unsigned char Write_Read_Spi_Byte(unsigned char byte)
{
  unsigned char xdata ret;
  SPI0DAT = byte;
  while(!SPIF);                      
  SPIF = 0;
  ret = SPI0DAT;
  return ret;
}

#endif

//----------------------------------------------------------------------------
// Wait_ms
//----------------------------------------------------------------------------
//
// Delay function with declared wait time in milliseconds
//
// Parameters   : count - time in ms
// Return Value :
//----------------------------------------------------------------------------

void Wait_ms(unsigned int count)
{
  int xdata i,j;
  for(i=0;i<count;i++) {
    for(j=0;j<1000;j++) {
      Wait_ns(1000);
    }
  }
}

//----------------------------------------------------------------------------
// Wait_ns
//----------------------------------------------------------------------------
//
// Delay function with declared wait time in nanoseconds
//
// Parameters   : count - time in ns
// Return Value :
//----------------------------------------------------------------------------

void Wait_ns(unsigned int count)
{

#ifdef F340_M24
  count/=40;
#else
  count/=20;
#endif

  while(count--) ;
}

//----------------------------------------------------------------------------
// Get_Status_MMC
//----------------------------------------------------------------------------
//
// Function returns the status of MMC card
//
// Parameters   : 
// Return Value :
//----------------------------------------------------------------------------

#ifdef __F340_VER__
void Get_Status_MMC()
{
  int xdata status= MMC_Command_Exec(SEND_STATUS,EMPTY,EMPTY);	
  printf("MMC Card Status 0x%02X",status);
}

#endif

⌨️ 快捷键说明

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