📄 sd.c
字号:
while (dwTimeOut)
{
dwCause = sMcard->McSdIc;
if ((dwCause & IC_RSPF) || (dwCause & IC_WXF))
{
//MP_DEBUG1("-E- IC_RSPF or IC_WXF FAIL (status: %x)", dwCause);
return PROGRAM_FAIL;
}
else if (dwCause & IC_SDTO)
{
//MP_DEBUG1("-E- IC_SDTO (status: %x)", dwCause);
return WRITE_TIMEOUT;
}
else if (dwCause & IC_TREND)
{
return SD_PASS;
}
dwTimeOut--;
TASK_YIELD();
}
MP_DEBUG1("-E- WaitDataWrite FAIL (status: %x)", dwCause);
return GENERAL_FAIL;
}
static SWORD WaitBusy(void)
{
DWORD dwTimeOut;
register MCARD *mcard;
mcard = (MCARD *) (MCARD_BASE);
dwTimeOut = 10000;
OpenDrainDelay();
while (dwTimeOut)
{
if (mcard->McSdIc & IM_CRC16F)
{
OpenDrainDelay();
return SD_PASS;
}
else
{
dwTimeOut--;
}
}
MP_DEBUG1("-E- WaitBusy FAIL (status: %x)", mcard->McSdIc);
return WRITE_TIMEOUT;
}
static SWORD FlatRead(DWORD dwBufferAddress, DWORD dwSectorCount, DWORD dwLogAddr)
{
DWORD dwRetryTime;
SWORD swRetValue;
dwRetryTime = RETRY_TIME;
MP_DEBUG2("-I- SD FlatRead dwLogAddr %d,dwSectorCount %d",dwLogAddr,dwSectorCount);
while (dwRetryTime)
{
if (!(swRetValue = LogicalRead(dwBufferAddress, dwSectorCount, dwLogAddr)))
{
return SD_PASS;
}
dwRetryTime--;
MP_DEBUG1("-I- FlatRead remain retry times %d", dwRetryTime);
}
MP_DEBUG1("-E- FlatRead FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
static SWORD LogicalRead(DWORD dwBufferAddress, DWORD dwSectorCount, DWORD dwLogAddr)
{
register MCARD *sMcard;
register CHANNEL *sChannel;
DWORD dwTempDataBuf, dwTempBlkAddr, dwTempBlkCount, dwTemp;
SWORD swStatus;
if (!dwSectorCount)
{
return SD_PASS;
}
sMcard = (MCARD *) (MCARD_BASE);
dwTempDataBuf = dwBufferAddress;
dwTempBlkAddr = dwLogAddr;
if (dwSectorCount > 0x80)
{
dwTempBlkCount = 0x80;
}
else
{
dwTempBlkCount = dwSectorCount;
}
dwTemp = 0;
if (WaitTranState() != SD_PASS)
{
return GENERAL_FAIL;
}
SetBlockLen(MCARD_SECTOR_SIZE);
OpenDrainDelay();
WaitHostReady();
do
{
dwTemp += dwTempBlkCount;
SetDataDma(dwTempDataBuf, (dwTempBlkCount << MCARD_SECTOR_BIT_OFFSET),MCARD_DMA_DIR_CM);
if (WaitTranState() != SD_PASS)
{
return GENERAL_FAIL;
}
if (dwTempBlkCount > 1)
{
if(sInfo.bType == SDHC_TYPE)
SetCommand(CMD_READ_MULTIPLE_BLOCK, dwTempBlkAddr);
else
SetCommand(CMD_READ_MULTIPLE_BLOCK, (dwTempBlkAddr << MCARD_SECTOR_BIT_OFFSET));
}
else
{
if(sInfo.bType == SDHC_TYPE)
SetCommand(CMD_READ_SINGLE_BLOCK, dwTempBlkAddr );
else
SetCommand(CMD_READ_SINGLE_BLOCK, (dwTempBlkAddr << MCARD_SECTOR_BIT_OFFSET));
}
if ((swStatus = WaitDataRead()) != SD_PASS)
{
DmaReset();
if (dwTempBlkCount > 1)
{
SetCommand(CMD_STOP_TRANSMISSION, 0);
OpenDrainDelay();
}
if (swStatus == GENERAL_FAIL)
{
MP_DEBUG("-I- re init WaitDataRead");
OpenDrainDelay();
OpenDrainDelay();
GoTransferMode();
}
SetCommand(CMD_SELECT_CARD, 0);
OpenDrainDelay();
SetCommand(CMD_SELECT_CARD, sInfo.dwRelativeCardAddr);
OpenDrainDelay();
return swStatus;
}
WaitHostReady();
if (dwTempBlkCount > 1)
{
SetCommand(CMD_STOP_TRANSMISSION, 0);
OpenDrainDelay();
}
if ((dwSectorCount - dwTemp) > 0x80)
{
dwTempBlkCount = 0x80;
}
else
{
dwTempBlkCount = dwSectorCount - dwTemp;
}
dwTempDataBuf = dwBufferAddress + (dwTemp << MCARD_SECTOR_BIT_OFFSET);
dwTempBlkAddr = dwLogAddr + dwTemp;
}
while (dwTemp != dwSectorCount);
DmaReset();
return SD_PASS;
}
#pragma alignvar(4)
//BYTE bTestBuffer[1024];
static SWORD FlatWrite(DWORD dwBufferAddress, DWORD dwSectorCount, DWORD dwLogAddr)
{
DWORD dwRetryTime;
SWORD swRetValue;
DWORD dwTemp;
MP_DEBUG2("-I- SD FlatWrite dwLogAddr %d,dwSectorCount %d",dwLogAddr,dwSectorCount);
dwTemp = dwSectorCount;
dwRetryTime = RETRY_TIME;
while (dwRetryTime)
{
if (!(swRetValue = LogicalWrite(dwBufferAddress, dwSectorCount, dwLogAddr)))
{
if(!Ui_CheckUsbdPlugIn())
McardIODelay(500);
return SD_PASS;
}
dwRetryTime--;
MP_DEBUG1("-I- FlatWrite remain retry times %d", dwRetryTime);
}
MP_DEBUG1("-E- FlatWrite FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
static SWORD LogicalWrite(DWORD dwBufferAddress, DWORD dwSectorCount, DWORD dwLogAddr)
{
volatile DWORD dwTempDataBuf, dwTempBlkAddr, dwTempBlkCount, dwTemp;
SWORD swStatus;
if (dwSectorCount == 0)
{
return SD_PASS;
}
dwTempDataBuf = dwBufferAddress;
dwTempBlkAddr = dwLogAddr;
if (dwSectorCount > MULTI_W_SECTOR_NUM) // abel modify from 0x80 to 0x20 2006.03.06
{
dwTempBlkCount = MULTI_W_SECTOR_NUM; // abel modify from 0x80 to 0x20 2006.03.06
}
else
{
dwTempBlkCount = dwSectorCount;
}
dwTemp = 0;
if (WaitTranState() != SD_PASS)
{
return GENERAL_FAIL;
}
do
{
SetBlockLen(MCARD_SECTOR_SIZE);
if (WaitTranState() != SD_PASS)
{
return GENERAL_FAIL;
}
dwTemp += dwTempBlkCount;
SetDataDma(dwTempDataBuf, (dwTempBlkCount << MCARD_SECTOR_BIT_OFFSET), MCARD_DMA_DIR_MC);
if (dwTempBlkCount > 1)
{
if(sInfo.bType == SDHC_TYPE)
SetCommand(CMD_WRITE_MULTIPLE_BLOCK, dwTempBlkAddr );
else
SetCommand(CMD_WRITE_MULTIPLE_BLOCK, (dwTempBlkAddr << MCARD_SECTOR_BIT_OFFSET));
}
else
{
if(sInfo.bType == SDHC_TYPE)
SetCommand(CMD_WRITE_SINGLE_BLOCK, dwTempBlkAddr);
else
SetCommand(CMD_WRITE_SINGLE_BLOCK, (dwTempBlkAddr << MCARD_SECTOR_BIT_OFFSET));
}
if ((swStatus = WaitDataWrite()) != SD_PASS)
{
DmaReset();
if (dwTempBlkCount > 1)
{
SetCommand(CMD_STOP_TRANSMISSION, 0);
OpenDrainDelay();
}
if (swStatus == GENERAL_FAIL)
{
MP_DEBUG("-I- re init WaitDataWrite");
OpenDrainDelay();
OpenDrainDelay();
GoTransferMode();
}
SetCommand(CMD_SELECT_CARD, 0);
OpenDrainDelay();
SetCommand(CMD_SELECT_CARD, sInfo.dwRelativeCardAddr);
OpenDrainDelay();
return swStatus;
}
WaitHostReady();
if (dwTempBlkCount > 1)
{
SetCommand(CMD_STOP_TRANSMISSION, 0);
}
DmaReset();
OpenDrainDelay();
if ((dwSectorCount - dwTemp) > MULTI_W_SECTOR_NUM) // abel modify from 0x80 to 0x20 2006.03.06
{
dwTempBlkCount = MULTI_W_SECTOR_NUM; // abel modify from 0x80 to 0x20 2006.03.06
}
else
{
dwTempBlkCount = dwSectorCount - dwTemp;
}
dwTempDataBuf = dwBufferAddress + (dwTemp << MCARD_SECTOR_BIT_OFFSET);
dwTempBlkAddr = dwLogAddr + dwTemp;
}
while (dwTemp != dwSectorCount);
return SD_PASS;
}
static SWORD Format(void)
{
ST_DISK_PARAMETER sDiskParam;
DWORD i, dwSectorOffset;
SWORD swRetValue;
#pragma alignvar(4)
BYTE bTempBuffer[MCARD_SECTOR_SIZE];
BYTE *pbBuffer;
pbBuffer = (BYTE *) (((DWORD) (&bTempBuffer[0])) | 0xa0000000);
if (sInfo.bInitFlag == FALSE)
{
MP_DEBUG("-E- initial Sd FAIL");
return GENERAL_FAIL;
}
if (McardGetParameter(&sDiskParam, (sInfo.dwCapacity * MCARD_SECTOR_SIZE)))
{
MP_DEBUG("-E- Get Parameter FAIL");
return GENERAL_FAIL;
}
//create and check master boot record
memset(pbBuffer, 0, MCARD_SECTOR_SIZE);
McardMakeMbr(&sDiskParam, pbBuffer,0);
if ((swRetValue = FlatWrite((DWORD) pbBuffer, 1, 0)))
{
MP_DEBUG1("-E- write master boot record FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
memset(pbBuffer, 0, MCARD_SECTOR_SIZE);
if ((swRetValue = FlatRead((DWORD) pbBuffer, 1, 0)))
{
MP_DEBUG1("-E- Read master boot record FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
if (McardCheckMbr(&sDiskParam, pbBuffer,0))
{
MP_DEBUG("-E- Check master boot record FAIL");
return GENERAL_FAIL;
}
//create and check partition boot record
memset(pbBuffer, 0, MCARD_SECTOR_SIZE);
McardMakePbr(&sDiskParam, pbBuffer);
if ((swRetValue = FlatWrite((DWORD) pbBuffer, 1, sDiskParam.dwHiddenSectors)))
{
MP_DEBUG1("-E- write partition boot record FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
memset(pbBuffer, 0, MCARD_SECTOR_SIZE);
if ((swRetValue = FlatRead((DWORD) pbBuffer, 1, sDiskParam.dwHiddenSectors)))
{
MP_DEBUG1("-E- Read partition boot record FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
if (McardCheckPbr(&sDiskParam, pbBuffer))
{
MP_DEBUG1("-E- Check partition boot record FAIL (swRetValue: %d)", swRetValue);
return GENERAL_FAIL;
}
// initial fat table
memset(pbBuffer, 0, MCARD_SECTOR_SIZE);
pbBuffer[0] = 0xf8;
pbBuffer[1] = pbBuffer[2] = 0xff;
dwSectorOffset = (sDiskParam.dwLogStartSector + sDiskParam.wReserveSectors);
if (sDiskParam.bFatType == 16)
{
pbBuffer[3] = 0xff;
}
if ((swRetValue = FlatWrite((DWORD) pbBuffer, 1, dwSectorOffset)))
{
MP_DEBUG1("-E- write fat table 1(0) FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
if ((swRetValue = FlatWrite((DWORD) pbBuffer, 1, (dwSectorOffset + sDiskParam.wNumFatSectors))))
{
MP_DEBUG1("-E- write fat table 2(0) FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
pbBuffer[0] = pbBuffer[1] = pbBuffer[2] = pbBuffer[3] = 0x00;
for (i = 1; i < sDiskParam.wNumFatSectors; i++)
{
if ((swRetValue = FlatWrite((DWORD) pbBuffer, 1, (dwSectorOffset + i))))
{
MP_DEBUG2("-E- write fat table 1(%d) FAIL (swRetValue: %d)", i, swRetValue);
return swRetValue;
}
if ((swRetValue =
FlatWrite((DWORD) pbBuffer, 1, (dwSectorOffset + sDiskParam.wNumFatSectors + i))))
{
MP_DEBUG2("-E- write fat table 2(%d) FAIL (swRetValue: %d)", i, swRetValue);
return swRetValue;
}
}
// initial directory table
memset(pbBuffer, 0, MCARD_SECTOR_SIZE);
dwSectorOffset =
sDiskParam.dwLogStartSector + sDiskParam.wReserveSectors +
(sDiskParam.bNumFats * sDiskParam.wNumFatSectors);
// 512x32/512=32 Sectors(so Root Directory occupy 32 Sectors)
for (i = 0; i < (sDiskParam.wRootEntries * 32 / sDiskParam.wSectorSize); i++)
{
if ((swRetValue = FlatWrite((DWORD) pbBuffer, 1, (dwSectorOffset + i))))
{
MP_DEBUG2("-E- write directory table %d FAIL (swRetValue: %d)", i, swRetValue);
return swRetValue;
}
}
return SD_PASS;
}
SWORD SD_READ_WRITE_COMPARE (void)
{
WORD i,j,loopcount = 1,pagecount=1,verifycount;
BYTE bWriteBuffer[512*pagecount];
BYTE bReadBuffer[512*pagecount];
BYTE *pbWriteBuffer,*pbReadBuffer,*pbWriteBufferTemp,*pbReadBufferTemp;
pbWriteBuffer = (BYTE *) (((DWORD) (&bWriteBuffer) | 0xa0000000));
pbReadBuffer = (BYTE *) (((DWORD) (&bReadBuffer) | 0xa0000000));
SWORD swRetValue;
// Read_Flag =1;
while(loopcount--)
{
// memset(pbWriteBuffer,0,512);
for(j=0;j<pagecount;j++)
{
for(i=0;i<512;i++)
{
// bWriteBuffer[i+j*512]=j;
pbWriteBuffer[i+j*512]=0x5a;
}
}
pbWriteBufferTemp = pbWriteBuffer;
pbReadBufferTemp = pbReadBuffer;
UartOutText("start flat write\r\n");
if ((swRetValue = FlatWrite((DWORD) pbWriteBuffer, pagecount, 500)))
{
UartOutText("flat write fail\r\n");
MP_DEBUG1("-E- write master boot record FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
UartOutText("flat write ok\r\n");
// bTempMemID = OsTempMemAllocate();
// buffer = (DWORD) GetOsTempMemory(bTempMemID); //(&FSBuffer[0]); //OsTempMemAllocate();
UartOutText("start flat read\r\n");
if ((swRetValue = FlatRead((DWORD) pbReadBuffer, pagecount, 500)))
{
UartOutText("flat read fail\r\n");
MP_DEBUG1("-E- write master boot record FAIL (swRetValue: %d)", swRetValue);
return swRetValue;
}
UartOutText("flat read pass\r\n");
verifycount = pagecount*512/4;
while(verifycount--) // 1
{
if(*pbWriteBufferTemp != *pbReadBufferTemp)
{
// mpDebugPrint("-E- pbWriteBufferTemp : %d)", *pbWriteBufferTemp);
// mpDebugPrint("-E- pbReadBufferTemp : %d)", *pbReadBufferTemp);
// UartOutValue(*pbWriteBufferTemp, 8);
// UartOutValue(*pbReadBufferTemp, 8);
// mpDebugPrint("-E- verifycount : %d)", verifycount);
UartOutText("compare 1 fail\r\n");
break;
}
pbReadBufferTemp++;
pbWriteBufferTemp++;
}
memset(pbWriteBuffer,0,512*pagecount);
memset(pbReadBuffer,0,512*pagecount);
}
UartOutText("flat write & read 16 times pass\r\n");
// Read_Flag = 0;
//while(1);
}
void Set_Fn0_BlkSize(void)
{
}
#endif //#if SD_MMC_ENABLE //byAlexWang 24jun2007 m2project
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -