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

📄 sd.c

📁 用at91sam7s64+ucos写的mp3播放程序
💻 C
字号:
//sd.c
#include "AT91SAM7S64.h"
#include "Board.h"
#include "type.h"
#include "spi.h"
#include "sd.h"
#include "port.h"

//------------------------------------------------------------------------
void delay(INT16U time)
{
    INT16U i;
       for(i=0; i<time; i++);
}


//-------------------------------------------------------------------------
INT8U sd_WriteCommand(INT8U* pCommand)
{
   INT8U i;
   INT8U temp;
   INT8U u8Retry = 0;
   
   MMC_CS_HIGH();
   spi_vSendByte(0xff);
   MMC_CS_LOW();
   
   for(i=0; i<6; i++)
      spi_vSendByte(*pCommand ++);
     
   spi_vReadByte();
   
   do
   {
      temp = spi_vReadByte();
      u8Retry ++;
   }
   while( (temp == 0xff) && (u8Retry < 100) );
   
 //  MMC_CS_HIGH();
   
   return(temp); 
}


//------------------------------------------------------------------------
INT8U sd_ReadBlock(INT8U* pCommand, INT8U* pBuffer, INT16U u16Bytes)
{
    INT16U i;
    INT8U u8Retry,temp;
   
    u8Retry = 0;
    do
    {
       temp = sd_WriteCommand(pCommand);
       u8Retry ++;
       if(u8Retry == 100)
       {
          return(0xff);
       }
    }
    while(temp != 0);
    
    while (spi_vReadByte() != 0xfe);
   
   //Write blocks(normal 512Bytes) to MMC/SD-Card
    for (i=0; i<u16Bytes; i++)
    {
       *pBuffer++ = spi_vReadByte();
    }
   
   //CRC-Byte
    spi_vReadByte();//CRC - Byte 
    spi_vReadByte();//CRC - Byte
	
   //set MMC_Chip_Select to high (MMC/SD-Card invalid)
    MMC_CS_HIGH();
    return(0);
}


//------------------------------------------------------------------------
INT8U sd_ReadSector(INT32U addr, INT8U *Buffer)
{	
   //Command 16 is reading Blocks from MMC/SD-Card
   INT8U CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF}; 
   INT8U temp;
   
   //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 );

   temp = sd_ReadBlock(CMD, Buffer, 512);
   
   return(temp);
}



//------------------------------------------------------------------------
INT8U sd_Init()
{
    INT8U CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
    INT8U i;
    INT8U u8Retry;
    INT8U temp;
    
    
    VS1003_CS_HIGH();
    VS1003_DS_HIGH();
    
    delay(0xffff);

    for(i=0; i<0x0f; i++)    //at lease 74 clock
    {
        spi_vSendByte(0xff);
    } 
    
    u8Retry = 0;
    do
    {
       temp = sd_WriteCommand(CMD);
       u8Retry ++;
       if(u8Retry == 200)
       {
         //  *AT91C_PIOA_CODR = LED1;
           return (0);
       }
    }
    while(temp != 1);
    
    CMD[0] = 0x41;
    CMD[5] = 0xff;
    
    u8Retry = 0;
    do
    {
       temp = sd_WriteCommand(CMD);
       u8Retry ++;
       if(u8Retry == 100)
       {
         //  *AT91C_PIOA_CODR = LED1;
           return (0);
       }
    }
    while(temp != 0); 
 
    return (1);    
}


⌨️ 快捷键说明

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