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

📄 sdmmc.c

📁 mp3代码 要用的干净下啊 希望用东西共享啊
💻 C
📖 第 1 页 / 共 2 页
字号:
   }
   else
   	   {
	   #if !(HW_SPI_Mode) 
	   SPDR=value;
	   while(!(SPSR & 0x80)){}; 
	   #else
	   Spi_SendReceive(&value,1,0);
	   #endif
	   }
   MMC_PORT|=(1<<SPI_Busy);
}

//****************************************************************************
//Routine for writing a Block(512Byte) to MMC/SD-Card
//Return 0 if sector writing is completed.
uint8 MMC_Write_Sector(uint32 addr,uint8 *Buffer)
//****************************************************************************
{  
   uint8 tmp,retry;
   uint16 i;
   //Command 24 is a writing blocks command for MMC/SD-Card.
   uint8 CMD[] = {0x58,0x00,0x00,0x00,0x00,0xFF}; 
   #if !(HW_SPI_Mode) 
   uint8 spimode,spispeeddub;
   spimode=SPCR; 
   spispeeddub=SPSR;
   SPCR&=~(1<<SPIE);
   #endif
//   CLI(); //clear all interrupt.
   addr = addr << 9; //addr = addr * 512
	
   CMD[1] = ((addr & 0xFF000000) >>24 );
   CMD[2] = ((addr & 0x00FF0000) >>16 );
   CMD[3] = ((addr & 0x0000FF00) >>8 );

   //Send Command CMD24 to MMC/SD-Card (Write 1 Block/512 Bytes)
   retry=0;
   do
   {  //Retry 100 times to send command.
      tmp=Write_Command_MMC(CMD);
      retry++;
      if(retry==100) 
      { 
	     #if !(HW_SPI_Mode) 
   		 SPCR=spimode; 
  		 SPSR=spispeeddub;
   		 #endif
        return(FALSE); //send commamd Error!
      }
   }
   while(tmp!=0); 
   
   //Before writing,send 100 clock to MMC/SD-Card
   for (i=0;i<100;i++)
   {
      Read_Byte_MMC();       //此处会产生死等待
   }
	
   //Send Start Byte to MMC/SD-Card
   Write_Byte_MMC(0xFE);	
	
   //Now send real data Bolck (512Bytes) to MMC/SD-Card
   for (i=0;i<512;i++)
   {
      Write_Byte_MMC(*Buffer++); //send 512 bytes to Card
   }

   //CRC-Byte 
   Write_Byte_MMC(0xFF); //Dummy CRC
   Write_Byte_MMC(0xFF); //CRC Code
   
    
   tmp=Read_Byte_MMC();   // read response
   if((tmp & 0x1F)!=0x05) // data block accepted ?
   {
     MMC_Disable();
	 #if !(HW_SPI_Mode) 
   	 SPCR=spimode; 
   	 SPSR=spispeeddub;
   	 #endif
     return(FALSE); //Error!
   }
   //Wait till MMC/SD-Card is not busy
   while (Read_Byte_MMC()!=0xff){};
	
   //set MMC_Chip_Select to high (MMC/SD-Card Invalid)
   MMC_Disable();
   #if !(HW_SPI_Mode) 
   SPCR=spimode; 
   SPSR=spispeeddub;
   #endif
   return(TRUE);
} 

//****************************************************************************
//Routine for reading data Registers of MMC/SD-Card
//Return 0 if no Error.
uint8 MMC_Read_Block(uint8 *CMD,uint8 *Buffer,uint16 Bytes)
//****************************************************************************
{  
   uint16 i; 
   uint8 retry,temp;
   #if !(HW_SPI_Mode) 
   uint8 spimode,spispeeddub;
   spimode=SPCR; 
   spispeeddub=SPSR;
   SPCR&=~(1<<SPIE);
   #endif
   //Send Command CMD to MMC/SD-Card
   retry=0;
   do
   {  //Retry 100 times to send command.
      temp=Write_Command_MMC(CMD);
      retry++;
      if(retry==100) 
      {
	      #if !(HW_SPI_Mode) 
   		  SPCR=spimode; 
   		  SPSR=spispeeddub;
   		  #endif
        return(READ_BLOCK_ERROR); //block read Error!
      }
   }
   while(temp!=0); 
   			
   //Read Start Byte form MMC/SD-Card (FEh/Start Byte)
   while (Read_Byte_MMC() != 0xfe){};
	
   //Write blocks(normal 512Bytes) to MMC/SD-Card
   for (i=0;i<Bytes;i++)
   {
      *Buffer++ = Read_Byte_MMC();
   }
   
   //CRC-Byte
   Read_Byte_MMC();//CRC - Byte 
   Read_Byte_MMC();//CRC - Byte
	
   //set MMC_Chip_Select to high (MMC/SD-Card invalid)
   MMC_Disable();
   #if !(HW_SPI_Mode) 
   SPCR=spimode; 
   SPSR=spispeeddub;
   #endif
   return(0);
}


//****************************************************************************
//Routine for reading Blocks(512Byte) from MMC/SD-Card
//Return 0 if no Error.
uint8 MMC_Read_Sector(uint32 addr,uint8 *Buffer)
//****************************************************************************
{	
   //Command 16 is reading Blocks from MMC/SD-Card
   uint8 CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF}; 
   uint8 temp;
   
   //#asm("cli"); //clear all interrupt.
   //Address conversation(logic block address-->byte address)  
   addr = addr << 9; //addr = addr * 512

   CMD[1] = ((addr & 0xFF000000) >>24 );
   CMD[2] = ((addr & 0x00FF0000) >>16 );
   CMD[3] = ((addr & 0x0000FF00) >>8 );

   if (MMC_Read_Block(CMD,Buffer,512)==READ_BLOCK_ERROR)
      {
	  return FALSE;
	  }
	else
	  {
	  return TRUE;
	  }
} 

//***************************************************************************
//Routine for reading CID Registers from MMC/SD-Card (16Bytes) 
//Return 0 if no Error.
uint8 Read_CID_MMC(uint8 *Buffer)
//***************************************************************************
{
   //Command for reading CID Registers
   uint8 CMD[] = {0x4A,0x00,0x00,0x00,0x00,0xFF}; 
   uint8 temp;
   temp=MMC_Read_Block(CMD,Buffer,16); //read 16 bytes
   return(temp);
}

//***************************************************************************
//Routine for reading CSD Registers from MMC/SD-Card (16Bytes)
//Return 0 if no Error.
uint8 Read_CSD_MMC(uint8 *Buffer)
//***************************************************************************
{	
   //Command for reading CSD Registers
   uint8 CMD[] = {0x49,0x00,0x00,0x00,0x00,0xFF};
   uint8 temp;
   temp=MMC_Read_Block(CMD,Buffer,16); //read 16 bytes

   return(temp);
}

//***************************************************************************
//Return: [0]-success or something error!
uint8 MMC_Start_Read_Sector(uint32 sector)
//***************************************************************************
{  
   uint8 retry;
   //Command 16 is reading Blocks from MMC/SD-Card
   uint8 CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF}; 
   uint8 temp;
   #if !(HW_SPI_Mode) 
   uint8 spimode,spispeeddub;
   spimode=SPCR; 
   spispeeddub=SPSR;
   SPCR&=~(1<<SPIE);
   #endif
//   CLI(); //clear all interrupt.
   //Address conversation(logic block address-->byte address)  
   sector = sector << 9; //sector = sector * 512

   CMD[1] = ((sector & 0xFF000000) >>24 );
   CMD[2] = ((sector & 0x00FF0000) >>16 );
   CMD[3] = ((sector & 0x0000FF00) >>8 );
   //Send Command CMD to MMC/SD-Card
   retry=0;
   do
   {  //Retry 100 times to send command.
      temp=Write_Command_MMC(CMD);
      retry++;
      if(retry==100) 
      {
	     #if !(HW_SPI_Mode) 
   		 SPCR=spimode; 
   		 SPSR=spispeeddub;
   		 #endif
        return(READ_BLOCK_ERROR); //block write Error!
      }
   }
   while(temp!=0); 
   			
   //Read Start Byte form MMC/SD-Card (FEh/Start Byte)
   //Now data is ready,you can read it out.
   while (Read_Byte_MMC() != 0xfe){};
   MMC_Disable();
   #if !(HW_SPI_Mode) 
   SPCR=spimode; 
   SPSR=spispeeddub;
   #endif
   return 0; //Open a sector successfully!
}

//***************************************************************************
void MMC_get_data(uint16 Bytes,uint8 *buffer) 
//***************************************************************************
{
   uint16 j;
   uint8 spimode,spispeeddub;
   spimode=SPCR; 
   spispeeddub=SPSR;
//   SPCR&=~(1<<SPIE);
//   CLI(); //clear all interrupt.
   for (j=0;((j<Bytes) && (readPos<512));j++)
   {	
      *buffer++ = Read_Byte_MMC();
      readPos++; //read a byte,increase read position
   }
   if (readPos==512)  
   {  //CRC-Bytes
      Read_Byte_MMC();//CRC - Byte 
      Read_Byte_MMC();//CRC - Byte
      readPos=0;      //reset sector read offset 
      sectorPos++;    //Need to read next sector
      LBA_Opened=0;   //Set to 1 when a sector is opened.
      //set MMC_Chip_Select to high (MMC/SD-Card invalid)
      MMC_Disable();  //MMC disable
	  SPCR=spimode; 
      SPSR=spispeeddub;
   }
}

//***************************************************************************
void MMC_get_data_LBA(uint32 lba, uint16 Bytes,uint8 *buffer)
//***************************************************************************
{ //get data from lba address of MMC/SD-Card
  //If a new sector has to be read then move head
  if (readPos==0) MMC_Start_Read_Sector(lba); 
  MMC_get_data(Bytes,buffer);
}

//***************************************************************************
void MMC_GotoSectorOffset(uint32 LBA,uint16 offset)
//***************************************************************************
{  
   //Find the offset in the sector
   uint8 temp[1];
   MMC_LBA_Close(); //close MMC when read a new sector(readPos=0)
   while (readPos<offset) MMC_get_data_LBA(LBA,1,temp); //go to offset  
}

//***************************************************************************
void MMC_LBA_Close(void)
//***************************************************************************
{  
   uint8 temp[1];
   while((readPos!=0x00)|(LBA_Opened==1))
   { //read MMC till readPos==0x00
     MMC_get_data(1, temp); //dummy read,temp is a valid data.
   }  
}


void delay_us(uint8 us)
{
uint8 number,i;
number=us*(F_CPU/1000000);
for(i=0;i<number;i++)
   {
   NOP();
   }

}




//---------------------------------------------------------------------------- 
#endif //USE_MMC

⌨️ 快捷键说明

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