s3c2440的SD/MMC的应用

 

SD(全名为Secure Digital Memory Card,安全数码卡),是一种存储卡的标准,它被广泛地用于便携式设备上,如数码相机、个人数字助理(PDA)和多媒体播放器等。它的技术是基于MMCMultiMedia Card)格式,因此SD兼容MMC

 

       s3c2440集成了SD控制器,可以方便地读写SDMMC和对SDIO进行操作。在这里,我们只研究对SD/MMC的操作。

 

       要想能够使s3c2440正确读写SD/MMC,就首先要清楚SD的规范协议,由于SD兼容MMC,所以两者的协议差别不大。SD的协议较为繁琐,下面只简略介绍最基本的内容:

 

       SD进行操作包括两个阶段:卡的识别和卡的数据传输。主机通过各种命令对SD进行操作,绝大多数命令都需要SD进行应答响应。在卡的识别阶段,用到的命令只有CMD1(得到主机的操作电压)、CMD2(得到卡的识别码)和CMD3(配置或得到卡的相对地址),其中也可以使用CMD0命令使卡进入空闲状态。CMD1只能用于MMCSD要用ACMD41辅助命令。在正确配置完该阶段后,卡进入待机状态。在卡的数据传输阶段可以完成对卡内存地址的读写等操作。

 

       卡内还配备了几个寄存器,主要有OCR寄存器,用于配置操作电压范围,使用命令CMD1可以获得;CID寄存器,用于得到卡的基本信息,使用命令CMD2CMD10可以获得;CSD寄存器,用于提供卡特性信息,使用CMD9可以获得;RCA寄存器,保存卡的相对地址。另外在卡应答响应信息中,会包括卡的状态信息,主机可以利用该信息获知卡的各种状态,以便进一步操作。

 

       s3c2440只要按照SD的协议去操作,就能正确读写SD。在初始化阶段,要配置SDICON寄存器以及负责传输频率的SDIPRE寄存器,并且还要等待一段时间,以保证初始化正确执行。在命令传输阶段,SDICmdArg寄存器负责传输命令参数,SDICmgCon寄存器负责传输命令索引值,通过SDICmdSta寄存器可以获知命令传输过程中的各种状态,命令的响应信息存储在SDIRSPn中。在数据传输阶段,SDIDTimer寄存器可以设置数据传输的超时时间,SDIBSize寄存器用于设置数据传输块的大小,寄存器SDIDatConSDIDatSta用于数据传输的控制和状态,而数据是通过SDIDAT寄存器利用内部的FIFO来进行传输的,其中寄存器SDIFSTA用于获知FIFO的各种状态。

 

       下面就具体给出一个读写SD的测试实例。该段程序是先对SD进行写操作,然后再从SD中读取该组数据,检查写入的数据和读取的数据是否一致,其中我们利用UART来获知一些必要的传输状态。我们只用查询方式进行数据传输,并且使用的是块操作模式。该段程序是针对MMC所编写,并不适用于SD,但只需做少许改动(在设置相对地址的地方)就可以用于SD

 

 

unsigned int *Tx_buffer;             

unsigned int *Rx_buffer;      

 

……  ……

 

void Main(void)

{

       int i;

       int tempSta;

       int block=16;                //传输数据块大小

       char flag;

       int response;

 

       //UART0的基本配置

……   ……

   

//SDI端口配置

rGPEUP = 0xf83f;               //SDCMD, SDDAT[3:0]上拉有效.

       rGPECON = 0xaaa<<10;             //SDCMD, SDDAT[3:0], SDCLK

 

//初始化SDI

rSDIPRE=124;             //SDI初始阶段传输频率为400KHz

rSDICON=(3<<4)|1;     //SDCLKMMC类型,字节顺序为Type B,使能SDCLK输出

rSDIFSTA|=1<<16;              //FIFO复位

rSDIBSIZE=0x200;              //传输数据块大小为512字节(128)

       rSDIDTIMER=0x7fffff;              //设置数据传输的超时时间

      

       flag=1;

      

       for(i=0;i<0x1000;i++)

              ;            //等待74SDCLK

   

//卡的识别阶段

//CMD0   GO_IDLE_STATE

rSDICARG=0x0;                 //设置CMD0参数为0

       rSDICCON=(1<<8)|0x40;     //无响应,开始传输CMD0,命令信息为命令索引值加0x40

       //等待CMD0结束

       tempSta=rSDICSTA;                          //读取命令状态寄存器

       while((tempSta&0x800)!=0x800)        //判断命令是否结束

              tempSta=rSDICSTA;            //没有结束,则继续等待

       rSDICSTA=tempSta;            //清命令状态

rSDICSTA=0xa00;               //

   

//CMD1   CEND_OP_COND

for(i=0;i<200;i++)       

       {

              rSDICARG=0xff8000;                 //CMD1参数:2.7V~3.6V

              rSDICCON=(0x1<<9)|(0x1<<8)|0x41;               //有响应,开始传输CMD1

 

              //检查命令状态

              tempSta=rSDICSTA;

              while( !( ((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400) ))   

                     tempSta=rSDICSTA;           //检查命令传输是否结束或超时

                    

              if( (tempSta&0xf00) == 0xa00 )  //如果命令传输没有错误或超时

              {

                     if((rSDIRSP0>>16)==0x80ff)      //OCR内容正确,且不忙

                     {

                            rSDICSTA=0xa00;        //清命令状态

                            break;            //退出循环

                     }

              }

       }

      

       if(i>190)        //没有检测到MMC

       {

              flag=0;                  //清标志

             while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=0x66;             //UART0发送信息,表示无MMC

              rGPBDAT =  ~0x1e0;                 //4LED

       }

       else                //检测到了MMC

       {

              rSDICSTA=0xa00;        //清命令状态

 

              //CMD2   ALL_SEND_CID

              while(flag)

              {

              rSDICARG=0x0;          //CMD2无需参数

                     rSDICCON=(0x1<<10)|(0x1<<9)|(0x1<<8)|0x42;      //128位长响应,开始传输CMD2

                     tempSta=rSDICSTA;

                     while(!(((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400)))   

                            tempSta=rSDICSTA;           //检查命令传输是否结束或超时

             

                     if( (tempSta&0x1f00) == 0xa00 )  //如果命令传输没有错误或超时

                     {

                            rSDICSTA=0xa00;

                            break;            //退出循环体

                     }

                     rSDICSTA=0xF<<9;           

       }

       //通过UART0输出MMCCID信息,一共128

       //在这里得到的CID信息为15 00 00 30 30 30 30 30 30 11 F1 01 11 28 29 ED

       while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=0xee;

       response=rSDIRSP0;

       while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>24);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>16);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>8);

              while(!(rUTRSTAT0 & 0x2))  ;            

              rUTXH0=(char)(response);

              

              response=rSDIRSP1;

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>24);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>16);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>8);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response);

                         

              response=rSDIRSP2;

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>24);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>16);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>8);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response);

                          

              response=rSDIRSP3;

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>24);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>16);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response>>8);

              while(!(rUTRSTAT0 & 0x2))  ;

              rUTXH0=(char)(response);

                    

              //CMD3   SET_RELATIVE_ADDR,设置卡的相对地址

       while(flag)

              {

                     rSDICARG=1<<16;          //设置CMD3参数,即相对地址,为1

                     rSDICCON=(0x1<<9)|(0x1<<8)|0x43;        //等待响应,开始传输CMD3

             

                     tempSta=rSDICSTA;

                     while( !( ((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400) ))   

                            tempSta=rSDICSTA;

             

                     if( (tempSta&0x1f00) == 0xa00 )

                     {

                            rSDICSTA=0xa00;

                            break;

                     }

                     rSDICSTA=0xF<<9;

              }

           

              //卡的数据传输阶段

              rSDIPRE=2;          //重新设置SDI的传输频率,约为16MHz

      

              //CMD13   SEND_STATUS

              //检查当前状态是否为待机状态,否则等待直到变为待机状态为止

            while(flag)

            {

                   rSDICARG=1<<16;      //设置CMD13参数,即相对地址

                     rSDICCON= (0x1<<9)|(0x1<<8)|0x4d;

             

                     tempSta=rSDICSTA;

                     while( !( ((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400) ))  

                            tempSta=rSDICSTA;

             

                     if( (tempSta&0x1f00) == 0xa00 )

                     {

                            rSDICSTA=0xa00;

                            if((rSDIRSP0 & 0x1e00)==0x600)

                                   break;

                     }

                     rSDICSTA=0xF<<9;

              }

             

              //CMD7   SELECT/DESELECT_CARD,把当前状态从待机状态变为传输状态

              while(flag)

              {

                     rSDICARG=1<<16;     

                     rSDICCON= (0x1<<9)|(0x1<<8)|0x47;

             

                     tempSta=rSDICSTA;

                     while( !( ((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400) ))   

                            tempSta=rSDICSTA;

             

                     if( ((tempSta&0x1f00) == 0xa00) )  // Check no error and tranfro state

                     {

                            rSDICSTA=0xa00;

                            break;

                     }

                     rSDICSTA=0xF<<9;

              }

             

              //CMD13   SEND_STATUS

//检查当前状态是否为传输状态,否则等待直到变为传输状态为止

              while(flag)

              {

                     rSDICARG=1<<16;     

                     rSDICCON= (0x1<<9)|(0x1<<8)|0x4d; 

             

                     tempSta=rSDICSTA;

                     while( !( ((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400) )) 

                            tempSta=rSDICSTA;

             

                     if( (tempSta&0x1f00) == 0xa00 )

                     {

                            rSDICSTA=0xa00;

                            if((rSDIRSP0 & 0x1e00)==0x800)

                                   break;

                     }

                     rSDICSTA=0xF<<9;//clear all

              }

 

              //由于MMC1位的总线宽,而系统默认就是1位总线宽,所以在这里无需改动数据总线宽度

      

              //为数据的读写,准备缓存数组

              Tx_buffer=(unsigned int *)0x31000000;             //发送数组

              for(i=0;i<2048;i++)                     //512(一块数据的字节数)×16(数据块)=2048×4

                     *(Tx_buffer+i)=2*i+1;         //写值

    

              Rx_buffer=(unsigned int *)0x31800000;             //接收数组

              for(i=0;i<2048;i++)      

                     *(Rx_buffer+i)=0;               //清零

   

       //写数据,查询方式

       rSDIFSTA |= 1<<16;            //FIFO复位

   

       rSDIDCON=(2<<22)|(1<<20)|(1<<17)|(1<<14)|(3<<12)|(block<<0);//字、块发送

       rSDICARG=0;              //写入MMC的内存首地址

   

       //CMD25   WRITE_MULTIPLE_BLOCK,多块写入命令

       while(flag)

              {

              rSDICCON=(0x1<<9)|(0x1<<8)|0x59;

 

              tempSta=rSDICSTA;

                     while( !( ((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400) ))

                            tempSta=rSDICSTA;

             

                     if( (tempSta&0x1f00) == 0xa00 ) 

                     {

                            rSDICSTA=0xa00;

                            break;

                     }

                     rSDICSTA=0xF<<9;

       }

          

              //写入MMC内存数据

i=0;

       while(i<128*block)

              {

                     //检查FIFO状态

                     tempSta=rSDIFSTA;

                     if((tempSta&0x2000)==0x2000)         //FIFO没有满

                     {

                            rSDIDAT=*Tx_buffer++;

                            i++;

                     }

              }

             

              //判断数据是否发送正确

              //检查数据状态

              tempSta=rSDIDSTA;

              while( !( ((tempSta&0x10)==0x10) | ((tempSta&0x20)==0x20) ))   

                     tempSta=rSDIDSTA;            //判断数据传输是否结束或超时

             

              if( (tempSta&0xfc) != 0x10 )              //数据传输没有结束

              {

                     rSDIDSTA=0xec;          //清状态

                     while(!(rUTRSTAT0 & 0x2))  ;

                     rUTXH0=0x88;             //UART0发送一个信息,表示SDI的某种错误

                     flag=0;                  //清标志,结束SDI的传输

              }

             

              if(flag)

              {

                     rSDIDCON=rSDIDCON&~(7<<12);          //清数据传输

                     rSDIDSTA=0x10; 

              }

 

              //CMD12   STOP_TRANSMISSION,结束传输,回到传输状态

              while(flag)

              {

                     rSDIDCON=(1<<18)|(1<<17)|(0<<16)|(1<<14)|(1<<12)|(block<<0);//忙检查

                     rSDICARG=0x0;       //CMD12无参数

                     rSDICCON=(0x1<<9)|(0x1<<8)|0x4c;

             

                     tempSta=rSDICSTA;

                     while( !( ((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400) )) 

                            tempSta=rSDICSTA;

             

                     if( (tempSta&0x1f00) == 0xa00 )

                     {

                            rSDICSTA=0xa00;

                            break;

                     }

                     rSDICSTA=0xF<<9;

              }

             

              //检查是否忙

              if(flag)

              {

                     tempSta=rSDIDSTA;

                     while( !( ((tempSta&0x08)==0x08) | ((tempSta&0x20)==0x20) ))

                            tempSta=rSDIDSTA;

             

                     if( (tempSta&0xfc) != 0x08 )                     //数据传输不是正确结束

                     {

                            flag=0;           //清标志,结束SDI传输

                     }

                     rSDIDSTA=0xf4;

              }

      

              //读取MMC内存数据

              if(flag)

              {

                     rSDIFSTA|=(1<<16);    //FIFO复位

                     rSDIDCON=(2<<22)|(1<<19)|(1<<17)|(1<<14)|(2<<12)|(block<<0);              //字、块接收

                     rSDICARG=0;       //设置读取MMC的内存首地址,要与写入时的首地址一致

              }

 

              //CMD18   READ_MULTIPLE_BLOCK,多块读命令

       while(flag)

              {

              rSDICCON=(0x1<<9)|(0x1<<8)|0x52;

 

                     tempSta=rSDICSTA;

                     while( !( ((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400) ))  

                            tempSta=rSDICSTA;

             

                     if( (tempSta&0x1f00) == 0xa00 )

                     {

                            rSDICSTA=0xa00;

                            break;

                     }

                     rSDICSTA=0xF<<9;//clear all

       }

               

       if(flag)

       {

              i=0;

              //MMC内存

                  while(i<128*block)             

                     {

                            if((rSDIDSTA&0x20)==0x20)            //判断是否超时

                            {

                                   rSDIDSTA=(0x1<<0x5);       // 清状态

                                   flag=0;                                //清标志,退出

                                   break;

                            }

                            tempSta=rSDIFSTA;

                            if((tempSta&0x1000)==0x1000)          //检查FIFO是否为空

                            {

                                   *Rx_buffer++=rSDIDAT;

                                   i++;

                            }

                     }                  

 

                     rSDIDCON=rSDIDCON&~(7<<12);          //清数据传输

                     rSDIFSTA &= 0x200;                //FIFO

                     rSDIDSTA=0x10;         // 清数据状态

              }

 

              //CMD12   STOP_TRANSMISSION,结束传输,回到传输状态

              while(flag)

              {

                     rSDICARG=0x0;    

                     rSDICCON=(0x1<<9)|(0x1<<8)|0x4c; 

             

                     tempSta=rSDICSTA;

                     while( !( ((tempSta&0x200)==0x200) | ((tempSta&0x400)==0x400) ))  

                            tempSta=rSDICSTA;

             

                     if( (tempSta&0x1f00) == 0xa00 ) 

                     {

                            rSDICSTA=0xa00;

                            break;

                     }

                     rSDICSTA=0xF<<9;//clear all

              }

      

              if(flag)

              {

                     //比较两个数组的内容

                     Tx_buffer=(unsigned int *)0x31000000;

                     Rx_buffer=(unsigned int *)0x31800000;

      

                     for(i=0;i<128*block;i++)

                     {

                            if(Rx_buffer[i] != Tx_buffer[i])          //有不相等的情况

                            {

                                   while(!(rUTRSTAT0 & 0x2))  ;

                                   rUTXH0=0x44;      //UART0发送一个信息,表示SDI的某种错误

                                   break;           

                            }

                     }

              }

      

              //CMD7   SELECT/DESELECT_CARD,把当前状态从传输状态变为待机状态

              while(flag)

              {

                     rSDICARG=0<<16;             //不带参数

                     rSDICCON= (0x1<<8)|0x47;             //无回复

             

                     tempSta=rSDICSTA;           

                     if( (tempSta&0x800) != 0x800 ) 

                     {

                            rSDICSTA=0xa00;

                            break;

                     }

                     rSDICSTA=0xF<<9;

              }

             

      

              rSDICSTA=0x800;              //结束SD

              rSDIDCON=0;

              rSDICSTA=0xffff;

 

              if(flag)

                     rGPBDAT = ~0x60;              //SD数据传输正确,亮2LED

              else

                     rGPBDAT =~0xe0;               //SD数据传输失败,亮3LED

       }

      

       while(1)

              ;

}

你可能感兴趣的:(c,cmd,测试,存储,buffer,Go)