📄 sd old.c
字号:
dwTempDataBuf = dwBufferAddress;
dwTempBlkAddr = dwLogAddr;
if (dwSectorCount > 0x80)
{
dwTempBlkCount = 0x80;
}
else
{
dwTempBlkCount = dwSectorCount;
}
dwTemp = 0;
//SelSdCard();
//OpenDrainDelay();
//WaitHostReady();
if (WaitTranState() != SD_PASS)
{
return GENERAL_FAIL;
}
SetBlockLen(MCARD_SECTOR_SIZE);
OpenDrainDelay();
WaitHostReady();
do
{
dwTemp += dwTempBlkCount;
#ifdef ENABLE_CHACK_TACC_MECHANISM
if (sInfo.bMultiReadFlag)
{
#endif
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));
}
#ifndef SD_TIMEOUT_HANDLE
WaitResponse();
#endif
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();
OpenDrainDelay();
if (dwTempBlkCount > 1)
{
SetCommand(CMD_STOP_TRANSMISSION, 0);
OpenDrainDelay();
}
WaitHostReady();
#ifdef ENABLE_CHACK_TACC_MECHANISM
}
else
{
while (dwTempBlkCount)
{
SetDataDma(dwTempDataBuf, (dwTempBlkCount << MCARD_SECTOR_BIT_OFFSET),
MCARD_DMA_DIR_CM);
if (WaitTranState() != SD_PASS)
{
return GENERAL_FAIL;
}
SetCommand(CMD_READ_SINGLE_BLOCK, (dwTempBlkAddr << MCARD_SECTOR_BIT_OFFSET));
dwTempBlkCount--;
dwTempBlkAddr++;
dwTempDataBuf += MCARD_SECTOR_SIZE;
WaitResponse();
if ((swStatus = WaitDataRead()) != SD_PASS)
{
DmaReset();
SetCommand(CMD_SELECT_CARD, 0);
OpenDrainDelay();
SetCommand(CMD_SELECT_CARD, sInfo.dwRelativeCardAddr);
OpenDrainDelay();
return swStatus;
}
WaitHostReady();
DmaReset();
OpenDrainDelay();
}
}
#endif
DmaReset();
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();
OpenDrainDelay();
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 (!(swRetValue = LogicalRead((DWORD)(&bTestBuffer[0]), 2, 0))) //abel add for sandDisk 1.0G SD card
return SD_PASS; // if read 4-> write 4 -> read 1 -> write 1
// else // then chang to access CF, low 4bit will be 0x0e
// return swRetValue;
}
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;
BYTE MULTI_W_SECTOR;
register GPIO *sGpio;
sGpio = (GPIO *) (GPIO_BASE);
register MCARD *sMcard;
sMcard = (MCARD *) (MCARD_BASE);
DWORD dwRetryCount;
if (dwSectorCount == 0)
{
return SD_PASS;
}
// MP_DEBUG2("-I- SD FlatWrite ,dwLogAddr %d,dwSectorCount %d",dwLogAddr,dwSectorCount);
/*
if(bSlow_Card == 1)
{
MP_DEBUG("Single Write");
MULTI_W_SECTOR = 1;
}
else
{
MP_DEBUG("Single Write");
MULTI_W_SECTOR = MULTI_W_SECTOR_NUM;
}
*/
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;
//SelSdCard();
//OpenDrainDelay();
//WaitHostReady();
if (WaitTranState() != SD_PASS)
{
return GENERAL_FAIL;
}
SetBlockLen(MCARD_SECTOR_SIZE);
OpenDrainDelay();
WaitHostReady();
do
{
// SetBlockLen(MCARD_SECTOR_SIZE);
// OpenDrainDelay();
// WaitHostReady();
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));
}
#ifndef SD_TIMEOUT_HANDLE
OpenDrainDelay();
WaitResponse();
#endif
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;
}
WaitChannelStop();
WaitHostReady();
OpenDrainDelay();
if (dwTempBlkCount > 1)
{
SetCommand(CMD_STOP_TRANSMISSION, 0);
OpenDrainDelay();
WaitHostReady(); //abel add
OpenDrainDelay(); //abel add
}
DmaReset();
OpenDrainDelay();
#if 0
// abel add for Transcend 512 SD card, the last page can not write into
// so write the last page again by single write command. 2006.03.09
if (WaitTranState() != SD_PASS)
{
return GENERAL_FAIL;
}
dwTempDataBuf = dwBufferAddress + ((dwTemp - 1) << MCARD_SECTOR_BIT_OFFSET);
dwTempBlkAddr = dwLogAddr + dwTemp - 1;
SetDataDma(dwTempDataBuf, (1 << MCARD_SECTOR_BIT_OFFSET), MCARD_DMA_DIR_MC);
SetCommand(CMD_WRITE_SINGLE_BLOCK, (dwTempBlkAddr << MCARD_SECTOR_BIT_OFFSET));
#ifndef SD_TIMEOUT_HANDLE
OpenDrainDelay();
WaitResponse();
#endif
if ((swStatus = WaitDataWrite()) != SD_PASS)
{
DmaReset();
SetCommand(CMD_SELECT_CARD, 0);
OpenDrainDelay();
return swStatus;
}
WaitChannelStop();
WaitHostReady();
OpenDrainDelay();
DmaReset();
OpenDrainDelay();
// end abel add 2006.03.09
#endif
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);
SetCommand(CMD_SELECT_CARD, 0);
OpenDrainDelay();
WaitHostReady(); //abel add
OpenDrainDelay(); //abel add
DmaReset();
OpenDrainDelay();
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 + -