#include <stdio.h> #include <string.h> #include "def.h" #include "option.h" #include "2440addr.h" #include "2440lib.h" #include "sdi.h"
#define INICLK 300000 #define SDCLK 24000000 //PCLK=49.392MHz
#define MMCCLK 15000000 //PCLK=49.392MHz
#define POL 0 #define INT 1 #define DMA 2 int CMD13(void); // Send card status
int CMD9(void);
unsigned int *Tx_buffer; //128[word]*16[blk]=8192[byte]
unsigned int *Rx_buffer; //128[word]*16[blk]=8192[byte]
volatile unsigned int rd_cnt;//读数据计数器
volatile unsigned int wt_cnt;//写数据计数器
volatile unsigned int block; //读写块总数
volatile unsigned int TR_end=0; int Wide=0; // 0:1bit, 1:4bit
int MMC=0; // 0:SD , 1:MMC
int Maker_ID; char Product_Name[7]; int Serial_Num; volatile int RCA; void Test_SDI(void) { U32 save_rGPEUP, save_rGPECON; RCA=0; MMC=0; block=3072; //3072Blocks=1.5MByte, ((2Block=1024Byte)*1024Block=1MByte)
save_rGPEUP=rGPEUP; save_rGPECON=rGPECON; //**配置SD/MMC控制器
rGPEUP = 0xf83f; // SDCMD, SDDAT[3:0] => PU En.
rGPECON = 0xaaaaaaaa; //SDCMD, SDDAT[3:0]
Uart_Printf("\nSDI Card Write and Read Test\n"); if(!SD_card_init()) //等待SD卡初始化完成
return; TR_Buf_new();//发送数据缓冲区初始化
Wt_Block();//写卡
Rd_Block();//读卡
View_Rx_buf(); if(MMC) TR_Buf_new(); if(MMC) { rSDICON |=(1<<5); // YH 0519, MMC Type SDCLK
Wt_Stream(); Rd_Stream(); View_Rx_buf(); } Card_sel_desel(0); // Card deselect
if(!CMD9()) Uart_Printf("Get CSD fail!!!\n"); rSDIDCON=0;//tark???
rSDICSTA=0xffff; rGPEUP=save_rGPEUP; rGPECON=save_rGPECON; } void TR_Buf_new(void) //发送数据缓冲区初始化
{ //-- Tx & Rx Buffer initialize
int i, j; Tx_buffer=(unsigned int *)0x31000000; j=0; for(i=0;i<2048;i++) //128[word]*16[blk]=8192[byte]
*(Tx_buffer+i)=i+j; Flush_Rx_buf(); } void Flush_Rx_buf(void) //接收数据缓冲区清0
{ //-- Flushing Rx buffer
int i; Rx_buffer=(unsigned int *)0x31800000; for(i=0;i<2048;i++) //128[word]*16[blk]=8192[byte]
*(Rx_buffer+i)=0; Uart_Printf("End Rx buffer flush\n"); } void View_Rx_buf() { //-- Display Rx buffer
int i,error=0; Tx_buffer=(unsigned int *)0x31000000; Rx_buffer=(unsigned int *)0x31800000; Uart_Printf("Check Rx data\n"); for(i=0;i<128*block;i++) { if(Rx_buffer[i] != Tx_buffer[i]) { Uart_Printf("\nTx/Rx error\n"); Uart_Printf("%d:Tx-0x%08x, Rx-0x%08x\n",i,Tx_buffer[i], Rx_buffer[i]); error=1; break; } } if(!error) { Uart_Printf("\nThe Tx_buffer is same to Rx_buffer!\n"); Uart_Printf("SD CARD Write and Read test is OK!\n"); } } void View_Tx_buf(void) { } int SD_card_init(void) //SD卡初始化
{ //-- SD controller & card initialize
int i; /* Important notice for MMC test condition */ /* Cmd & Data lines must be enabled by pull up resister */ rSDIPRE=PCLK/(INICLK)-1; // 400KHz
Uart_Printf("Init. Frequency is %dHz\n",(PCLK/(rSDIPRE+1))); rSDICON=(1<<4)|1; //先传高位,再传低位,使能CLK
rSDIFSTA=rSDIFSTA|(1<<16); //SDI FIFO status register,FIFO复位
rSDIBSIZE=0x200; // SDI block size register,设置每块大小为512byte(128word)
rSDIDTIMER=0x7fffff; // SDI data / busy timer register,设置超时周期
for(i=0;i<0x1000;i++); // 延时,CARD自身初始化需要74个CLK
CMD0(); //发送CMD0
Uart_Printf("In idle\n"); //-- Check MMC card OCR
if(Chk_MMC_OCR()) { Uart_Printf("In MMC ready\n"); MMC=1; goto RECMD2; } Uart_Printf("MMC check end!!\n"); //-- Check SD card OCR
if(Chk_SD_OCR()) Uart_Printf("In SD ready\n"); else { Uart_Printf("Initialize fail\nNo Card assertion\n"); return 0; } RECMD2: //检查连接的卡,识别卡的状态
rSDICARG=0x0; // CMD2(stuff bit)
rSDICCON=(0x1<<10)|(0x1<<9)|(0x1<<8)|0x42; //CMD2为长应答并等待应答,开始发
//送CMD2,命令卡发回CID寄存器(保存了生产厂家/时间/批号等等),产生RSP2
//检查CMD2是否成功发送并收到响应
if(!Chk_CMDend(2, 1)) goto RECMD2; //CMD2出错,重新发送
rSDICSTA=0xa00; // 清除命令和应答结束标志位
Uart_Printf("End id\n"); RECMD3: //--发送CMD3,给卡分配RCA,
rSDICARG=MMC<<16; //CMD3参数, MMC:设置 RCA, SD:请求发RCA
rSDICCON=(0x1<<9)|(0x1<<8)|0x43; //CMD3为短应答并等待应答,开始发
//送CMD3, MMC(设置RCA,产生RSP1),SD(设置RCA,产生RSP6)
//检查CMD3是否成功发送并收到响应
if(!Chk_CMDend(3, 1)) goto RECMD3; //CMD3出错,重新发送
rSDICSTA=0xa00; // 清除命令和应答结束标志位
//--Publish RCA
if(MMC) { RCA=1; rSDIPRE=(PCLK/MMCCLK)-1; Uart_Printf("MMC Frequency is %dHz\n",(PCLK/(rSDIPRE+1))); } else { RCA=( rSDIRSP0 & 0xffff0000 )>>16; //回读RCA,卡被分配RCA后进入//TransferMODE,准备读写
Uart_Printf("RCA=0x%x\n",RCA); rSDIPRE=PCLK/(SDCLK)-1; // Normal clock=25MHz
Uart_Printf("SD Frequency is %dHz\n",(PCLK/(rSDIPRE+1))); } //--State(stand-by) check
//根据SD规范,rSDIRSP0高16位存储RCA,低16位存储CARD的状态
if( rSDIRSP0 & 0x1e00!=0x600 ) // 检查CARD状态,不是处于stand-by状态
goto RECMD3; ////未就绪,重新发送CMD3
Uart_Printf("In stand-by\n"); Card_sel_desel(1); // Select
if(!MMC) //若位SD卡,设置4bit的数据传输模式
Set_4bit_bus(); else //若位MMC卡,设置1bit的数据传输模式
Set_1bit_bus(); return 1; } void Card_sel_desel(char sel_desel) { //-- Card select or deselect
if(sel_desel) //选择该卡
{ RECMDS7: //送CMD7表示选择该卡,准备读写,产生RSP1.
rSDICARG=RCA<<16; // CMD7参数(RCA,stuff bit),其中高16位为RCA
rSDICCON= (0x1<<9)|(0x1<<8)|0x47; //CMD7为短应答并等待应答,开始发
//送CMD7选择该卡,准备读写,产生RSP1
//检查CMD7是否成功发送并收到响应
if(!Chk_CMDend(7, 1)) goto RECMDS7; //出错,重新发送
rSDICSTA=0xa00; //清除命令和应答结束标志位
//检查是否处于transfer mode
if( rSDIRSP0 & 0x1e00!=0x800 ) goto RECMDS7; //不处于transfer mode,重新发送CMD7
} else //卸载该卡
{ RECMDD7: rSDICARG=0<<16; // CMD7参数(RCA,stuff bit),其中高16位为RCA
rSDICCON=(0x1<<8)|0x47; // 无应答并,并开始发送CMD7
//检查CMD7是否成功
if(!Chk_CMDend(7, 0)) goto RECMDD7; //失败,重新卸载
rSDICSTA=0x800; // //清除命令结束标志位
} } void __irq Rd_Int(void) //读中断函数
{ U32 i,status; status=rSDIFSTA; if( (status&0x200) == 0x200 ) //检查接收FIFO最后是否有数据到来
{ for(i=(status & 0x7f)/4;i>0;i--) { *Rx_buffer++=rSDIDAT; rd_cnt++; } rSDIFSTA=rSDIFSTA&0x200; //清 Rx FIFO Last data Ready标志位
} else if( (status&0x80) == 0x80 )// 检查Half FULL interrupt标志,只要大于31个字节,就会将该标志置1
{ for(i=0;i<8;i++) { *Rx_buffer++=rSDIDAT; rd_cnt++; } } ClearPending(BIT_SDI);//清零源中断挂起寄存器和中断挂起寄存器
} void __irq Wt_Int(void) //写中断函数
{ ClearPending(BIT_SDI);//清零源中断挂起寄存器和中断挂起寄存器
rSDIDAT=*Tx_buffer++; wt_cnt++; if(wt_cnt==128*block) { rINTMSK |= BIT_SDI;//屏蔽BIT_SDI中断
rSDIDAT=*Tx_buffer; TR_end=1; } } void __irq DMA_end(void) { ClearPending(BIT_DMA0);//清零源中断挂起寄存器和中断挂起寄存器
TR_end=1; } void Rd_Block(void) { U32 mode; int status; rd_cnt=0; Uart_Printf("Block read test[ Polling read ]\n");
mode = 0 ; rSDIFSTA=rSDIFSTA|(1<<16); // 复位FIFO
if(mode!=2) rSDIDCON=(2<<22)|(1<<19)|(1<<17)|(Wide<<16)|(1<<14)|(2<<12)|(block<<0); //YH 040220
//设置数据控制寄存器:字传输,块数据传输,4bit数据传输,开始数据传输,数据发送模//式,共读block个块
rSDICARG=0x0; // CMD17/18地址参数
RERDCMD: switch(mode) { case POL: if(block<2) // SINGLE_READ写单块
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x51; //CMD17为短应答并等待应答,开始发
//送CMD17单块读命令,开始读,产生RSP1
if(!Chk_CMDend(17, 1)) //-- Check end of CMD17
goto RERDCMD; //失败,继续发送
} else // MULTI_READ,读多块
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x52; //CMD18为短应答并等待应答,开始发
//送CMD18多块读命令,开始读,产生RSP1
if(!Chk_CMDend(18, 1)) //-- Check end of CMD18
goto RERDCMD; //失败,继续发送
} rSDICSTA=0xa00; // 清命令和应答结束标志
while(rd_cnt<128*block) // 512块个字节
{ if((rSDIDSTA&0x20)==0x20) // 是否超时
{ rSDIDSTA=(0x1<<0x5); // 清超时标志位
break; } status=rSDIFSTA; if((status&0x1000)==0x1000) // FIFO非空
{ *Rx_buffer++=rSDIDAT; rd_cnt++; } } break; case INT: pISR_SDI=(unsigned)Rd_Int; rINTMSK = ~(BIT_SDI);//屏蔽除所有其他中断
rSDIIMSK=5; // 开启Last & Rx FIFO half 中断.
if(block<2) // SINGLE_READ
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x51; //CMD17为短应答并等待应答,开始发
//送CMD17单块读命令,开始读,产生RSP1
if(!Chk_CMDend(17, 1)) //-- Check end of CMD17
goto RERDCMD; //失败,继续发送
} else // MULTI_READ
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x52; //CMD18为短应答并等待应答,开始发
//送CMD18多块读命令,开始读,产生RSP1
if(!Chk_CMDend(18, 1)) //-- Check end of CMD18
goto RERDCMD; //失败,继续发送
} rSDICSTA=0xa00; // 清命令和应答结束标志
while(rd_cnt<128*block); rINTMSK |= (BIT_SDI);//屏蔽BIT_SDI中断
rSDIIMSK=0; //屏蔽所有中断
break; case DMA: pISR_DMA0=(unsigned)DMA_end; rINTMSK = ~(BIT_DMA0); rSDIDCON=rSDIDCON|(1<<24); //YH 040227, Burst4 Enable
rDISRC0=(int)(SDIDAT); // SDIDAT
rDISRCC0=(1<<1)+(1<<0); // APB, fix
rDIDST0=(U32)(Rx_buffer); // Rx_buffer
rDIDSTC0=(0<<1)+(0<<0); // AHB, inc
rDCON0=(1<<31)+(0<<30)+(1<<29)+(0<<28)+(0<<27)+(2<<24)+(1<<23)+(1<<22)+(2<<20)+128*block; rDMASKTRIG0=(0<<2)+(1<<1)+0; //no-stop, DMA2 channel on, no-sw trigger
rSDIDCON=(2<<22)|(1<<19)|(1<<17)|(Wide<<16)|(1<<15)|(1<<14)|(2<<12)|(block<<0); if(block<2) // SINGLE_READ
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x51; // sht_resp, wait_resp, dat, start, CMD17
if(!Chk_CMDend(17, 1)) //-- Check end of CMD17
goto RERDCMD; } else // MULTI_READ
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x52; // sht_resp, wait_resp, dat, start, CMD18
if(!Chk_CMDend(18, 1)) //-- Check end of CMD18
goto RERDCMD; } rSDICSTA=0xa00; // Clear cmd_end(with rsp)
while(!TR_end); //Uart_Printf("rSDIFSTA=0x%x\n",rSDIFSTA);
rINTMSK |= (BIT_DMA0); TR_end=0; rDMASKTRIG0=(1<<2); //DMA0 stop
break; default: break; } //-- Check end of DATA
if(!Chk_DATend()) Uart_Printf("dat error\n"); rSDIDCON=rSDIDCON&~(7<<12); rSDIFSTA=rSDIFSTA&0x200; //Clear Rx FIFO Last data Ready, YH 040221
rSDIDSTA=0x10; // Clear data Tx/Rx end detect
if(block>1) { RERCMD12: //--Stop cmd(CMD12)
rSDICARG=0x0; //CMD12(stuff bit)
rSDICCON=(0x1<<9)|(0x1<<8)|0x4c;//sht_resp, wait_resp, start, CMD12
//-- Check end of CMD12
if(!Chk_CMDend(12, 1)) goto RERCMD12; rSDICSTA=0xa00; // Clear cmd_end(with rsp)
} }
void Rd_Stream(void) // only for MMC, 3blk read
{ int status, rd_cnt=0; if(MMC!=1) { Uart_Printf("Stream read command supports only MMC!\n"); return; } Uart_Printf("\n[Stream read test]\n"); RECMD11: rSDIDCON=(2<<22)|(1<<19)|(0<<17)|(0<<16)|(1<<14)|(2<<12);
rSDICARG=0x0; // CMD11(addr)
rSDICCON=(0x1<<9)|(0x1<<8)|0x4b; //sht_resp, wait_resp, dat, start, CMD11
while(rd_cnt<128*block) { if( (rSDIDSTA&0x20) == 0x20 ) { Uart_Printf("Rread timeout error"); return ; } status=rSDIFSTA; if((status&0x1000)==0x1000) { //*Rx_buffer++=rSDIDAT;
//rd_cnt++;
Rx_buffer[rd_cnt++]=rSDIDAT; } } //-- Check end of CMD11
if(!Chk_CMDend(11, 1)) goto RECMD11; rSDICSTA=0xa00; // Clear cmd_end(with rsp)
//-- Check end of DATA
rSDIDCON=(2<<22)|(1<<19)|(0<<17)|(0<<16); //YH 040220
rSDIDCON=rSDIDCON&~(7<<12); //YH 040220, no operation, data ready
while( rSDIDSTA&0x3 !=0x0 ); if(rSDIDSTA!=0) Uart_Printf("rSDIDSTA=0x%x\n", rSDIDSTA); rSDIDSTA=0xff; //YH 040221
STRCMD12: //--Stop cmd(CMD12)
rSDICARG=0x0; //CMD12(stuff bit)
rSDICCON=(0x1<<9)|(0x1<<8)|0x4c; //sht_resp, wait_resp, start, CMD12
//-- Check end of CMD12
if(!Chk_CMDend(12, 1)) goto STRCMD12; rSDICSTA=0xa00; // Clear cmd_end(with rsp)
rSDIFSTA=rSDIFSTA&0x200; //Clear Rx FIFO Last data Ready, YH 040221
Uart_Printf("rSDIFSTA1=0x%x\n", rSDIFSTA); //YH 040221
rSDIFSTA=rSDIFSTA&0x200; //Clear Rx FIFO Last data Ready, YH 040221
Uart_Printf("rSDIFSTA2=0x%x\n", rSDIFSTA); //YH 040221
Uart_Printf("\n--End stream read test\n"); }
void Wt_Block(void) { U32 mode; int status; wt_cnt=0; Uart_Printf("Block write test[ Polling write ]\n"); mode = 0 ; rSDIFSTA=rSDIFSTA|(1<<16); //复位FIFO
if(mode!=2) rSDIDCON=(2<<22)|(1<<20)|(1<<17)|(Wide<<16)|(1<<14)|(3<<12)|(block<<0); //设置数据控制寄存器:字传输,块数据传输,4bit数据传输,开始数据传输,数据发送模//式,共写block个块
rSDICARG=0x0; // CMD24/25地址参数
REWTCMD: switch(mode) { case POL: if(block<2) // SINGLE_WRITE,写单块
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x58; //CMD24为短应答并等待应答,开始发
//送CMD24单块写命令,开始写,产生RSP1
if(!Chk_CMDend(24, 1)) //-- Check end of CMD24
goto REWTCMD; //命令发送失败
} else // MULTI_WRITE,写多块
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x59; //CMD25为短应答并等待应答,开发
//送CMD25多块写命令,开始读写,产生RSP1
if(!Chk_CMDend(25, 1)) //-- Check end of CMD25
goto REWTCMD; //命令发送失败
} rSDICSTA=0xa00; // 清命令和应答结束标志
while(wt_cnt<128*block) { status=rSDIFSTA; if((status&0x2000)==0x2000) //FIFO未满
{ rSDIDAT=*Tx_buffer++; wt_cnt++; //Uart_Printf("Block No.=%d, wt_cnt=%d\n",block,wt_cnt);
} } break; case INT: pISR_SDI=(unsigned)Wt_Int; rINTMSK = ~(BIT_SDI); rSDIIMSK=0x10; // Tx FIFO half int.
if(block<2) // SINGLE_WRITE
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x58; //sht_resp, wait_resp, dat, start, CMD24
if(!Chk_CMDend(24, 1)) //-- Check end of CMD24
goto REWTCMD; } else // MULTI_WRITE
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x59; //sht_resp, wait_resp, dat, start, CMD25
if(!Chk_CMDend(25, 1)) //-- Check end of CMD25
goto REWTCMD; } rSDICSTA=0xa00; // Clear cmd_end(with rsp)
while(!TR_end); //while(wt_cnt<128);
rINTMSK |= (BIT_SDI); TR_end=0; rSDIIMSK=0; // All mask
break; case DMA: pISR_DMA0=(unsigned)DMA_end; rINTMSK = ~(BIT_DMA0); rSDIDCON=rSDIDCON|(1<<24); //YH 040227, Burst4 Enable
rDISRC0=(int)(Tx_buffer); // Tx_buffer
rDISRCC0=(0<<1)+(0<<0); // AHB, inc
rDIDST0=(U32)(SDIDAT); // SDIDAT
rDIDSTC0=(1<<1)+(1<<0); // APB, fix
rDCON0=(1<<31)+(0<<30)+(1<<29)+(0<<28)+(0<<27)+(2<<24)+(1<<23)+(1<<22)+(2<<20)+128*block; //handshake, sync PCLK, TC int, single tx, single service, SDI, H/W request,
//auto-reload off, word, 128blk*num
rDMASKTRIG0=(0<<2)+(1<<1)+0; //no-stop, DMA0 channel on, no-sw trigger
rSDIDCON=(2<<22)|(1<<20)|(1<<17)|(Wide<<16)|(1<<15)|(1<<14)|(3<<12)|(block<<0); //YH 040220
// Word Tx, Tx after rsp, blk, 4bit bus, dma enable, Tx start, blk num
if(block<2) // SINGLE_WRITE
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x58; //sht_resp, wait_resp, dat, start, CMD24
if(!Chk_CMDend(24, 1)) //-- Check end of CMD24
goto REWTCMD; } else // MULTI_WRITE
{ rSDICCON=(0x1<<9)|(0x1<<8)|0x59; //sht_resp, wait_resp, dat, start, CMD25
if(!Chk_CMDend(25, 1)) //-- Check end of CMD25
goto REWTCMD; } rSDICSTA=0xa00; // Clear cmd_end(with rsp)
while(!TR_end); rINTMSK |= (BIT_DMA0); TR_end=0; rDMASKTRIG0=(1<<2); //DMA0 stop
break; default: break; } //-- Check end of DATA
if(!Chk_DATend()) Uart_Printf("dat error\n"); rSDIDCON=rSDIDCON&~(7<<12); //YH 040220, Clear Data Transfer mode => no operation, Cleata Data Transfer start
rSDIDSTA=0x10; // Clear data Tx/Rx end
if(block>1) { //--Stop cmd(CMD12)
REWCMD12: rSDIDCON=(1<<18)|(1<<17)|(0<<16)|(1<<14)|(1<<12)|(block<<0); //YH 040220
rSDICARG=0x0; //CMD12(stuff bit)
rSDICCON=(0x1<<9)|(0x1<<8)|0x4c; //sht_resp, wait_resp, start, CMD12
//-- Check end of CMD12
if(!Chk_CMDend(12, 1)) goto REWCMD12; rSDICSTA=0xa00; // Clear cmd_end(with rsp)
//-- Check end of DATA(with busy state)
if(!Chk_BUSYend()) Uart_Printf("error\n"); rSDIDSTA=0x08; //! Should be cleared by writing '1'.
} } void Wt_Stream(void) // only for MMC, 3blk write
{ int status, wt_cnt=0; if(MMC!=1) { Uart_Printf("Stream write command supports only MMC!\n"); return; } Uart_Printf("\n[Stream write test]\n"); RECMD20: rSDIDCON=(2<<22)|(1<<20)|(0<<17)|(0<<16)|(1<<14)|(3<<12); // stream mode
rSDICARG=0x0; // CMD20(addr)
rSDICCON=(0x1<<9)|(0x1<<8)|0x54; //sht_resp, wait_resp, dat, start, CMD20
//-- Check end of CMD25
if(!Chk_CMDend(20, 1)) goto RECMD20; rSDICSTA=0xa00; // Clear cmd_end(with rsp)
while(wt_cnt<128*block) { status=rSDIFSTA; if((status&0x2000)==0x2000) rSDIDAT=Tx_buffer[wt_cnt++]; } //-- Check end of DATA
while( rSDIFSTA&0x400 ); Delay(10); // for the empty of DATA line(Hardware)
rSDIDCON=(1<<20)|(0<<17)|(0<<16); //YH 040220
rSDIDCON=rSDIDCON&~(7<<12); //YH 040220, no operation, data ready
while( (rSDIDSTA&0x3)!=0x0 ); if(rSDIDSTA!=0x0) Uart_Printf("rSDIDSTA=0x%x\n", rSDIDSTA); rSDIDSTA=0xff; //Clear rSDIDSTA
STWCMD12: //--Stop cmd(CMD12)
rSDIDCON=(1<<18)|(1<<17)|(0<<16)|(1<<14)|(1<<12);
rSDICARG=0x0; //CMD12(stuff bit)
rSDICCON=(0x1<<9)|(0x1<<8)|0x4c; //sht_resp, wait_resp, start, CMD12
//-- Check end of CMD12
if(!Chk_CMDend(12, 1)) goto STWCMD12; rSDICSTA=0xa00; // Clear cmd_end(with rsp)
//-- Check end of DATA(with busy state)
if(!Chk_BUSYend()) Uart_Printf("error\n"); rSDIDSTA=0x08; Uart_Printf("\n--End Stream write test\n"); }
int Chk_CMDend(int cmd, int be_resp) //0: Timeout
{ int finish0; if(!be_resp) // 没有应答信号
{ finish0=rSDICSTA; //读取SDI command status register,
while((finish0&0x800)!=0x800) // 等待命令结束
finish0=rSDICSTA; rSDICSTA=finish0;// 清命令结束标志
return 1; //成功,返回1
} else // 有应答信号
{ finish0=rSDICSTA; //读取SDI command status register,
while( !( ((finish0&0x200)==0x200) | ((finish0&0x400)==0x400) )) //检查超时和应答信号
finish0=rSDICSTA; if(cmd==1 | cmd==41) // CRC no check, CMD9 is a long Resp. command.
{ if( (finish0&0xf00) != 0xa00 ) // 命令,应答有错,或超时
{ rSDICSTA=finish0; // 清错误标志
if(((finish0&0x400)==0x400)) // 若超时,返回0
return 0; } rSDICSTA=finish0; // 清命令和应答结束标志
} else // 进行CRC效验
{ if( (finish0&0x1f00) != 0xa00 ) // CRC效验出错
{ Uart_Printf("CMD%d:rSDICSTA=0x%x, rSDIRSP0=0x%x\n",cmd, rSDICSTA, rSDIRSP0); //输出对应寄存器的值
rSDICSTA=finish0; // 清除错误标志位
if(((finish0&0x400)==0x400)) // 若超时,返回0
return 0; } rSDICSTA=finish0; } return 1; } } int Chk_DATend(void) { int finish; finish=rSDIDSTA; while( !( ((finish&0x10)==0x10) | ((finish&0x20)==0x20) )) // 检查数据结束和超时位
finish=rSDIDSTA; if( (finish&0xfc) != 0x10 )//数据传输结束
{ Uart_Printf("DATA:finish=0x%x\n", finish); rSDIDSTA=0xec; //清除错误标志
return 0; } return 1; } int Chk_BUSYend(void) { int finish; finish=rSDIDSTA; while( !( ((finish&0x08)==0x08) | ((finish&0x20)==0x20) ))//检查忙标志位
finish=rSDIDSTA; if( (finish&0xfc) != 0x08 ) { Uart_Printf("DATA:finish=0x%x\n", finish); rSDIDSTA=0xf4; //清除错误标志
return 0; } return 1; } void CMD0(void) //CMD0用于对SD实现软件复位,不论卡处于何种状态,使SD卡处于//空闲状态,等待下一个命令的到来
{ //-- Make card idle state
rSDICARG=0x0; // ,CMD0(stuff bit)
rSDICCON=(1<<8)|0x40; // 不等待应答信号,命令开始//并再次发送CMD0 关于cmd0定义:0(start_bit)1(cmd标志)000000(6位cmd编//码)后面是参数 (无参数就全0)
//-- Check end of CMD0
Chk_CMDend(0, 0); // 检查命令是否成功发送
rSDICSTA=0x800; // 清除命令结束标志
} int Chk_MMC_OCR(void) { int i;
//-- MMC卡识别的等待操作, 使卡处于空闲状态
for(i=0;i<100;i++) //等待的时间取决于厂商
{ // rSDICARG=0xffc000; //CMD1(MMC OCR:2.6V~3.6V), 设置工作电压
rSDICARG=0xff8000; //CMD1(SD OCR:2.7V~3.6V),设置工作电压
rSDICCON=(0x1<<9)|(0x1<<8)|0x41; //CMD1为短应答并等待应答,开始发送CMD1
//-- 检查CMD1是否发送成功,并检查卡状态
// if(Chk_CMDend(1, 1) & rSDIRSP0==0x80ffc000) //31:忙状态标志位,0表示忙
//0xffc000为卡的正常工作电压范围
if(Chk_CMDend(1, 1) && (rSDIRSP0>>16)==0x80ff) //卡处于空闲
// if(Chk_CMDend(1, 1) & rSDIRSP0==0x80ff8000)
{ rSDICSTA=0xa00; // 清除命令和应答结束标志位
return 1; // 成功
} } rSDICSTA=0xa00; //清除命令和应答结束标志位
return 0; // 失败
} int Chk_SD_OCR(void) { int i; //-- SD卡识别的等待操作, 使卡处于空闲状态
for(i=0;i<50;i++) // 如果这段时间太短,SD卡的初始化可能会失败
{ CMD55(); //送CMD55,表示下个命令将是特殊功能命令acmd,而非一般命令cmd
rSDICARG=0xff8000; //ACMD41(SD OCR:2.7V~3.6V) ,设置工作电压
// rSDICARG=0xffc000;//ACMD41(MMC OCR:2.6V~3.6V)设置工作电压 rSDICCON=(0x1<<9)|(0x1<<8)|0x69;// ACMD41为短应答并等待应答,开始发
//送ACMD41,命令卡发回OCR寄存器(保存了电压参数/busy信号等等),产生RSP3
//-- 检查ACMD41是否发送成功,并检查卡状态
if( Chk_CMDend(41, 1) & rSDIRSP0==0x80ff8000 ) //发送成功并且卡处于空闲状态
{ rSDICSTA=0xa00; //清除命令和应答结束标志位
return 1; //成功
} Delay(200); //等待卡上电后处于空闲状态
} //Uart_Printf("SDIRSP0=0x%x\n",rSDIRSP0);
rSDICSTA=0xa00; //清除命令和应答结束标志位
return 0; // 失败
} int CMD55(void) { //CMD55,表示下个命令将是特殊功能acmd,而非一般命令cmd
rSDICARG=RCA<<16; //CMD55(RCA,stuff bit)
rSDICCON=(0x1<<9)|(0x1<<8)|0x77; //CMD55为短应答(R1)并等待应答,并开始发送CMD55
//-- 检查CMD55是成功发送
if(!Chk_CMDend(55, 1)) return 0; //出错,返回
rSDICSTA=0xa00; ////清除命令和应答结束标志位
return 1; } int CMD13(void)//送CMD13,命令卡的当前状态
{ int response0; rSDICARG=RCA<<16;// CMD13(RCA,stuff bit),高16位为RCA
rSDICCON=(0x1<<9)|(0x1<<8)|0x4d;//CMD13为短应答(R1)并等待应答,并开始发送CMD13
//-- Check end of CMD13
if(!Chk_CMDend(13, 1)) //-- 检查CMD13是成功发送
return 0; //出错,返回
//Uart_Printf("rSDIRSP0=0x%x\n", rSDIRSP0);
if(rSDIRSP0&0x100) //数据是否就绪
//Uart_Printf("Ready for Data\n");
// else
//Uart_Printf("Not Ready\n");
response0=rSDIRSP0; response0 &= 0x3c00; response0 = response0 >> 9; //Uart_Printf("Current Status=%d\n", response0);
if(response0==6) //卡处于receive data state
Test_SDI(); rSDICSTA=0xa00; //清除命令和应答结束标志位
return 1; } int CMD9(void)//送CMD9,命令卡发回CSD寄存器(保存了读写参数/卡的容量等内容),产生RSP2
{ rSDICARG=RCA<<16; // CMD9参数,高16位为RCA
rSDICCON=(0x1<<10)|(0x1<<9)|(0x1<<8)|0x49; //CMD9为长应答并等待应答,开始发
//送CMD9,产生RSP2
Uart_Printf("\nCSD register :\n"); //-- Check end of CMD9
if(!Chk_CMDend(9, 1)) // 检查命令是否成功发送
return 0; //失败则返回
Uart_Printf("SDIRSP0=0x%x\nSDIRSP1=0x%x\nSDIRSP2=0x%x\nSDIRSP3=0x%x\n", rSDIRSP0,rSDIRSP1,rSDIRSP2,rSDIRSP3); return 1; } void Set_1bit_bus(void)//设置1位数据线
{ Wide=0; if(!MMC) SetBus(); //Uart_Printf("\n****1bit bus****\n");
} void Set_4bit_bus(void) //设置4位数据线
{ Wide=1; SetBus(); //Uart_Printf("\n****4bit bus****\n");
} void SetBus(void) //设置数据线宽度
{ SET_BUS: CMD55(); //CMD55,表示下个命令将是特殊功能acmd,而非一般命令cmd
rSDICARG=Wide<<1; //数据宽度00: 1bit, 10: 4bit
rSDICCON=(0x1<<9)|(0x1<<8)|0x46; //ACMD6为短应答(R1)并等待应答,并开始发送
// ACMD6设置数据线宽度位4bit
if(!Chk_CMDend(6, 1)) // 设置失败,则重新设置
goto SET_BUS; rSDICSTA=0xa00; //清除命令和应答结束标志位
} void Set_Prt(void)//写保护
{ //-- Set protection addr.0 ~ 262144(32*16*512)
Uart_Printf("[Set protection(addr.0 ~ 262144) test]\n"); RECMD28: //--Make ACMD
rSDICARG=0; // CMD28(addr)
rSDICCON=(0x1<<9)|(0x1<<8)|0x5c; //sht_resp, wait_resp, start, CMD28
//-- Check end of CMD28
if(!Chk_CMDend(28, 1)) goto RECMD28; rSDICSTA=0xa00; // Clear cmd_end(with rsp)
} void Clr_Prt(void)//清除写保护
{ //-- Clear protection addr.0 ~ 262144(32*16*512)
//Uart_Printf("[Clear protection(addr.0 ~ 262144) test]\n");
RECMD29: //--Make ACMD
rSDICARG=0; // CMD29(addr)
rSDICCON=(0x1<<9)|(0x1<<8)|0x5d; //sht_resp, wait_resp, start, CMD29
//-- Check end of CMD29
if(!Chk_CMDend(29, 1)) goto RECMD29; rSDICSTA=0xa00; // Clear cmd_end(with rsp)
}
|