⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmcsd.c

📁 老外的一个开源项目
💻 C
📖 第 1 页 / 共 4 页
字号:
    // The card needs to be in STANDBY state
    resErr = mmcCommandAndResponse ((((ULONG)mmcRCA) << 16),
                                    SELECT_DESELECT_CARD,
                                    0,
                                    R1 );
	if ((resErr == MMC_NO_ERROR) || (resErr == MMC_CARD_IS_NOT_RESPONDING))
		goto REASSURE_STANDBY_STATE;

DONE_SET_TO_STANDBY_STATE:

	DeviceController.error_code = (UINT16)resErr;
	return (resErr);
}

/*******************************************************************************
* Name: mmcSetXferState - set the card in transfer state
*
* Description:
*       In MMC mode, a select card command is sent to the card
*       to put it in transfer state.
*
* Input:
*       RCA     The address of the card to be selected.
*
* Output:
*       The card state is set to transfer state.
*
* Returns:
*       Completion code.
*
********************************************************************************/
MMC_CC mmcSetXferState (UINT16 RCA)
{
	MMC_CC  resErr;
	INT16   loopCnt;

REASSURE_XFER_STATE:        
	for (loopCnt = 0; loopCnt < 100; loopCnt++)
	{
		resErr = mmcCommandAndResponse ((((ULONG)RCA) << 16),
								SEND_STATUS,
								0,
								R1 );

		if (resErr != MMC_NO_ERROR )
			goto DONE_SET_TO_XFER_STATE;

		if (DeviceController.currentState == TRANSFER)
			goto DONE_SET_TO_XFER_STATE;

		else if (DeviceController.currentState == STANDBY)
			break;
	}

	if (loopCnt == 100)
	{
		resErr = MMC_CARD_IS_BUSY;
		goto DONE_SET_TO_XFER_STATE;
	}

	// Check for the card current state
	// The card needs to be in TRANSFER state
	resErr = mmcCommandAndResponse ((((ULONG)RCA) << 16),
						SELECT_DESELECT_CARD, 
						0,
						R1 );
	if (resErr == MMC_NO_ERROR)
		goto REASSURE_XFER_STATE;

DONE_SET_TO_XFER_STATE:

	DeviceController.error_code = (UINT16)resErr;
	return resErr;
}

/*******************************************************************************
* Name: mmcGetCardIdentification - Card CID
*       
* Description:
*       Get the card Identification (reads the card CID reg.)
*
* Input:
*       mmcIdent        Pointer to CID buffer
*       RCA             Card address
*
* Output:
*       mmcIdent        Card Identification record.
*
* Returns:
*       Completion code
*
********************************************************************************/
MMC_CC mmcGetCardIdentification (UCHAR *mmcIdent, UINT16 RCA )
{
	UCHAR   *resp_bytes;
	MMC_CC  resErr;
	INT16   ij;

	resp_bytes = (UCHAR *)DeviceController.LastResponse;

	resErr = mmcSetStandbyState( RCA );
	resErr = mmcCommandAndResponse ((((ULONG)RCA) << 16),
									SEND_CID,
									0,
									R2 );

	if ( resErr == MMC_NO_ERROR )
	{
		for (ij = 0; ij < (CID_BYTE_LENGTH-1); ij++)
		{
			mmcIdent[ij] = resp_bytes[ij+1];
		}
	}
        
    DeviceController.error_code = (UINT16)resErr;
    return resErr;
}



/*******************************************************************************
* Name: mmcGetConfiguration - Card CSD
* 
* Description:
*       Get the card configuration parameters (reads the card CSD reg.)
* 
* Input:
*       UINT16  RCA             Card address.
*       UCHAR   respCSD         Pointer to CSD information
*
* Output:
*       respCSD         Card configuration record.
*
* Returns:
*       Completion code 
* 
********************************************************************************/
MMC_CC mmcGetConfiguration(UCHAR *respCSD, UINT16 RCA )
{
	UCHAR   *resp_bytes;
	MMC_CC  resErr;
	UINT16  ij;

	resp_bytes = (UCHAR *)DeviceController.LastResponse;

    // Correct RCA when assigned the ID to the card
    // Set the card to standby state
	resErr = mmcSetStandbyState( RCA );
	resErr = mmcCommandAndResponse ((((ULONG)RCA) << 16),
									SEND_CSD, 
									0,
									R2 );     

	if ( resErr == MMC_NO_ERROR )
	{
		for (ij = 0; ij < (CID_BYTE_LENGTH - 1); ij++)
			respCSD[ij] = resp_bytes[ij+1];
	}

	DeviceController.error_code = (UINT16)resErr;
	return resErr;
}


/*******************************************************************************
* Name: mmcGetWPMap
*
* Description:
*       Reads the card's group-write-protect map. It will return the
*       WP state of 32 groups starting ast the addressed group. A '1'
*       In the returned block means group is write protected.
*
* Input:
*       dataAddress     WP group address
*       RCA             card address
* 
* Output:
*       bufPtr                  read data
*
* Returns:
*       Completion code.
*
********************************************************************************/
MMC_CC mmcGetWPMap (ULONG dataAddress, UINT16 RCA )
{
	MMC_CC  resErr;
	UINT16  saveBlkLen;

	resErr = mmcSetXferState( RCA );
	saveBlkLen = DeviceController.block_size;
	resErr = mmcCommandAndResponse ( 4L,
									SET_BLOCKLEN, 
									0,
									R1 );
	if ( resErr == MMC_NO_ERROR )
	{
		resErr = mmcCommandAndResponse ( (dataAddress << 18),
										SEND_WRITE_PROT, 
										0,
										R1 );

		if ( resErr == MMC_NO_ERROR )
			resErr = mmcReceiveData( 4, 1, FALSE );
    }

	if ( resErr == MMC_NO_ERROR )
	{
		resErr = mmcSetXferState( RCA );
		DeviceController.block_size = saveBlkLen;
		resErr = mmcCommandAndResponse ((ULONG)saveBlkLen,
				SET_BLOCKLEN, 
				0,
				R1 );
	}
	DeviceController.error_code = (UINT16)resErr;
	return resErr;
}

/*******************************************************************************
* Name: mmcSetGroupWP
*
* Description:
*       Sets the write protection bit of the addressed group
*
* Input:
*       dataAddress     Write protect group address
*       RCA             Card address.
*
* Output:
*       None.
*
* Returns:
*       Completion code
*
********************************************************************************/
MMC_CC mmcSetGroupWP (ULONG dataAddress, UINT16 RCA)
{
	MMC_CC  resErr;

	resErr = mmcSetXferState( RCA );
	resErr = mmcCommandAndResponse ((dataAddress << 18),
									SET_WRITE_PROT, 
									0,
									R1 );

	DeviceController.error_code = (UINT16)resErr;
	return resErr;
}

/*******************************************************************************
* Name: mmcClearGroupWP
*
* Description:
*       Clears the write protection bit of the addressed group
* 
*
* Input:
*       dataAddress     Write protect group address
*       RCA             Card address.
*
* Output:
*       None.
*
* Returns:
*       Completion code
*
************************************************************************************/
MMC_CC mmcClearGroupWP (ULONG dataAddress, UINT16 RCA)
{
	MMC_CC  resErr;

	resErr = mmcSetXferState( RCA );

	resErr = mmcCommandAndResponse ((dataAddress << 18),
									CLR_WRITE_PROT, 
									0,
									R1 );

	DeviceController.error_code = (UINT16)resErr;
	return (resErr);
}

/*******************************************************************************
* Name: mmcBlkLengthConfiguration - Set the card block length
*
* Description:
*       Set the block length
* 
* Input:
*       RCA                     Card address.
*
* Output:
*       The card block length is set.
*
* Returns:
*       Completion code
*
************************************************************************************/
MMC_CC mmcBlkLengthConfiguration( UINT16 RCA )
{
	MMC_CC  resErr;

	resErr = mmcSetXferState( RCA );
	resErr = mmcCommandAndResponse ((ULONG)DeviceController.block_size,
									SET_BLOCKLEN, 
									0,
									R1 );

	DeviceController.error_code = (UINT16)resErr;
	return (resErr);
}

/***********************************************************************************
* Name: mmcRead - Reads one sector from card
*                                                                                                                                                                       |
* Description:
*       This function will (unless already selecetd) select
*       an mmc card using the given RCA (CMD7) set the block
*       length to 512 bytes (CMD16) and read one data
*       block using single block read (CMD17)
* 
* Input:
*       dataAddress -    Sector address
*       RCA         -    card address
*
* Output:
*       bufPtr      -    read data
*
* Returns:
*       Completion code
*
************************************************************************************/
MMC_CC mmcRead( ULONG dataAddress,
				UINT16 RCA,
				UINT16 noBlocks )
{
	MMC_CC  resErr;

	resErr = mmcSetXferState( RCA );
	resErr = mmcCommandAndResponse ((dataAddress * (ULONG)DeviceController.block_size), 
									READ_BLOCK, 
									noBlocks,
									R1 );
	if (resErr == MMC_NO_ERROR)
	{
		resErr = mmcReceiveData(DeviceController.block_size, 1, FALSE);
	}
	DeviceController.error_code = (UINT16)resErr;
	return resErr;
}



/***********************************************************************************
* Name: mmcWrite
*
* Description:
*       Writes one block of data to the device card sector
*
*       This function will (unless already selecetd) select an
*       mmc card using the given RCA (CMD7) set the block
*       length to 512 bytes (CMD16) and write one data
*       block using single block write (CMD24) 
* 
* Input:
*       dataAddress     -       Sector address
*       RCA             -       Card address.
* 
* Output:
*       None.
*                                                                                                     
* Returns:
*       Completion code
*
************************************************************************************/
MMC_CC mmcWrite (ULONG dataAddress,	UINT16 RCA,	UINT16 noBlocks )
{
	MMC_CC  resErr;

	resErr = mmcSetXferState( RCA );
	resErr = mmcCommandAndResponse ((dataAddress * (ULONG)DeviceController.block_size), 
									WRITE_BLOCK, 
									noBlocks,
									R1 );
	if ( resErr == MMC_NO_ERROR )
		resErr = mmcSendData(DeviceController.block_size, 1, FALSE);

	DeviceController.error_code = (UINT16)resErr;
	return resErr;
}


MMC_CC mmcSelectBusWidth(UINT16 RCA, UINT16 option)
{
	UINT32 multipp;        

	multipp = (0x0F & ((UINT32)option));
	return(mmcSDApplCmd(NULL, multipp, 0, RCA, R1, SET_BUS_WIDTH));
}

/***********************************************************************************
* Name: mmcStopTransmission
*
* Description:
*       Stop Read multiple card sectors (CMD12)
*
* Input:
*
* Output:
*
* Returns:
*       Completion code
*
************************************************************************************/
MMC_CC mmcStopTransmission (UINT16 RCA)
{
	MMC_CC  resErr;

	RCA = RCA;
	resErr = mmcCommandAndResponse (0L,
									STOP_TRANSMISSION,
									0,
									R1 );
	return resErr;
}

/*******************************************************************************
* Name: mmcReadMultiple -  (SD and MMC card in MMC mode only)
*
* Description:
*       Initiate a multiple block read operation and start data transfer.
*
*       This function will (unless already selecetd) select an SD/MMC
*       card using the given RCA (CMD7) and starts the multiple read (CMD18).
*
* Input:
*       dataAddress -    Sector address
*       dataAddress -    Location of data
*       RCA         -    card address
*       noBlocks    -    Number of blocks to tansfer 
* Output:
*       bufPtr      -    read data
*
* Returns:

⌨️ 快捷键说明

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