- //-----------------------------------------------------------------------------------------------
-
向SD卡中写入命令,并返回回应的第二个字节 - //-----------------------------------------------------------------------------------------------
- unsigned
char Write_Command_SD(unsigned char *CMD) - {
-
unsigned char tmp; -
unsigned char retry=0; -
unsigned char i; -
-
//禁止SD卡片选 -
SPI_CS=1; -
//发送8个时钟信号 -
Write_Byte_SD(0xFF); -
//使能SD卡片选 -
SPI_CS=0; -
-
//向SD卡发送6字节命令 -
for (i=0;i<0x06;i++) -
{ -
Write_Byte_SD(*CMD++); -
} -
-
//获得16位的回应 -
Read_Byte_SD(); //read the first byte,ignore it. -
do -
{ //读取后8位 -
tmp = Read_Byte_SD(); -
retry++; -
} -
while((tmp==0xff)&&(retry<100)); -
return(tmp); - }
2)
SD卡的初始化是非常重要的,只有进行了正确的初始化,才能进行后面的各项操作。在初始化过程中,SPI的时钟不能太快,否则会造初始化失败。在初始化成功后,应尽量提高SPI的速率。在刚开始要先发送至少74个时钟信号,这是必须的。在很多读者的实验中,很多是因为疏忽了这一点,而使初始化不成功。随后就是写入两个命令CMD0与CMD1,使SD卡进入SPI模式
- //--------------------------------------------------------------------------
-
初始化SD卡到SPI模式 - //--------------------------------------------------------------------------
- unsigned
char SD_Init() - {
-
unsigned char retry,temp; -
unsigned char i; -
unsigned char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95}; -
SD_Port_Init(); //初始化驱动端口 -
-
Init_Flag=1; //将初始化标志置1 -
-
for (i=0;i<0x0f;i++) -
{ -
Write_Byte_SD(0xff); //发送至少74个时钟信号 -
} -
-
//向SD卡发送CMD0 -
retry=0; -
do -
{ //为了能够成功写入CMD0,在这里写200次 -
temp=Write_Command_SD(CMD); -
retry++; -
if(retry==200) -
{ //超过200次 -
return(INIT_CMD0_ERROR);//CMD0 Error! -
} -
} -
while(temp!=1); //回应01h,停止写入 -
-
//发送CMD1到SD卡 -
CMD[0] = 0x41; //CMD1 -
CMD[5] = 0xFF; -
retry=0; -
do -
{ //为了能成功写入CMD1,写100次 -
temp=Write_Command_SD(CMD); -
retry++; -
if(retry==100) -
{ //超过100次 -
return(INIT_CMD1_ERROR);//CMD1 Error! -
} -
} -
while(temp!=0);//回应00h停止写入 -
-
Init_Flag=0; //初始化完毕,初始化标志清零 -
-
SPI_CS=1; //片选无效 -
return(0); //初始化成功 - }
-
-
3)
CID寄存器存储了SD卡的标识码。每一个卡都有唯一的标识码。
CID寄存器长度为128位。它的寄存器结构如下:
名称
|
域
|
数据宽度
|
CID划分
|
生产标识号
|
MID
|
8
|
[127:120]
|
OEM/应用标识
|
OID
|
16
|
[119:104]
|
产品名称
|
PNM
|
40
|
[103:64]
|
产品版本
|
PRV
|
8
|
[63:56]
|
产品序列号
|
PSN
|
32
|
[55:24]
|
保留
|
-
|
4
|
[23:20]
|
生产日期
|
MDT
|
12
|
[19:8]
|
CRC7校验合
|
CRC
|
7
|
[7:1]
|
未使用,始终为1
|
-
|
1
|
[0:0]
|
- //------------------------------------------------------------------------------------
-
读取SD卡的CID寄存器 16字节 成功返回0 - //-------------------------------------------------------------------------------------
- unsigned
char Read_CID_SD(unsigned char *Buffer) - {
-
//读取CID寄存器的命令 -
unsigned char CMD[] = {0x4A,0x00,0x00,0x00,0x00,0xFF}; -
unsigned char temp; -
temp=SD_Read_Block(CMD,Buffer,16); //read 16 bytes -
return(temp); - }
4)读取CSD
CSD(Card-Specific Data)寄存器提供了读写SD卡的一些信息。其中的一些单元可以由用户重新编程。
- //-----------------------------------------------------------------------------------------
-
读SD卡的CSD寄存器 共16字节 返回0说明读取成功 - //-----------------------------------------------------------------------------------------
- unsigned
char Read_CSD_SD(unsigned char *Buffer) - {
-
//读取CSD寄存器的命令 -
unsigned char CMD[] = {0x49,0x00,0x00,0x00,0x00,0xFF}; -
unsigned char temp; -
temp=SD_Read_Block(CMD,Buffer,16); //read 16 bytes -
return(temp); - }
4)
综合上面对CID与CSD寄存器的读取,可以知道很多关于SD卡的信息,以下程序可以获取这些信息。如下:
- //-----------------------------------------------------------------------------------------------
- //返回
- //
SD卡的容量,单位为M - //
sector count and multiplier MB are in - u08
== C_SIZE / (2^(9-C_SIZE_MULT)) - //
SD卡的名称 - //-----------------------------------------------------------------------------------------------
- void
SD_get_volume_info() - {
-
unsigned char i; -
unsigned char c_temp[5]; -
VOLUME_INFO_TYPE SD_volume_Info,*vinf; -
vinf=&SD_volume_Info; //Init the pointoer; - /读取CSD寄存器
-
Read_CSD_SD(sectorBuffer.dat); - //获取总扇区数
-
vinf->sector_count = sectorBuffer.dat[6] & 0x03; -
vinf->sector_count <<= 8; -
vinf->sector_count += sectorBuffer.dat[7]; -
vinf->sector_count <<= 2; -
vinf->sector_count += (sectorBuffer.dat[8] & 0xc0) >> 6; -
// 获取multiplier -
vinf->sector_multiply = sectorBuffer.dat[9] & 0x03; -
vinf->sector_multiply <<= 1; -
vinf->sector_multiply += (sectorBuffer.dat[10] & 0x80) >> 7; - //获取SD卡的容量
-
vinf->size_MB = vinf->sector_count >> (9-vinf->sector_multiply); -
// get the name of the card -
Read_CID_SD(sectorBuffer.dat); -
vinf->name[0] = sectorBuffer.dat[3]; -
vinf->name[1] = sectorBuffer.dat[4]; -
vinf->name[2] = sectorBuffer.dat[5]; -
vinf->name[3] = sectorBuffer.dat[6]; -
vinf->name[4] = sectorBuffer.dat[7]; -
vinf->name[5] = 0x00; //end flag - }
-
以上程序将信息装载到一个结构体中,这个结构体的定义如下: - typedef
struct SD_VOLUME_INFO - {
//SD/SD Card info -
unsigned int size_MB; -
unsigned char sector_multiply; -
unsigned int sector_count; -
unsigned char name[6]; - }
VOLUME_INFO_TYPE;
5)
扇区读是对SD卡驱动的目的之一。SD卡的每一个扇区中有512个字节,一次扇区读操作将把某一个扇区内的512个字节全部读出。过程很简单,先写入命令,在得到相应的回应后,开始数据读取。
扇区读的时序:
- unsigned
char SD_Read_Sector(unsigned long sector,unsigned char *buffer) - {
-
unsigned char retry; -
//命令16 -
unsigned char CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF}; -
unsigned char temp; -
-
//地址变换 由逻辑块地址转为字节地址 -
sector = sector << 9; //sector = sector * 512 -
-
CMD[1] = ((sector & 0xFF000000) >>24 ); -
CMD[2] = ((sector & 0x00FF0000) >>16 ); -
CMD[3] = ((sector & 0x0000FF00) >>8 ); -
-
//将命令16写入SD卡 -
retry=0; -
do -
{ //为了保证写入命令 一共写100次 -
temp=Write_Command_MMC(CMD); -
retry++; -
if(retry==100) -
{ -
return(READ_BLOCK_ERROR); //block write Error! -
} -
} -
while(temp!=0); -
-
//Read Start Byte form MMC/SD-Card (FEh/Start Byte) -
//Now data is ready,you can read it out. -
while (Read_Byte_MMC() != 0xfe); -
readPos=0; -
SD_get_data(512,buffer) ; //512字节被读出到buffer中 -
return 0; - }
- 其中SD_get_data函数如下:
- //----------------------------------------------------------------------------
-
获取数据到buffer中 - //----------------------------------------------------------------------------
- void
SD_get_data(unsigned int Bytes,unsigned char *buffer) - {
-
unsigned int j; -
for (j=0;j<="" span="" style="word-wrap: break-word;"> -
*buffer++ = Read_Byte_SD(); - }
-
6)
扇区写是SD卡驱动的另一目的。每次扇区写操作将向SD卡的某个扇区中写入512个字节。过程与扇区读相似,只是数据的方向相反与写入命令不同而已。
扇区写的程序例程:
- //--------------------------------------------------------------------------------------------
-
写512个字节到SD卡的某一个扇区中去 返回0说明写入成功 - //--------------------------------------------------------------------------------------------
- unsigned
char SD_write_sector(unsigned long addr,unsigned char *Buffer) - {
-
unsigned char tmp,retry; -
unsigned int i; -
//命令24 -
unsigned char CMD[] = {0x58,0x00,0x00,0x00,0x00,0xFF}; -
addr = addr << 9; //addr = addr * 512 -
-
CMD[1] = ((addr & 0xFF000000) >>24 ); -
CMD[2] = ((addr & 0x00FF0000) >>16 ); -
CMD[3] = ((addr & 0x0000FF00) >>8 ); -
-
//写命令24到SD卡中去 -
retry=0; -
do -
{ //为了可靠写入,写100次 -
tmp=Write_Command_SD(CMD); -
retry++; -
if(retry==100) -
{ -
return(tmp); //send commamd Error! -
} -
} -
while(tmp!=0); -
-
-
//在写之前先产生100个时钟信号 -
for (i=0;i<100;i++) -
{ -
Read_Byte_SD(); -
} -
-
//写入开始字节 -
Write_Byte_MMC(0xFE); -
-
//现在可以写入512个字节 -
for (i=0;i<512;i++) -
{ -
Write_Byte_MMC(*Buffer++); -
} -
-
//CRC-Byte -
Write_Byte_MMC(0xFF); //Dummy CRC -
Write_Byte_MMC(0xFF); //CRC Code -
-
-
tmp=Read_Byte_MMC(); // read response -
if((tmp & 0x1F)!=0x05) // 写入的512个字节是未被接受 -
{ -
SPI_CS=1; -
return(WRITE_BLOCK_ERROR); //Error! -
} -
//等到SD卡不忙为止 - //因为数据被接受后,SD卡在向储存阵列中编程数据
-
while (Read_Byte_MMC()!=0xff){}; -
-
//禁止SD卡 -
SPI_CS=1; -
return(0);//写入成功 - }
-
单片机采用STC89LE单片机(SD卡的初始化电压为2.0V~3.6V,操作电压为3.1V~3.5V,因此不能用5V单片机,或进行分压处理),工作于22.1184M的时钟下,由于所采用的单片机中没硬件SPI,采用软件模拟SPI,因此读写速率都较慢。如果要半SD卡应用于音频、视频等要求高速场合,则需要选用有硬件SPI的控制器,或使用SD模式,有了 SPI模式的基础,SD模式应该不是什么难事。
上一篇:单片机对SD卡读写系列(三)
下一篇:单片机对SD卡读写系列(一)
推荐阅读最新更新时间:2024-03-16 14:44