📄 mci.c
字号:
{
/* Send CMD3 command repeatedly until the response is back correctly */
MCI_SendCmd( SET_RELATIVE_ADDR, CmdArgument, EXPECT_SHORT_RESP, 0 );
respStatus = MCI_GetCmdResp( SET_RELATIVE_ADDR, EXPECT_SHORT_RESP, (DWORD *)&respValue[0] );
/* bit 0 and bit 2 must be zero, or it's timeout or CRC error */
/* It should go to IDEN state and bit 8 should be 1 */
if ( !(respStatus & MCI_CMD_TIMEOUT) && ((respValue[0] & (0x0F << 8)) == 0x0500) )
{
CardRCA = respValue[0] & 0xFFFF0000; /* Save the RCA value from SD card */
return ( TRUE ); /* response is back and correct. */
}
for ( i = 0; i < 0x20; i++ );
retryCount--;
}
return ( FALSE );
}
/******************************************************************************
** Function name: MCI_Send_CSD
**
** Descriptions: CMD9, SEND_CSD cmd, it should be sent only at
** STBY state and after CMD3. See MMC and SD spec. state
** diagram.
**
** parameters: None
** Returned value: Response value
**
******************************************************************************/
DWORD MCI_Send_CSD( void )
{
DWORD i, retryCount;
DWORD respStatus;
DWORD respValue[4];
DWORD CmdArgument;
if ( CardType == SD_CARD )
{
CmdArgument = CardRCA;
}
else /* if MMC or unknown card type, use default RCA addr. */
{
CmdArgument = 0x00010000;
}
retryCount = 0x20;
while ( retryCount > 0 )
{
/* Send SET_BLOCK_LEN command before read and write */
MCI_CLEAR |= (MCI_CMD_TIMEOUT | MCI_CMD_CRC_FAIL | MCI_CMD_RESP_END);
MCI_SendCmd( SEND_CSD, CmdArgument, EXPECT_LONG_RESP, 0 );
respStatus = MCI_GetCmdResp( SEND_CSD, EXPECT_LONG_RESP, (DWORD *)&respValue[0] );
if ( !respStatus )
{
return ( TRUE );
}
for ( i = 0; i < 0x20; i++ );
retryCount--;
}
return ( FALSE );
}
/******************************************************************************
** Function name: MCI_Select_Card
**
** Descriptions: CMD7, SELECT_CARD, should be after CMD9, the state
** will be inter-changed between STBY and TRANS after
** this cmd.
**
** parameters: None
** Returned value: return false if response times out.
**
******************************************************************************/
DWORD MCI_Select_Card( void )
{
DWORD i, retryCount;
DWORD respStatus;
DWORD respValue[4];
DWORD CmdArgument;
if ( CardType == SD_CARD )
{
CmdArgument = CardRCA;
}
else /* if MMC or unknown card type, use default RCA addr. */
{
CmdArgument = 0x00010000;
}
retryCount = 0x20;
while ( retryCount > 0 )
{
/* Send SELECT_CARD command before read and write */
MCI_CLEAR |= (MCI_CMD_TIMEOUT | MCI_CMD_CRC_FAIL | MCI_CMD_RESP_END);
MCI_SendCmd( SELECT_CARD, CmdArgument, EXPECT_SHORT_RESP, 0 );
respStatus = MCI_GetCmdResp( SELECT_CARD, EXPECT_SHORT_RESP, (DWORD *)&respValue[0] );
if ( !respStatus && ((respValue[0] & (0x0F << 8)) == 0x0700 ))
{ /* Should be in STANDBY state now and ready */
return ( TRUE );
}
for ( i = 0; i < 0x20; i++ );
retryCount--;
}
return ( FALSE );
}
/******************************************************************************
** Function name: MCI_Send_Status
**
** Descriptions: CMD13, SEND_STATUS, the most important cmd to
** debug the state machine of the card.
**
** parameters: None
** Returned value: Response value(card status), true if the ready bit
** is set in the card status register, if timeout, return
** INVALID_RESPONSE 0xFFFFFFFF.
**
******************************************************************************/
DWORD MCI_Send_Status( void )
{
DWORD retryCount;
DWORD respStatus;
DWORD respValue[4];
DWORD CmdArgument;
if ( CardType == SD_CARD )
{
CmdArgument = CardRCA;
}
else /* if MMC or unknown card type, use default RCA addr. */
{
CmdArgument = 0x00010000;
}
/* Note that, since it's called after the block write and read, this timeout
is important based on the clock you set for the data communication. */
retryCount = 0x2000;
while ( retryCount > 0 )
{
/* Send SELECT_CARD command before read and write */
MCI_CLEAR |= (MCI_CMD_TIMEOUT | MCI_CMD_CRC_FAIL | MCI_CMD_RESP_END);
MCI_SendCmd( SEND_STATUS, CmdArgument, EXPECT_SHORT_RESP, 0 );
respStatus = MCI_GetCmdResp( SEND_STATUS, EXPECT_SHORT_RESP, (DWORD *)&respValue[0] );
if ( !respStatus && (respValue[0] & (1 << 8)) )
{ /* The ready bit should be set, it should be in either TRAN or RCV
state now */
return ( respValue[0] );
}
retryCount--;
}
return ( INVALID_RESPONSE );
}
/******************************************************************************
** Function name: MCI_Set_BlockLen
**
** Descriptions: CMD16, SET_BLOCKLEN, called after CMD7(SELECT_CARD)
** called in the TRANS state.
**
** parameters: The length of the data block to be written or read.
** Returned value: true or false, return TRUE if ready bit is set, and it's
** in TRANS state.
**
******************************************************************************/
DWORD MCI_Set_BlockLen( DWORD blockLength )
{
DWORD i, retryCount;
DWORD respStatus;
DWORD respValue[4];
retryCount = 0x20;
while ( retryCount > 0 )
{
/* Send SET_BLOCK_LEN command before read and write */
MCI_CLEAR |= (MCI_CMD_TIMEOUT | MCI_CMD_CRC_FAIL | MCI_CMD_RESP_END);
MCI_SendCmd( SET_BLOCK_LEN, blockLength, EXPECT_SHORT_RESP, 0 );
respStatus = MCI_GetCmdResp( SET_BLOCK_LEN, EXPECT_SHORT_RESP, (DWORD *)&respValue[0] );
/* bit 9 through 12 should be in transfer state now. bit 8 is ready. */
if ( !respStatus && ((respValue[0] & (0x0F << 8)) == 0x0900))
{
return ( TRUE );
}
for ( i = 0; i < 0x20; i++ );
retryCount--;
}
return ( FALSE );
}
/******************************************************************************
** Function name: MCI_Send_ACMD_Bus_Width
**
** Descriptions: ACMD6, SET_BUS_WIDTH, if it's SD card, we can
** use the 4-bit bus instead of 1-bit. This cmd
** can only be called during TRANS state.
** Since it's a ACMD, CMD55 APP_CMD needs to be
** sent out first.
**
** parameters: Bus width value, 1-bit is 0, 4-bit is 10
** Returned value: true or false, true if the card is still in the
** TRANS state after the cmd.
**
******************************************************************************/
DWORD MCI_Send_ACMD_Bus_Width( DWORD buswidth )
{
DWORD i, retryCount;
DWORD respStatus;
DWORD respValue[4];
retryCount = 0x20; /* reset retry counter */
while ( retryCount > 0 )
{
if ( MCI_Send_ACMD() == FALSE )
{
continue;
}
/* Send ACMD6 command to set the bus width */
MCI_SendCmd( SET_ACMD_BUS_WIDTH, buswidth, EXPECT_SHORT_RESP, 0 );
respStatus = MCI_GetCmdResp( SET_ACMD_BUS_WIDTH, EXPECT_SHORT_RESP, (DWORD *)&respValue[0] );
if ( !respStatus && ((respValue[0] & (0x0F << 8)) == 0x0900) )
{
return ( TRUE ); /* response is back and correct. */
}
for ( i = 0; i < 0x20; i++ );
retryCount--;
}
return( FALSE );
}
/******************************************************************************
** Function name: MCI_Send_Stop
**
** Descriptions: CMD12, STOP_TRANSMISSION. if that happens, the card is
** maybe in a unknown state that need a warm reset.
**
** parameters: None
** Returned value: true or false, true if, at least, the card status
** shows ready bit is set.
**
******************************************************************************/
DWORD MCI_Send_Stop( void )
{
DWORD i, retryCount;
DWORD respStatus;
DWORD respValue[4];
retryCount = 0x20;
while ( retryCount > 0 )
{
MCI_CLEAR = 0x7FF;
MCI_SendCmd( STOP_TRANSMISSION, 0x00000000, EXPECT_SHORT_RESP, 0 );
respStatus = MCI_GetCmdResp( STOP_TRANSMISSION, EXPECT_SHORT_RESP, (DWORD *)respValue );
/* ready bit, bit 8, should be set in the card status register */
if ( !respStatus && (respValue[0] & (1 << 8)) )
{
return( TRUE );
}
for ( i = 0; i < 0x20; i++ );
retryCount--;
}
return ( FALSE );
}
/******************************************************************************
** Function name: MCI_Send_Write_Block
**
** Descriptions: CMD24, WRITE_BLOCK, send this cmd in the TRANS state
** to write a block of data to the card.
**
** parameters: block number
** Returned value: Response value
**
******************************************************************************/
DWORD MCI_Send_Write_Block( DWORD blockNum )
{
DWORD i, retryCount;
DWORD respStatus;
DWORD respValue[4];
retryCount = 0x20;
while ( retryCount > 0 )
{
MCI_CLEAR = 0x7FF;
MCI_SendCmd( WRITE_BLOCK, blockNum * BLOCK_LENGTH, EXPECT_SHORT_RESP, 0 );
respStatus = MCI_GetCmdResp( WRITE_BLOCK, EXPECT_SHORT_RESP, (DWORD *)&respValue[0] );
/* it should be in the transfer state, bit 9~12 is 0x0100 and bit 8 is 1 */
if ( !respStatus && ((respValue[0] & (0x0F << 8)) == 0x0900) )
{
return( TRUE ); /* ready and in TRAN state */
}
for ( i = 0; i < 0x20; i++ );
retryCount--;
}
return ( FALSE ); /* Fatal error */
}
/******************************************************************************
** Function name: MCI_Send_Read_Block
**
** Descriptions: CMD17, READ_SINGLE_BLOCK, send this cmd in the TRANS
** state to read a block of data from the card.
**
** parameters: block number
** Returned value: Response value
**
******************************************************************************/
DWORD MCI_Send_Read_Block( DWORD blockNum )
{
DWORD i, retryCount;
DWORD respStatus;
DWORD respValue[4];
retryCount = 0x20;
while ( retryCount > 0 )
{
MCI_CLEAR = 0x7FF;
MCI_SendCmd( READ_SINGLE_BLOCK, blockNum * BLOCK_LENGTH, EXPECT_SHORT_RESP, 0 );
respStatus = MCI_GetCmdResp( READ_SINGLE_BLOCK, EXPECT_SHORT_RESP, (DWORD *)&respValue[0] );
/* it should be in the transfer state, bit 9~12 is 0x0100 and bit 8 is 1 */
if ( !respStatus && ((respValue[0] & (0x0F << 8)) == 0x0900) )
{
return( TRUE ); /* ready and in TRAN state */
}
for ( i = 0; i < 0x20; i++ );
retryCount--;
}
return ( FALSE ); /* Fatal error */
}
/******************************************************************************
** Function name: MCI_Write_Block
**
** Descriptions: Set MCI data control register, data length and data
** timeout, send WRITE_BLOCK cmd, finally, enable
** interrupt. On completion of WRITE_BLOCK cmd, TX_ACTIVE
** interrupt will occurs, data can be written continuously
** into the FIFO until the block data length is reached.
**
** parameters: block number
** Returned value: true or false, if cmd times out, return false and no
** need to continue.
**
******************************************************************************/
DWORD MCI_Write_Block( DWORD blockNum )
{
DWORD i;
DWORD DataCtrl = 0;
MCI_CLEAR = 0x7FF;
MCI_DATA_CTRL = 0;
for ( i = 0; i < 0x10; i++ );
/* Below status check is redundant, but ensure card is in TRANS state
before writing and reading to from the card. */
if ( MCI_CheckStatus() != TRUE )
{
MCI_Send_Stop();
return( FALSE );
}
MCI_DATA_TMR = DATA_TIMER_VALUE;
MCI_DATA_LEN = BLOCK_LENGTH;
if ( MCI_Send_Write_Block( blockNum ) == FALSE )
{
return ( FALSE );
}
#if MCI_DMA_ENABLED
DMA_CH_Enable(0);
/* Write, block transfer, DMA, and data length */
DataCtrl |= ((1 << 0) | (1 << 3) | (DATA_BLOCK_LEN << 4));
#else
/* Write, block transfer, and data length */
DataCtrl |= ((1 << 0) | (DATA_BLOCK_LEN << 4));
#endif
MCI_DATA_CTRL = DataCtrl;
for ( i = 0; i < 0x10; i++ );
MCI_TXEnable();
return ( TRUE );
}
/******************************************************************************
** Function name: MCI_Read_Block
**
** Descriptions: Set MCI data control register, data length and data
** timeout, send READ_SINGLE_BLOCK cmd, finally, enable
** interrupt. On completion of READ_SINGLE_BLOCK cmd,
** RX_ACTIVE interrupt will occurs, data can be read
** continuously into the FIFO until the block data
** length is reached.
**
** parameters: block number
** Returned value: true or false, if cmd times out, return false and no
** need to continue.
**
**
******************************************************************************/
DWORD MCI_Read_Block( DWORD blockNum )
{
DWORD i;
DWORD DataCtrl = 0;
MCI_CLEAR = 0x7FF;
MCI_DATA_CTRL = 0;
for ( i = 0; i < 0x10; i++ );
/* Below status check is redundant, but ensure card is in TRANS state
before writing and reading to from the card. */
if ( MCI_CheckStatus() != TRUE )
{
MCI_Send_Stop();
return( FALSE );
}
MCI_RXEnable();
MCI_DATA_TMR = DATA_TIMER_VALUE;
MCI_DATA_LEN = BLOCK_LENGTH;
if ( MCI_Send_Read_Block( blockNum ) == FALSE )
{
return ( FALSE );
}
#if MCI_DMA_ENABLED
/* Write, block transfer, DMA, and data length */
DataCtrl |= ((1 << 0) | (1 << 1) | (1 << 3) | (DATA_BLOCK_LEN << 4));
#else
/* Read, enable, block transfer, and data length */
DataCtrl |= ((1 << 0) | (1 << 1) | (DATA_BLOCK_LEN << 4));
#endif
MCI_DATA_CTRL = DataCtrl;
#if MCI_DMA_ENABLED
DMA_CH_Enable(1);
#endif
for ( i = 0; i < 0x10; i++ );
return ( TRUE );
}
/*****************************************************************************
** End Of File
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -