📄 sd.c
字号:
while (!(mmcReg->MMC_STAT & XLLP_STAT_ENDCMDRES)); //wait intrupt
goto DataTrans;
DataTrans:
// EdbgOutputDebugString("DataTrans\r\n");
XllpMmcSdInts(mmcReg,XLLP_MMC_I_MASK_DATATRANSDONE);
// while (!(mmcReg->MMC_STAT & XLLP_STAT_DATATRANDONE));
if (MMC->DATATRANS == WRITE)
{
goto ProgDone;
}
else
{
goto DataTransDone;
}
ProgDone:
// EdbgOutputDebugString("ProgDone\r\n");
XllpMmcSdInts(mmcReg,XLLP_MMC_I_MASK_PRGDONE);
// while (!(mmcReg->MMC_STAT & XLLP_STAT_PRGDONE));
goto DataTransDone;
DataTransDone:
// EdbgOutputDebugString("DataTransDone\r\n");
if (MMC->NOB > 1)
{
goto MBStopClock;
}
else
{
goto ExtractResponse;
}
MBStopClock:
XllpMmcSdInts(mmcReg,XLLP_MMC_I_MASK_CLKISOFF);
MMC->CMD = XLLP_MMC_CMD12;
MMC->ADDR = 0;
XllpMmcSdInts(mmcReg,XLLP_MMC_I_MASK_ENDCMDRES);
XllpMmcSdSetupCmd(mmcReg, MMC->CMD, MMC->ADDR, 64,0);
while (!(mmcReg->MMC_STAT & XLLP_STAT_ENDCMDRES));
goto MBProgDone;
MBProgDone:
XllpMmcSdInts(mmcReg,XLLP_MMC_I_MASK_PRGDONE);
while (!(mmcReg->MMC_STAT & XLLP_STAT_PRGDONE));
goto ExtractResponse;
//EdbgOutputDebugString("ExtractResponse\r\n");
ExtractResponse:
if((mmcReg->MMC_CMDAT & 0x3) == XLLP_MMC_MMCSPIR2)
{
XllpMmcXtractResp(mmcReg, res, XLLP_MMC_MMCSPIR2);
statusRes.lresponse[3] = parseResponse(&res[5], 1);
statusRes.lresponse[2] = parseResponse(&res[3], 1);
statusRes.lresponse[1] = parseResponse(&res[1], 1);
statusRes.lresponse[0] = parseResponse(&res[0], 0);
}
else
{
XllpMmcXtractResp(mmcReg, res, XLLP_MMC_MMCSPIR1);
statusRes.sresponse = parseResponse(&res[0], 1);
}
end:
statusRes.status = checkStatus(mmcReg,statusRes.sresponse);
return(statusRes);
}
static UINT32 MMCSDStatusGet(MMCSDAttribT *mmcsdAttribT)
{
STATRES mmcsdStatus;
mmcStack MMCSTK;
MMCSTK.FSIZE = 0;
MMCSTK.BSIZE = 0;
MMCSTK.RATE = 0;
MMCSTK.NOB = 0;
MMCSTK.BUSY= 0;
MMCSTK.READSCR = 0;
MMCSTK.ADDR = mmcsdAttribT->rca;
MMCSTK.DATATRANS=DNONE;
MMCSTK.CMD = XLLP_MMC_SPICMD13; //get the addressed card status register
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
return(mmcsdStatus.sresponse);
}
int retrytime = 0;
BOOL SetSDHW()
{
UINT32 C_SIZE;
UINT32 READ_BL_LEN;
UINT32 C_SIZE_MULT;
UINT32 SIZE;
MMCSDAttribT mmcsdAttribT;
UINT32 ret;
INT SDBusWidth;
STATRES mmcsdStatus;
INT size=0;
mmcStack MMCSTK;
int i;
// EdbgOutputDebugString("Now start SD card test\r\n");
pc5000_init_sd_hardware();
// EdbgOutputDebugString("init sd hardware end\r\n");
MMCSTK.NOB = 1;
MMCSTK.FSIZE = SO_BLOCK;
// MMCSTK.RATE = XLLP_MMC_304KHz;
MMCSTK.RATE = XLLP_MMC_19_5MHz;
MMCSTK.BSIZE = SO_BLOCK;
MMCSTK.BUSY = XLLP_TRUE;
MMCSTK.READSCR = XLLP_TRUE;
MMCSTK.DATATRANS = DNONE;
mmcsdAttribT.rca = RCA_MMC_CARD;
MMCSTK.DATATRANS=DNONE;
MMCSTK.CMD = XLLP_MMC_SPICMD0;
MMCSTK.ADDR = 0x0;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
ret=checkStatus(v_pMMCReg,mmcsdStatus.status);
// EdbgOutputDebugString("status:0x%x\r\n",ret);
MMCSTK.CMD = XLLP_MMC_SPICMD1;
MMCSTK.ADDR = CMD1_ADDR;
MMCSTK.DATATRANS=DNONE;
// EdbgOutputDebugString("start mmcProcessCMD start\r\n");
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
// EdbgOutputDebugString("start mmcProcessCMD end\r\n");
ret=checkStatus(v_pMMCReg,mmcsdStatus.status);
// EdbgOutputDebugString("status:0x%x\r\n",ret);
mmcsdAttribT.OCR = 0x0;
if( (mmcsdStatus.status & XLLP_STAT_TORSPNS) == XLLP_STAT_TORSPNS)
{
mmcsdAttribT.mmc = XLLP_FALSE;
// EdbgOutputDebugString("mmcsdAttribT.mmc is false\r\n");
goto SendAppCMD;
}
if (!(mmcsdStatus.sresponse & MMCSD_CARD_OCR_nBUSY)) // Card power up status bit (!busy)
{
// EdbgOutputDebugString(" Card power up status bit\r\n");
goto SendCMD1;
}
else
{
MMCSTK.BUSY = XLLP_FALSE;
// EdbgOutputDebugString("MMCSTK.BUSY is XLLP_FALSE\r\n");
goto SendCMD2;
}
SendCMD1:
// EdbgOutputDebugString("SendCMD1\r\n");
MMCSTK.CMD = XLLP_MMC_SPICMD1;
MMCSTK.ADDR = CMD1_ADDR;
MMCSTK.DATATRANS=DNONE;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
mmcsdAttribT.OCR = mmcsdStatus.sresponse;
if (mmcsdAttribT.OCR & MMCSD_CARD_OCR_nBUSY) // Card power up status bit (!busy)
{
mmcsdAttribT.mmc = XLLP_TRUE;
MMCSTK.BUSY = XLLP_FALSE;
// EdbgOutputDebugString("ok card power up\r\n");
goto SendCMD2;
}
else
{
goto SendCMD1;
}
SendAppCMD:
//EdbgOutputDebugString("SendAppCMD\r\n");
MMCSTK.CMD = XLLP_MMC_SPICMD55;
if (mmcsdAttribT.OCR & MMCSD_CARD_OCR_nBUSY) // Card power up status bit (!busy)
MMCSTK.ADDR = mmcsdAttribT.rca;
else
MMCSTK.ADDR = 0x0;
MMCSTK.DATATRANS=DNONE;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
// EdbgOutputDebugString("if (mmcsdAttribT.OCR & MMCSD_CARD_OCR_nBUSY)\r\n");
if(SDBusWidth == 4)
goto SendACMD6;
if (mmcsdAttribT.OCR & MMCSD_CARD_OCR_nBUSY) // Card power up status bit (!busy)
goto SendACMD51;
else
goto SendCMD41;
SendCMD41:
if(retrytime >= 0x3000)
{
retrytime = 0x0;
EdbgOutputDebugString("Time Out!\r\n");
return FALSE;
}
// EdbgOutputDebugString("SendCMD41\r\n");
MMCSTK.ADDR = 0x200000;
MMCSTK.BSIZE = SO_SCR;
MMCSTK.CMD = XLLP_SD_ACMD41;
MMCSTK.DATATRANS = DNONE;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
mmcsdAttribT.OCR = mmcsdStatus.sresponse;
if (mmcsdAttribT.OCR & MMCSD_CARD_OCR_nBUSY) // Card power up status bit (!busy)
{
retrytime = 0;
MMCSTK.BUSY = XLLP_FALSE;
goto SendCMD2;
}
else
{
retrytime ++;
goto SendAppCMD;
}
SendCMD2:
// EdbgOutputDebugString("SendCMD2\r\n");
MMCSTK.CMD = XLLP_MMC_CMD2;
MMCSTK.ADDR = 0x000;
MMCSTK.DATATRANS=DNONE;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
for(size=0; size<4; size++)
{
XsSD_CID.qb[size] = mmcsdStatus.lresponse[size];
}
// mmcsdAttribT.mmc_cid = &XsMMC_CID.mmc_cid;
goto SendCMD3;
SendCMD3:
//EdbgOutputDebugString("SendCMD3\r\n");
for(i=0;i<2;i++)
{
MMCSTK.DATATRANS=DNONE;
MMCSTK.CMD = XLLP_MMC_CMD3;
MMCSTK.ADDR = mmcsdAttribT.rca;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
if(!mmcsdAttribT.mmc)
{
mmcsdAttribT.rca = (mmcsdStatus.sresponse & 0xffff0000) ;
//EdbgOutputDebugString("mmcsdAttribT.rca1: 0x%x\r\n",mmcsdAttribT.rca);
}
}
goto EndInitClock;
EndInitClock:
goto SendCMD9;
SendCMD7: //select card
// EdbgOutputDebugString("SendCMD7\r\n");
MMCSTK.CMD = XLLP_MMC_CMD7;
MMCSTK.ADDR = mmcsdAttribT.rca;
MMCSTK.DATATRANS=DNONE;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
goto SendCMD16;
SendCMD9: //to get CSD
// EdbgOutputDebugString("SendCMD9\r\n");
MMCSTK.CMD = XLLP_MMC_SPICMD9;
MMCSTK.ADDR = mmcsdAttribT.rca;
MMCSTK.DATATRANS=DNONE;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
for(size=0; size<4; size++ )
{
XsMMC_CSD.qb[size] = mmcsdStatus.lresponse[size];
}
mmcsdAttribT.mmc_csd = &XsMMC_CSD.mmc_csd; //共用体,注意此用法
C_SIZE = ((XsMMC_CSD.qb[1] & 0xC0000000)>>30)+((XsMMC_CSD.qb[2] & 0x000003FF)<<2);
C_SIZE_MULT = (XsMMC_CSD.qb[1] & 0x00038000)>>15;
READ_BL_LEN = (XsMMC_CSD.qb[2] & 0x000F0000)>>16;
SIZE = (C_SIZE+1)*(1<<(C_SIZE_MULT+2))*(1<<READ_BL_LEN);
EdbgOutputDebugString("SD CARD SIZE: %d MB\r\n",SIZE/1024/1024);
EdbgOutputDebugString("TRANSPEED: %d \r\n",GetSpeed(mmcsdAttribT.mmc_csd->TRAN_SPEED));
goto SendCMD7;
SendCMD16: //set block
// EdbgOutputDebugString("SendCMD16\r\n");
MMCSTK.CMD = XLLP_MMC_SPICMD16;
if(MMCSTK.READSCR && !mmcsdAttribT.mmc) //not mmc card and will read scr
{
MMCSTK.BSIZE = SO_SCR; // set BLK=0x8
}
else
{
MMCSTK.BSIZE = SO_BLOCK; // set BLK=0x200
}
MMCSTK.ADDR = MMCSTK.BSIZE; // the arg is block length
MMCSTK.DATATRANS=DNONE;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
if((mmcsdAttribT.mmc) || !(MMCSTK.READSCR))
goto SendCMD17;
else
goto SendAppCMD;
SendACMD6: //configue the bus width
// EdbgOutputDebugString("SendACMD6\r\n");
MMCSTK.CMD = XLLP_SD_ACMD6;
MMCSTK.ADDR = 0x10;
MMCSTK.DATATRANS = DNONE;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
goto SendCMD16;
SendACMD51: //read the sd configuration register
// EdbgOutputDebugString("SendACMD51\r\n");
MMCSTK.CMD = XLLP_SD_ACMD51;
MMCSTK.ADDR = mmcsdAttribT.rca; //stuff bits
MMCSTK.FSIZE = SO_SCR;
MMCSTK.BSIZE = SO_SCR;
MMCSTK.DATATRANS = READ;
MMCSTK.READSCR = 0;
MMCSTK.NOB = 1;
// rxRead = 0;
getBuf = (UINT32*)&mmcsdAttribT.sd_scr;
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
EdbgOutputDebugString("SCR: 0x%x\r\n",(mmcsdAttribT.sd_scr)->SD_BUS_WIDTH);
if((mmcsdAttribT.sd_scr)->SD_BUS_WIDTH == 0x5)
{
SDBusWidth = 4;
goto SendAppCMD;
}
else
goto SendCMD16;
//end:
SendCMD17:
return 1;
}
ULONG ATAReadSectors(
ULONG Sector,
ULONG Count,
PUCHAR SectorBuf)
{
UINT8 databuf[512];
STATRES mmcsdStatus;
mmcStack MMCSTK;
int j,i;
UCHAR *pBuf = databuf;
UCHAR *psBuf = SectorBuf;
volatile UCHAR *pMMC_RX_Fifo = (volatile UCHAR *)&v_pMMCReg->MB_RXFIFO;
//EdbgOutputDebugString("SendCMD17\r\n");
MMCSTK.CMD = XLLP_MMC_SPICMD17;
// MMCSTK.CMD = XLLP_MMC_CMD18;
MMCSTK.ADDR =512*Sector;
MMCSTK.FSIZE = SO_BLOCK;
MMCSTK.BSIZE = SO_BLOCK;
MMCSTK.DATATRANS = READ;
MMCSTK.NOB = 1; //block number
mmcsdStatus = mmcProcessCmd(v_pMMCReg, &MMCSTK);
pBuf = databuf;
for(j=0;j<16;)
{
//if(!(v_pMMCReg->MMC_STAT & XLLP_STAT_DATATRANDONE))
// EdbgOutputDebugString ( "\r\nDATATRANing!!\r\n");
// msWait(1);
//while(!(v_pMMCReg->MMC_I_REG & XLLP_MMC_I_REG_RXFIFORDREQ));
if((v_pMMCReg->MMC_I_REG & XLLP_MMC_I_REG_RXFIFORDREQ) == XLLP_MMC_I_REG_RXFIFORDREQ)
{
/* *(UINT32 *) pBuf = (UINT32)v_pMMCReg->MB_RXFIFO;
*(UINT32 *)(pBuf + 4) = (UINT32)v_pMMCReg->MB_RXFIFO;
*(UINT32 *)(pBuf + 8) = (UINT32)v_pMMCReg->MB_RXFIFO;
*(UINT32 *)(pBuf + 0xc) = (UINT32)v_pMMCReg->MB_RXFIFO;
*(UINT32 *)(pBuf + 0x10)= (UINT32)v_pMMCReg->MB_RXFIFO;
*(UINT32 *)(pBuf + 0x14)= (UINT32)v_pMMCReg->MB_RXFIFO;
*(UINT32 *)(pBuf + 0x18)= (UINT32)v_pMMCReg->MB_RXFIFO;
*(UINT32 *)(pBuf + 0x1C)= (UINT32)v_pMMCReg->MB_RXFIFO;
pBuf += 32;
j++;*/
for(i=0;i<32;i++)
*pBuf++ = *pMMC_RX_Fifo;
j++;
}
}
memcpy(psBuf,databuf,512);
while (!(v_pMMCReg->MMC_STAT & XLLP_STAT_DATATRANDONE));
//EdbgOutputDebugString ("\r\nDATATRANDONE!\r\n");
return(0);
}
ULONG CFATAReadSectors(
ULONG Sector,
ULONG Count,
PUCHAR SectorBuf,
ULONG PAtoLA)
{
// int flag;
PUCHAR pSec = SectorBuf;
// EdbgOutputDebugString ("+CFATAReadSectors\r\n");
// EdbgOutputDebugString ("Sectors :%d\r\n",Sector);
// if(Sector == 1)
// flag = 1;
Sector+=PAtoLA;
while(Count--)
{
ATAReadSectors(Sector,Count,pSec);
pSec+=512;
Sector+=1;
// if(flag == 1)
// EdbgOutputDebugString ("Count :%d\r\n",Count);
}
// EdbgOutputDebugString ("-CFATAReadSectors\r\n");
return(0);
}
void SDPowerOff(void)
{
XllpMmcSdHwShutdown(v_pCLKReg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -