/**
******************************************************************************
* @file stm32_eval_sdio_sd.c
* @author MCD Application Team
* @version V4.5.0
* @date 07-March-2011
* @brief This file provides a set of functions needed to manage the SDIO SD
* Card memory mounted on STM32xx-EVAL board (refer to stm32_eval.h
* to know about the boards supporting this memory).
*
*
* @verbatim
*
* ===================================================================
* How to use this driver
* ===================================================================
* It implements a high level communication layer for read and write
* from/to this memory. The needed STM32 hardware resources (SDIO and
* GPIO) are defined in stm32xx_eval.h file, and the initialization is
* performed in SD_LowLevel_Init() function declared in stm32xx_eval.c
* file.
* You can easily tailor this driver to any other development board,
* by just adapting the defines for hardware resources and
* SD_LowLevel_Init() function.
*
* A - SD Card Initialization and configuration
* ============================================
* - To initialize the SD Card, use the SD_Init() function. It
* Initializes the SD Card and put it into StandBy State (Ready
* for data transfer). This function provide the following operations:
*
* 1 - Apply the SD Card initialization process at 400KHz and check
* the SD Card type (Standard Capacity or High Capacity). You
* can change or adapt this frequency by adjusting the
* "SDIO_INIT_CLK_DIV" define inside the stm32xx_eval.h file.
* The SD Card frequency (SDIO_CK) is computed as follows:
*
* +---------------------------------------------+
* | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
* +---------------------------------------------+
*
* In initialization mode and according to the SD Card standard,
* make sure that the SDIO_CK frequency don't exceed 400KHz.
*
* 2 - Get the SD CID and CSD data. All these information are
* managed by the SDCardInfo structure. This structure provide
* also ready computed SD Card capacity and Block size.
*
* 3 - Configure the SD Card Data transfer frequency. By Default,
* the card transfer frequency is set to 24MHz. You can change
* or adapt this frequency by adjusting the "SDIO_TRANSFER_CLK_DIV"
* define inside the stm32xx_eval.h file.
* The SD Card frequency (SDIO_CK) is computed as follows:
*
* +---------------------------------------------+
* | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
* +---------------------------------------------+
*
* In transfer mode and according to the SD Card standard,
* make sure that the SDIO_CK frequency don't exceed 25MHz
* and 50MHz in High-speed mode switch.
* To be able to use a frequency higher than 24MHz, you should
* use the SDIO peripheral in bypass mode. Refer to the
* corresponding reference manual for more details.
*
* 4 - Select the corresponding SD Card according to the address
* read with the step 2.
*
* 5 - Configure the SD Card in wide bus mode: 4-bits data.
*
* B - SD Card Read operation
* ==========================
* - You can read SD card by using two function: SD_ReadBlock() and
* SD_ReadMultiBlocks() functions. These functions support only
* 512-byte block length.
* - The SD_ReadBlock() function read only one block (512-byte). This
* function can transfer the data using DMA controller or using
* polling mode. To select between DMA or polling mode refer to
* "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm32_eval_sdio_sd.h
* file and uncomment the corresponding line. By default the SD DMA
* mode is selected
* - The SD_ReadMultiBlocks() function read only mutli blocks (multiple
* of 512-byte).
* - Any read operation should be followed by two functions to check
* if the DMA Controller and SD Card status.
* - SD_ReadWaitOperation(): this function insure that the DMA
* controller has finished all data transfer.
* - SD_GetStatus(): to check that the SD Card has finished the
* data transfer and it is ready for data.
*
* - The DMA transfer is finished by the SDIO Data End interrupt. User
* has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler().
* Don't forget to enable the SDIO_IRQn interrupt using the NVIC controller.
*
* C - SD Card Write operation
* ===========================
* - You can write SD card by using two function: SD_WriteBlock() and
* SD_WriteMultiBlocks() functions. These functions support only
* 512-byte block length.
* - The SD_WriteBlock() function write only one block (512-byte). This
* function can transfer the data using DMA controller or using
* polling mode. To select between DMA or polling mode refer to
* "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm32_eval_sdio_sd.h
* file and uncomment the corresponding line. By default the SD DMA
* mode is selected
* - The SD_WriteMultiBlocks() function write only mutli blocks (multiple
* of 512-byte).
* - Any write operation should be followed by two functions to check
* if the DMA Controller and SD Card status.
* - SD_ReadWaitOperation(): this function insure that the DMA
* controller has finished all data transfer.
* - SD_GetStatus(): to check that the SD Card has finished the
* data transfer and it is ready for data.
*
* - The DMA transfer is finished by the SDIO Data End interrupt. User
* has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler().
* Don't forget to enable the SDIO_IRQn interrupt using the NVIC controller.
*
* D - SD card status
* ==================
* - At any time, you can check the SD Card status and get the SD card
* state by using the SD_GetStatus() function. This function checks
* first if the SD card is still connected and then get the internal
* SD Card transfer state.
* - You can also get the SD card SD Status register by using the
* SD_SendSDStatus() function.
*
* E - Programming Model
* =====================
* Status = SD_Init(); // Initialization Step as described in section A
*
* // SDIO Interrupt ENABLE
* NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
* NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
* NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
* NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
* NVIC_Init(&NVIC_InitStructure);
*
* // Write operation as described in Section C
* Status = SD_WriteBlock(buffer, address, 512);
* Status = SD_WaitWriteOperation();
* while(SD_GetStatus() != SD_TRANSFER_OK);
*
* Status = SD_WriteMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
* Status = SD_WaitWriteOperation();
* while(SD_GetStatus() != SD_TRANSFER_OK);
*
* // Read operation as described in Section B
* Status = SD_ReadBlock(buffer, address, 512);
* Status = SD_WaitReadOperation();
* while(SD_GetStatus() != SD_TRANSFER_OK);
*
* Status = SD_ReadMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
* Status = SD_WaitReadOperation();
* while(SD_GetStatus() != SD_TRANSFER_OK);
*
*
* STM32 SDIO Pin assignment
* =========================
* +-----------------------------------------------------------+
* | Pin assignment |
* +-----------------------------+---------------+-------------+
* | STM32 SDIO Pins | SD | Pin |
* +-----------------------------+---------------+-------------+
* | SDIO D2 | D2 | 1 |
* | SDIO D3 | D3 | 2 |
* | SDIO CMD | CMD | 3 |
* | | VCC | 4 (3.3 V)|
* | SDIO CLK | CLK | 5 |
* | | GND | 6 (0 V) |
* | SDIO D0 | D0 | 7 |
* | SDIO D1 | D1 | 8 |
* +-----------------------------+---------------+-------------+
*
* @endverbatim
*
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
*
/**
* @brief Following commands are SD Card Specific commands.
* SDIO_APP_CMD should be sent before sending these commands.
*/
#define SDIO_SEND_IF_COND ((uint32_t)0x00000008)
/**
* @brief Initializes the SD Card and put it into StandBy State (Ready for data
* transfer).
* @param None
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_Init(void)
{
SD_Error errorstatus = SD_OK;
if (errorstatus == SD_OK)
{
errorstatus = SD_EnableWideBusOperation(SDIO_BusWide_4b);
}
return(errorstatus);
}
/**
* @brief Gets the cuurent sd card data transfer status.
* @param None
* @retval SDTransferState: Data Transfer state.
* This value can be:
* - SD_TRANSFER_OK: No data transfer is acting
* - SD_TRANSFER_BUSY: Data transfer is acting
*/
SDTransferState SD_GetStatus(void)
{
SDCardState cardstate = SD_CARD_TRANSFER;
/**
* @brief Detect if SD card is correctly plugged in the memory slot.
* @param None
* @retval Return if SD is detected or not
*/
uint8_t SD_Detect(void)
{
__IO uint8_t status = SD_PRESENT;
/*!< Check GPIO to detect SD */
if (GPIO_ReadInputDataBit(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != Bit_RESET)
{
status = SD_NOT_PRESENT;
}
return status;
}
/*!< If errorstatus is Command TimeOut, it is a MMC card */
/*!< If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
or SD card 1.x */
if (errorstatus == SD_OK)
{
/*!< SD CARD */
/*!< Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
{
/*!< Set Power State to OFF */
SDIO_SetPowerState(SDIO_PowerState_OFF);
return(errorstatus);
}
/**
* @brief Intialises all cards or single card as the case may be Card(s) come
* into standby state.
* @param None
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_InitializeCards(void)
{
SD_Error errorstatus = SD_OK;
uint16_t rca = 0x01;
if (SDIO_GetPowerState() == SDIO_PowerState_OFF)
{
errorstatus = SD_REQUEST_NOT_APPLICABLE;
return(errorstatus);
}
/**
* @brief Enables wide bus opeartion for the requeseted card if supported by
* card.
* @param WideMode: Specifies the SD card wide bus mode.
* This parameter can be one of the following values:
* @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
* @arg SDIO_BusWide_4b: 4-bit data transfer
* @arg SDIO_BusWide_1b: 1-bit data transfer
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus)
{
SD_Error errorstatus = SD_OK;
uint8_t tmp = 0;
/**
* @brief Enables wide bus opeartion for the requeseted card if supported by
* card.
* @param WideMode: Specifies the SD card wide bus mode.
* This parameter can be one of the following values:
* @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
* @arg SDIO_BusWide_4b: 4-bit data transfer
* @arg SDIO_BusWide_1b: 1-bit data transfer
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_EnableWideBusOperation(uint32_t WideMode)
{
SD_Error errorstatus = SD_OK;
/*!< MMC Card doesn't support this feature */
if (SDIO_MULTIMEDIA_CARD == CardType)
{
errorstatus = SD_UNSUPPORTED_FEATURE;
return(errorstatus);
}
else if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
{
if (SDIO_BusWide_8b == WideMode)
{
errorstatus = SD_UNSUPPORTED_FEATURE;
return(errorstatus);
}
else if (SDIO_BusWide_4b == WideMode)
{
errorstatus = SDEnWideBus(ENABLE);
/**
* @brief Allows to read one block from a specified address in a card. The Data
* transfer can be managed by DMA mode or Polling mode.
* @note This operation should be followed by two functions to check if the
* DMA Controller and SD Card status.
* - SD_ReadWaitOperation(): this function insure that the DMA
* controller has finished all data transfer.
* - SD_GetStatus(): to check that the SD Card has finished the
* data transfer and it is ready for data.
* @param readbuff: pointer to the buffer that will contain the received data
* @param ReadAddr: Address from where data are to be read.
* @param BlockSize: the SD card Data block size. The Block size should be 512.
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)
{
SD_Error errorstatus = SD_OK;
#if defined (SD_POLLING_MODE)
uint32_t count = 0, *tempbuff = (uint32_t *)readbuff;
#endif
if (errorstatus != SD_OK)
{
return(errorstatus);
}
#if defined (SD_POLLING_MODE)
/*!< In case of single block transfer, no need of stop transfer at all.*/
/*!< Polling mode */
while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
{
if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
{
for (count = 0; count < 8; count++)
{
*(tempbuff + count) = SDIO_ReadData();
}
tempbuff += 8;
}
}
/*!< Clear all the static flags */
SDIO_ClearFlag(SDIO_STATIC_FLAGS);
#elif defined (SD_DMA_MODE)
SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE);
SDIO_DMACmd(ENABLE);
SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, BlockSize);
#endif
return(errorstatus);
}
/**
* @brief Allows to read blocks from a specified address in a card. The Data
* transfer can be managed by DMA mode or Polling mode.
* @note This operation should be followed by two functions to check if the
* DMA Controller and SD Card status.
* - SD_ReadWaitOperation(): this function insure that the DMA
* controller has finished all data transfer.
* - SD_GetStatus(): to check that the SD Card has finished the
* data transfer and it is ready for data.
* @param readbuff: pointer to the buffer that will contain the received data.
* @param ReadAddr: Address from where data are to be read.
* @param BlockSize: the SD card Data block size. The Block size should be 512.
* @param NumberOfBlocks: number of blocks to be read.
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
{
SD_Error errorstatus = SD_OK;
TransferError = SD_OK;
TransferEnd = 0;
StopCondition = 1;
/**
* @brief This function waits until the SDIO DMA data transfer is finished.
* This function should be called after SDIO_ReadMultiBlocks() function
* to insure that all data sent by the card are already transferred by
* the DMA controller.
* @param None.
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_WaitReadOperation(void)
{
SD_Error errorstatus = SD_OK;
if (TransferError != SD_OK)
{
return(TransferError);
}
return(errorstatus);
}
/**
* @brief Allows to write one block starting from a specified address in a card.
* The Data transfer can be managed by DMA mode or Polling mode.
* @note This operation should be followed by two functions to check if the
* DMA Controller and SD Card status.
* - SD_ReadWaitOperation(): this function insure that the DMA
* controller has finished all data transfer.
* - SD_GetStatus(): to check that the SD Card has finished the
* data transfer and it is ready for data.
* @param writebuff: pointer to the buffer that contain the data to be transferred.
* @param WriteAddr: Address from where data are to be read.
* @param BlockSize: the SD card Data block size. The Block size should be 512.
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize)
{
SD_Error errorstatus = SD_OK;
/*!< In case of single data block transfer no need of stop command at all */
#if defined (SD_POLLING_MODE)
while (!(SDIO->STA & (SDIO_FLAG_DBCKEND | SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
{
if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET)
{
if ((512 - bytestransferred) < 32)
{
restwords = ((512 - bytestransferred) % 4 == 0) ? ((512 - bytestransferred) / 4) : (( 512 - bytestransferred) / 4 + 1);
for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4)
{
SDIO_WriteData(*tempbuff);
}
}
else
{
for (count = 0; count < 8; count++)
{
SDIO_WriteData(*(tempbuff + count));
}
tempbuff += 8;
bytestransferred += 32;
}
}
}
if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
{
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
errorstatus = SD_DATA_TIMEOUT;
return(errorstatus);
}
else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
{
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
errorstatus = SD_DATA_CRC_FAIL;
return(errorstatus);
}
else if (SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET)
{
SDIO_ClearFlag(SDIO_FLAG_TXUNDERR);
errorstatus = SD_TX_UNDERRUN;
return(errorstatus);
}
else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
{
SDIO_ClearFlag(SDIO_FLAG_STBITERR);
errorstatus = SD_START_BIT_ERR;
return(errorstatus);
}
#elif defined (SD_DMA_MODE)
SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE);
SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, BlockSize);
SDIO_DMACmd(ENABLE);
#endif
return(errorstatus);
}
/**
* @brief Allows to write blocks starting from a specified address in a card.
* The Data transfer can be managed by DMA mode only.
* @note This operation should be followed by two functions to check if the
* DMA Controller and SD Card status.
* - SD_ReadWaitOperation(): this function insure that the DMA
* controller has finished all data transfer.
* - SD_GetStatus(): to check that the SD Card has finished the
* data transfer and it is ready for data.
* @param WriteAddr: Address from where data are to be read.
* @param writebuff: pointer to the buffer that contain the data to be transferred.
* @param BlockSize: the SD card Data block size. The Block size should be 512.
* @param NumberOfBlocks: number of blocks to be written.
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
{
SD_Error errorstatus = SD_OK;
__IO uint32_t count = 0;
/**
* @brief This function waits until the SDIO DMA data transfer is finished.
* This function should be called after SDIO_WriteBlock() and
* SDIO_WriteMultiBlocks() function to insure that all data sent by the
* card are already transferred by the DMA controller.
* @param None.
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_WaitWriteOperation(void)
{
SD_Error errorstatus = SD_OK;
if (TransferError != SD_OK)
{
return(TransferError);
}
/*!< Clear all the static flags */
SDIO_ClearFlag(SDIO_STATIC_FLAGS);
return(errorstatus);
}
/**
* @brief Gets the cuurent data transfer state.
* @param None
* @retval SDTransferState: Data Transfer state.
* This value can be:
* - SD_TRANSFER_OK: No data transfer is acting
* - SD_TRANSFER_BUSY: Data transfer is acting
*/
SDTransferState SD_GetTransferState(void)
{
if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT))
{
return(SD_TRANSFER_BUSY);
}
else
{
return(SD_TRANSFER_OK);
}
}
/**
* @brief Returns the current card's status.
* @param pcardstatus: pointer to the buffer that will contain the SD card
* status (Card Status register).
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_SendStatus(uint32_t *pcardstatus)
{
SD_Error errorstatus = SD_OK;
/**
* @brief Returns the current SD card's status.
* @param psdstatus: pointer to the buffer that will contain the SD card status
* (SD Status register).
* @retval SD_Error: SD Card Error code.
*/
SD_Error SD_SendSDStatus(uint32_t *psdstatus)
{
SD_Error errorstatus = SD_OK;
uint32_t count = 0;
if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
{
errorstatus = SD_LOCK_UNLOCK_FAILED;
return(errorstatus);
}
/*!< Set block size for card if it is not equal to current block size for card. */
SDIO_CmdInitStructure.SDIO_Argument = 64;
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
if (errorstatus != SD_OK)
{
return(errorstatus);
}
while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) && (timeout > 0))
{
timeout--;
status = SDIO->STA;
}
if ((timeout == 0) || (status & SDIO_FLAG_CTIMEOUT))
{
/*!< Card is not V2.0 complient or card does not support the set voltage range */
errorstatus = SD_CMD_RSP_TIMEOUT;
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
return(errorstatus);
}
if (status & SDIO_FLAG_CMDREND)
{
/*!< Card is SD V2.0 compliant */
errorstatus = SD_OK;
SDIO_ClearFlag(SDIO_FLAG_CMDREND);
return(errorstatus);
}
return(errorstatus);
}
/**
* @brief Checks for error conditions for R1 response.
* @param cmd: The sent command index.
* @retval SD_Error: SD Card Error code.
*/
static SD_Error CmdResp1Error(uint8_t cmd)
{
while (!(SDIO->STA & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
{
}
if (response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR)
{
return(SD_GENERAL_UNKNOWN_ERROR);
}
if (response_r1 & SD_R6_ILLEGAL_CMD)
{
return(SD_ILLEGAL_CMD);
}
if (response_r1 & SD_R6_COM_CRC_FAILED)
{
return(SD_COM_CRC_FAILED);
}
return(errorstatus);
}
/**
* @brief Enables or disables the SDIO wide bus mode.
* @param NewState: new state of the SDIO wide bus mode.
* This parameter can be: ENABLE or DISABLE.
* @retval SD_Error: SD Card Error code.
*/
static SD_Error SDEnWideBus(FunctionalState NewState)
{
SD_Error errorstatus = SD_OK;
uint32_t scr[2] = {0, 0};
if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
{
errorstatus = SD_LOCK_UNLOCK_FAILED;
return(errorstatus);
}
/*!< Get SCR Register */
errorstatus = FindSCR(RCA, scr);
if (errorstatus != SD_OK)
{
return(errorstatus);
}
/*!< If wide bus operation to be enabled */
if (NewState == ENABLE)
{
/*!< If requested card supports wide bus operation */
if ((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
{
/*!< Send CMD55 APP_CMD with argument as card's RCA.*/
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
if (errorstatus != SD_OK)
{
return(errorstatus);
}
/*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
SDIO_CmdInitStructure.SDIO_Argument = 0x2;
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
/**
* @brief Converts the number of bytes in power of two and returns the power.
* @param NumberOfBytes: number of bytes.
* @retval None
*/
uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes)
{
uint8_t count = 0;