大家读写SD卡怎么不用SD模式方式读取,是SPI方式读取简单?
下面贴一段代码,是在FPGA上程序,只能用SD模式进行读,没有文件系统,没有用AVR硬件实现,
电路图:
image001.jpg (23.4 KB, 下载次数: 113)
-4-20 17:54 上传
电路说明,SD_DAT3一直给高电平
程序:
#ifndef __SD_Card_H__
#define __SD_Card_H__
#define High 1
#define Low 0
//-------------------------------------------------------------------------
//SD Card Set I/O Direction
#define SD_CMD_IN DDRX.1 = Low
#define SD_CMD_OUTDDRX.1 = High
#define SD_DAT_IN DDRX.2 = Low
#define SD_DAT_OUTDDRX.2 = High
#define SD_CLK_OUTDDRX.3 = High
#define SD_DAT3_OUT DDRX.0 = High
//SD Card Output High/Low
#define SD_CMD_LOWPORTX.1 = Low
#define SD_CMD_HIGH PORTX.1 = High
#define SD_DAT_LOWPORTX.2 = Low
#define SD_DAT_HIGH PORTX.2 = High
#define SD_CLK_LOWPORTX.3 = Low
#define SD_CLK_HIGH PORTX.3 = High
#define SD_DAT3_HIGH PORTX.0 = High
//SD Card Input Read
#define SD_TEST_CMD PINX.1
#define SD_TEST_DAT PINX.2
//-------------------------------------------------------------------------
#define BYTE unsigned char
#define UINT16unsigned int
#define UINT32unsigned long
//-------------------------------------------------------------------------
void Ncr(void);
void Ncc(void);
BYTE response_R(BYTE);
BYTE send_cmd(BYTE *);
BYTE SD_read_lba(BYTE *,UINT32,UINT32);
BYTE SD_card_init(void);
//-------------------------------------------------------------------------
BYTE read_status;
BYTE response_buffer[20];
BYTE RCA[2];
BYTE cmd_buffer[5];
const BYTE cmd0[5] = {0x40,0x00,0x00,0x00,0x00};
const BYTE cmd55[5]= {0x77,0x00,0x00,0x00,0x00};
const BYTE cmd2[5] = {0x42,0x00,0x00,0x00,0x00};
const BYTE cmd3[5] = {0x43,0x00,0x00,0x00,0x00};
const BYTE cmd7[5] = {0x47,0x00,0x00,0x00,0x00};
const BYTE cmd9[5] = {0x49,0x00,0x00,0x00,0x00};
const BYTE cmd16[5]= {0x50,0x00,0x00,0x02,0x00};
const BYTE cmd17[5]= {0x51,0x00,0x00,0x00,0x00};
const BYTE acmd6[5]= {0x46,0x00,0x00,0x00,0x02};
const BYTE acmd41[5] = {0x69,0x0f,0xf0,0x00,0x00};
const BYTE acmd51[5] = {0x73,0x00,0x00,0x00,0x00};
//-------------------------------------------------------------------------
void Ncr(void)
{
SD_CMD_IN;
SD_CLK_LOW;
SD_CLK_HIGH;
SD_CLK_LOW;
SD_CLK_HIGH;
}
//-------------------------------------------------------------------------
void Ncc(void)
{
int i;
for(i=0;i<8;i++)
{
SD_CLK_LOW;
SD_CLK_HIGH;
}
}
//-------------------------------------------------------------------------
BYTE SD_card_init(void)
{
BYTE x,y;
SD_CMD_OUT;
SD_DAT_IN;
SD_CLK_HIGH;
SD_CMD_HIGH;
SD_DAT_LOW;
read_status=0;
for(x=0;x<40;x++)
Ncr();
for(x=0;x<5;x++)
cmd_buffer[x]=cmd0[x];
y = send_cmd(cmd_buffer);
do
{
for(x=0;x<40;x++);
Ncc();
for(x=0;x<5;x++)
cmd_buffer[x]=cmd55[x];
y = send_cmd(cmd_buffer);
Ncr();
if(response_R(1)>1) //response too long or crc error
return 1;
Ncc();
for(x=0;x<5;x++)
cmd_buffer[x]=acmd41[x];
y = send_cmd(cmd_buffer);
Ncr();
} while(response_R(3)==1);
Ncc();
for(x=0;x<5;x++)
cmd_buffer[x]=cmd2[x];
y = send_cmd(cmd_buffer);
Ncr();
if(response_R(2)>1)
return 1;
Ncc();
for(x=0;x<5;x++)
cmd_buffer[x]=cmd3[x];
y = send_cmd(cmd_buffer);
Ncr();
if(response_R(6)>1)
return 1;
RCA[0]=response_buffer[1];
RCA[1]=response_buffer[2];
Ncc();
for(x=0;x<5;x++)
cmd_buffer[x]=cmd9[x];
cmd_buffer[1] = RCA[0];
cmd_buffer[2] = RCA[1];
y = send_cmd(cmd_buffer);
Ncr();
if(response_R(2)>1)
return 1;
Ncc();
for(x=0;x<5;x++)
cmd_buffer[x]=cmd7[x];
cmd_buffer[1] = RCA[0];
cmd_buffer[2] = RCA[1];
y = send_cmd(cmd_buffer);
Ncr();
if(response_R(1)>1)
return 1;
Ncc();
for(x=0;x<5;x++)
cmd_buffer[x]=cmd16[x];
y = send_cmd(cmd_buffer);
Ncr();
if(response_R(1)>1)
return 1;
read_status =1; //sd card ready
return 0;
}
//-------------------------------------------------------------------------
BYTE SD_read_lba(BYTE *buff,UINT32 lba,UINT32 seccnt)
{
BYTE c=0;
UINT32i,j;
lba+=101;
for(j=0;j
{
{
Ncc();
cmd_buffer[0] = cmd17[0];
cmd_buffer[1] = (lba>>15)&0xff;
cmd_buffer[2] = (lba>>7)&0xff;
cmd_buffer[3] = (lba<<1)&0xff;
cmd_buffer[4] = 0;
lba++;
send_cmd(cmd_buffer);
Ncr();
}
while(1)
{
SD_CLK_LOW;
SD_CLK_HIGH;
if(!(SD_TEST_DAT))
break;
}
for(i=0;i<512;i++)
{
BYTE k;
for(k=0;k<8;k++)
{
SD_CLK_LOW;
SD_CLK_HIGH;
c <<= 1;
if(SD_TEST_DAT)
c |= 0x01;
}
*buff=c;
buff++;
}
for(i=0; i<16; i++)
{
SD_CLK_LOW;
SD_CLK_HIGH;
}
}
read_status = 1;//SD data next in
return 0;
}
//-------------------------------------------------------------------------
BYTE response_R(BYTE s)
{
BYTE a=0,b=0,c=0,r=0,crc=0;
BYTE i,j=6,k;
while(1)
{
SD_CLK_LOW;
SD_CLK_HIGH;
if(!(SD_TEST_CMD))
break;
if(crc++ >100)
return 2;
}
crc =0;
if(s == 2)
j = 17;
for(k=0; k
{
c = 0;
if(k > 0) //for crc culcar
b = response_buffer[k-1];
for(i=0; i<8; i++)
{
SD_CLK_LOW;
if(a > 0)
c <<= 1;
else
i++;
a++;
SD_CLK_HIGH;
if(SD_TEST_CMD)
c |= 0x01;
if(k > 0)
{
crc <<= 1;
if((crc ^ b) & 0x80)
crc ^= 0x09;
b <<= 1;
crc &= 0x7f;
}
}
if(s==3)
{
if( k==1 &&(!(c&0x80)))
r=1;
}
response_buffer[k] = c;
}
if(s==1 || s==6)
{
if(c != ((crc<<1)+1))
r=2;
}
return r;
}
//-------------------------------------------------------------------------
BYTE send_cmd(BYTE *in)
{
int i,j;
BYTE b,crc=0;
SD_CMD_OUT;
for(i=0; i < 5; i++)
{
b = in[ i ];
for(j=0; j<8; j++)
{
SD_CLK_LOW;
if(b&0x80)
SD_CMD_HIGH;
else
SD_CMD_LOW;
crc <<= 1;
SD_CLK_HIGH;
if((crc ^ b) & 0x80)
crc ^= 0x09;
b<<=1;
}
crc &= 0x7f;
}
crc =((crc<<1)|0x01);
b = crc;
for(j=0; j<8; j++)
{
SD_CLK_LOW;
if(crc&0x80)
SD_CMD_HIGH;
else
SD_CMD_LOW;
SD_CLK_HIGH;
crc<<=1;
}
return b;
}
//-------------------------------------------------------------------------
#endif
这些程序有些我也不明白,贴出来希望大家一起探讨交流,打破网站SD卡用SPI方式读写的垄断局面,哈哈
sd模式下应该有4各data吧,怎么你的只用一个?
SPI模式 是1Bit的
SD模式 是1Bit或4Bit
SD卡确实也像USB一样分为全速(FULL-SPEED)卡和低速卡(LOW-SPEED)。全速卡支持SPI,
1BIT和4BIT的传输模式,可以工作在0-25MHz,并且可以提供高达10MB/S的传输速率;低速卡
仅支持SPI和1BIT的传输模式,4BIT属于可选模式,可以工作在0-400KHz.
在SD模式下,用软件将数据拆分,计算CRC16的时间都占了大头,实际速度不比SPI模式快多少!
SD模式的读有优势吧。写的时候SD模式下就多了CRC16(刚接触实际的东西,所以不太了解CRC16的时间问题,见笑),但是读不管哪种模式下,不是都有CRC校验的?SD模式到底还有那些优势呢,请教
关注一下,SD的读写方式资料太少,或是英文资料太难看