?? mmc_sd.c
字號(hào):
/*************************************************************/
/* SD/MMC操作函數(shù)庫(kù) */
/* 環(huán)境WinAVR 20060421 */
/* 作者:Bozai(章其波) */
/* E-mail:sudazqb@163.com */
/* 2006年11月26日 */
/*************************************************************/
/* FAT diriver for MiniMP3 Player */
/* */
/* Platform : AVRStudio4.12 sp4 + WinAVR20060421 */
/* optimize -0s */
/* Author : bozai(Zhang Qibo) */
/* E-mail : sudazqb@163.com */
/* MSN : zhangqibo_1985@hotmail.com */
/* Date : 2006-12-26 */
/*******************************************************************/
#include <avr/io.h>
#include "MMC_SD.h"
//低速模式 //spi low speed
void SPI_Low(void)
{
SPCR = _BV(SPE)|_BV(MSTR)|_BV(SPR1)|_BV(SPR0);
SPSR &= ~_BV(SPI2X);
}
//高速模式 //spi full speed
void SPI_High(void)
{
SPCR = _BV(SPE)|_BV(MSTR);
SPSR |= _BV(SPI2X);
}
//端口初始化,模式初始化 //port initialize
void SPI_Init(void)
{
DDR_INI();
SPI_Low();
}
//寫(xiě)讀一個(gè)字節(jié) //read and write one byte
uint8 SPI_WriteByte(uint8 val)
{
SPDR = val;
while(!(SPSR & _BV(SPIF)));
return SPDR;
}
/*uint8 SPI_ReadByte(void)
{
SPDR = 0xff;
while(!(SPSR & _BV(SPIF)));
return SPDR;
}*/
//sd卡初始化 //sd card initialize
void MMC_SD_Init(void)
{
SPI_Init();
SPI_CS_Deassert();
}
//sd卡寫(xiě)命令 //sd send command
uint8 MMC_SD_SendCommand(uint8 cmd, uint32 arg)
{
uint8 r1;
uint8 retry=0;
SPI_WriteByte(0xff);
SPI_CS_Assert();
SPI_WriteByte(cmd | 0x40);//分別寫(xiě)入命令 //send command
SPI_WriteByte(arg>>24);
SPI_WriteByte(arg>>16);
SPI_WriteByte(arg>>8);
SPI_WriteByte(arg);
SPI_WriteByte(0x95);
while((r1 = SPI_WriteByte(0xff)) == 0xff)//等待響應(yīng), //wait response
if(retry++ > 20) break;//超時(shí)退出 //time out error
SPI_CS_Deassert();
return r1;//返回狀態(tài)值 //return state
}
//sd卡復(fù)位 //reset sd card (software)
uint8 MMC_SD_Reset(void)
{
uint8 i;
uint8 retry;
uint8 r1=0;
retry = 0;
SPI_Low();
do
{
for(i=0;i<10;i++) SPI_WriteByte(0xff);
r1 = MMC_SD_SendCommand(0, 0);//發(fā)idle命令 //send idle command
retry++;
if(retry>10) return 1;//超時(shí)退出 //time out
} while(r1 != 0x01);
retry = 0;
do
{
r1 = MMC_SD_SendCommand(1, 0);//發(fā)active命令 //send active command
retry++;
if(retry>100) return 1;//超時(shí)退出 //time out
} while(r1);
SPI_High();
r1 = MMC_SD_SendCommand(59, 0);//關(guān)crc //disable CRC
r1 = MMC_SD_SendCommand(16, 512);//設(shè)扇區(qū)大小512 //set sector size to 512
return 0;//正常返回 //normal return
}
//讀一個(gè)扇區(qū) //read one sector
uint8 MMC_SD_ReadSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
//uint8 retry=0;
if(r1 != 0x00)
return r1;
SPI_CS_Assert();
//等數(shù)據(jù)的開(kāi)始 //wait to start recieve data
while(SPI_WriteByte(0xff) != 0xfe);//if(retry++ > 50){SPI_CS_Deassert();return 1;}
for(i=0; i<512; i++)//讀512個(gè)數(shù)據(jù) //read 512 bytes
{
*buffer++ = SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);//偽crc
SPI_WriteByte(0xff);
SPI_CS_Deassert();
return 0;
}
//寫(xiě)一個(gè)扇區(qū) //wirite one sector //not used in this application
uint8 MMC_SD_WriteSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
//uint8 retry=0;
r1 = MMC_SD_SendCommand(24, sector<<9);//寫(xiě)命令 //send command
if(r1 != 0x00)
return r1;
SPI_CS_Assert();
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xfe);//發(fā)開(kāi)始符 //send start byte
for(i=0; i<512; i++)//送512字節(jié)數(shù)據(jù) //send 512 bytes data
{
SPI_WriteByte(*buffer++);
}
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
r1 = SPI_WriteByte(0xff);
if( (r1&0x1f) != 0x05)//等待是否成功 //judge if it successful
{
SPI_CS_Deassert();
return r1;
}
//等待操作完 //wait no busy
while(!SPI_WriteByte(0xff));//if(retry++ > 50){SPI_CS_Deassert();return 1;}
SPI_CS_Deassert();
return 0;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -