?? mmc._c
字號:
# include "iom16v.h"
# include "macros.h"
#include "mmc.h"
//低速模式 //spi low speed
void SPI_Low(void)
{
SPCR = BIT(SPE)|BIT(MSTR)|BIT(SPR1)|BIT(SPR0); //SCK:XTAL-CLOCK/128
SPSR &= ~BIT(SPI2X); //不用倍速
}
//高速模式 //spi full speed
void SPI_High(void)
{
SPCR = BIT(SPE)|BIT(MSTR); //SCK: XTAL-CLOCK/2,采用倍速
SPSR |= BIT(SPI2X);
}
//端口初始化,模式初始化 //port initialize
void SPI_Init(void)
{
DDR_INI();
SPI_High();
}
//寫[讀]一個字節 //read [and write] one byte
uint8 SPI_WriteByte(register uint8 val)
{
SPDR = val;
while(!(SPSR & BIT(SPIF))); //等待SPI發送完數據
return SPDR;
}
//檢測SD卡是否插入,已插入,返回1,否則,返回0
uint8 MMC_SD_Detect(void)
{
DDRA&=(~BIT(MMC_SD_INSDETC)); //PA0設置為輸入
//;;//讀取MMC_SD_INSDETC,為低電平,表示已經插入SD卡
;
if((PINA&=BIT(MMC_SD_INSDETC))==0)
return 1;
else
return 0;
}
//sd卡初始化 //sd card initialize
void MMC_SD_Init(void)
{
SPI_Init();
SPI_CS_Deassert();;
}
//sd卡寫命令 //sd send command
uint8 MMC_SD_SendCommand(uint8 cmd, uint32 arg)
{
uint8 r1;
register uint8 retry=0;
SPI_WriteByte(0xff);
SPI_CS_Assert();
SPI_WriteByte(cmd | 0x40);//分別寫入命令 //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)//等待響應, //wait response
if(retry++ > 20) break;//超時退出 //time out error
SPI_CS_Deassert();
return r1;//返回狀態值 //return state
}
//sd卡復位,正常時,返回0
uint8 MMC_SD_Reset(void)
{
uint8 i;
register uint8 retry=0;
uint8 r1=0;
SPI_Low();
do
{
for(i=0;i<10;i++) SPI_WriteByte(0xff);
r1 = MMC_SD_SendCommand(0, 0);//發idle命令 //send idle command
retry++;
if(retry>10) return 1;//超時退出 //time out
} while(r1 != 0x01);
retry = 0;
do
{
r1 = MMC_SD_SendCommand(1, 0);//發active命令 //send active command
retry++;
if(retry>100) return 1;//超時退出 //time out
} while(r1);
SPI_High();
r1 = MMC_SD_SendCommand(59, 0);//關crc //disable CRC
r1 = MMC_SD_SendCommand(16, 512);//設扇區大小512 //set sector size to 512
return 0;//正常返回 //normal return
}
//讀一個扇區,成功,返回0
uint8 MMC_SD_ReadSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
register uint16 i;
register uint16 retry=0;
r1 = MMC_SD_SendCommand(17, sector<<9);//讀命令 //read command
if(r1 != 0x00)
return r1;
SPI_CS_Assert();
//等數據的開始
while(SPI_WriteByte(0xff) != 0xfe) if(retry++ > 1000){SPI_CS_Deassert(); return 1;}
for(i=0; i<512; i++)//讀512個數據 //read 512 bytes
{
*buffer++ = SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);//偽crc
SPI_WriteByte(0xff);
SPI_CS_Deassert();
return 0;
}
/*
//寫一個扇區 //wirite one sector //not used in this application
uint8 MMC_SD_WriteSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
uint16 retry=0;
r1 = MMC_SD_SendCommand(24, sector<<9);//寫命令 //send command
if(r1 != 0x00)
return r1;
SPI_CS_Assert();
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xfe);//發開始符 //send start byte
for(i=0; i<512; i++)//送512字節數據 //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++ > 2000){SPI_CS_Deassert();return 1;}
SPI_CS_Deassert();
return 0;
}
uint32 MMC_SD_ReadCapacity(void)
{
uint8 r1;
uint16 i;
uint16 temp;
uint8 buffer[16];
uint32 Capacity;
//uint8 retry=0;
r1 = MMC_SD_SendCommand(9, 0);//寫命令 //send command //READ CSD
if(r1 != 0x00)
return r1;
SPI_CS_Assert();
while(SPI_WriteByte(0xff) != 0xfe);
for(i=0;i<16;i++)
{
buffer[i]=SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_CS_Deassert();
//////////////////////////////////////
// C_SIZE
i = buffer[6]&0x03;
i<<=8;
i += buffer[7];
i<<=2;
i += ((buffer[8]&0xc0)>>6);
///////////////////////////////////////
// C_SIZE_MULT
r1 = buffer[9]&0x03;
r1<<=1;
r1 += ((buffer[10]&0x80)>>7);
///////////////////////////////////////
// BLOCKNR
r1+=2;
temp = 1;
while(r1)
{
temp*=2;
r1--;
}
Capacity = ((uint32)(i+1))*((uint32)temp);
/////////////////////////
// READ_BL_LEN
i = buffer[5]&0x0f;
/////////////////////////
//BLOCK_LEN
temp = 1;
while(i)
{
temp*=2;
i--;
}
/////////////////////////
////////////////////////////////////////////////////////////
//
// memory capacity = BLOCKNR * BLOCK_LEN
//
// BLOCKNR = (C_SIZE + 1)* MULT
//
// C_SIZE_MULT+2
// MULT = 2
//
// READ_BL_LEN
// BLOCK_LEN = 2
////////////////////////////////////////////////////////////
//The final result
Capacity *= (uint32)temp;
return Capacity;
}
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -