📄 sd._c
字号:
//---------------------------- SD卡驱动程序 ----------------------------
//
//简化版SD卡驱动程序
//整理日期:2009.7.8
//
//------------------------------------------------------------------------
#include"sd.h"
#include"Usart.h"
//---------------------------------
//
// SD卡接口初始化函数
//
//---------------------------------
void SD_Port_Init(void)
{
SD_CS_DDR |= 1<<SD_CS_BIT;
SD_CS_PORT |= 1<<SD_CS_BIT;
}
//---------------------------------
//
// SD卡写寄存器函数
// arg:命令
// cmd:命令号
//
//---------------------------------
uint8 SD_SendCommand(uint8 cmd, uint32 arg)
{
uint8 r1=1;
uint8 retry=0;
SPI_SrByte(0xff);
SPI_CS_Assert;
while(r1--);
SPI_SrByte((cmd & 0x3f) | 0x40); /* send command */
SPI_SrByte(arg>>24);
SPI_SrByte(arg>>16);
SPI_SrByte(arg>>8);
SPI_SrByte(arg);
SPI_SrByte(0x95);
SPI_SrByte(0xff);
while((r1 = SPI_SrByte(0xff)) == 0xff) /* wait response */
if(retry++ > 0xfe) break; /* time out error */
SPI_CS_Deassert;
SPI_SrByte(0xff); // extra 8 CLK
return r1; /* return state */
}
//---------------------------------
//
// SD卡初始化函数
//
//---------------------------------
uint8 SD_Init(void)
{
uint8 i;
uint8 retry = 0;
uint8 r1 = 0;
SD_Port_Init();
SPI_CS_Assert;
delay_ms(1);
SPI_CS_Deassert;
delay_ms(1);
SPI_Low();
do
{
for(i=0;i<10;i++) SPI_SrByte(0xff);
r1 = SD_SendCommand(0, 0);//发idle命令 //send idle command
retry++;
if(retry>0xfe) return 1;//超时退出 //time out
} while(r1 != 0x01);
retry = 0;
do
{
r1 = SD_SendCommand(1, 0);//发active命令 //send active command
retry++;
if(retry>0xfe) return 1;//超时退出 //time out
} while(r1);
SPI_High(); /* Use High Speed SPI*/
r1 = SD_SendCommand(59, 0);//关crc //disable CRC
r1 = SD_SendCommand(16, 512);//设扇区大小512 //set sector size to 512
return 0;//正常返回 //normal return
}
//---------------------------------------------
//
// SD卡扇区读取函数
// sector:扇区号
// buffer:数据缓存
//
//---------------------------------------------
uint8 SD_ReadBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
uint16 retry=0;
SPI_High(); /* Use High Speed SPI*/
r1 = SD_SendCommand(17, sector<<9);//读命令 //read command
if(r1 != 0x00)
return r1;
SPI_CS_Assert;
//等数据的开始 //wait to start recieve data
while(SPI_SrByte(0xff) != 0xfe)
if(retry++ > 0xfffe)
{SPI_CS_Deassert;return 1;}
for(i=0; i<512; i++)//读512个数据 //read 512 bytes
{
*buffer++ = SPI_SrByte(0xff);
}
SPI_SrByte(0xff);//伪crc //dummy crc
SPI_SrByte(0xff);
SPI_CS_Deassert;
SPI_SrByte(0xff);// extra 8 CLK
return 0;
}
//---------------------------------------------
//
// SD卡扇区写入函数
// sector:扇区号
// buffer:数据缓存
//
//---------------------------------------------
uint8 SD_WriteSingleBlock(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
uint16 retry=0;
//SPI_High(); /* Use High Speed SPI*/
r1 = SD_SendCommand(24, sector<<9);//写命令 //send command
if(r1 != 0x00)
return r1;
SPI_CS_Assert;
SPI_SrByte(0xff);
SPI_SrByte(0xff);
SPI_SrByte(0xff);
SPI_SrByte(0xfe);//发开始符 //send start byte "token"
for(i=0; i<512; i++)//送512字节数据 //send 512 bytes data
{
SPI_SrByte(buffer[i]);
}
SPI_SrByte(0xff); //dummy crc
SPI_SrByte(0xff);
r1 = SPI_SrByte(0xff);
if( (r1&0x1f) != 0x05)//等待是否成功 //judge if it successful
{
SPI_CS_Deassert;
return r1;
}
//等待操作完 //wait no busy
while(!SPI_SrByte(0xff))if(retry++ > 0xfffe){SPI_CS_Deassert;return 1;}
SPI_CS_Deassert;
//SPI_SrByte(0xff);// extra 8 CLK
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -