sdcontrol.c

来自「pxa27x library for windows ce 5.0」· C语言 代码 · 共 1,690 行 · 第 1/5 页

C
1,690
字号
                    // check to see if we've read enough
                if ((pRequest->HCParam + receiveBufferIndex) >= MaxBytes) {
                    break;
                }

            } else {

                    // receive buffer is full, now transfer the data safely
                SDPerformSafeCopy(&pRequest->pBlockBuffer[pRequest->HCParam],
                                  pController->RcvBuffer,
                                  receiveBufferIndex);
                    // bump the running count
                pRequest->HCParam += receiveBufferIndex;
                    // reset receive buffer index
                receiveBufferIndex = 0;
            }
              
        } // while

            // check for any left over data
        if (receiveBufferIndex) {
                // safely copy the data
            SDPerformSafeCopy(&pRequest->pBlockBuffer[pRequest->HCParam],
                              pController->RcvBuffer,
                              receiveBufferIndex);
                // bump the running count
            pRequest->HCParam += receiveBufferIndex;
        }

    } SD_RESTORE_PROC_PERMISSIONS();

}

///////////////////////////////////////////////////////////////////////////////
//  HandleTransferDone- Handle transfer done interrupt 
//  Input:  pController - the controller that is interrupting
//  Output: 
//  Return:
//  Notes:  
///////////////////////////////////////////////////////////////////////////////
VOID HandleTransferDone(PSDH_HARDWARE_CONTEXT pController)
{
    PSD_BUS_REQUEST pRequest;       // current request
    DWORD           regValue;       // intermediate byte value
    ULONG           maxBytes;       // max bytes

        // turn off the transfer done interrupt
    TRANSFER_DONE_INTERRUPT_OFF(pController);
      
        // get the current request  
    pRequest = SDHCDGetAndLockCurrentRequest(pController->pHCContext, 0);

        // this should never happen because we mark the request as un-cancelable
    DEBUG_ASSERT(NULL != pRequest);

    if (TRANSFER_IS_READ(pRequest)) {
            // make sure RX fifo interrupt is off 
        RX_FIFO_INTERRUPT_OFF(pController);
    } else if (TRANSFER_IS_WRITE(pRequest)) {
            // make sure TX fifo interrupt is off 
        TX_FIFO_INTERRUPT_OFF(pController);
            // can't turn off the clock until the prog done interrupt!
    } else {
        DEBUG_ASSERT(FALSE);
    }

        // check the transfer status
    regValue = READ_MMC_REGISTER_DWORD(pController, MMC_STAT);

        // check for errors
    if (regValue & MMC_STAT_FLASH_ERROR) {
        ASSERT(0);
    }

    if (regValue & MMC_STAT_SPI_WR_ERROR) {
        ASSERT(0);
    }

    if (regValue & MMC_STAT_RD_STALLED) {
        ASSERT(0);
    }

    if (regValue & MMC_STAT_READ_TIMEOUT) {
        DbgPrintZo(SDCARD_ZONE_ERROR, (TEXT("HandleTransferDoneInterrupt: Read Data TimedOut \n")));     
        
        if( !( pController->fClockAlwaysOn || 
               ( pController->fClockOnIfInterruptsEnabled && pController->fSDIOEnabled ) ) )
        {
                // turn off the clock
            SDClockOff(pController);
        }

        DbgPrintZo(SDH_SDBUS_INTERACTION_ZONE, (TEXT("HandleTransferDone reports DATA TIMEOUT\n")));
        SDHCDIndicateBusRequestComplete(pController->pHCContext,
                                        pRequest ,
                                        SD_API_STATUS_DATA_TIMEOUT);
        return;

    } else if (regValue & MMC_STAT_READ_DATA_CRC_ERROR) {

        DbgPrintZo(SDCARD_ZONE_ERROR, (TEXT("HandleTransferDoneInterrupt: Read Data Contains CRC error \n"))); 
        if( !( pController->fClockAlwaysOn || 
               ( pController->fClockOnIfInterruptsEnabled && pController->fSDIOEnabled ) ) )
        {
                // turn off the clock
            SDClockOff(pController);
        }
        DbgPrintZo(SDH_SDBUS_INTERACTION_ZONE, (TEXT("HandleTransferDone reports CRC ERROR\n")));
        SDHCDIndicateBusRequestComplete(pController->pHCContext,
                                        pRequest ,
                                        SD_API_STATUS_CRC_ERROR);
        return;
    } else if (regValue & MMC_STAT_WRITE_DATA_CRC_ERROR) {

        DbgPrintZo(SDCARD_ZONE_ERROR, (TEXT("HandleTransferDoneInterrupt: Card received Write Data with CRC error \n"))); 
        if( !( pController->fClockAlwaysOn || 
               ( pController->fClockOnIfInterruptsEnabled && pController->fSDIOEnabled ) ) )
        {
                // turn off the clock
            SDClockOff(pController);
        }
        DbgPrintZo(SDH_SDBUS_INTERACTION_ZONE, (TEXT("HandleTransferDone reports CRC ERROR\n")));
        SDHCDIndicateBusRequestComplete(pController->pHCContext,
                                        pRequest ,
                                        SD_API_STATUS_CRC_ERROR);
        return;
    }

    
    if (TRANSFER_IS_READ(pRequest)) {
            // why are we doing this here? If the remaining read data is less than a Fifo's worth (32)
            // we won't get the RX Fifo Read Request interrupt because the fifo won't be full.
            // also even if it is full or if this isn't the case the TRANSFER_DONE bit seems to mask it out
            // anyways this prevents the problem where there are bytes stuck in the Fifo
        maxBytes = pRequest->NumBlocks * pRequest->BlockSize;

        if (pRequest->HCParam < maxBytes) {
            DbgPrintZo(SDH_RECEIVE_ZONE, (TEXT("HandleTransferDoneInterrupt: Fifo contains remaining data, Max: %d, current count %d  \n"),
                maxBytes, pRequest->HCParam));
                // get the remaining bytes out of the FIFO
            EmptyReceiveFifo(pController, 
                             pRequest, 
                             (maxBytes - pRequest->HCParam), 
                             maxBytes);
        }

        SetCurrentState(pController, ReadDataDone);

        if( !( pController->fClockAlwaysOn || 
               ( pController->fClockOnIfInterruptsEnabled && pController->fSDIOEnabled ) ) )
        {
                // now it is safe to turn off the clock
            SDClockOff(pController);
        }

        DbgPrintZo(SDH_SDBUS_INTERACTION_ZONE, (TEXT("Bytes read: [%S]\n"), HexDisplay( pRequest->pBlockBuffer, TRANSFER_SIZE(pRequest) ) ) );
    }


    if (TRANSFER_IS_WRITE(pRequest)) {

        if (!IS_PROGRAM_DONE(pController)) {
            SetCurrentState(pController, ProgramWait);
            
                // turn on programming done interrupt
            PROGRAM_DONE_INTERRUPT_ON(pController); 
            //Sleep(500);

                // check to see if programming is finished
            if (!IS_PROGRAM_DONE(pController)) {
                DbgPrintZo(SDH_TRANSMIT_ZONE, (TEXT("HandleTransferDoneInterrupt: Programming Not Complete \n")));   
            }

            // if we wait on the programming done interrupt this could
            // go on forever because now it is up to the memory card, 
            // we may have to make this request cancelable at this point
            
            return;
        }
    } 

    DEBUG_ASSERT((pRequest->HCParam >= (pRequest->NumBlocks * pRequest->BlockSize)));
        // complete the request
    DbgPrintZo(SDH_SDBUS_INTERACTION_ZONE, (TEXT("HandleTransferDone reports Data Transfer Completed\n")));
    if (TRANSFER_IS_READ(pRequest)) {
		DbgPrintZo(SDH_SDBUS_INTERACTION_ZONE, (TEXT("Bytes read: [%S]\n"), HexDisplay( pRequest->pBlockBuffer, TRANSFER_SIZE(pRequest) ) ) );
	}

    SDHCDIndicateBusRequestComplete(pController->pHCContext,
                                    pRequest ,
                                    SD_API_STATUS_SUCCESS);
}


///////////////////////////////////////////////////////////////////////////////
//  SDLoadXmitFifo - load the transmit fifo
//  Input:  pController - the controler
//          pRequest    - the request 
//  Output: 
//  Return:  returns TRUE if the request has been fullfilled
//  Notes:  
///////////////////////////////////////////////////////////////////////////////
BOOL SDLoadXmitFifo(PSDH_HARDWARE_CONTEXT pController, PSD_BUS_REQUEST pRequest)
{
    DWORD           maxBytes;       // max bytes
    ULONG           ii = 0;         // loop variable
    ULONG           remainingBytes; // remaining bytes in the transfer
    volatile UCHAR *pMMC_TX_Fifo = (volatile UCHAR *)&(pController->pSDMMCRegisters->txfifo);


        // precalc
    maxBytes = pRequest->NumBlocks * pRequest->BlockSize;

    DbgPrintZo(SDH_TRANSMIT_ZONE, (TEXT("SDLoadXmitFifo: Current %d \n"),pRequest->HCParam));

        // make sure the partial full flag is cleared
    TX_BUFFER_PARTIAL_NOT_FULL(pController);

        // according to the spec the TX Fifo Empty interrupt asserts once when the fifo is
        // empty 
    if ((maxBytes - pRequest->HCParam) >= (LONG)MMC_TXFIFO_SIZE) {
            // because the remaining bytes is greater than or equal to the fifo size,
            // the Tx Fifo better be empty according to Intel's spec!
        // DEBUG_ASSERT(TX_FIFO_EMPTY(pController)); @todo
    }

        // we are touching the block buffer, we must set the process permissions
    SD_SET_PROC_PERMISSIONS_FROM_REQUEST(pRequest) {

            // figure out how much to prefetch from the user
            // buffer safely
        remainingBytes = maxBytes - pRequest->HCParam;

        if (remainingBytes > MMC_TXFIFO_SIZE) {
                // fix it
            remainingBytes = MMC_TXFIFO_SIZE;
        } 

            // perform the safe copy
        SDPerformSafeCopy(pController->XmitBuffer,
                          &pRequest->pBlockBuffer[pRequest->HCParam],
                          remainingBytes);            

            // according to the spec (15.2.8.3) the TX Fifo interrupt asserts for every empty fifo
            // (32 bytes)
            // so we write a Fifo's worth, as per spec 
        for (ii = 0; ii < remainingBytes; ii++) {
            
            if( !IsCardPresent() )
            {
                DbgPrintZo(SDCARD_ZONE_ERROR, (TEXT("SDLoadXmitFifo: Card ejected!\n")));     
                break;
            }

                // transfer bytes to the fifo from the safe buffer
            *pMMC_TX_Fifo = pController->XmitBuffer[ii];

            pRequest->HCParam++;

                // check to see if we've read enough
            if (pRequest->HCParam >= maxBytes) {
                break;
            }
        }

    } SD_RESTORE_PROC_PERMISSIONS();

        // check for a partial buffer
    if (ii < MMC_TXFIFO_SIZE) {
        TX_BUFFER_PARTIAL_FULL(pController);
    }

    DbgPrintZo(SDH_TRANSMIT_ZONE, (TEXT("SDLoadXmitFifo: New Current %d  \n"),pRequest->HCParam));
    
        // see if we are done
    if (pRequest->HCParam >= maxBytes) {
        return TRUE;
    } 

    return FALSE;
}


///////////////////////////////////////////////////////////////////////////////
//  HandleXmitInterrupt - handle the Xmit Fifo Empty interrupt
//  Input:  pController - the controler
//  Output: 
//  Return:
//  Notes:   
///////////////////////////////////////////////////////////////////////////////
VOID HandleXmitInterrupt(PSDH_HARDWARE_CONTEXT pController)
{
    PSD_BUS_REQUEST pRequest;       // current request
  
        // get the current request  
    pRequest = SDHCDGetAndLockCurrentRequest(pController->pHCContext, 0);

        // this should never happen because we mark the request as un-cancelable
    DEBUG_ASSERT(NULL != pRequest);

    if (SDLoadXmitFifo(pController, pRequest)) {
            // the request is complete  
            // turn off the Fifo Interrupts
        TX_FIFO_INTERRUPT_OFF(pController);
        SetCurrentState(pController, WriteDataTransferDone);
    }

    // now we need to wait for the controller to transmit (transfer done) and the card 
    // to complete programming (program done)
    // if the transfer is done or programming is done before we go back into the interrupt wait
    // the interrupt bit will be set and the IST loop will handle the transfer done in this same thread     

}

///////////////////////////////////////////////////////////////////////////////
//  HandleReceiveInterrupt - Handle recieve data interrupt
//  Input:  pController - the controller that is interrupting
//  Output: 
//  Return:
//  Notes:  
///////////////////////////////////////////////////////////////////////////////
VOID HandleReceiveInterrupt(PSDH_HARDWARE_CONTEXT pController)
{
    PSD_BUS_REQUEST pRequest;       // current request
    DWORD           maxBytes;       // max bytes
    ULONG           ii = 0;         // loop variable

        // get the current request  
    pRequest = SDHCDGetAndLockCurrentRequest(pController->pHCContext, 0);

        // this should never happen because we mark the request as un-cancelable
    DEBUG_ASSERT(NULL != pRequest);

    maxBytes = pRequest->NumBlocks * pRequest->BlockSize;
    
    DbgPrintZo(SDH_RECEIVE_ZONE, (TEXT("HandleReceiveInterrupt: Max: %d, Current %d \n"),
        maxBytes, pRequest->HCParam));
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?