📄 sdcontrol.cpp
字号:
}
if ((remainder%4)==2)
{
tempData = ((tempBuff[0]) | (tempBuff[1] << 8));
OUTREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS, tempData);
byteCount+=2; //count the bytes written
}
if ((remainder%4)==3)
{
tempData = (tempBuff[0] | (tempBuff[1]<<8) | (tempBuff[2]<<16));
OUTREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS, tempData);
byteCount+=3; //count the bytes written
}
}
//Print the bytes no of bytes written
DEBUGMSG(ZONE_INFO, (TEXT("m_dwNumBytesWritten:%d \r\n"), byteCount));
}
return FALSE;
}
else if (pRequest->TransferClass == SD_READ)
{
DWORD m_dwNumBytesToTransfer ;
m_dwNumBytesToTransfer = pRequest->NumBlocks * pRequest->BlockSize;
pController->SDCommandStatus = SD_COMMAND_STATUS_READ;
//Check whether DMA or CPU transfer
if (pController->fDMATransfer == TRUE)
{
DDKDmacSetTransCount(pController->DmaReqRxCH, m_dwNumBytesToTransfer);
// Start the output DMA
DDKDmacStartChan(pController->DmaReqRxCH);
}
else
{
BYTE* tempBuff =pRequest->pBlockBuffer;
DWORD no_of_empty_fifo, j ,k,remainder, tempData;
DWORD byteCount = 0; //byte count
no_of_empty_fifo = (m_dwNumBytesToTransfer)/(pController->Bytes_in_fifo);
//remaining bytes which are less than FIFO size
remainder = (m_dwNumBytesToTransfer)-(pController->Bytes_in_fifo)*no_of_empty_fifo;
DEBUGMSG(ZONE_INFO, (TEXT("m_dwNumBytesToTransfer:%d Bytes_in_fifo:%d no_of_empty_fifo:%d\r\n")
,m_dwNumBytesToTransfer, (pController->Bytes_in_fifo), no_of_empty_fifo));
for (j=0; j<no_of_empty_fifo; j++)
{
//Wait till buffer ready or read time out
while (!EXTREG32BF(&pController->pSDMMCRegisters->STATUS, SDHC_SR_BRR) &&
!EXTREG32BF(&pController->pSDMMCRegisters->STATUS, SDHC_SR_TOREAD) &&
!EXTREG32BF(&pController->pSDMMCRegisters->STATUS, SDHC_SR_RODONE)) Sleep(0);
//A Read Time Out can happen only in the begining
if (EXTREG32BF(&pController->pSDMMCRegisters->STATUS, SDHC_SR_TOREAD))
{
//0 bytes read.
DEBUGMSG(ZONE_INFO, (TEXT("m_dwNumBytesRead:%d \r\n"), byteCount));
//TODO: why FALSE???
return FALSE;
}
for (k=0; k<pController->Units_in_fifo; k++)
{
tempData = INREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS);
tempBuff[0] = (BYTE)tempData;
tempBuff[1] = (BYTE)(tempData >> 8);
tempBuff[2] = (BYTE)(tempData >> 16);
tempBuff[3] = (BYTE)(tempData >> 24);
tempBuff+=4 ;
byteCount+=4; //count the bytes written
}
}
if (remainder)
{
while (!EXTREG32BF(&pController->pSDMMCRegisters->STATUS, SDHC_SR_BRR) &&
!EXTREG32BF(&pController->pSDMMCRegisters->STATUS, SDHC_SR_RODONE)) Sleep(0);
for (j=0;j<remainder/4;j++)
{
tempData = INREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS);
tempBuff[0] = (BYTE)tempData;
tempBuff[1] = (BYTE)(tempData >> 8);
tempBuff[2] = (BYTE)(tempData >> 16);
tempBuff[3] = (BYTE)(tempData >> 24);
tempBuff+=4 ;
byteCount+=4; //count the bytes written
}
if ((remainder%4)==1)
{
tempData = (BYTE)INREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS);
tempBuff[0] = (BYTE)tempData;
byteCount++; //count the bytes written
}
if ((remainder%4)==2)
{
tempData = (WORD)INREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS);
tempBuff[0] = (BYTE)tempData;
tempBuff[1] = (BYTE)(tempData >> 8);
byteCount+=2; //count the bytes written
}
if ((remainder%4)==3)
{
tempData = INREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS);
tempBuff[0] = (BYTE)tempData;
tempBuff[1] = (BYTE)(tempData >> 8);
tempBuff[2] = (BYTE)(tempData >> 16);
byteCount+=3; //count the bytes written
}
}
//Print the bytes no of bytes written
DEBUGMSG(ZONE_INFO, (TEXT("m_dwNumBytesRead:%d \r\n"), byteCount));
}
return FALSE;
}
}
DEBUGMSG(ZONE_FUNCTION, (TEXT("HandleCommandComplete()- request must have been canceled due to an error!\r\n")));
return TRUE;
}
//------------------------------------------------------------------------------
//
// Function: HandleTransferDone
//
// Handles an data transfer done event
//
// Parameters:
// pController - hardware context
//
// Returns:
// returns TRUE is command is completed. FALSE otherwise
//
//------------------------------------------------------------------------------
static BOOL HandleTransferDone(PSDH_HARDWARE_CONTEXT pController)
{
PSD_BUS_REQUEST pRequest;
SD_API_STATUS status = SD_API_STATUS_SUCCESS;
BOOL fRet = FALSE;
DEBUGMSG(ZONE_FUNCTION, (TEXT("+HandleTransferDone\r\n")));
pRequest = SDHCDGetAndLockCurrentRequest(pController->pHCContext, 0);
if (pRequest)
{
if (pRequest->TransferClass == SD_READ)
{
DWORD m_dwNumBytesToTransfer;
DWORD DmaTransferedData, DmaRemainderData = 0;
m_dwNumBytesToTransfer = pRequest->NumBlocks * pRequest->BlockSize;
//Check whether DMA or CPU transfer
if (pController->fDMATransfer == TRUE)
{
// Kill the output DMA channel
DDKDmacStopChan(pController->DmaReqRxCH);
// During read operations, the SDHC generates DMA requests if one of its data buffer is full.
//
DmaTransferedData = DDKDmacGetTransSize(pController->DmaReqRxCH);
DEBUGMSG(ZONE_DMA, (TEXT("DMA Transfer Data count 0x%X, total data count 0x%X\r\n"), DmaTransferedData, m_dwNumBytesToTransfer));
if(DmaTransferedData < m_dwNumBytesToTransfer)
DmaRemainderData = m_dwNumBytesToTransfer -DmaTransferedData;
if(DmaRemainderData > 0)
{
DWORD k, tempData;
BYTE* tempBuff = pController->DmaLogicalAddressRX + DmaTransferedData;
DWORD byteCount = 0; //byte count
for (k=0; k<(DmaRemainderData/4); k++)
{
tempData = INREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS);
tempBuff[0] = (BYTE)tempData;
tempBuff[1] = (BYTE)(tempData >> 8);
tempBuff[2] = (BYTE)(tempData >> 16);
tempBuff[3] = (BYTE)(tempData >> 24);
tempBuff+=4 ;
byteCount+=4; //count the bytes written
}
if ((DmaRemainderData%4)==1)
{
tempData = (BYTE)INREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS);
tempBuff[0] = (BYTE)tempData;
byteCount++; //count the bytes written
}
if ((DmaRemainderData%4)==2)
{
tempData = (WORD)INREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS);
tempBuff[0] = (BYTE)tempData;
tempBuff[1] = (BYTE)(tempData >> 8);
byteCount+=2; //count the bytes written
}
if ((DmaRemainderData%4)==3)
{
tempData = INREG32(&pController->pSDMMCRegisters->BUFFER_ACCESS);
tempBuff[0] = (BYTE)tempData;
tempBuff[1] = (BYTE)(tempData >> 8);
tempBuff[2] = (BYTE)(tempData >> 16);
byteCount+=3; //count the bytes written
}
}
}
else
{
//............nothing to do...................
}
// if read crc error
if (EXTREG32BF(&pController->pSDMMCRegisters->STATUS, SDHC_SR_RCERR))
{
DEBUGMSG(ZONE_ERROR, (TEXT("Cmd %d: Read CRC Error! Arg 0x%08X Status 0x%08X\r\n"),
pRequest->CommandCode, pRequest->CommandArgument,
INREG32(&pController->pSDMMCRegisters->STATUS)));
//Clear status by writing 1
OUTREG32(&pController->pSDMMCRegisters->STATUS, CSP_BITFVAL(SDHC_SR_RCERR, 1));
status = SD_API_STATUS_CRC_ERROR;
goto _done;
}
// if Read Time Out
if (EXTREG32BF(&pController->pSDMMCRegisters->STATUS, SDHC_SR_TOREAD))
{
DEBUGMSG(ZONE_ERROR, (TEXT("Cmd %d: Read Time out! Arg 0x%08X Status 0x%08X\r\n"),
pRequest->CommandCode, pRequest->CommandArgument,
INREG32(&pController->pSDMMCRegisters->STATUS)));
//Clear status by writing 1
OUTREG32(&pController->pSDMMCRegisters->STATUS, CSP_BITFVAL(SDHC_SR_TOREAD, 1));
status = SD_API_STATUS_DATA_TIMEOUT;
goto _done;
}
//Check whether DMA or CPU transfer
if (pController->fDMATransfer == TRUE)
{
BOOL fNoException;
fNoException = SDPerformSafeCopy( pRequest->pBlockBuffer, (void*) pController->DmaLogicalAddressRX, m_dwNumBytesToTransfer );
if (fNoException == FALSE)
{
ERRORMSG(ZONE_ERROR, (TEXT("SD_API_STATUS_ACCESS_VIOLATION\r\n")));
status = SD_API_STATUS_ACCESS_VIOLATION;
goto _done;
}
}
fRet = TRUE;
}
else if (pRequest->TransferClass == SD_WRITE)
{
DWORD m_dwNumBytesToTransfer ;
m_dwNumBytesToTransfer = pRequest->NumBlocks * pRequest->BlockSize;
//Check whether DMA or CPU transfer
if (pController->fDMATransfer == TRUE)
{
//Query DMA transfer status DMAC_TRANSFER_STATUS_NONE DMA indicates that the DMA cycle is ongoing
while(DDKDmacGetTransStatus(pController->DmaReqTxCH) == DMAC_TRANSFER_STATUS_NONE)
{
DEBUGMSG(ZONE_INFO,(TEXT("DDKDmacGetTransSize 0x%X m_dwNumBytesToTransfer 0x%X\r\n"),
DDKDmacGetTransSize(pController->DmaReqTxCH), m_dwNumBytesToTransfer));
Sleep(0);
}
// Kill the output DMA channel
DDKDmacStopChan(pController->DmaReqTxCH);
}
else
{
//.............nothing to do......................
}
// if write crc error
if (EXTREG32BF(&pController->pSDMMCRegisters->STATUS, SDHC_SR_WCERR) )
{
DEBUGMSG(ZONE_ERROR, (TEXT("Cmd %d: Write CRC Error! Arg 0x%08X Status 0x%08X\r\n"),
pRequest->CommandCode, pRequest->CommandArgument,
INREG32(&pController->pSDMMCRegisters->STATUS)));
//Clear status by writing 1
OUTREG32(&pController->pSDMMCRegisters->STATUS, CSP_BITFVAL(SDHC_SR_WCERR, 1));
status = SD_API_STATUS_CRC_ERROR;
goto _done;
}
fRet = TRUE;
}
}
_done:
if (pRequest)
IndicateBusRequestComplete(pController, pRequest, status);
DEBUGMSG(ZONE_FUNCTION, (TEXT("-HandleTransferDone\r\n")));
return fRet;
}
//------------------------------------------------------------------------------
//
// Function: ProcessCardInsertion
//
// Handles card insertion event
//
// Parameters:
// pController - hardware context
//
// Returns:
//
//------------------------------------------------------------------------------
static void ProcessCardInsertion(void *pContext)
{
PSDH_HARDWARE_CONTEXT pHCDevice = (PSDH_HARDWARE_CONTEXT)pContext;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -