Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /**
sahilmgandhi 18:6a4db94011d3 2 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 3 * @file stm32f4xx_hal_sd.c
sahilmgandhi 18:6a4db94011d3 4 * @author MCD Application Team
sahilmgandhi 18:6a4db94011d3 5 * @version V1.5.0
sahilmgandhi 18:6a4db94011d3 6 * @date 06-May-2016
sahilmgandhi 18:6a4db94011d3 7 * @brief SD card HAL module driver.
sahilmgandhi 18:6a4db94011d3 8 * This file provides firmware functions to manage the following
sahilmgandhi 18:6a4db94011d3 9 * functionalities of the Secure Digital (SD) peripheral:
sahilmgandhi 18:6a4db94011d3 10 * + Initialization and de-initialization functions
sahilmgandhi 18:6a4db94011d3 11 * + IO operation functions
sahilmgandhi 18:6a4db94011d3 12 * + Peripheral Control functions
sahilmgandhi 18:6a4db94011d3 13 * + Peripheral State functions
sahilmgandhi 18:6a4db94011d3 14 *
sahilmgandhi 18:6a4db94011d3 15 @verbatim
sahilmgandhi 18:6a4db94011d3 16 ==============================================================================
sahilmgandhi 18:6a4db94011d3 17 ##### How to use this driver #####
sahilmgandhi 18:6a4db94011d3 18 ==============================================================================
sahilmgandhi 18:6a4db94011d3 19 [..]
sahilmgandhi 18:6a4db94011d3 20 This driver implements a high level communication layer for read and write from/to
sahilmgandhi 18:6a4db94011d3 21 this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by
sahilmgandhi 18:6a4db94011d3 22 the user in HAL_SD_MspInit() function (MSP layer).
sahilmgandhi 18:6a4db94011d3 23 Basically, the MSP layer configuration should be the same as we provide in the
sahilmgandhi 18:6a4db94011d3 24 examples.
sahilmgandhi 18:6a4db94011d3 25 You can easily tailor this configuration according to hardware resources.
sahilmgandhi 18:6a4db94011d3 26
sahilmgandhi 18:6a4db94011d3 27 [..]
sahilmgandhi 18:6a4db94011d3 28 This driver is a generic layered driver for SDIO memories which uses the HAL
sahilmgandhi 18:6a4db94011d3 29 SDIO driver functions to interface with SD and uSD cards devices.
sahilmgandhi 18:6a4db94011d3 30 It is used as follows:
sahilmgandhi 18:6a4db94011d3 31
sahilmgandhi 18:6a4db94011d3 32 (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API:
sahilmgandhi 18:6a4db94011d3 33 (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE();
sahilmgandhi 18:6a4db94011d3 34 (##) SDIO pins configuration for SD card
sahilmgandhi 18:6a4db94011d3 35 (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
sahilmgandhi 18:6a4db94011d3 36 (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init()
sahilmgandhi 18:6a4db94011d3 37 and according to your pin assignment;
sahilmgandhi 18:6a4db94011d3 38 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
sahilmgandhi 18:6a4db94011d3 39 and HAL_SD_WriteBlocks_DMA() APIs).
sahilmgandhi 18:6a4db94011d3 40 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
sahilmgandhi 18:6a4db94011d3 41 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
sahilmgandhi 18:6a4db94011d3 42 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
sahilmgandhi 18:6a4db94011d3 43 (+++) Configure the SDIO and DMA interrupt priorities using functions
sahilmgandhi 18:6a4db94011d3 44 HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
sahilmgandhi 18:6a4db94011d3 45 (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
sahilmgandhi 18:6a4db94011d3 46 (+++) SDIO interrupts are managed using the macros __HAL_SD_SDIO_ENABLE_IT()
sahilmgandhi 18:6a4db94011d3 47 and __HAL_SD_SDIO_DISABLE_IT() inside the communication process.
sahilmgandhi 18:6a4db94011d3 48 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_SDIO_GET_IT()
sahilmgandhi 18:6a4db94011d3 49 and __HAL_SD_SDIO_CLEAR_IT()
sahilmgandhi 18:6a4db94011d3 50 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
sahilmgandhi 18:6a4db94011d3 51
sahilmgandhi 18:6a4db94011d3 52
sahilmgandhi 18:6a4db94011d3 53 *** SD Card Initialization and configuration ***
sahilmgandhi 18:6a4db94011d3 54 ================================================
sahilmgandhi 18:6a4db94011d3 55 [..]
sahilmgandhi 18:6a4db94011d3 56 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
sahilmgandhi 18:6a4db94011d3 57 the SD Card and put it into Standby State (Ready for data transfer).
sahilmgandhi 18:6a4db94011d3 58 This function provide the following operations:
sahilmgandhi 18:6a4db94011d3 59
sahilmgandhi 18:6a4db94011d3 60 (#) Apply the SD Card initialization process at 400KHz and check the SD Card
sahilmgandhi 18:6a4db94011d3 61 type (Standard Capacity or High Capacity). You can change or adapt this
sahilmgandhi 18:6a4db94011d3 62 frequency by adjusting the "ClockDiv" field.
sahilmgandhi 18:6a4db94011d3 63 The SD Card frequency (SDIO_CK) is computed as follows:
sahilmgandhi 18:6a4db94011d3 64
sahilmgandhi 18:6a4db94011d3 65 SDIO_CK = SDIOCLK / (ClockDiv + 2)
sahilmgandhi 18:6a4db94011d3 66
sahilmgandhi 18:6a4db94011d3 67 In initialization mode and according to the SD Card standard,
sahilmgandhi 18:6a4db94011d3 68 make sure that the SDIO_CK frequency doesn't exceed 400KHz.
sahilmgandhi 18:6a4db94011d3 69
sahilmgandhi 18:6a4db94011d3 70 (#) Get the SD CID and CSD data. All these information are managed by the SDCardInfo
sahilmgandhi 18:6a4db94011d3 71 structure. This structure provide also ready computed SD Card capacity
sahilmgandhi 18:6a4db94011d3 72 and Block size.
sahilmgandhi 18:6a4db94011d3 73
sahilmgandhi 18:6a4db94011d3 74 -@- These information are stored in SD handle structure in case of future use.
sahilmgandhi 18:6a4db94011d3 75
sahilmgandhi 18:6a4db94011d3 76 (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
sahilmgandhi 18:6a4db94011d3 77 frequency is set to 24MHz. You can change or adapt this frequency by adjusting
sahilmgandhi 18:6a4db94011d3 78 the "ClockDiv" field.
sahilmgandhi 18:6a4db94011d3 79 In transfer mode and according to the SD Card standard, make sure that the
sahilmgandhi 18:6a4db94011d3 80 SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
sahilmgandhi 18:6a4db94011d3 81 To be able to use a frequency higher than 24MHz, you should use the SDIO
sahilmgandhi 18:6a4db94011d3 82 peripheral in bypass mode. Refer to the corresponding reference manual
sahilmgandhi 18:6a4db94011d3 83 for more details.
sahilmgandhi 18:6a4db94011d3 84
sahilmgandhi 18:6a4db94011d3 85 (#) Select the corresponding SD Card according to the address read with the step 2.
sahilmgandhi 18:6a4db94011d3 86
sahilmgandhi 18:6a4db94011d3 87 (#) Configure the SD Card in wide bus mode: 4-bits data.
sahilmgandhi 18:6a4db94011d3 88
sahilmgandhi 18:6a4db94011d3 89 *** SD Card Read operation ***
sahilmgandhi 18:6a4db94011d3 90 ==============================
sahilmgandhi 18:6a4db94011d3 91 [..]
sahilmgandhi 18:6a4db94011d3 92 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
sahilmgandhi 18:6a4db94011d3 93 This function support only 512-bytes block length (the block size should be
sahilmgandhi 18:6a4db94011d3 94 chosen as 512 bytes).
sahilmgandhi 18:6a4db94011d3 95 You can choose either one block read operation or multiple block read operation
sahilmgandhi 18:6a4db94011d3 96 by adjusting the "NumberOfBlocks" parameter.
sahilmgandhi 18:6a4db94011d3 97
sahilmgandhi 18:6a4db94011d3 98 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
sahilmgandhi 18:6a4db94011d3 99 This function support only 512-bytes block length (the block size should be
sahilmgandhi 18:6a4db94011d3 100 chosen as 512 bytes).
sahilmgandhi 18:6a4db94011d3 101 You can choose either one block read operation or multiple block read operation
sahilmgandhi 18:6a4db94011d3 102 by adjusting the "NumberOfBlocks" parameter.
sahilmgandhi 18:6a4db94011d3 103 After this, you have to call the function HAL_SD_CheckReadOperation(), to insure
sahilmgandhi 18:6a4db94011d3 104 that the read transfer is done correctly in both DMA and SD sides.
sahilmgandhi 18:6a4db94011d3 105
sahilmgandhi 18:6a4db94011d3 106 *** SD Card Write operation ***
sahilmgandhi 18:6a4db94011d3 107 ===============================
sahilmgandhi 18:6a4db94011d3 108 [..]
sahilmgandhi 18:6a4db94011d3 109 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
sahilmgandhi 18:6a4db94011d3 110 This function support only 512-bytes block length (the block size should be
sahilmgandhi 18:6a4db94011d3 111 chosen as 512 bytes).
sahilmgandhi 18:6a4db94011d3 112 You can choose either one block read operation or multiple block read operation
sahilmgandhi 18:6a4db94011d3 113 by adjusting the "NumberOfBlocks" parameter.
sahilmgandhi 18:6a4db94011d3 114
sahilmgandhi 18:6a4db94011d3 115 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
sahilmgandhi 18:6a4db94011d3 116 This function support only 512-bytes block length (the block size should be
sahilmgandhi 18:6a4db94011d3 117 chosen as 512 byte).
sahilmgandhi 18:6a4db94011d3 118 You can choose either one block read operation or multiple block read operation
sahilmgandhi 18:6a4db94011d3 119 by adjusting the "NumberOfBlocks" parameter.
sahilmgandhi 18:6a4db94011d3 120 After this, you have to call the function HAL_SD_CheckWriteOperation(), to insure
sahilmgandhi 18:6a4db94011d3 121 that the write transfer is done correctly in both DMA and SD sides.
sahilmgandhi 18:6a4db94011d3 122
sahilmgandhi 18:6a4db94011d3 123 *** SD card status ***
sahilmgandhi 18:6a4db94011d3 124 ======================
sahilmgandhi 18:6a4db94011d3 125 [..]
sahilmgandhi 18:6a4db94011d3 126 (+) At any time, you can check the SD Card status and get the SD card state
sahilmgandhi 18:6a4db94011d3 127 by using the HAL_SD_GetStatus() function. This function checks first if the
sahilmgandhi 18:6a4db94011d3 128 SD card is still connected and then get the internal SD Card transfer state.
sahilmgandhi 18:6a4db94011d3 129 (+) You can also get the SD card SD Status register by using the HAL_SD_SendSDStatus()
sahilmgandhi 18:6a4db94011d3 130 function.
sahilmgandhi 18:6a4db94011d3 131
sahilmgandhi 18:6a4db94011d3 132 *** SD HAL driver macros list ***
sahilmgandhi 18:6a4db94011d3 133 ==================================
sahilmgandhi 18:6a4db94011d3 134 [..]
sahilmgandhi 18:6a4db94011d3 135 Below the list of most used macros in SD HAL driver.
sahilmgandhi 18:6a4db94011d3 136
sahilmgandhi 18:6a4db94011d3 137 (+) __HAL_SD_SDIO_ENABLE : Enable the SD device
sahilmgandhi 18:6a4db94011d3 138 (+) __HAL_SD_SDIO_DISABLE : Disable the SD device
sahilmgandhi 18:6a4db94011d3 139 (+) __HAL_SD_SDIO_DMA_ENABLE: Enable the SDIO DMA transfer
sahilmgandhi 18:6a4db94011d3 140 (+) __HAL_SD_SDIO_DMA_DISABLE: Disable the SDIO DMA transfer
sahilmgandhi 18:6a4db94011d3 141 (+) __HAL_SD_SDIO_ENABLE_IT: Enable the SD device interrupt
sahilmgandhi 18:6a4db94011d3 142 (+) __HAL_SD_SDIO_DISABLE_IT: Disable the SD device interrupt
sahilmgandhi 18:6a4db94011d3 143 (+) __HAL_SD_SDIO_GET_FLAG:Check whether the specified SD flag is set or not
sahilmgandhi 18:6a4db94011d3 144 (+) __HAL_SD_SDIO_CLEAR_FLAG: Clear the SD's pending flags
sahilmgandhi 18:6a4db94011d3 145
sahilmgandhi 18:6a4db94011d3 146 (@) You can refer to the SD HAL driver header file for more useful macros
sahilmgandhi 18:6a4db94011d3 147
sahilmgandhi 18:6a4db94011d3 148 @endverbatim
sahilmgandhi 18:6a4db94011d3 149 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 150 * @attention
sahilmgandhi 18:6a4db94011d3 151 *
sahilmgandhi 18:6a4db94011d3 152 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
sahilmgandhi 18:6a4db94011d3 153 *
sahilmgandhi 18:6a4db94011d3 154 * Redistribution and use in source and binary forms, with or without modification,
sahilmgandhi 18:6a4db94011d3 155 * are permitted provided that the following conditions are met:
sahilmgandhi 18:6a4db94011d3 156 * 1. Redistributions of source code must retain the above copyright notice,
sahilmgandhi 18:6a4db94011d3 157 * this list of conditions and the following disclaimer.
sahilmgandhi 18:6a4db94011d3 158 * 2. Redistributions in binary form must reproduce the above copyright notice,
sahilmgandhi 18:6a4db94011d3 159 * this list of conditions and the following disclaimer in the documentation
sahilmgandhi 18:6a4db94011d3 160 * and/or other materials provided with the distribution.
sahilmgandhi 18:6a4db94011d3 161 * 3. Neither the name of STMicroelectronics nor the names of its contributors
sahilmgandhi 18:6a4db94011d3 162 * may be used to endorse or promote products derived from this software
sahilmgandhi 18:6a4db94011d3 163 * without specific prior written permission.
sahilmgandhi 18:6a4db94011d3 164 *
sahilmgandhi 18:6a4db94011d3 165 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
sahilmgandhi 18:6a4db94011d3 166 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sahilmgandhi 18:6a4db94011d3 167 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
sahilmgandhi 18:6a4db94011d3 168 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
sahilmgandhi 18:6a4db94011d3 169 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sahilmgandhi 18:6a4db94011d3 170 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
sahilmgandhi 18:6a4db94011d3 171 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
sahilmgandhi 18:6a4db94011d3 172 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
sahilmgandhi 18:6a4db94011d3 173 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
sahilmgandhi 18:6a4db94011d3 174 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sahilmgandhi 18:6a4db94011d3 175 *
sahilmgandhi 18:6a4db94011d3 176 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 177 */
sahilmgandhi 18:6a4db94011d3 178
sahilmgandhi 18:6a4db94011d3 179 /* Includes ------------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 180 #include "stm32f4xx_hal.h"
sahilmgandhi 18:6a4db94011d3 181
sahilmgandhi 18:6a4db94011d3 182 #ifdef HAL_SD_MODULE_ENABLED
sahilmgandhi 18:6a4db94011d3 183 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
sahilmgandhi 18:6a4db94011d3 184 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
sahilmgandhi 18:6a4db94011d3 185 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
sahilmgandhi 18:6a4db94011d3 186 defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
sahilmgandhi 18:6a4db94011d3 187 defined(STM32F412Rx) || defined(STM32F412Cx)
sahilmgandhi 18:6a4db94011d3 188 /** @addtogroup STM32F4xx_HAL_Driver
sahilmgandhi 18:6a4db94011d3 189 * @{
sahilmgandhi 18:6a4db94011d3 190 */
sahilmgandhi 18:6a4db94011d3 191
sahilmgandhi 18:6a4db94011d3 192 /** @addtogroup SD
sahilmgandhi 18:6a4db94011d3 193 * @{
sahilmgandhi 18:6a4db94011d3 194 */
sahilmgandhi 18:6a4db94011d3 195
sahilmgandhi 18:6a4db94011d3 196 /* Private typedef -----------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 197 /* Private define ------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 198 /** @addtogroup SD_Private_Defines
sahilmgandhi 18:6a4db94011d3 199 * @{
sahilmgandhi 18:6a4db94011d3 200 */
sahilmgandhi 18:6a4db94011d3 201 /**
sahilmgandhi 18:6a4db94011d3 202 * @brief SDIO Data block size
sahilmgandhi 18:6a4db94011d3 203 */
sahilmgandhi 18:6a4db94011d3 204 #define DATA_BLOCK_SIZE ((uint32_t)(9U << 4U))
sahilmgandhi 18:6a4db94011d3 205 /**
sahilmgandhi 18:6a4db94011d3 206 * @brief SDIO Static flags, Timeout, FIFO Address
sahilmgandhi 18:6a4db94011d3 207 */
sahilmgandhi 18:6a4db94011d3 208 #define SDIO_STATIC_FLAGS ((uint32_t)(SDIO_FLAG_CCRCFAIL | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_CTIMEOUT |\
sahilmgandhi 18:6a4db94011d3 209 SDIO_FLAG_DTIMEOUT | SDIO_FLAG_TXUNDERR | SDIO_FLAG_RXOVERR |\
sahilmgandhi 18:6a4db94011d3 210 SDIO_FLAG_CMDREND | SDIO_FLAG_CMDSENT | SDIO_FLAG_DATAEND |\
sahilmgandhi 18:6a4db94011d3 211 SDIO_FLAG_DBCKEND))
sahilmgandhi 18:6a4db94011d3 212
sahilmgandhi 18:6a4db94011d3 213 #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000U)
sahilmgandhi 18:6a4db94011d3 214
sahilmgandhi 18:6a4db94011d3 215 /**
sahilmgandhi 18:6a4db94011d3 216 * @brief Mask for errors Card Status R1 (OCR Register)
sahilmgandhi 18:6a4db94011d3 217 */
sahilmgandhi 18:6a4db94011d3 218 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000U)
sahilmgandhi 18:6a4db94011d3 219 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000U)
sahilmgandhi 18:6a4db94011d3 220 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000U)
sahilmgandhi 18:6a4db94011d3 221 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000U)
sahilmgandhi 18:6a4db94011d3 222 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000U)
sahilmgandhi 18:6a4db94011d3 223 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000U)
sahilmgandhi 18:6a4db94011d3 224 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000U)
sahilmgandhi 18:6a4db94011d3 225 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000U)
sahilmgandhi 18:6a4db94011d3 226 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000U)
sahilmgandhi 18:6a4db94011d3 227 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000U)
sahilmgandhi 18:6a4db94011d3 228 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000U)
sahilmgandhi 18:6a4db94011d3 229 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000U)
sahilmgandhi 18:6a4db94011d3 230 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000U)
sahilmgandhi 18:6a4db94011d3 231 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000U)
sahilmgandhi 18:6a4db94011d3 232 #define SD_OCR_CID_CSD_OVERWRITE ((uint32_t)0x00010000U)
sahilmgandhi 18:6a4db94011d3 233 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000U)
sahilmgandhi 18:6a4db94011d3 234 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000U)
sahilmgandhi 18:6a4db94011d3 235 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000U)
sahilmgandhi 18:6a4db94011d3 236 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008U)
sahilmgandhi 18:6a4db94011d3 237 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008U)
sahilmgandhi 18:6a4db94011d3 238
sahilmgandhi 18:6a4db94011d3 239 /**
sahilmgandhi 18:6a4db94011d3 240 * @brief Masks for R6 Response
sahilmgandhi 18:6a4db94011d3 241 */
sahilmgandhi 18:6a4db94011d3 242 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000U)
sahilmgandhi 18:6a4db94011d3 243 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000U)
sahilmgandhi 18:6a4db94011d3 244 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000U)
sahilmgandhi 18:6a4db94011d3 245
sahilmgandhi 18:6a4db94011d3 246 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000U)
sahilmgandhi 18:6a4db94011d3 247 #define SD_HIGH_CAPACITY ((uint32_t)0x40000000U)
sahilmgandhi 18:6a4db94011d3 248 #define SD_STD_CAPACITY ((uint32_t)0x00000000U)
sahilmgandhi 18:6a4db94011d3 249 #define SD_CHECK_PATTERN ((uint32_t)0x000001AAU)
sahilmgandhi 18:6a4db94011d3 250
sahilmgandhi 18:6a4db94011d3 251 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFFU)
sahilmgandhi 18:6a4db94011d3 252 #define SD_ALLZERO ((uint32_t)0x00000000U)
sahilmgandhi 18:6a4db94011d3 253
sahilmgandhi 18:6a4db94011d3 254 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000U)
sahilmgandhi 18:6a4db94011d3 255 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000U)
sahilmgandhi 18:6a4db94011d3 256 #define SD_CARD_LOCKED ((uint32_t)0x02000000U)
sahilmgandhi 18:6a4db94011d3 257
sahilmgandhi 18:6a4db94011d3 258 #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFFU)
sahilmgandhi 18:6a4db94011d3 259 #define SD_0TO7BITS ((uint32_t)0x000000FFU)
sahilmgandhi 18:6a4db94011d3 260 #define SD_8TO15BITS ((uint32_t)0x0000FF00U)
sahilmgandhi 18:6a4db94011d3 261 #define SD_16TO23BITS ((uint32_t)0x00FF0000U)
sahilmgandhi 18:6a4db94011d3 262 #define SD_24TO31BITS ((uint32_t)0xFF000000U)
sahilmgandhi 18:6a4db94011d3 263 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFFU)
sahilmgandhi 18:6a4db94011d3 264
sahilmgandhi 18:6a4db94011d3 265 #define SD_HALFFIFO ((uint32_t)0x00000008U)
sahilmgandhi 18:6a4db94011d3 266 #define SD_HALFFIFOBYTES ((uint32_t)0x00000020U)
sahilmgandhi 18:6a4db94011d3 267
sahilmgandhi 18:6a4db94011d3 268 /**
sahilmgandhi 18:6a4db94011d3 269 * @brief Command Class Supported
sahilmgandhi 18:6a4db94011d3 270 */
sahilmgandhi 18:6a4db94011d3 271 #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080U)
sahilmgandhi 18:6a4db94011d3 272 #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040U)
sahilmgandhi 18:6a4db94011d3 273 #define SD_CCCC_ERASE ((uint32_t)0x00000020U)
sahilmgandhi 18:6a4db94011d3 274
sahilmgandhi 18:6a4db94011d3 275 /**
sahilmgandhi 18:6a4db94011d3 276 * @brief Following commands are SD Card Specific commands.
sahilmgandhi 18:6a4db94011d3 277 * SDIO_APP_CMD should be sent before sending these commands.
sahilmgandhi 18:6a4db94011d3 278 */
sahilmgandhi 18:6a4db94011d3 279 #define SD_SDIO_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD)
sahilmgandhi 18:6a4db94011d3 280
sahilmgandhi 18:6a4db94011d3 281 /**
sahilmgandhi 18:6a4db94011d3 282 * @}
sahilmgandhi 18:6a4db94011d3 283 */
sahilmgandhi 18:6a4db94011d3 284
sahilmgandhi 18:6a4db94011d3 285 /* Private macro -------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 286 /* Private variables ---------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 287 /* Private function prototypes -----------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 288 /** @addtogroup SD_Private_Functions_Prototypes
sahilmgandhi 18:6a4db94011d3 289 * @{
sahilmgandhi 18:6a4db94011d3 290 */
sahilmgandhi 18:6a4db94011d3 291 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 292 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr);
sahilmgandhi 18:6a4db94011d3 293 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 294 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 295 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
sahilmgandhi 18:6a4db94011d3 296 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 297 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus);
sahilmgandhi 18:6a4db94011d3 298 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 299 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD);
sahilmgandhi 18:6a4db94011d3 300 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 301 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 302 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 303 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA);
sahilmgandhi 18:6a4db94011d3 304 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 305 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd);
sahilmgandhi 18:6a4db94011d3 306 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
sahilmgandhi 18:6a4db94011d3 307 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma);
sahilmgandhi 18:6a4db94011d3 308 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma);
sahilmgandhi 18:6a4db94011d3 309 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma);
sahilmgandhi 18:6a4db94011d3 310 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma);
sahilmgandhi 18:6a4db94011d3 311 /**
sahilmgandhi 18:6a4db94011d3 312 * @}
sahilmgandhi 18:6a4db94011d3 313 */
sahilmgandhi 18:6a4db94011d3 314 /* Exported functions --------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 315 /** @addtogroup SD_Exported_Functions
sahilmgandhi 18:6a4db94011d3 316 * @{
sahilmgandhi 18:6a4db94011d3 317 */
sahilmgandhi 18:6a4db94011d3 318
sahilmgandhi 18:6a4db94011d3 319 /** @addtogroup SD_Exported_Functions_Group1
sahilmgandhi 18:6a4db94011d3 320 * @brief Initialization and de-initialization functions
sahilmgandhi 18:6a4db94011d3 321 *
sahilmgandhi 18:6a4db94011d3 322 @verbatim
sahilmgandhi 18:6a4db94011d3 323 ==============================================================================
sahilmgandhi 18:6a4db94011d3 324 ##### Initialization and de-initialization functions #####
sahilmgandhi 18:6a4db94011d3 325 ==============================================================================
sahilmgandhi 18:6a4db94011d3 326 [..]
sahilmgandhi 18:6a4db94011d3 327 This section provides functions allowing to initialize/de-initialize the SD
sahilmgandhi 18:6a4db94011d3 328 card device to be ready for use.
sahilmgandhi 18:6a4db94011d3 329
sahilmgandhi 18:6a4db94011d3 330
sahilmgandhi 18:6a4db94011d3 331 @endverbatim
sahilmgandhi 18:6a4db94011d3 332 * @{
sahilmgandhi 18:6a4db94011d3 333 */
sahilmgandhi 18:6a4db94011d3 334
sahilmgandhi 18:6a4db94011d3 335 /**
sahilmgandhi 18:6a4db94011d3 336 * @brief Initializes the SD card according to the specified parameters in the
sahilmgandhi 18:6a4db94011d3 337 SD_HandleTypeDef and create the associated handle.
sahilmgandhi 18:6a4db94011d3 338 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 339 * @param SDCardInfo: HAL_SD_CardInfoTypedef structure for SD card information
sahilmgandhi 18:6a4db94011d3 340 * @retval HAL SD error state
sahilmgandhi 18:6a4db94011d3 341 */
sahilmgandhi 18:6a4db94011d3 342 HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo)
sahilmgandhi 18:6a4db94011d3 343 {
sahilmgandhi 18:6a4db94011d3 344 __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 345 SD_InitTypeDef tmpinit;
sahilmgandhi 18:6a4db94011d3 346
sahilmgandhi 18:6a4db94011d3 347 /* Allocate lock resource and initialize it */
sahilmgandhi 18:6a4db94011d3 348 hsd->Lock = HAL_UNLOCKED;
sahilmgandhi 18:6a4db94011d3 349 /* Initialize the low level hardware (MSP) */
sahilmgandhi 18:6a4db94011d3 350 HAL_SD_MspInit(hsd);
sahilmgandhi 18:6a4db94011d3 351
sahilmgandhi 18:6a4db94011d3 352 /* Default SDIO peripheral configuration for SD card initialization */
sahilmgandhi 18:6a4db94011d3 353 tmpinit.ClockEdge = SDIO_CLOCK_EDGE_RISING;
sahilmgandhi 18:6a4db94011d3 354 tmpinit.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
sahilmgandhi 18:6a4db94011d3 355 tmpinit.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
sahilmgandhi 18:6a4db94011d3 356 tmpinit.BusWide = SDIO_BUS_WIDE_1B;
sahilmgandhi 18:6a4db94011d3 357 tmpinit.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
sahilmgandhi 18:6a4db94011d3 358 tmpinit.ClockDiv = SDIO_INIT_CLK_DIV;
sahilmgandhi 18:6a4db94011d3 359
sahilmgandhi 18:6a4db94011d3 360 /* Initialize SDIO peripheral interface with default configuration */
sahilmgandhi 18:6a4db94011d3 361 SDIO_Init(hsd->Instance, tmpinit);
sahilmgandhi 18:6a4db94011d3 362
sahilmgandhi 18:6a4db94011d3 363 /* Identify card operating voltage */
sahilmgandhi 18:6a4db94011d3 364 errorstate = SD_PowerON(hsd);
sahilmgandhi 18:6a4db94011d3 365
sahilmgandhi 18:6a4db94011d3 366 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 367 {
sahilmgandhi 18:6a4db94011d3 368 return errorstate;
sahilmgandhi 18:6a4db94011d3 369 }
sahilmgandhi 18:6a4db94011d3 370
sahilmgandhi 18:6a4db94011d3 371 /* Initialize the present SDIO card(s) and put them in idle state */
sahilmgandhi 18:6a4db94011d3 372 errorstate = SD_Initialize_Cards(hsd);
sahilmgandhi 18:6a4db94011d3 373
sahilmgandhi 18:6a4db94011d3 374 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 375 {
sahilmgandhi 18:6a4db94011d3 376 return errorstate;
sahilmgandhi 18:6a4db94011d3 377 }
sahilmgandhi 18:6a4db94011d3 378
sahilmgandhi 18:6a4db94011d3 379 /* Read CSD/CID MSD registers */
sahilmgandhi 18:6a4db94011d3 380 errorstate = HAL_SD_Get_CardInfo(hsd, SDCardInfo);
sahilmgandhi 18:6a4db94011d3 381
sahilmgandhi 18:6a4db94011d3 382 if (errorstate == SD_OK)
sahilmgandhi 18:6a4db94011d3 383 {
sahilmgandhi 18:6a4db94011d3 384 /* Select the Card */
sahilmgandhi 18:6a4db94011d3 385 errorstate = SD_Select_Deselect(hsd, (uint32_t)(((uint32_t)SDCardInfo->RCA) << 16U));
sahilmgandhi 18:6a4db94011d3 386 }
sahilmgandhi 18:6a4db94011d3 387
sahilmgandhi 18:6a4db94011d3 388 /* Configure SDIO peripheral interface */
sahilmgandhi 18:6a4db94011d3 389 SDIO_Init(hsd->Instance, hsd->Init);
sahilmgandhi 18:6a4db94011d3 390
sahilmgandhi 18:6a4db94011d3 391 return errorstate;
sahilmgandhi 18:6a4db94011d3 392 }
sahilmgandhi 18:6a4db94011d3 393
sahilmgandhi 18:6a4db94011d3 394 /**
sahilmgandhi 18:6a4db94011d3 395 * @brief De-Initializes the SD card.
sahilmgandhi 18:6a4db94011d3 396 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 397 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 398 */
sahilmgandhi 18:6a4db94011d3 399 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 400 {
sahilmgandhi 18:6a4db94011d3 401
sahilmgandhi 18:6a4db94011d3 402 /* Set SD power state to off */
sahilmgandhi 18:6a4db94011d3 403 SD_PowerOFF(hsd);
sahilmgandhi 18:6a4db94011d3 404
sahilmgandhi 18:6a4db94011d3 405 /* De-Initialize the MSP layer */
sahilmgandhi 18:6a4db94011d3 406 HAL_SD_MspDeInit(hsd);
sahilmgandhi 18:6a4db94011d3 407
sahilmgandhi 18:6a4db94011d3 408 return HAL_OK;
sahilmgandhi 18:6a4db94011d3 409 }
sahilmgandhi 18:6a4db94011d3 410
sahilmgandhi 18:6a4db94011d3 411
sahilmgandhi 18:6a4db94011d3 412 /**
sahilmgandhi 18:6a4db94011d3 413 * @brief Initializes the SD MSP.
sahilmgandhi 18:6a4db94011d3 414 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 415 * @retval None
sahilmgandhi 18:6a4db94011d3 416 */
sahilmgandhi 18:6a4db94011d3 417 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 418 {
sahilmgandhi 18:6a4db94011d3 419 /* Prevent unused argument(s) compilation warning */
sahilmgandhi 18:6a4db94011d3 420 UNUSED(hsd);
sahilmgandhi 18:6a4db94011d3 421 /* NOTE : This function Should not be modified, when the callback is needed,
sahilmgandhi 18:6a4db94011d3 422 the HAL_SD_MspInit could be implemented in the user file
sahilmgandhi 18:6a4db94011d3 423 */
sahilmgandhi 18:6a4db94011d3 424 }
sahilmgandhi 18:6a4db94011d3 425
sahilmgandhi 18:6a4db94011d3 426 /**
sahilmgandhi 18:6a4db94011d3 427 * @brief De-Initialize SD MSP.
sahilmgandhi 18:6a4db94011d3 428 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 429 * @retval None
sahilmgandhi 18:6a4db94011d3 430 */
sahilmgandhi 18:6a4db94011d3 431 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 432 {
sahilmgandhi 18:6a4db94011d3 433 /* Prevent unused argument(s) compilation warning */
sahilmgandhi 18:6a4db94011d3 434 UNUSED(hsd);
sahilmgandhi 18:6a4db94011d3 435 /* NOTE : This function Should not be modified, when the callback is needed,
sahilmgandhi 18:6a4db94011d3 436 the HAL_SD_MspDeInit could be implemented in the user file
sahilmgandhi 18:6a4db94011d3 437 */
sahilmgandhi 18:6a4db94011d3 438 }
sahilmgandhi 18:6a4db94011d3 439
sahilmgandhi 18:6a4db94011d3 440 /**
sahilmgandhi 18:6a4db94011d3 441 * @}
sahilmgandhi 18:6a4db94011d3 442 */
sahilmgandhi 18:6a4db94011d3 443
sahilmgandhi 18:6a4db94011d3 444 /** @addtogroup SD_Exported_Functions_Group2
sahilmgandhi 18:6a4db94011d3 445 * @brief Data transfer functions
sahilmgandhi 18:6a4db94011d3 446 *
sahilmgandhi 18:6a4db94011d3 447 @verbatim
sahilmgandhi 18:6a4db94011d3 448 ==============================================================================
sahilmgandhi 18:6a4db94011d3 449 ##### IO operation functions #####
sahilmgandhi 18:6a4db94011d3 450 ==============================================================================
sahilmgandhi 18:6a4db94011d3 451 [..]
sahilmgandhi 18:6a4db94011d3 452 This subsection provides a set of functions allowing to manage the data
sahilmgandhi 18:6a4db94011d3 453 transfer from/to SD card.
sahilmgandhi 18:6a4db94011d3 454
sahilmgandhi 18:6a4db94011d3 455 @endverbatim
sahilmgandhi 18:6a4db94011d3 456 * @{
sahilmgandhi 18:6a4db94011d3 457 */
sahilmgandhi 18:6a4db94011d3 458
sahilmgandhi 18:6a4db94011d3 459 /**
sahilmgandhi 18:6a4db94011d3 460 * @brief Reads block(s) from a specified address in a card. The Data transfer
sahilmgandhi 18:6a4db94011d3 461 * is managed by polling mode.
sahilmgandhi 18:6a4db94011d3 462 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 463 * @param pReadBuffer: pointer to the buffer that will contain the received data
sahilmgandhi 18:6a4db94011d3 464 * @param ReadAddr: Address from where data is to be read
sahilmgandhi 18:6a4db94011d3 465 * @param BlockSize: SD card Data block size
sahilmgandhi 18:6a4db94011d3 466 * @note BlockSize must be 512 bytes.
sahilmgandhi 18:6a4db94011d3 467 * @param NumberOfBlocks: Number of SD blocks to read
sahilmgandhi 18:6a4db94011d3 468 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 469 */
sahilmgandhi 18:6a4db94011d3 470 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
sahilmgandhi 18:6a4db94011d3 471 {
sahilmgandhi 18:6a4db94011d3 472 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 473 SDIO_DataInitTypeDef sdio_datainitstructure;
sahilmgandhi 18:6a4db94011d3 474 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 475 uint32_t count = 0U, *tempbuff = (uint32_t *)pReadBuffer;
sahilmgandhi 18:6a4db94011d3 476
sahilmgandhi 18:6a4db94011d3 477 /* Initialize data control register */
sahilmgandhi 18:6a4db94011d3 478 hsd->Instance->DCTRL = 0U;
sahilmgandhi 18:6a4db94011d3 479
sahilmgandhi 18:6a4db94011d3 480 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
sahilmgandhi 18:6a4db94011d3 481 {
sahilmgandhi 18:6a4db94011d3 482 BlockSize = 512U;
sahilmgandhi 18:6a4db94011d3 483 ReadAddr /= 512U;
sahilmgandhi 18:6a4db94011d3 484 }
sahilmgandhi 18:6a4db94011d3 485
sahilmgandhi 18:6a4db94011d3 486 /* Set Block Size for Card */
sahilmgandhi 18:6a4db94011d3 487 sdio_cmdinitstructure.Argument = (uint32_t) BlockSize;
sahilmgandhi 18:6a4db94011d3 488 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
sahilmgandhi 18:6a4db94011d3 489 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 490 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 491 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 492 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 493
sahilmgandhi 18:6a4db94011d3 494 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 495 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
sahilmgandhi 18:6a4db94011d3 496
sahilmgandhi 18:6a4db94011d3 497 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 498 {
sahilmgandhi 18:6a4db94011d3 499 return errorstate;
sahilmgandhi 18:6a4db94011d3 500 }
sahilmgandhi 18:6a4db94011d3 501
sahilmgandhi 18:6a4db94011d3 502 /* Configure the SD DPSM (Data Path State Machine) */
sahilmgandhi 18:6a4db94011d3 503 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 504 sdio_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
sahilmgandhi 18:6a4db94011d3 505 sdio_datainitstructure.DataBlockSize = DATA_BLOCK_SIZE;
sahilmgandhi 18:6a4db94011d3 506 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
sahilmgandhi 18:6a4db94011d3 507 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
sahilmgandhi 18:6a4db94011d3 508 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 509 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
sahilmgandhi 18:6a4db94011d3 510
sahilmgandhi 18:6a4db94011d3 511 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 512 {
sahilmgandhi 18:6a4db94011d3 513 /* Send CMD18 READ_MULT_BLOCK with argument data address */
sahilmgandhi 18:6a4db94011d3 514 sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
sahilmgandhi 18:6a4db94011d3 515 }
sahilmgandhi 18:6a4db94011d3 516 else
sahilmgandhi 18:6a4db94011d3 517 {
sahilmgandhi 18:6a4db94011d3 518 /* Send CMD17 READ_SINGLE_BLOCK */
sahilmgandhi 18:6a4db94011d3 519 sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
sahilmgandhi 18:6a4db94011d3 520 }
sahilmgandhi 18:6a4db94011d3 521
sahilmgandhi 18:6a4db94011d3 522 sdio_cmdinitstructure.Argument = (uint32_t)ReadAddr;
sahilmgandhi 18:6a4db94011d3 523 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 524
sahilmgandhi 18:6a4db94011d3 525 /* Read block(s) in polling mode */
sahilmgandhi 18:6a4db94011d3 526 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 527 {
sahilmgandhi 18:6a4db94011d3 528 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 529 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
sahilmgandhi 18:6a4db94011d3 530
sahilmgandhi 18:6a4db94011d3 531 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 532 {
sahilmgandhi 18:6a4db94011d3 533 return errorstate;
sahilmgandhi 18:6a4db94011d3 534 }
sahilmgandhi 18:6a4db94011d3 535
sahilmgandhi 18:6a4db94011d3 536 /* Poll on SDIO flags */
sahilmgandhi 18:6a4db94011d3 537 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 538 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 539 #else /* SDIO_STA_STBITERR not defined */
sahilmgandhi 18:6a4db94011d3 540 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
sahilmgandhi 18:6a4db94011d3 541 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 542 {
sahilmgandhi 18:6a4db94011d3 543 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
sahilmgandhi 18:6a4db94011d3 544 {
sahilmgandhi 18:6a4db94011d3 545 /* Read data from SDIO Rx FIFO */
sahilmgandhi 18:6a4db94011d3 546 for (count = 0U; count < 8U; count++)
sahilmgandhi 18:6a4db94011d3 547 {
sahilmgandhi 18:6a4db94011d3 548 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 549 }
sahilmgandhi 18:6a4db94011d3 550
sahilmgandhi 18:6a4db94011d3 551 tempbuff += 8U;
sahilmgandhi 18:6a4db94011d3 552 }
sahilmgandhi 18:6a4db94011d3 553 }
sahilmgandhi 18:6a4db94011d3 554 }
sahilmgandhi 18:6a4db94011d3 555 else
sahilmgandhi 18:6a4db94011d3 556 {
sahilmgandhi 18:6a4db94011d3 557 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 558 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
sahilmgandhi 18:6a4db94011d3 559
sahilmgandhi 18:6a4db94011d3 560 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 561 {
sahilmgandhi 18:6a4db94011d3 562 return errorstate;
sahilmgandhi 18:6a4db94011d3 563 }
sahilmgandhi 18:6a4db94011d3 564
sahilmgandhi 18:6a4db94011d3 565 /* In case of single block transfer, no need of stop transfer at all */
sahilmgandhi 18:6a4db94011d3 566 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 567 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 568 #else /* SDIO_STA_STBITERR not defined */
sahilmgandhi 18:6a4db94011d3 569 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
sahilmgandhi 18:6a4db94011d3 570 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 571 {
sahilmgandhi 18:6a4db94011d3 572 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
sahilmgandhi 18:6a4db94011d3 573 {
sahilmgandhi 18:6a4db94011d3 574 /* Read data from SDIO Rx FIFO */
sahilmgandhi 18:6a4db94011d3 575 for (count = 0U; count < 8U; count++)
sahilmgandhi 18:6a4db94011d3 576 {
sahilmgandhi 18:6a4db94011d3 577 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 578 }
sahilmgandhi 18:6a4db94011d3 579
sahilmgandhi 18:6a4db94011d3 580 tempbuff += 8U;
sahilmgandhi 18:6a4db94011d3 581 }
sahilmgandhi 18:6a4db94011d3 582 }
sahilmgandhi 18:6a4db94011d3 583 }
sahilmgandhi 18:6a4db94011d3 584
sahilmgandhi 18:6a4db94011d3 585 /* Send stop transmission command in case of multiblock read */
sahilmgandhi 18:6a4db94011d3 586 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
sahilmgandhi 18:6a4db94011d3 587 {
sahilmgandhi 18:6a4db94011d3 588 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) ||\
sahilmgandhi 18:6a4db94011d3 589 (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
sahilmgandhi 18:6a4db94011d3 590 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
sahilmgandhi 18:6a4db94011d3 591 {
sahilmgandhi 18:6a4db94011d3 592 /* Send stop transmission command */
sahilmgandhi 18:6a4db94011d3 593 errorstate = HAL_SD_StopTransfer(hsd);
sahilmgandhi 18:6a4db94011d3 594 }
sahilmgandhi 18:6a4db94011d3 595 }
sahilmgandhi 18:6a4db94011d3 596
sahilmgandhi 18:6a4db94011d3 597 /* Get error state */
sahilmgandhi 18:6a4db94011d3 598 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
sahilmgandhi 18:6a4db94011d3 599 {
sahilmgandhi 18:6a4db94011d3 600 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
sahilmgandhi 18:6a4db94011d3 601
sahilmgandhi 18:6a4db94011d3 602 errorstate = SD_DATA_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 603
sahilmgandhi 18:6a4db94011d3 604 return errorstate;
sahilmgandhi 18:6a4db94011d3 605 }
sahilmgandhi 18:6a4db94011d3 606 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
sahilmgandhi 18:6a4db94011d3 607 {
sahilmgandhi 18:6a4db94011d3 608 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
sahilmgandhi 18:6a4db94011d3 609
sahilmgandhi 18:6a4db94011d3 610 errorstate = SD_DATA_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 611
sahilmgandhi 18:6a4db94011d3 612 return errorstate;
sahilmgandhi 18:6a4db94011d3 613 }
sahilmgandhi 18:6a4db94011d3 614 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
sahilmgandhi 18:6a4db94011d3 615 {
sahilmgandhi 18:6a4db94011d3 616 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
sahilmgandhi 18:6a4db94011d3 617
sahilmgandhi 18:6a4db94011d3 618 errorstate = SD_RX_OVERRUN;
sahilmgandhi 18:6a4db94011d3 619
sahilmgandhi 18:6a4db94011d3 620 return errorstate;
sahilmgandhi 18:6a4db94011d3 621 }
sahilmgandhi 18:6a4db94011d3 622 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 623 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 624 {
sahilmgandhi 18:6a4db94011d3 625 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
sahilmgandhi 18:6a4db94011d3 626
sahilmgandhi 18:6a4db94011d3 627 errorstate = SD_START_BIT_ERR;
sahilmgandhi 18:6a4db94011d3 628
sahilmgandhi 18:6a4db94011d3 629 return errorstate;
sahilmgandhi 18:6a4db94011d3 630 }
sahilmgandhi 18:6a4db94011d3 631 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 632 else
sahilmgandhi 18:6a4db94011d3 633 {
sahilmgandhi 18:6a4db94011d3 634 /* No error flag set */
sahilmgandhi 18:6a4db94011d3 635 }
sahilmgandhi 18:6a4db94011d3 636
sahilmgandhi 18:6a4db94011d3 637 count = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 638
sahilmgandhi 18:6a4db94011d3 639 /* Empty FIFO if there is still any data */
sahilmgandhi 18:6a4db94011d3 640 while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0U))
sahilmgandhi 18:6a4db94011d3 641 {
sahilmgandhi 18:6a4db94011d3 642 *tempbuff = SDIO_ReadFIFO(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 643 tempbuff++;
sahilmgandhi 18:6a4db94011d3 644 count--;
sahilmgandhi 18:6a4db94011d3 645 }
sahilmgandhi 18:6a4db94011d3 646
sahilmgandhi 18:6a4db94011d3 647 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 648 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 649
sahilmgandhi 18:6a4db94011d3 650 return errorstate;
sahilmgandhi 18:6a4db94011d3 651 }
sahilmgandhi 18:6a4db94011d3 652
sahilmgandhi 18:6a4db94011d3 653 /**
sahilmgandhi 18:6a4db94011d3 654 * @brief Allows to write block(s) to a specified address in a card. The Data
sahilmgandhi 18:6a4db94011d3 655 * transfer is managed by polling mode.
sahilmgandhi 18:6a4db94011d3 656 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 657 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
sahilmgandhi 18:6a4db94011d3 658 * @param WriteAddr: Address from where data is to be written
sahilmgandhi 18:6a4db94011d3 659 * @param BlockSize: SD card Data block size
sahilmgandhi 18:6a4db94011d3 660 * @note BlockSize must be 512 bytes.
sahilmgandhi 18:6a4db94011d3 661 * @param NumberOfBlocks: Number of SD blocks to write
sahilmgandhi 18:6a4db94011d3 662 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 663 */
sahilmgandhi 18:6a4db94011d3 664 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
sahilmgandhi 18:6a4db94011d3 665 {
sahilmgandhi 18:6a4db94011d3 666 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 667 SDIO_DataInitTypeDef sdio_datainitstructure;
sahilmgandhi 18:6a4db94011d3 668 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 669 uint32_t totalnumberofbytes = 0U, bytestransferred = 0U, count = 0U, restwords = 0U;
sahilmgandhi 18:6a4db94011d3 670 uint32_t *tempbuff = (uint32_t *)pWriteBuffer;
sahilmgandhi 18:6a4db94011d3 671 uint8_t cardstate = 0U;
sahilmgandhi 18:6a4db94011d3 672
sahilmgandhi 18:6a4db94011d3 673 /* Initialize data control register */
sahilmgandhi 18:6a4db94011d3 674 hsd->Instance->DCTRL = 0U;
sahilmgandhi 18:6a4db94011d3 675
sahilmgandhi 18:6a4db94011d3 676 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
sahilmgandhi 18:6a4db94011d3 677 {
sahilmgandhi 18:6a4db94011d3 678 BlockSize = 512U;
sahilmgandhi 18:6a4db94011d3 679 WriteAddr /= 512U;
sahilmgandhi 18:6a4db94011d3 680 }
sahilmgandhi 18:6a4db94011d3 681
sahilmgandhi 18:6a4db94011d3 682 /* Set Block Size for Card */
sahilmgandhi 18:6a4db94011d3 683 sdio_cmdinitstructure.Argument = (uint32_t)BlockSize;
sahilmgandhi 18:6a4db94011d3 684 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
sahilmgandhi 18:6a4db94011d3 685 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 686 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 687 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 688 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 689
sahilmgandhi 18:6a4db94011d3 690 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 691 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
sahilmgandhi 18:6a4db94011d3 692
sahilmgandhi 18:6a4db94011d3 693 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 694 {
sahilmgandhi 18:6a4db94011d3 695 return errorstate;
sahilmgandhi 18:6a4db94011d3 696 }
sahilmgandhi 18:6a4db94011d3 697
sahilmgandhi 18:6a4db94011d3 698 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 699 {
sahilmgandhi 18:6a4db94011d3 700 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
sahilmgandhi 18:6a4db94011d3 701 sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
sahilmgandhi 18:6a4db94011d3 702 }
sahilmgandhi 18:6a4db94011d3 703 else
sahilmgandhi 18:6a4db94011d3 704 {
sahilmgandhi 18:6a4db94011d3 705 /* Send CMD24 WRITE_SINGLE_BLOCK */
sahilmgandhi 18:6a4db94011d3 706 sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
sahilmgandhi 18:6a4db94011d3 707 }
sahilmgandhi 18:6a4db94011d3 708
sahilmgandhi 18:6a4db94011d3 709 sdio_cmdinitstructure.Argument = (uint32_t)WriteAddr;
sahilmgandhi 18:6a4db94011d3 710 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 711
sahilmgandhi 18:6a4db94011d3 712 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 713 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 714 {
sahilmgandhi 18:6a4db94011d3 715 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
sahilmgandhi 18:6a4db94011d3 716 }
sahilmgandhi 18:6a4db94011d3 717 else
sahilmgandhi 18:6a4db94011d3 718 {
sahilmgandhi 18:6a4db94011d3 719 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
sahilmgandhi 18:6a4db94011d3 720 }
sahilmgandhi 18:6a4db94011d3 721
sahilmgandhi 18:6a4db94011d3 722 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 723 {
sahilmgandhi 18:6a4db94011d3 724 return errorstate;
sahilmgandhi 18:6a4db94011d3 725 }
sahilmgandhi 18:6a4db94011d3 726
sahilmgandhi 18:6a4db94011d3 727 /* Set total number of bytes to write */
sahilmgandhi 18:6a4db94011d3 728 totalnumberofbytes = NumberOfBlocks * BlockSize;
sahilmgandhi 18:6a4db94011d3 729
sahilmgandhi 18:6a4db94011d3 730 /* Configure the SD DPSM (Data Path State Machine) */
sahilmgandhi 18:6a4db94011d3 731 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 732 sdio_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
sahilmgandhi 18:6a4db94011d3 733 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
sahilmgandhi 18:6a4db94011d3 734 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
sahilmgandhi 18:6a4db94011d3 735 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
sahilmgandhi 18:6a4db94011d3 736 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 737 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
sahilmgandhi 18:6a4db94011d3 738
sahilmgandhi 18:6a4db94011d3 739 /* Write block(s) in polling mode */
sahilmgandhi 18:6a4db94011d3 740 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 741 {
sahilmgandhi 18:6a4db94011d3 742 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 743 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 744 #else /* SDIO_STA_STBITERR not defined */
sahilmgandhi 18:6a4db94011d3 745 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
sahilmgandhi 18:6a4db94011d3 746 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 747 {
sahilmgandhi 18:6a4db94011d3 748 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
sahilmgandhi 18:6a4db94011d3 749 {
sahilmgandhi 18:6a4db94011d3 750 if ((totalnumberofbytes - bytestransferred) < 32U)
sahilmgandhi 18:6a4db94011d3 751 {
sahilmgandhi 18:6a4db94011d3 752 restwords = ((totalnumberofbytes - bytestransferred) % 4U == 0U) ? ((totalnumberofbytes - bytestransferred) / 4U) : (( totalnumberofbytes - bytestransferred) / 4U + 1U);
sahilmgandhi 18:6a4db94011d3 753
sahilmgandhi 18:6a4db94011d3 754 /* Write data to SDIO Tx FIFO */
sahilmgandhi 18:6a4db94011d3 755 for (count = 0U; count < restwords; count++)
sahilmgandhi 18:6a4db94011d3 756 {
sahilmgandhi 18:6a4db94011d3 757 SDIO_WriteFIFO(hsd->Instance, tempbuff);
sahilmgandhi 18:6a4db94011d3 758 tempbuff++;
sahilmgandhi 18:6a4db94011d3 759 bytestransferred += 4U;
sahilmgandhi 18:6a4db94011d3 760 }
sahilmgandhi 18:6a4db94011d3 761 }
sahilmgandhi 18:6a4db94011d3 762 else
sahilmgandhi 18:6a4db94011d3 763 {
sahilmgandhi 18:6a4db94011d3 764 /* Write data to SDIO Tx FIFO */
sahilmgandhi 18:6a4db94011d3 765 for (count = 0U; count < 8U; count++)
sahilmgandhi 18:6a4db94011d3 766 {
sahilmgandhi 18:6a4db94011d3 767 SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
sahilmgandhi 18:6a4db94011d3 768 }
sahilmgandhi 18:6a4db94011d3 769
sahilmgandhi 18:6a4db94011d3 770 tempbuff += 8U;
sahilmgandhi 18:6a4db94011d3 771 bytestransferred += 32U;
sahilmgandhi 18:6a4db94011d3 772 }
sahilmgandhi 18:6a4db94011d3 773 }
sahilmgandhi 18:6a4db94011d3 774 }
sahilmgandhi 18:6a4db94011d3 775 }
sahilmgandhi 18:6a4db94011d3 776 else
sahilmgandhi 18:6a4db94011d3 777 {
sahilmgandhi 18:6a4db94011d3 778 /* In case of single data block transfer no need of stop command at all */
sahilmgandhi 18:6a4db94011d3 779 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 780 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 781 #else /* SDIO_STA_STBITERR not defined */
sahilmgandhi 18:6a4db94011d3 782 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
sahilmgandhi 18:6a4db94011d3 783 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 784 {
sahilmgandhi 18:6a4db94011d3 785 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
sahilmgandhi 18:6a4db94011d3 786 {
sahilmgandhi 18:6a4db94011d3 787 if ((totalnumberofbytes - bytestransferred) < 32U)
sahilmgandhi 18:6a4db94011d3 788 {
sahilmgandhi 18:6a4db94011d3 789 restwords = ((totalnumberofbytes - bytestransferred) % 4U == 0U) ? ((totalnumberofbytes - bytestransferred) / 4U) : (( totalnumberofbytes - bytestransferred) / 4U + 1U);
sahilmgandhi 18:6a4db94011d3 790
sahilmgandhi 18:6a4db94011d3 791 /* Write data to SDIO Tx FIFO */
sahilmgandhi 18:6a4db94011d3 792 for (count = 0U; count < restwords; count++)
sahilmgandhi 18:6a4db94011d3 793 {
sahilmgandhi 18:6a4db94011d3 794 SDIO_WriteFIFO(hsd->Instance, tempbuff);
sahilmgandhi 18:6a4db94011d3 795 tempbuff++;
sahilmgandhi 18:6a4db94011d3 796 bytestransferred += 4U;
sahilmgandhi 18:6a4db94011d3 797 }
sahilmgandhi 18:6a4db94011d3 798 }
sahilmgandhi 18:6a4db94011d3 799 else
sahilmgandhi 18:6a4db94011d3 800 {
sahilmgandhi 18:6a4db94011d3 801 /* Write data to SDIO Tx FIFO */
sahilmgandhi 18:6a4db94011d3 802 for (count = 0U; count < 8U; count++)
sahilmgandhi 18:6a4db94011d3 803 {
sahilmgandhi 18:6a4db94011d3 804 SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
sahilmgandhi 18:6a4db94011d3 805 }
sahilmgandhi 18:6a4db94011d3 806
sahilmgandhi 18:6a4db94011d3 807 tempbuff += 8U;
sahilmgandhi 18:6a4db94011d3 808 bytestransferred += 32U;
sahilmgandhi 18:6a4db94011d3 809 }
sahilmgandhi 18:6a4db94011d3 810 }
sahilmgandhi 18:6a4db94011d3 811 }
sahilmgandhi 18:6a4db94011d3 812 }
sahilmgandhi 18:6a4db94011d3 813
sahilmgandhi 18:6a4db94011d3 814 /* Send stop transmission command in case of multiblock write */
sahilmgandhi 18:6a4db94011d3 815 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
sahilmgandhi 18:6a4db94011d3 816 {
sahilmgandhi 18:6a4db94011d3 817 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
sahilmgandhi 18:6a4db94011d3 818 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
sahilmgandhi 18:6a4db94011d3 819 {
sahilmgandhi 18:6a4db94011d3 820 /* Send stop transmission command */
sahilmgandhi 18:6a4db94011d3 821 errorstate = HAL_SD_StopTransfer(hsd);
sahilmgandhi 18:6a4db94011d3 822 }
sahilmgandhi 18:6a4db94011d3 823 }
sahilmgandhi 18:6a4db94011d3 824
sahilmgandhi 18:6a4db94011d3 825 /* Get error state */
sahilmgandhi 18:6a4db94011d3 826 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
sahilmgandhi 18:6a4db94011d3 827 {
sahilmgandhi 18:6a4db94011d3 828 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
sahilmgandhi 18:6a4db94011d3 829
sahilmgandhi 18:6a4db94011d3 830 errorstate = SD_DATA_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 831
sahilmgandhi 18:6a4db94011d3 832 return errorstate;
sahilmgandhi 18:6a4db94011d3 833 }
sahilmgandhi 18:6a4db94011d3 834 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
sahilmgandhi 18:6a4db94011d3 835 {
sahilmgandhi 18:6a4db94011d3 836 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
sahilmgandhi 18:6a4db94011d3 837
sahilmgandhi 18:6a4db94011d3 838 errorstate = SD_DATA_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 839
sahilmgandhi 18:6a4db94011d3 840 return errorstate;
sahilmgandhi 18:6a4db94011d3 841 }
sahilmgandhi 18:6a4db94011d3 842 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
sahilmgandhi 18:6a4db94011d3 843 {
sahilmgandhi 18:6a4db94011d3 844 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR);
sahilmgandhi 18:6a4db94011d3 845
sahilmgandhi 18:6a4db94011d3 846 errorstate = SD_TX_UNDERRUN;
sahilmgandhi 18:6a4db94011d3 847
sahilmgandhi 18:6a4db94011d3 848 return errorstate;
sahilmgandhi 18:6a4db94011d3 849 }
sahilmgandhi 18:6a4db94011d3 850 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 851 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 852 {
sahilmgandhi 18:6a4db94011d3 853 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
sahilmgandhi 18:6a4db94011d3 854
sahilmgandhi 18:6a4db94011d3 855 errorstate = SD_START_BIT_ERR;
sahilmgandhi 18:6a4db94011d3 856
sahilmgandhi 18:6a4db94011d3 857 return errorstate;
sahilmgandhi 18:6a4db94011d3 858 }
sahilmgandhi 18:6a4db94011d3 859 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 860 else
sahilmgandhi 18:6a4db94011d3 861 {
sahilmgandhi 18:6a4db94011d3 862 /* No error flag set */
sahilmgandhi 18:6a4db94011d3 863 }
sahilmgandhi 18:6a4db94011d3 864
sahilmgandhi 18:6a4db94011d3 865 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 866 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 867
sahilmgandhi 18:6a4db94011d3 868 /* Wait till the card is in programming state */
sahilmgandhi 18:6a4db94011d3 869 errorstate = SD_IsCardProgramming(hsd, &cardstate);
sahilmgandhi 18:6a4db94011d3 870
sahilmgandhi 18:6a4db94011d3 871 while ((errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
sahilmgandhi 18:6a4db94011d3 872 {
sahilmgandhi 18:6a4db94011d3 873 errorstate = SD_IsCardProgramming(hsd, &cardstate);
sahilmgandhi 18:6a4db94011d3 874 }
sahilmgandhi 18:6a4db94011d3 875
sahilmgandhi 18:6a4db94011d3 876 return errorstate;
sahilmgandhi 18:6a4db94011d3 877 }
sahilmgandhi 18:6a4db94011d3 878
sahilmgandhi 18:6a4db94011d3 879 /**
sahilmgandhi 18:6a4db94011d3 880 * @brief Reads block(s) from a specified address in a card. The Data transfer
sahilmgandhi 18:6a4db94011d3 881 * is managed by DMA mode.
sahilmgandhi 18:6a4db94011d3 882 * @note This API should be followed by the function HAL_SD_CheckReadOperation()
sahilmgandhi 18:6a4db94011d3 883 * to check the completion of the read process
sahilmgandhi 18:6a4db94011d3 884 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 885 * @param pReadBuffer: Pointer to the buffer that will contain the received data
sahilmgandhi 18:6a4db94011d3 886 * @param ReadAddr: Address from where data is to be read
sahilmgandhi 18:6a4db94011d3 887 * @param BlockSize: SD card Data block size
sahilmgandhi 18:6a4db94011d3 888 * @note BlockSize must be 512 bytes.
sahilmgandhi 18:6a4db94011d3 889 * @param NumberOfBlocks: Number of blocks to read.
sahilmgandhi 18:6a4db94011d3 890 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 891 */
sahilmgandhi 18:6a4db94011d3 892 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
sahilmgandhi 18:6a4db94011d3 893 {
sahilmgandhi 18:6a4db94011d3 894 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 895 SDIO_DataInitTypeDef sdio_datainitstructure;
sahilmgandhi 18:6a4db94011d3 896 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 897
sahilmgandhi 18:6a4db94011d3 898 /* Initialize data control register */
sahilmgandhi 18:6a4db94011d3 899 hsd->Instance->DCTRL = 0U;
sahilmgandhi 18:6a4db94011d3 900
sahilmgandhi 18:6a4db94011d3 901 /* Initialize handle flags */
sahilmgandhi 18:6a4db94011d3 902 hsd->SdTransferCplt = 0U;
sahilmgandhi 18:6a4db94011d3 903 hsd->DmaTransferCplt = 0U;
sahilmgandhi 18:6a4db94011d3 904 hsd->SdTransferErr = SD_OK;
sahilmgandhi 18:6a4db94011d3 905
sahilmgandhi 18:6a4db94011d3 906 /* Initialize SD Read operation */
sahilmgandhi 18:6a4db94011d3 907 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 908 {
sahilmgandhi 18:6a4db94011d3 909 hsd->SdOperation = SD_READ_MULTIPLE_BLOCK;
sahilmgandhi 18:6a4db94011d3 910 }
sahilmgandhi 18:6a4db94011d3 911 else
sahilmgandhi 18:6a4db94011d3 912 {
sahilmgandhi 18:6a4db94011d3 913 hsd->SdOperation = SD_READ_SINGLE_BLOCK;
sahilmgandhi 18:6a4db94011d3 914 }
sahilmgandhi 18:6a4db94011d3 915
sahilmgandhi 18:6a4db94011d3 916 /* Enable transfer interrupts */
sahilmgandhi 18:6a4db94011d3 917 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 918 __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
sahilmgandhi 18:6a4db94011d3 919 SDIO_IT_DTIMEOUT |\
sahilmgandhi 18:6a4db94011d3 920 SDIO_IT_DATAEND |\
sahilmgandhi 18:6a4db94011d3 921 SDIO_IT_RXOVERR |\
sahilmgandhi 18:6a4db94011d3 922 SDIO_IT_STBITERR));
sahilmgandhi 18:6a4db94011d3 923 #else /* SDIO_STA_STBITERR not defined */
sahilmgandhi 18:6a4db94011d3 924 __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
sahilmgandhi 18:6a4db94011d3 925 SDIO_IT_DTIMEOUT |\
sahilmgandhi 18:6a4db94011d3 926 SDIO_IT_DATAEND |\
sahilmgandhi 18:6a4db94011d3 927 SDIO_IT_RXOVERR));
sahilmgandhi 18:6a4db94011d3 928 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 929
sahilmgandhi 18:6a4db94011d3 930 /* Enable SDIO DMA transfer */
sahilmgandhi 18:6a4db94011d3 931 __HAL_SD_SDIO_DMA_ENABLE();
sahilmgandhi 18:6a4db94011d3 932
sahilmgandhi 18:6a4db94011d3 933 /* Configure DMA user callbacks */
sahilmgandhi 18:6a4db94011d3 934 hsd->hdmarx->XferCpltCallback = SD_DMA_RxCplt;
sahilmgandhi 18:6a4db94011d3 935 hsd->hdmarx->XferErrorCallback = SD_DMA_RxError;
sahilmgandhi 18:6a4db94011d3 936
sahilmgandhi 18:6a4db94011d3 937 /* Enable the DMA Stream */
sahilmgandhi 18:6a4db94011d3 938 HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pReadBuffer, (uint32_t)(BlockSize * NumberOfBlocks)/4);
sahilmgandhi 18:6a4db94011d3 939
sahilmgandhi 18:6a4db94011d3 940 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
sahilmgandhi 18:6a4db94011d3 941 {
sahilmgandhi 18:6a4db94011d3 942 BlockSize = 512U;
sahilmgandhi 18:6a4db94011d3 943 ReadAddr /= 512U;
sahilmgandhi 18:6a4db94011d3 944 }
sahilmgandhi 18:6a4db94011d3 945
sahilmgandhi 18:6a4db94011d3 946 /* Set Block Size for Card */
sahilmgandhi 18:6a4db94011d3 947 sdio_cmdinitstructure.Argument = (uint32_t)BlockSize;
sahilmgandhi 18:6a4db94011d3 948 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
sahilmgandhi 18:6a4db94011d3 949 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 950 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 951 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 952 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 953
sahilmgandhi 18:6a4db94011d3 954 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 955 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
sahilmgandhi 18:6a4db94011d3 956
sahilmgandhi 18:6a4db94011d3 957 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 958 {
sahilmgandhi 18:6a4db94011d3 959 return errorstate;
sahilmgandhi 18:6a4db94011d3 960 }
sahilmgandhi 18:6a4db94011d3 961
sahilmgandhi 18:6a4db94011d3 962 /* Configure the SD DPSM (Data Path State Machine) */
sahilmgandhi 18:6a4db94011d3 963 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 964 sdio_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
sahilmgandhi 18:6a4db94011d3 965 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
sahilmgandhi 18:6a4db94011d3 966 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
sahilmgandhi 18:6a4db94011d3 967 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
sahilmgandhi 18:6a4db94011d3 968 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 969 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
sahilmgandhi 18:6a4db94011d3 970
sahilmgandhi 18:6a4db94011d3 971 /* Check number of blocks command */
sahilmgandhi 18:6a4db94011d3 972 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 973 {
sahilmgandhi 18:6a4db94011d3 974 /* Send CMD18 READ_MULT_BLOCK with argument data address */
sahilmgandhi 18:6a4db94011d3 975 sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
sahilmgandhi 18:6a4db94011d3 976 }
sahilmgandhi 18:6a4db94011d3 977 else
sahilmgandhi 18:6a4db94011d3 978 {
sahilmgandhi 18:6a4db94011d3 979 /* Send CMD17 READ_SINGLE_BLOCK */
sahilmgandhi 18:6a4db94011d3 980 sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
sahilmgandhi 18:6a4db94011d3 981 }
sahilmgandhi 18:6a4db94011d3 982
sahilmgandhi 18:6a4db94011d3 983 sdio_cmdinitstructure.Argument = (uint32_t)ReadAddr;
sahilmgandhi 18:6a4db94011d3 984 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 985
sahilmgandhi 18:6a4db94011d3 986 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 987 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 988 {
sahilmgandhi 18:6a4db94011d3 989 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
sahilmgandhi 18:6a4db94011d3 990 }
sahilmgandhi 18:6a4db94011d3 991 else
sahilmgandhi 18:6a4db94011d3 992 {
sahilmgandhi 18:6a4db94011d3 993 errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
sahilmgandhi 18:6a4db94011d3 994 }
sahilmgandhi 18:6a4db94011d3 995
sahilmgandhi 18:6a4db94011d3 996 /* Update the SD transfer error in SD handle */
sahilmgandhi 18:6a4db94011d3 997 hsd->SdTransferErr = errorstate;
sahilmgandhi 18:6a4db94011d3 998
sahilmgandhi 18:6a4db94011d3 999 return errorstate;
sahilmgandhi 18:6a4db94011d3 1000 }
sahilmgandhi 18:6a4db94011d3 1001
sahilmgandhi 18:6a4db94011d3 1002
sahilmgandhi 18:6a4db94011d3 1003 /**
sahilmgandhi 18:6a4db94011d3 1004 * @brief Writes block(s) to a specified address in a card. The Data transfer
sahilmgandhi 18:6a4db94011d3 1005 * is managed by DMA mode.
sahilmgandhi 18:6a4db94011d3 1006 * @note This API should be followed by the function HAL_SD_CheckWriteOperation()
sahilmgandhi 18:6a4db94011d3 1007 * to check the completion of the write process (by SD current status polling).
sahilmgandhi 18:6a4db94011d3 1008 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1009 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
sahilmgandhi 18:6a4db94011d3 1010 * @param WriteAddr: Address from where data is to be read
sahilmgandhi 18:6a4db94011d3 1011 * @param BlockSize: the SD card Data block size
sahilmgandhi 18:6a4db94011d3 1012 * @note BlockSize must be 512 bytes.
sahilmgandhi 18:6a4db94011d3 1013 * @param NumberOfBlocks: Number of blocks to write
sahilmgandhi 18:6a4db94011d3 1014 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 1015 */
sahilmgandhi 18:6a4db94011d3 1016 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
sahilmgandhi 18:6a4db94011d3 1017 {
sahilmgandhi 18:6a4db94011d3 1018 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 1019 SDIO_DataInitTypeDef sdio_datainitstructure;
sahilmgandhi 18:6a4db94011d3 1020 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 1021
sahilmgandhi 18:6a4db94011d3 1022 /* Initialize data control register */
sahilmgandhi 18:6a4db94011d3 1023 hsd->Instance->DCTRL = 0U;
sahilmgandhi 18:6a4db94011d3 1024
sahilmgandhi 18:6a4db94011d3 1025 /* Initialize handle flags */
sahilmgandhi 18:6a4db94011d3 1026 hsd->SdTransferCplt = 0U;
sahilmgandhi 18:6a4db94011d3 1027 hsd->DmaTransferCplt = 0U;
sahilmgandhi 18:6a4db94011d3 1028 hsd->SdTransferErr = SD_OK;
sahilmgandhi 18:6a4db94011d3 1029
sahilmgandhi 18:6a4db94011d3 1030 /* Initialize SD Write operation */
sahilmgandhi 18:6a4db94011d3 1031 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 1032 {
sahilmgandhi 18:6a4db94011d3 1033 hsd->SdOperation = SD_WRITE_MULTIPLE_BLOCK;
sahilmgandhi 18:6a4db94011d3 1034 }
sahilmgandhi 18:6a4db94011d3 1035 else
sahilmgandhi 18:6a4db94011d3 1036 {
sahilmgandhi 18:6a4db94011d3 1037 hsd->SdOperation = SD_WRITE_SINGLE_BLOCK;
sahilmgandhi 18:6a4db94011d3 1038 }
sahilmgandhi 18:6a4db94011d3 1039
sahilmgandhi 18:6a4db94011d3 1040 /* Enable transfer interrupts */
sahilmgandhi 18:6a4db94011d3 1041 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 1042 __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
sahilmgandhi 18:6a4db94011d3 1043 SDIO_IT_DTIMEOUT |\
sahilmgandhi 18:6a4db94011d3 1044 SDIO_IT_DATAEND |\
sahilmgandhi 18:6a4db94011d3 1045 SDIO_IT_TXUNDERR |\
sahilmgandhi 18:6a4db94011d3 1046 SDIO_IT_STBITERR));
sahilmgandhi 18:6a4db94011d3 1047 #else /* SDIO_STA_STBITERR not defined */
sahilmgandhi 18:6a4db94011d3 1048 __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
sahilmgandhi 18:6a4db94011d3 1049 SDIO_IT_DTIMEOUT |\
sahilmgandhi 18:6a4db94011d3 1050 SDIO_IT_DATAEND |\
sahilmgandhi 18:6a4db94011d3 1051 SDIO_IT_TXUNDERR));
sahilmgandhi 18:6a4db94011d3 1052 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 1053
sahilmgandhi 18:6a4db94011d3 1054 /* Configure DMA user callbacks */
sahilmgandhi 18:6a4db94011d3 1055 hsd->hdmatx->XferCpltCallback = SD_DMA_TxCplt;
sahilmgandhi 18:6a4db94011d3 1056 hsd->hdmatx->XferErrorCallback = SD_DMA_TxError;
sahilmgandhi 18:6a4db94011d3 1057
sahilmgandhi 18:6a4db94011d3 1058 /* Enable the DMA Stream */
sahilmgandhi 18:6a4db94011d3 1059 HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pWriteBuffer, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BlockSize * NumberOfBlocks)/4);
sahilmgandhi 18:6a4db94011d3 1060
sahilmgandhi 18:6a4db94011d3 1061 /* Enable SDIO DMA transfer */
sahilmgandhi 18:6a4db94011d3 1062 __HAL_SD_SDIO_DMA_ENABLE();
sahilmgandhi 18:6a4db94011d3 1063
sahilmgandhi 18:6a4db94011d3 1064 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
sahilmgandhi 18:6a4db94011d3 1065 {
sahilmgandhi 18:6a4db94011d3 1066 BlockSize = 512U;
sahilmgandhi 18:6a4db94011d3 1067 WriteAddr /= 512U;
sahilmgandhi 18:6a4db94011d3 1068 }
sahilmgandhi 18:6a4db94011d3 1069
sahilmgandhi 18:6a4db94011d3 1070 /* Set Block Size for Card */
sahilmgandhi 18:6a4db94011d3 1071 sdio_cmdinitstructure.Argument = (uint32_t)BlockSize;
sahilmgandhi 18:6a4db94011d3 1072 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
sahilmgandhi 18:6a4db94011d3 1073 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 1074 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 1075 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 1076 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 1077
sahilmgandhi 18:6a4db94011d3 1078 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 1079 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
sahilmgandhi 18:6a4db94011d3 1080
sahilmgandhi 18:6a4db94011d3 1081 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 1082 {
sahilmgandhi 18:6a4db94011d3 1083 return errorstate;
sahilmgandhi 18:6a4db94011d3 1084 }
sahilmgandhi 18:6a4db94011d3 1085
sahilmgandhi 18:6a4db94011d3 1086 /* Check number of blocks command */
sahilmgandhi 18:6a4db94011d3 1087 if(NumberOfBlocks <= 1U)
sahilmgandhi 18:6a4db94011d3 1088 {
sahilmgandhi 18:6a4db94011d3 1089 /* Send CMD24 WRITE_SINGLE_BLOCK */
sahilmgandhi 18:6a4db94011d3 1090 sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
sahilmgandhi 18:6a4db94011d3 1091 }
sahilmgandhi 18:6a4db94011d3 1092 else
sahilmgandhi 18:6a4db94011d3 1093 {
sahilmgandhi 18:6a4db94011d3 1094 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
sahilmgandhi 18:6a4db94011d3 1095 sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
sahilmgandhi 18:6a4db94011d3 1096 }
sahilmgandhi 18:6a4db94011d3 1097
sahilmgandhi 18:6a4db94011d3 1098 sdio_cmdinitstructure.Argument = (uint32_t)WriteAddr;
sahilmgandhi 18:6a4db94011d3 1099 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 1100
sahilmgandhi 18:6a4db94011d3 1101 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 1102 if(NumberOfBlocks > 1U)
sahilmgandhi 18:6a4db94011d3 1103 {
sahilmgandhi 18:6a4db94011d3 1104 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
sahilmgandhi 18:6a4db94011d3 1105 }
sahilmgandhi 18:6a4db94011d3 1106 else
sahilmgandhi 18:6a4db94011d3 1107 {
sahilmgandhi 18:6a4db94011d3 1108 errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
sahilmgandhi 18:6a4db94011d3 1109 }
sahilmgandhi 18:6a4db94011d3 1110
sahilmgandhi 18:6a4db94011d3 1111 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 1112 {
sahilmgandhi 18:6a4db94011d3 1113 return errorstate;
sahilmgandhi 18:6a4db94011d3 1114 }
sahilmgandhi 18:6a4db94011d3 1115
sahilmgandhi 18:6a4db94011d3 1116 /* Configure the SD DPSM (Data Path State Machine) */
sahilmgandhi 18:6a4db94011d3 1117 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 1118 sdio_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
sahilmgandhi 18:6a4db94011d3 1119 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
sahilmgandhi 18:6a4db94011d3 1120 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
sahilmgandhi 18:6a4db94011d3 1121 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
sahilmgandhi 18:6a4db94011d3 1122 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 1123 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
sahilmgandhi 18:6a4db94011d3 1124
sahilmgandhi 18:6a4db94011d3 1125 hsd->SdTransferErr = errorstate;
sahilmgandhi 18:6a4db94011d3 1126
sahilmgandhi 18:6a4db94011d3 1127 return errorstate;
sahilmgandhi 18:6a4db94011d3 1128 }
sahilmgandhi 18:6a4db94011d3 1129
sahilmgandhi 18:6a4db94011d3 1130 /**
sahilmgandhi 18:6a4db94011d3 1131 * @brief This function waits until the SD DMA data read transfer is finished.
sahilmgandhi 18:6a4db94011d3 1132 * This API should be called after HAL_SD_ReadBlocks_DMA() function
sahilmgandhi 18:6a4db94011d3 1133 * to insure that all data sent by the card is already transferred by the
sahilmgandhi 18:6a4db94011d3 1134 * DMA controller.
sahilmgandhi 18:6a4db94011d3 1135 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1136 * @param Timeout: Timeout duration
sahilmgandhi 18:6a4db94011d3 1137 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 1138 */
sahilmgandhi 18:6a4db94011d3 1139 HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
sahilmgandhi 18:6a4db94011d3 1140 {
sahilmgandhi 18:6a4db94011d3 1141 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 1142 uint32_t timeout = Timeout;
sahilmgandhi 18:6a4db94011d3 1143 uint32_t tmp1, tmp2;
sahilmgandhi 18:6a4db94011d3 1144 HAL_SD_ErrorTypedef tmp3;
sahilmgandhi 18:6a4db94011d3 1145
sahilmgandhi 18:6a4db94011d3 1146 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
sahilmgandhi 18:6a4db94011d3 1147 tmp1 = hsd->DmaTransferCplt;
sahilmgandhi 18:6a4db94011d3 1148 tmp2 = hsd->SdTransferCplt;
sahilmgandhi 18:6a4db94011d3 1149 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
sahilmgandhi 18:6a4db94011d3 1150
sahilmgandhi 18:6a4db94011d3 1151 while ((tmp1 == 0U) && (tmp2 == 0U) && (tmp3 == SD_OK) && (timeout > 0U))
sahilmgandhi 18:6a4db94011d3 1152 {
sahilmgandhi 18:6a4db94011d3 1153 tmp1 = hsd->DmaTransferCplt;
sahilmgandhi 18:6a4db94011d3 1154 tmp2 = hsd->SdTransferCplt;
sahilmgandhi 18:6a4db94011d3 1155 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
sahilmgandhi 18:6a4db94011d3 1156 timeout--;
sahilmgandhi 18:6a4db94011d3 1157 }
sahilmgandhi 18:6a4db94011d3 1158
sahilmgandhi 18:6a4db94011d3 1159 timeout = Timeout;
sahilmgandhi 18:6a4db94011d3 1160
sahilmgandhi 18:6a4db94011d3 1161 /* Wait until the Rx transfer is no longer active */
sahilmgandhi 18:6a4db94011d3 1162 while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXACT)) && (timeout > 0U))
sahilmgandhi 18:6a4db94011d3 1163 {
sahilmgandhi 18:6a4db94011d3 1164 timeout--;
sahilmgandhi 18:6a4db94011d3 1165 }
sahilmgandhi 18:6a4db94011d3 1166
sahilmgandhi 18:6a4db94011d3 1167 /* Send stop command in multiblock read */
sahilmgandhi 18:6a4db94011d3 1168 if (hsd->SdOperation == SD_READ_MULTIPLE_BLOCK)
sahilmgandhi 18:6a4db94011d3 1169 {
sahilmgandhi 18:6a4db94011d3 1170 errorstate = HAL_SD_StopTransfer(hsd);
sahilmgandhi 18:6a4db94011d3 1171 }
sahilmgandhi 18:6a4db94011d3 1172
sahilmgandhi 18:6a4db94011d3 1173 if ((timeout == 0U) && (errorstate == SD_OK))
sahilmgandhi 18:6a4db94011d3 1174 {
sahilmgandhi 18:6a4db94011d3 1175 errorstate = SD_DATA_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 1176 }
sahilmgandhi 18:6a4db94011d3 1177
sahilmgandhi 18:6a4db94011d3 1178 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 1179 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 1180
sahilmgandhi 18:6a4db94011d3 1181 /* Return error state */
sahilmgandhi 18:6a4db94011d3 1182 if (hsd->SdTransferErr != SD_OK)
sahilmgandhi 18:6a4db94011d3 1183 {
sahilmgandhi 18:6a4db94011d3 1184 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
sahilmgandhi 18:6a4db94011d3 1185 }
sahilmgandhi 18:6a4db94011d3 1186
sahilmgandhi 18:6a4db94011d3 1187 return errorstate;
sahilmgandhi 18:6a4db94011d3 1188 }
sahilmgandhi 18:6a4db94011d3 1189
sahilmgandhi 18:6a4db94011d3 1190 /**
sahilmgandhi 18:6a4db94011d3 1191 * @brief This function waits until the SD DMA data write transfer is finished.
sahilmgandhi 18:6a4db94011d3 1192 * This API should be called after HAL_SD_WriteBlocks_DMA() function
sahilmgandhi 18:6a4db94011d3 1193 * to insure that all data sent by the card is already transferred by the
sahilmgandhi 18:6a4db94011d3 1194 * DMA controller.
sahilmgandhi 18:6a4db94011d3 1195 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1196 * @param Timeout: Timeout duration
sahilmgandhi 18:6a4db94011d3 1197 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 1198 */
sahilmgandhi 18:6a4db94011d3 1199 HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
sahilmgandhi 18:6a4db94011d3 1200 {
sahilmgandhi 18:6a4db94011d3 1201 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 1202 uint32_t timeout = Timeout;
sahilmgandhi 18:6a4db94011d3 1203 uint32_t tmp1, tmp2;
sahilmgandhi 18:6a4db94011d3 1204 HAL_SD_ErrorTypedef tmp3;
sahilmgandhi 18:6a4db94011d3 1205
sahilmgandhi 18:6a4db94011d3 1206 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
sahilmgandhi 18:6a4db94011d3 1207 tmp1 = hsd->DmaTransferCplt;
sahilmgandhi 18:6a4db94011d3 1208 tmp2 = hsd->SdTransferCplt;
sahilmgandhi 18:6a4db94011d3 1209 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
sahilmgandhi 18:6a4db94011d3 1210
sahilmgandhi 18:6a4db94011d3 1211 while ((tmp1 == 0U) && (tmp2 == 0U) && (tmp3 == SD_OK) && (timeout > 0U))
sahilmgandhi 18:6a4db94011d3 1212 {
sahilmgandhi 18:6a4db94011d3 1213 tmp1 = hsd->DmaTransferCplt;
sahilmgandhi 18:6a4db94011d3 1214 tmp2 = hsd->SdTransferCplt;
sahilmgandhi 18:6a4db94011d3 1215 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
sahilmgandhi 18:6a4db94011d3 1216 timeout--;
sahilmgandhi 18:6a4db94011d3 1217 }
sahilmgandhi 18:6a4db94011d3 1218
sahilmgandhi 18:6a4db94011d3 1219 timeout = Timeout;
sahilmgandhi 18:6a4db94011d3 1220
sahilmgandhi 18:6a4db94011d3 1221 /* Wait until the Tx transfer is no longer active */
sahilmgandhi 18:6a4db94011d3 1222 while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXACT)) && (timeout > 0U))
sahilmgandhi 18:6a4db94011d3 1223 {
sahilmgandhi 18:6a4db94011d3 1224 timeout--;
sahilmgandhi 18:6a4db94011d3 1225 }
sahilmgandhi 18:6a4db94011d3 1226
sahilmgandhi 18:6a4db94011d3 1227 /* Send stop command in multiblock write */
sahilmgandhi 18:6a4db94011d3 1228 if (hsd->SdOperation == SD_WRITE_MULTIPLE_BLOCK)
sahilmgandhi 18:6a4db94011d3 1229 {
sahilmgandhi 18:6a4db94011d3 1230 errorstate = HAL_SD_StopTransfer(hsd);
sahilmgandhi 18:6a4db94011d3 1231 }
sahilmgandhi 18:6a4db94011d3 1232
sahilmgandhi 18:6a4db94011d3 1233 if ((timeout == 0U) && (errorstate == SD_OK))
sahilmgandhi 18:6a4db94011d3 1234 {
sahilmgandhi 18:6a4db94011d3 1235 errorstate = SD_DATA_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 1236 }
sahilmgandhi 18:6a4db94011d3 1237
sahilmgandhi 18:6a4db94011d3 1238 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 1239 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 1240
sahilmgandhi 18:6a4db94011d3 1241 /* Return error state */
sahilmgandhi 18:6a4db94011d3 1242 if (hsd->SdTransferErr != SD_OK)
sahilmgandhi 18:6a4db94011d3 1243 {
sahilmgandhi 18:6a4db94011d3 1244 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
sahilmgandhi 18:6a4db94011d3 1245 }
sahilmgandhi 18:6a4db94011d3 1246
sahilmgandhi 18:6a4db94011d3 1247 /* Wait until write is complete */
sahilmgandhi 18:6a4db94011d3 1248 while(HAL_SD_GetStatus(hsd) != SD_TRANSFER_OK)
sahilmgandhi 18:6a4db94011d3 1249 {
sahilmgandhi 18:6a4db94011d3 1250 }
sahilmgandhi 18:6a4db94011d3 1251
sahilmgandhi 18:6a4db94011d3 1252 return errorstate;
sahilmgandhi 18:6a4db94011d3 1253 }
sahilmgandhi 18:6a4db94011d3 1254
sahilmgandhi 18:6a4db94011d3 1255 /**
sahilmgandhi 18:6a4db94011d3 1256 * @brief Erases the specified memory area of the given SD card.
sahilmgandhi 18:6a4db94011d3 1257 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1258 * @param startaddr: Start byte address
sahilmgandhi 18:6a4db94011d3 1259 * @param endaddr: End byte address
sahilmgandhi 18:6a4db94011d3 1260 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 1261 */
sahilmgandhi 18:6a4db94011d3 1262 HAL_SD_ErrorTypedef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint64_t startaddr, uint64_t endaddr)
sahilmgandhi 18:6a4db94011d3 1263 {
sahilmgandhi 18:6a4db94011d3 1264 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 1265 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 1266
sahilmgandhi 18:6a4db94011d3 1267 uint32_t delay = 0U;
sahilmgandhi 18:6a4db94011d3 1268 __IO uint32_t maxdelay = 0U;
sahilmgandhi 18:6a4db94011d3 1269 uint8_t cardstate = 0U;
sahilmgandhi 18:6a4db94011d3 1270
sahilmgandhi 18:6a4db94011d3 1271 /* Check if the card command class supports erase command */
sahilmgandhi 18:6a4db94011d3 1272 if (((hsd->CSD[1U] >> 20U) & SD_CCCC_ERASE) == 0U)
sahilmgandhi 18:6a4db94011d3 1273 {
sahilmgandhi 18:6a4db94011d3 1274 errorstate = SD_REQUEST_NOT_APPLICABLE;
sahilmgandhi 18:6a4db94011d3 1275
sahilmgandhi 18:6a4db94011d3 1276 return errorstate;
sahilmgandhi 18:6a4db94011d3 1277 }
sahilmgandhi 18:6a4db94011d3 1278
sahilmgandhi 18:6a4db94011d3 1279 /* Get max delay value */
sahilmgandhi 18:6a4db94011d3 1280 maxdelay = 120000U / (((hsd->Instance->CLKCR) & 0xFFU) + 2U);
sahilmgandhi 18:6a4db94011d3 1281
sahilmgandhi 18:6a4db94011d3 1282 if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
sahilmgandhi 18:6a4db94011d3 1283 {
sahilmgandhi 18:6a4db94011d3 1284 errorstate = SD_LOCK_UNLOCK_FAILED;
sahilmgandhi 18:6a4db94011d3 1285
sahilmgandhi 18:6a4db94011d3 1286 return errorstate;
sahilmgandhi 18:6a4db94011d3 1287 }
sahilmgandhi 18:6a4db94011d3 1288
sahilmgandhi 18:6a4db94011d3 1289 /* Get start and end block for high capacity cards */
sahilmgandhi 18:6a4db94011d3 1290 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
sahilmgandhi 18:6a4db94011d3 1291 {
sahilmgandhi 18:6a4db94011d3 1292 startaddr /= 512U;
sahilmgandhi 18:6a4db94011d3 1293 endaddr /= 512U;
sahilmgandhi 18:6a4db94011d3 1294 }
sahilmgandhi 18:6a4db94011d3 1295
sahilmgandhi 18:6a4db94011d3 1296 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
sahilmgandhi 18:6a4db94011d3 1297 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
sahilmgandhi 18:6a4db94011d3 1298 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
sahilmgandhi 18:6a4db94011d3 1299 {
sahilmgandhi 18:6a4db94011d3 1300 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
sahilmgandhi 18:6a4db94011d3 1301 sdio_cmdinitstructure.Argument =(uint32_t)startaddr;
sahilmgandhi 18:6a4db94011d3 1302 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_START;
sahilmgandhi 18:6a4db94011d3 1303 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 1304 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 1305 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 1306 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 1307
sahilmgandhi 18:6a4db94011d3 1308 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 1309 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_START);
sahilmgandhi 18:6a4db94011d3 1310
sahilmgandhi 18:6a4db94011d3 1311 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 1312 {
sahilmgandhi 18:6a4db94011d3 1313 return errorstate;
sahilmgandhi 18:6a4db94011d3 1314 }
sahilmgandhi 18:6a4db94011d3 1315
sahilmgandhi 18:6a4db94011d3 1316 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
sahilmgandhi 18:6a4db94011d3 1317 sdio_cmdinitstructure.Argument = (uint32_t)endaddr;
sahilmgandhi 18:6a4db94011d3 1318 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_END;
sahilmgandhi 18:6a4db94011d3 1319 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 1320
sahilmgandhi 18:6a4db94011d3 1321 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 1322 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_END);
sahilmgandhi 18:6a4db94011d3 1323
sahilmgandhi 18:6a4db94011d3 1324 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 1325 {
sahilmgandhi 18:6a4db94011d3 1326 return errorstate;
sahilmgandhi 18:6a4db94011d3 1327 }
sahilmgandhi 18:6a4db94011d3 1328 }
sahilmgandhi 18:6a4db94011d3 1329
sahilmgandhi 18:6a4db94011d3 1330 /* Send CMD38 ERASE */
sahilmgandhi 18:6a4db94011d3 1331 sdio_cmdinitstructure.Argument = 0U;
sahilmgandhi 18:6a4db94011d3 1332 sdio_cmdinitstructure.CmdIndex = SD_CMD_ERASE;
sahilmgandhi 18:6a4db94011d3 1333 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 1334
sahilmgandhi 18:6a4db94011d3 1335 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 1336 errorstate = SD_CmdResp1Error(hsd, SD_CMD_ERASE);
sahilmgandhi 18:6a4db94011d3 1337
sahilmgandhi 18:6a4db94011d3 1338 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 1339 {
sahilmgandhi 18:6a4db94011d3 1340 return errorstate;
sahilmgandhi 18:6a4db94011d3 1341 }
sahilmgandhi 18:6a4db94011d3 1342
sahilmgandhi 18:6a4db94011d3 1343 for (; delay < maxdelay; delay++)
sahilmgandhi 18:6a4db94011d3 1344 {
sahilmgandhi 18:6a4db94011d3 1345 }
sahilmgandhi 18:6a4db94011d3 1346
sahilmgandhi 18:6a4db94011d3 1347 /* Wait until the card is in programming state */
sahilmgandhi 18:6a4db94011d3 1348 errorstate = SD_IsCardProgramming(hsd, &cardstate);
sahilmgandhi 18:6a4db94011d3 1349
sahilmgandhi 18:6a4db94011d3 1350 delay = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 1351
sahilmgandhi 18:6a4db94011d3 1352 while ((delay > 0U) && (errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
sahilmgandhi 18:6a4db94011d3 1353 {
sahilmgandhi 18:6a4db94011d3 1354 errorstate = SD_IsCardProgramming(hsd, &cardstate);
sahilmgandhi 18:6a4db94011d3 1355 delay--;
sahilmgandhi 18:6a4db94011d3 1356 }
sahilmgandhi 18:6a4db94011d3 1357
sahilmgandhi 18:6a4db94011d3 1358 return errorstate;
sahilmgandhi 18:6a4db94011d3 1359 }
sahilmgandhi 18:6a4db94011d3 1360
sahilmgandhi 18:6a4db94011d3 1361 /**
sahilmgandhi 18:6a4db94011d3 1362 * @brief This function handles SD card interrupt request.
sahilmgandhi 18:6a4db94011d3 1363 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1364 * @retval None
sahilmgandhi 18:6a4db94011d3 1365 */
sahilmgandhi 18:6a4db94011d3 1366 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 1367 {
sahilmgandhi 18:6a4db94011d3 1368 /* Check for SDIO interrupt flags */
sahilmgandhi 18:6a4db94011d3 1369 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DATAEND))
sahilmgandhi 18:6a4db94011d3 1370 {
sahilmgandhi 18:6a4db94011d3 1371 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_IT_DATAEND);
sahilmgandhi 18:6a4db94011d3 1372
sahilmgandhi 18:6a4db94011d3 1373 /* SD transfer is complete */
sahilmgandhi 18:6a4db94011d3 1374 hsd->SdTransferCplt = 1U;
sahilmgandhi 18:6a4db94011d3 1375
sahilmgandhi 18:6a4db94011d3 1376 /* No transfer error */
sahilmgandhi 18:6a4db94011d3 1377 hsd->SdTransferErr = SD_OK;
sahilmgandhi 18:6a4db94011d3 1378
sahilmgandhi 18:6a4db94011d3 1379 HAL_SD_XferCpltCallback(hsd);
sahilmgandhi 18:6a4db94011d3 1380 }
sahilmgandhi 18:6a4db94011d3 1381 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DCRCFAIL))
sahilmgandhi 18:6a4db94011d3 1382 {
sahilmgandhi 18:6a4db94011d3 1383 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
sahilmgandhi 18:6a4db94011d3 1384
sahilmgandhi 18:6a4db94011d3 1385 hsd->SdTransferErr = SD_DATA_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 1386
sahilmgandhi 18:6a4db94011d3 1387 HAL_SD_XferErrorCallback(hsd);
sahilmgandhi 18:6a4db94011d3 1388
sahilmgandhi 18:6a4db94011d3 1389 }
sahilmgandhi 18:6a4db94011d3 1390 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DTIMEOUT))
sahilmgandhi 18:6a4db94011d3 1391 {
sahilmgandhi 18:6a4db94011d3 1392 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
sahilmgandhi 18:6a4db94011d3 1393
sahilmgandhi 18:6a4db94011d3 1394 hsd->SdTransferErr = SD_DATA_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 1395
sahilmgandhi 18:6a4db94011d3 1396 HAL_SD_XferErrorCallback(hsd);
sahilmgandhi 18:6a4db94011d3 1397 }
sahilmgandhi 18:6a4db94011d3 1398 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_RXOVERR))
sahilmgandhi 18:6a4db94011d3 1399 {
sahilmgandhi 18:6a4db94011d3 1400 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
sahilmgandhi 18:6a4db94011d3 1401
sahilmgandhi 18:6a4db94011d3 1402 hsd->SdTransferErr = SD_RX_OVERRUN;
sahilmgandhi 18:6a4db94011d3 1403
sahilmgandhi 18:6a4db94011d3 1404 HAL_SD_XferErrorCallback(hsd);
sahilmgandhi 18:6a4db94011d3 1405 }
sahilmgandhi 18:6a4db94011d3 1406 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_TXUNDERR))
sahilmgandhi 18:6a4db94011d3 1407 {
sahilmgandhi 18:6a4db94011d3 1408 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR);
sahilmgandhi 18:6a4db94011d3 1409
sahilmgandhi 18:6a4db94011d3 1410 hsd->SdTransferErr = SD_TX_UNDERRUN;
sahilmgandhi 18:6a4db94011d3 1411
sahilmgandhi 18:6a4db94011d3 1412 HAL_SD_XferErrorCallback(hsd);
sahilmgandhi 18:6a4db94011d3 1413 }
sahilmgandhi 18:6a4db94011d3 1414 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 1415 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_STBITERR))
sahilmgandhi 18:6a4db94011d3 1416 {
sahilmgandhi 18:6a4db94011d3 1417 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
sahilmgandhi 18:6a4db94011d3 1418
sahilmgandhi 18:6a4db94011d3 1419 hsd->SdTransferErr = SD_START_BIT_ERR;
sahilmgandhi 18:6a4db94011d3 1420
sahilmgandhi 18:6a4db94011d3 1421 HAL_SD_XferErrorCallback(hsd);
sahilmgandhi 18:6a4db94011d3 1422 }
sahilmgandhi 18:6a4db94011d3 1423 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 1424 else
sahilmgandhi 18:6a4db94011d3 1425 {
sahilmgandhi 18:6a4db94011d3 1426 /* No error flag set */
sahilmgandhi 18:6a4db94011d3 1427 }
sahilmgandhi 18:6a4db94011d3 1428
sahilmgandhi 18:6a4db94011d3 1429 /* Disable all SDIO peripheral interrupt sources */
sahilmgandhi 18:6a4db94011d3 1430 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 1431 __HAL_SD_SDIO_DISABLE_IT(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |\
sahilmgandhi 18:6a4db94011d3 1432 SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |\
sahilmgandhi 18:6a4db94011d3 1433 SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
sahilmgandhi 18:6a4db94011d3 1434 #else /* SDIO_STA_STBITERR not defined */
sahilmgandhi 18:6a4db94011d3 1435 __HAL_SD_SDIO_DISABLE_IT(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |\
sahilmgandhi 18:6a4db94011d3 1436 SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |\
sahilmgandhi 18:6a4db94011d3 1437 SDIO_IT_RXOVERR);
sahilmgandhi 18:6a4db94011d3 1438 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 1439 }
sahilmgandhi 18:6a4db94011d3 1440
sahilmgandhi 18:6a4db94011d3 1441
sahilmgandhi 18:6a4db94011d3 1442 /**
sahilmgandhi 18:6a4db94011d3 1443 * @brief SD end of transfer callback.
sahilmgandhi 18:6a4db94011d3 1444 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1445 * @retval None
sahilmgandhi 18:6a4db94011d3 1446 */
sahilmgandhi 18:6a4db94011d3 1447 __weak void HAL_SD_XferCpltCallback(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 1448 {
sahilmgandhi 18:6a4db94011d3 1449 /* Prevent unused argument(s) compilation warning */
sahilmgandhi 18:6a4db94011d3 1450 UNUSED(hsd);
sahilmgandhi 18:6a4db94011d3 1451 /* NOTE : This function Should not be modified, when the callback is needed,
sahilmgandhi 18:6a4db94011d3 1452 the HAL_SD_XferCpltCallback could be implemented in the user file
sahilmgandhi 18:6a4db94011d3 1453 */
sahilmgandhi 18:6a4db94011d3 1454 }
sahilmgandhi 18:6a4db94011d3 1455
sahilmgandhi 18:6a4db94011d3 1456 /**
sahilmgandhi 18:6a4db94011d3 1457 * @brief SD Transfer Error callback.
sahilmgandhi 18:6a4db94011d3 1458 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1459 * @retval None
sahilmgandhi 18:6a4db94011d3 1460 */
sahilmgandhi 18:6a4db94011d3 1461 __weak void HAL_SD_XferErrorCallback(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 1462 {
sahilmgandhi 18:6a4db94011d3 1463 /* Prevent unused argument(s) compilation warning */
sahilmgandhi 18:6a4db94011d3 1464 UNUSED(hsd);
sahilmgandhi 18:6a4db94011d3 1465 /* NOTE : This function Should not be modified, when the callback is needed,
sahilmgandhi 18:6a4db94011d3 1466 the HAL_SD_XferErrorCallback could be implemented in the user file
sahilmgandhi 18:6a4db94011d3 1467 */
sahilmgandhi 18:6a4db94011d3 1468 }
sahilmgandhi 18:6a4db94011d3 1469
sahilmgandhi 18:6a4db94011d3 1470 /**
sahilmgandhi 18:6a4db94011d3 1471 * @brief SD Transfer complete Rx callback in non blocking mode.
sahilmgandhi 18:6a4db94011d3 1472 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
sahilmgandhi 18:6a4db94011d3 1473 * the configuration information for the specified DMA module.
sahilmgandhi 18:6a4db94011d3 1474 * @retval None
sahilmgandhi 18:6a4db94011d3 1475 */
sahilmgandhi 18:6a4db94011d3 1476 __weak void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef *hdma)
sahilmgandhi 18:6a4db94011d3 1477 {
sahilmgandhi 18:6a4db94011d3 1478 /* Prevent unused argument(s) compilation warning */
sahilmgandhi 18:6a4db94011d3 1479 UNUSED(hdma);
sahilmgandhi 18:6a4db94011d3 1480 /* NOTE : This function Should not be modified, when the callback is needed,
sahilmgandhi 18:6a4db94011d3 1481 the HAL_SD_DMA_RxCpltCallback could be implemented in the user file
sahilmgandhi 18:6a4db94011d3 1482 */
sahilmgandhi 18:6a4db94011d3 1483 }
sahilmgandhi 18:6a4db94011d3 1484
sahilmgandhi 18:6a4db94011d3 1485 /**
sahilmgandhi 18:6a4db94011d3 1486 * @brief SD DMA transfer complete Rx error callback.
sahilmgandhi 18:6a4db94011d3 1487 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
sahilmgandhi 18:6a4db94011d3 1488 * the configuration information for the specified DMA module.
sahilmgandhi 18:6a4db94011d3 1489 * @retval None
sahilmgandhi 18:6a4db94011d3 1490 */
sahilmgandhi 18:6a4db94011d3 1491 __weak void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef *hdma)
sahilmgandhi 18:6a4db94011d3 1492 {
sahilmgandhi 18:6a4db94011d3 1493 /* Prevent unused argument(s) compilation warning */
sahilmgandhi 18:6a4db94011d3 1494 UNUSED(hdma);
sahilmgandhi 18:6a4db94011d3 1495 /* NOTE : This function Should not be modified, when the callback is needed,
sahilmgandhi 18:6a4db94011d3 1496 the HAL_SD_DMA_RxErrorCallback could be implemented in the user file
sahilmgandhi 18:6a4db94011d3 1497 */
sahilmgandhi 18:6a4db94011d3 1498 }
sahilmgandhi 18:6a4db94011d3 1499
sahilmgandhi 18:6a4db94011d3 1500 /**
sahilmgandhi 18:6a4db94011d3 1501 * @brief SD Transfer complete Tx callback in non blocking mode.
sahilmgandhi 18:6a4db94011d3 1502 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
sahilmgandhi 18:6a4db94011d3 1503 * the configuration information for the specified DMA module.
sahilmgandhi 18:6a4db94011d3 1504 * @retval None
sahilmgandhi 18:6a4db94011d3 1505 */
sahilmgandhi 18:6a4db94011d3 1506 __weak void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma)
sahilmgandhi 18:6a4db94011d3 1507 {
sahilmgandhi 18:6a4db94011d3 1508 /* Prevent unused argument(s) compilation warning */
sahilmgandhi 18:6a4db94011d3 1509 UNUSED(hdma);
sahilmgandhi 18:6a4db94011d3 1510 /* NOTE : This function Should not be modified, when the callback is needed,
sahilmgandhi 18:6a4db94011d3 1511 the HAL_SD_DMA_TxCpltCallback could be implemented in the user file
sahilmgandhi 18:6a4db94011d3 1512 */
sahilmgandhi 18:6a4db94011d3 1513 }
sahilmgandhi 18:6a4db94011d3 1514
sahilmgandhi 18:6a4db94011d3 1515 /**
sahilmgandhi 18:6a4db94011d3 1516 * @brief SD DMA transfer complete error Tx callback.
sahilmgandhi 18:6a4db94011d3 1517 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
sahilmgandhi 18:6a4db94011d3 1518 * the configuration information for the specified DMA module.
sahilmgandhi 18:6a4db94011d3 1519 * @retval None
sahilmgandhi 18:6a4db94011d3 1520 */
sahilmgandhi 18:6a4db94011d3 1521 __weak void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef *hdma)
sahilmgandhi 18:6a4db94011d3 1522 {
sahilmgandhi 18:6a4db94011d3 1523 /* Prevent unused argument(s) compilation warning */
sahilmgandhi 18:6a4db94011d3 1524 UNUSED(hdma);
sahilmgandhi 18:6a4db94011d3 1525 /* NOTE : This function Should not be modified, when the callback is needed,
sahilmgandhi 18:6a4db94011d3 1526 the HAL_SD_DMA_TxErrorCallback could be implemented in the user file
sahilmgandhi 18:6a4db94011d3 1527 */
sahilmgandhi 18:6a4db94011d3 1528 }
sahilmgandhi 18:6a4db94011d3 1529
sahilmgandhi 18:6a4db94011d3 1530 /**
sahilmgandhi 18:6a4db94011d3 1531 * @}
sahilmgandhi 18:6a4db94011d3 1532 */
sahilmgandhi 18:6a4db94011d3 1533
sahilmgandhi 18:6a4db94011d3 1534 /** @addtogroup SD_Exported_Functions_Group3
sahilmgandhi 18:6a4db94011d3 1535 * @brief management functions
sahilmgandhi 18:6a4db94011d3 1536 *
sahilmgandhi 18:6a4db94011d3 1537 @verbatim
sahilmgandhi 18:6a4db94011d3 1538 ==============================================================================
sahilmgandhi 18:6a4db94011d3 1539 ##### Peripheral Control functions #####
sahilmgandhi 18:6a4db94011d3 1540 ==============================================================================
sahilmgandhi 18:6a4db94011d3 1541 [..]
sahilmgandhi 18:6a4db94011d3 1542 This subsection provides a set of functions allowing to control the SD card
sahilmgandhi 18:6a4db94011d3 1543 operations.
sahilmgandhi 18:6a4db94011d3 1544
sahilmgandhi 18:6a4db94011d3 1545 @endverbatim
sahilmgandhi 18:6a4db94011d3 1546 * @{
sahilmgandhi 18:6a4db94011d3 1547 */
sahilmgandhi 18:6a4db94011d3 1548
sahilmgandhi 18:6a4db94011d3 1549 /**
sahilmgandhi 18:6a4db94011d3 1550 * @brief Returns information about specific card.
sahilmgandhi 18:6a4db94011d3 1551 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1552 * @param pCardInfo: Pointer to a HAL_SD_CardInfoTypedef structure that
sahilmgandhi 18:6a4db94011d3 1553 * contains all SD cardinformation
sahilmgandhi 18:6a4db94011d3 1554 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 1555 */
sahilmgandhi 18:6a4db94011d3 1556 HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo)
sahilmgandhi 18:6a4db94011d3 1557 {
sahilmgandhi 18:6a4db94011d3 1558 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 1559 uint32_t tmp = 0U;
sahilmgandhi 18:6a4db94011d3 1560
sahilmgandhi 18:6a4db94011d3 1561 pCardInfo->CardType = (uint8_t)(hsd->CardType);
sahilmgandhi 18:6a4db94011d3 1562 pCardInfo->RCA = (uint16_t)(hsd->RCA);
sahilmgandhi 18:6a4db94011d3 1563
sahilmgandhi 18:6a4db94011d3 1564 /* Byte 0 */
sahilmgandhi 18:6a4db94011d3 1565 tmp = (hsd->CSD[0U] & 0xFF000000U) >> 24U;
sahilmgandhi 18:6a4db94011d3 1566 pCardInfo->SD_csd.CSDStruct = (uint8_t)((tmp & 0xC0U) >> 6U);
sahilmgandhi 18:6a4db94011d3 1567 pCardInfo->SD_csd.SysSpecVersion = (uint8_t)((tmp & 0x3CU) >> 2U);
sahilmgandhi 18:6a4db94011d3 1568 pCardInfo->SD_csd.Reserved1 = tmp & 0x03U;
sahilmgandhi 18:6a4db94011d3 1569
sahilmgandhi 18:6a4db94011d3 1570 /* Byte 1 */
sahilmgandhi 18:6a4db94011d3 1571 tmp = (hsd->CSD[0U] & 0x00FF0000U) >> 16U;
sahilmgandhi 18:6a4db94011d3 1572 pCardInfo->SD_csd.TAAC = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 1573
sahilmgandhi 18:6a4db94011d3 1574 /* Byte 2 */
sahilmgandhi 18:6a4db94011d3 1575 tmp = (hsd->CSD[0U] & 0x0000FF00U) >> 8U;
sahilmgandhi 18:6a4db94011d3 1576 pCardInfo->SD_csd.NSAC = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 1577
sahilmgandhi 18:6a4db94011d3 1578 /* Byte 3 */
sahilmgandhi 18:6a4db94011d3 1579 tmp = hsd->CSD[0U] & 0x000000FFU;
sahilmgandhi 18:6a4db94011d3 1580 pCardInfo->SD_csd.MaxBusClkFrec = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 1581
sahilmgandhi 18:6a4db94011d3 1582 /* Byte 4 */
sahilmgandhi 18:6a4db94011d3 1583 tmp = (hsd->CSD[1U] & 0xFF000000U) >> 24U;
sahilmgandhi 18:6a4db94011d3 1584 pCardInfo->SD_csd.CardComdClasses = (uint16_t)(tmp << 4U);
sahilmgandhi 18:6a4db94011d3 1585
sahilmgandhi 18:6a4db94011d3 1586 /* Byte 5 */
sahilmgandhi 18:6a4db94011d3 1587 tmp = (hsd->CSD[1U] & 0x00FF0000U) >> 16U;
sahilmgandhi 18:6a4db94011d3 1588 pCardInfo->SD_csd.CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4U);
sahilmgandhi 18:6a4db94011d3 1589 pCardInfo->SD_csd.RdBlockLen = (uint8_t)(tmp & 0x0FU);
sahilmgandhi 18:6a4db94011d3 1590
sahilmgandhi 18:6a4db94011d3 1591 /* Byte 6 */
sahilmgandhi 18:6a4db94011d3 1592 tmp = (hsd->CSD[1U] & 0x0000FF00U) >> 8U;
sahilmgandhi 18:6a4db94011d3 1593 pCardInfo->SD_csd.PartBlockRead = (uint8_t)((tmp & 0x80U) >> 7U);
sahilmgandhi 18:6a4db94011d3 1594 pCardInfo->SD_csd.WrBlockMisalign = (uint8_t)((tmp & 0x40U) >> 6U);
sahilmgandhi 18:6a4db94011d3 1595 pCardInfo->SD_csd.RdBlockMisalign = (uint8_t)((tmp & 0x20U) >> 5U);
sahilmgandhi 18:6a4db94011d3 1596 pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10U) >> 4U);
sahilmgandhi 18:6a4db94011d3 1597 pCardInfo->SD_csd.Reserved2 = 0U; /*!< Reserved */
sahilmgandhi 18:6a4db94011d3 1598
sahilmgandhi 18:6a4db94011d3 1599 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0))
sahilmgandhi 18:6a4db94011d3 1600 {
sahilmgandhi 18:6a4db94011d3 1601 pCardInfo->SD_csd.DeviceSize = (tmp & 0x03U) << 10U;
sahilmgandhi 18:6a4db94011d3 1602
sahilmgandhi 18:6a4db94011d3 1603 /* Byte 7 */
sahilmgandhi 18:6a4db94011d3 1604 tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU);
sahilmgandhi 18:6a4db94011d3 1605 pCardInfo->SD_csd.DeviceSize |= (tmp) << 2U;
sahilmgandhi 18:6a4db94011d3 1606
sahilmgandhi 18:6a4db94011d3 1607 /* Byte 8 */
sahilmgandhi 18:6a4db94011d3 1608 tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U);
sahilmgandhi 18:6a4db94011d3 1609 pCardInfo->SD_csd.DeviceSize |= (tmp & 0xC0U) >> 6U;
sahilmgandhi 18:6a4db94011d3 1610
sahilmgandhi 18:6a4db94011d3 1611 pCardInfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38U) >> 3U;
sahilmgandhi 18:6a4db94011d3 1612 pCardInfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07U);
sahilmgandhi 18:6a4db94011d3 1613
sahilmgandhi 18:6a4db94011d3 1614 /* Byte 9 */
sahilmgandhi 18:6a4db94011d3 1615 tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U);
sahilmgandhi 18:6a4db94011d3 1616 pCardInfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0U) >> 5U;
sahilmgandhi 18:6a4db94011d3 1617 pCardInfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1CU) >> 2U;
sahilmgandhi 18:6a4db94011d3 1618 pCardInfo->SD_csd.DeviceSizeMul = (tmp & 0x03U) << 1U;
sahilmgandhi 18:6a4db94011d3 1619 /* Byte 10 */
sahilmgandhi 18:6a4db94011d3 1620 tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U);
sahilmgandhi 18:6a4db94011d3 1621 pCardInfo->SD_csd.DeviceSizeMul |= (tmp & 0x80U) >> 7U;
sahilmgandhi 18:6a4db94011d3 1622
sahilmgandhi 18:6a4db94011d3 1623 pCardInfo->CardCapacity = (pCardInfo->SD_csd.DeviceSize + 1U) ;
sahilmgandhi 18:6a4db94011d3 1624 pCardInfo->CardCapacity *= (1U << (pCardInfo->SD_csd.DeviceSizeMul + 2U));
sahilmgandhi 18:6a4db94011d3 1625 pCardInfo->CardBlockSize = 1U << (pCardInfo->SD_csd.RdBlockLen);
sahilmgandhi 18:6a4db94011d3 1626 pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
sahilmgandhi 18:6a4db94011d3 1627 }
sahilmgandhi 18:6a4db94011d3 1628 else if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
sahilmgandhi 18:6a4db94011d3 1629 {
sahilmgandhi 18:6a4db94011d3 1630 /* Byte 7 */
sahilmgandhi 18:6a4db94011d3 1631 tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU);
sahilmgandhi 18:6a4db94011d3 1632 pCardInfo->SD_csd.DeviceSize = (tmp & 0x3FU) << 16U;
sahilmgandhi 18:6a4db94011d3 1633
sahilmgandhi 18:6a4db94011d3 1634 /* Byte 8 */
sahilmgandhi 18:6a4db94011d3 1635 tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U);
sahilmgandhi 18:6a4db94011d3 1636
sahilmgandhi 18:6a4db94011d3 1637 pCardInfo->SD_csd.DeviceSize |= (tmp << 8U);
sahilmgandhi 18:6a4db94011d3 1638
sahilmgandhi 18:6a4db94011d3 1639 /* Byte 9 */
sahilmgandhi 18:6a4db94011d3 1640 tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U);
sahilmgandhi 18:6a4db94011d3 1641
sahilmgandhi 18:6a4db94011d3 1642 pCardInfo->SD_csd.DeviceSize |= (tmp);
sahilmgandhi 18:6a4db94011d3 1643
sahilmgandhi 18:6a4db94011d3 1644 /* Byte 10 */
sahilmgandhi 18:6a4db94011d3 1645 tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U);
sahilmgandhi 18:6a4db94011d3 1646
sahilmgandhi 18:6a4db94011d3 1647 pCardInfo->CardCapacity = (uint64_t)((((uint64_t)pCardInfo->SD_csd.DeviceSize + 1U)) * 512U * 1024U);
sahilmgandhi 18:6a4db94011d3 1648 pCardInfo->CardBlockSize = 512U;
sahilmgandhi 18:6a4db94011d3 1649 }
sahilmgandhi 18:6a4db94011d3 1650 else
sahilmgandhi 18:6a4db94011d3 1651 {
sahilmgandhi 18:6a4db94011d3 1652 /* Not supported card type */
sahilmgandhi 18:6a4db94011d3 1653 errorstate = SD_ERROR;
sahilmgandhi 18:6a4db94011d3 1654 }
sahilmgandhi 18:6a4db94011d3 1655
sahilmgandhi 18:6a4db94011d3 1656 pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40U) >> 6U;
sahilmgandhi 18:6a4db94011d3 1657 pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3FU) << 1U;
sahilmgandhi 18:6a4db94011d3 1658
sahilmgandhi 18:6a4db94011d3 1659 /* Byte 11 */
sahilmgandhi 18:6a4db94011d3 1660 tmp = (uint8_t)(hsd->CSD[2U] & 0x000000FFU);
sahilmgandhi 18:6a4db94011d3 1661 pCardInfo->SD_csd.EraseGrMul |= (tmp & 0x80U) >> 7U;
sahilmgandhi 18:6a4db94011d3 1662 pCardInfo->SD_csd.WrProtectGrSize = (tmp & 0x7FU);
sahilmgandhi 18:6a4db94011d3 1663
sahilmgandhi 18:6a4db94011d3 1664 /* Byte 12 */
sahilmgandhi 18:6a4db94011d3 1665 tmp = (uint8_t)((hsd->CSD[3U] & 0xFF000000U) >> 24U);
sahilmgandhi 18:6a4db94011d3 1666 pCardInfo->SD_csd.WrProtectGrEnable = (tmp & 0x80U) >> 7U;
sahilmgandhi 18:6a4db94011d3 1667 pCardInfo->SD_csd.ManDeflECC = (tmp & 0x60U) >> 5U;
sahilmgandhi 18:6a4db94011d3 1668 pCardInfo->SD_csd.WrSpeedFact = (tmp & 0x1CU) >> 2U;
sahilmgandhi 18:6a4db94011d3 1669 pCardInfo->SD_csd.MaxWrBlockLen = (tmp & 0x03U) << 2U;
sahilmgandhi 18:6a4db94011d3 1670
sahilmgandhi 18:6a4db94011d3 1671 /* Byte 13 */
sahilmgandhi 18:6a4db94011d3 1672 tmp = (uint8_t)((hsd->CSD[3U] & 0x00FF0000U) >> 16U);
sahilmgandhi 18:6a4db94011d3 1673 pCardInfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0U) >> 6U;
sahilmgandhi 18:6a4db94011d3 1674 pCardInfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20U) >> 5U;
sahilmgandhi 18:6a4db94011d3 1675 pCardInfo->SD_csd.Reserved3 = 0U;
sahilmgandhi 18:6a4db94011d3 1676 pCardInfo->SD_csd.ContentProtectAppli = (tmp & 0x01U);
sahilmgandhi 18:6a4db94011d3 1677
sahilmgandhi 18:6a4db94011d3 1678 /* Byte 14 */
sahilmgandhi 18:6a4db94011d3 1679 tmp = (uint8_t)((hsd->CSD[3U] & 0x0000FF00U) >> 8U);
sahilmgandhi 18:6a4db94011d3 1680 pCardInfo->SD_csd.FileFormatGrouop = (tmp & 0x80U) >> 7U;
sahilmgandhi 18:6a4db94011d3 1681 pCardInfo->SD_csd.CopyFlag = (tmp & 0x40U) >> 6U;
sahilmgandhi 18:6a4db94011d3 1682 pCardInfo->SD_csd.PermWrProtect = (tmp & 0x20U) >> 5U;
sahilmgandhi 18:6a4db94011d3 1683 pCardInfo->SD_csd.TempWrProtect = (tmp & 0x10U) >> 4U;
sahilmgandhi 18:6a4db94011d3 1684 pCardInfo->SD_csd.FileFormat = (tmp & 0x0CU) >> 2U;
sahilmgandhi 18:6a4db94011d3 1685 pCardInfo->SD_csd.ECC = (tmp & 0x03U);
sahilmgandhi 18:6a4db94011d3 1686
sahilmgandhi 18:6a4db94011d3 1687 /* Byte 15 */
sahilmgandhi 18:6a4db94011d3 1688 tmp = (uint8_t)(hsd->CSD[3U] & 0x000000FFU);
sahilmgandhi 18:6a4db94011d3 1689 pCardInfo->SD_csd.CSD_CRC = (tmp & 0xFEU) >> 1U;
sahilmgandhi 18:6a4db94011d3 1690 pCardInfo->SD_csd.Reserved4 = 1U;
sahilmgandhi 18:6a4db94011d3 1691
sahilmgandhi 18:6a4db94011d3 1692 /* Byte 0 */
sahilmgandhi 18:6a4db94011d3 1693 tmp = (uint8_t)((hsd->CID[0U] & 0xFF000000U) >> 24U);
sahilmgandhi 18:6a4db94011d3 1694 pCardInfo->SD_cid.ManufacturerID = tmp;
sahilmgandhi 18:6a4db94011d3 1695
sahilmgandhi 18:6a4db94011d3 1696 /* Byte 1 */
sahilmgandhi 18:6a4db94011d3 1697 tmp = (uint8_t)((hsd->CID[0U] & 0x00FF0000U) >> 16U);
sahilmgandhi 18:6a4db94011d3 1698 pCardInfo->SD_cid.OEM_AppliID = tmp << 8U;
sahilmgandhi 18:6a4db94011d3 1699
sahilmgandhi 18:6a4db94011d3 1700 /* Byte 2 */
sahilmgandhi 18:6a4db94011d3 1701 tmp = (uint8_t)((hsd->CID[0U] & 0x0000FF00U) >> 8U);
sahilmgandhi 18:6a4db94011d3 1702 pCardInfo->SD_cid.OEM_AppliID |= tmp;
sahilmgandhi 18:6a4db94011d3 1703
sahilmgandhi 18:6a4db94011d3 1704 /* Byte 3 */
sahilmgandhi 18:6a4db94011d3 1705 tmp = (uint8_t)(hsd->CID[0U] & 0x000000FFU);
sahilmgandhi 18:6a4db94011d3 1706 pCardInfo->SD_cid.ProdName1 = tmp << 24U;
sahilmgandhi 18:6a4db94011d3 1707
sahilmgandhi 18:6a4db94011d3 1708 /* Byte 4 */
sahilmgandhi 18:6a4db94011d3 1709 tmp = (uint8_t)((hsd->CID[1U] & 0xFF000000U) >> 24U);
sahilmgandhi 18:6a4db94011d3 1710 pCardInfo->SD_cid.ProdName1 |= tmp << 16U;
sahilmgandhi 18:6a4db94011d3 1711
sahilmgandhi 18:6a4db94011d3 1712 /* Byte 5 */
sahilmgandhi 18:6a4db94011d3 1713 tmp = (uint8_t)((hsd->CID[1U] & 0x00FF0000U) >> 16U);
sahilmgandhi 18:6a4db94011d3 1714 pCardInfo->SD_cid.ProdName1 |= tmp << 8U;
sahilmgandhi 18:6a4db94011d3 1715
sahilmgandhi 18:6a4db94011d3 1716 /* Byte 6 */
sahilmgandhi 18:6a4db94011d3 1717 tmp = (uint8_t)((hsd->CID[1U] & 0x0000FF00U) >> 8U);
sahilmgandhi 18:6a4db94011d3 1718 pCardInfo->SD_cid.ProdName1 |= tmp;
sahilmgandhi 18:6a4db94011d3 1719
sahilmgandhi 18:6a4db94011d3 1720 /* Byte 7 */
sahilmgandhi 18:6a4db94011d3 1721 tmp = (uint8_t)(hsd->CID[1U] & 0x000000FFU);
sahilmgandhi 18:6a4db94011d3 1722 pCardInfo->SD_cid.ProdName2 = tmp;
sahilmgandhi 18:6a4db94011d3 1723
sahilmgandhi 18:6a4db94011d3 1724 /* Byte 8 */
sahilmgandhi 18:6a4db94011d3 1725 tmp = (uint8_t)((hsd->CID[2U] & 0xFF000000U) >> 24U);
sahilmgandhi 18:6a4db94011d3 1726 pCardInfo->SD_cid.ProdRev = tmp;
sahilmgandhi 18:6a4db94011d3 1727
sahilmgandhi 18:6a4db94011d3 1728 /* Byte 9 */
sahilmgandhi 18:6a4db94011d3 1729 tmp = (uint8_t)((hsd->CID[2U] & 0x00FF0000U) >> 16U);
sahilmgandhi 18:6a4db94011d3 1730 pCardInfo->SD_cid.ProdSN = tmp << 24U;
sahilmgandhi 18:6a4db94011d3 1731
sahilmgandhi 18:6a4db94011d3 1732 /* Byte 10 */
sahilmgandhi 18:6a4db94011d3 1733 tmp = (uint8_t)((hsd->CID[2U] & 0x0000FF00U) >> 8U);
sahilmgandhi 18:6a4db94011d3 1734 pCardInfo->SD_cid.ProdSN |= tmp << 16U;
sahilmgandhi 18:6a4db94011d3 1735
sahilmgandhi 18:6a4db94011d3 1736 /* Byte 11 */
sahilmgandhi 18:6a4db94011d3 1737 tmp = (uint8_t)(hsd->CID[2U] & 0x000000FFU);
sahilmgandhi 18:6a4db94011d3 1738 pCardInfo->SD_cid.ProdSN |= tmp << 8U;
sahilmgandhi 18:6a4db94011d3 1739
sahilmgandhi 18:6a4db94011d3 1740 /* Byte 12 */
sahilmgandhi 18:6a4db94011d3 1741 tmp = (uint8_t)((hsd->CID[3U] & 0xFF000000U) >> 24U);
sahilmgandhi 18:6a4db94011d3 1742 pCardInfo->SD_cid.ProdSN |= tmp;
sahilmgandhi 18:6a4db94011d3 1743
sahilmgandhi 18:6a4db94011d3 1744 /* Byte 13 */
sahilmgandhi 18:6a4db94011d3 1745 tmp = (uint8_t)((hsd->CID[3U] & 0x00FF0000U) >> 16U);
sahilmgandhi 18:6a4db94011d3 1746 pCardInfo->SD_cid.Reserved1 |= (tmp & 0xF0U) >> 4U;
sahilmgandhi 18:6a4db94011d3 1747 pCardInfo->SD_cid.ManufactDate = (tmp & 0x0FU) << 8U;
sahilmgandhi 18:6a4db94011d3 1748
sahilmgandhi 18:6a4db94011d3 1749 /* Byte 14 */
sahilmgandhi 18:6a4db94011d3 1750 tmp = (uint8_t)((hsd->CID[3U] & 0x0000FF00U) >> 8U);
sahilmgandhi 18:6a4db94011d3 1751 pCardInfo->SD_cid.ManufactDate |= tmp;
sahilmgandhi 18:6a4db94011d3 1752
sahilmgandhi 18:6a4db94011d3 1753 /* Byte 15 */
sahilmgandhi 18:6a4db94011d3 1754 tmp = (uint8_t)(hsd->CID[3U] & 0x000000FFU);
sahilmgandhi 18:6a4db94011d3 1755 pCardInfo->SD_cid.CID_CRC = (tmp & 0xFEU) >> 1U;
sahilmgandhi 18:6a4db94011d3 1756 pCardInfo->SD_cid.Reserved2 = 1U;
sahilmgandhi 18:6a4db94011d3 1757
sahilmgandhi 18:6a4db94011d3 1758 return errorstate;
sahilmgandhi 18:6a4db94011d3 1759 }
sahilmgandhi 18:6a4db94011d3 1760
sahilmgandhi 18:6a4db94011d3 1761 /**
sahilmgandhi 18:6a4db94011d3 1762 * @brief Enables wide bus operation for the requested card if supported by
sahilmgandhi 18:6a4db94011d3 1763 * card.
sahilmgandhi 18:6a4db94011d3 1764 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1765 * @param WideMode: Specifies the SD card wide bus mode
sahilmgandhi 18:6a4db94011d3 1766 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 1767 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer (Only for MMC)
sahilmgandhi 18:6a4db94011d3 1768 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
sahilmgandhi 18:6a4db94011d3 1769 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
sahilmgandhi 18:6a4db94011d3 1770 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 1771 */
sahilmgandhi 18:6a4db94011d3 1772 HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config(SD_HandleTypeDef *hsd, uint32_t WideMode)
sahilmgandhi 18:6a4db94011d3 1773 {
sahilmgandhi 18:6a4db94011d3 1774 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 1775 SDIO_InitTypeDef tmpinit;
sahilmgandhi 18:6a4db94011d3 1776
sahilmgandhi 18:6a4db94011d3 1777 /* MMC Card does not support this feature */
sahilmgandhi 18:6a4db94011d3 1778 if (hsd->CardType == MULTIMEDIA_CARD)
sahilmgandhi 18:6a4db94011d3 1779 {
sahilmgandhi 18:6a4db94011d3 1780 errorstate = SD_UNSUPPORTED_FEATURE;
sahilmgandhi 18:6a4db94011d3 1781
sahilmgandhi 18:6a4db94011d3 1782 return errorstate;
sahilmgandhi 18:6a4db94011d3 1783 }
sahilmgandhi 18:6a4db94011d3 1784 else if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
sahilmgandhi 18:6a4db94011d3 1785 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
sahilmgandhi 18:6a4db94011d3 1786 {
sahilmgandhi 18:6a4db94011d3 1787 if (WideMode == SDIO_BUS_WIDE_8B)
sahilmgandhi 18:6a4db94011d3 1788 {
sahilmgandhi 18:6a4db94011d3 1789 errorstate = SD_UNSUPPORTED_FEATURE;
sahilmgandhi 18:6a4db94011d3 1790 }
sahilmgandhi 18:6a4db94011d3 1791 else if (WideMode == SDIO_BUS_WIDE_4B)
sahilmgandhi 18:6a4db94011d3 1792 {
sahilmgandhi 18:6a4db94011d3 1793 errorstate = SD_WideBus_Enable(hsd);
sahilmgandhi 18:6a4db94011d3 1794 }
sahilmgandhi 18:6a4db94011d3 1795 else if (WideMode == SDIO_BUS_WIDE_1B)
sahilmgandhi 18:6a4db94011d3 1796 {
sahilmgandhi 18:6a4db94011d3 1797 errorstate = SD_WideBus_Disable(hsd);
sahilmgandhi 18:6a4db94011d3 1798 }
sahilmgandhi 18:6a4db94011d3 1799 else
sahilmgandhi 18:6a4db94011d3 1800 {
sahilmgandhi 18:6a4db94011d3 1801 /* WideMode is not a valid argument*/
sahilmgandhi 18:6a4db94011d3 1802 errorstate = SD_INVALID_PARAMETER;
sahilmgandhi 18:6a4db94011d3 1803 }
sahilmgandhi 18:6a4db94011d3 1804
sahilmgandhi 18:6a4db94011d3 1805 if (errorstate == SD_OK)
sahilmgandhi 18:6a4db94011d3 1806 {
sahilmgandhi 18:6a4db94011d3 1807 /* Configure the SDIO peripheral */
sahilmgandhi 18:6a4db94011d3 1808 tmpinit.ClockEdge = hsd->Init.ClockEdge;
sahilmgandhi 18:6a4db94011d3 1809 tmpinit.ClockBypass = hsd->Init.ClockBypass;
sahilmgandhi 18:6a4db94011d3 1810 tmpinit.ClockPowerSave = hsd->Init.ClockPowerSave;
sahilmgandhi 18:6a4db94011d3 1811 tmpinit.BusWide = WideMode;
sahilmgandhi 18:6a4db94011d3 1812 tmpinit.HardwareFlowControl = hsd->Init.HardwareFlowControl;
sahilmgandhi 18:6a4db94011d3 1813 tmpinit.ClockDiv = hsd->Init.ClockDiv;
sahilmgandhi 18:6a4db94011d3 1814 SDIO_Init(hsd->Instance, tmpinit);
sahilmgandhi 18:6a4db94011d3 1815 }
sahilmgandhi 18:6a4db94011d3 1816 }
sahilmgandhi 18:6a4db94011d3 1817
sahilmgandhi 18:6a4db94011d3 1818 return errorstate;
sahilmgandhi 18:6a4db94011d3 1819 }
sahilmgandhi 18:6a4db94011d3 1820
sahilmgandhi 18:6a4db94011d3 1821 /**
sahilmgandhi 18:6a4db94011d3 1822 * @brief Aborts an ongoing data transfer.
sahilmgandhi 18:6a4db94011d3 1823 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1824 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 1825 */
sahilmgandhi 18:6a4db94011d3 1826 HAL_SD_ErrorTypedef HAL_SD_StopTransfer(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 1827 {
sahilmgandhi 18:6a4db94011d3 1828 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 1829 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 1830
sahilmgandhi 18:6a4db94011d3 1831 /* Send CMD12 STOP_TRANSMISSION */
sahilmgandhi 18:6a4db94011d3 1832 sdio_cmdinitstructure.Argument = 0U;
sahilmgandhi 18:6a4db94011d3 1833 sdio_cmdinitstructure.CmdIndex = SD_CMD_STOP_TRANSMISSION;
sahilmgandhi 18:6a4db94011d3 1834 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 1835 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 1836 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 1837 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 1838
sahilmgandhi 18:6a4db94011d3 1839 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 1840 errorstate = SD_CmdResp1Error(hsd, SD_CMD_STOP_TRANSMISSION);
sahilmgandhi 18:6a4db94011d3 1841
sahilmgandhi 18:6a4db94011d3 1842 return errorstate;
sahilmgandhi 18:6a4db94011d3 1843 }
sahilmgandhi 18:6a4db94011d3 1844
sahilmgandhi 18:6a4db94011d3 1845 /**
sahilmgandhi 18:6a4db94011d3 1846 * @brief Switches the SD card to High Speed mode.
sahilmgandhi 18:6a4db94011d3 1847 * This API must be used after "Transfer State"
sahilmgandhi 18:6a4db94011d3 1848 * @note This operation should be followed by the configuration
sahilmgandhi 18:6a4db94011d3 1849 * of PLL to have SDIOCK clock between 67 and 75 MHz
sahilmgandhi 18:6a4db94011d3 1850 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 1851 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 1852 */
sahilmgandhi 18:6a4db94011d3 1853 HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 1854 {
sahilmgandhi 18:6a4db94011d3 1855 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 1856 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 1857 SDIO_DataInitTypeDef sdio_datainitstructure;
sahilmgandhi 18:6a4db94011d3 1858
sahilmgandhi 18:6a4db94011d3 1859 uint8_t SD_hs[64U] = {0U};
sahilmgandhi 18:6a4db94011d3 1860 uint32_t SD_scr[2U] = {0U, 0U};
sahilmgandhi 18:6a4db94011d3 1861 uint32_t SD_SPEC = 0U;
sahilmgandhi 18:6a4db94011d3 1862 uint32_t count = 0U, *tempbuff = (uint32_t *)SD_hs;
sahilmgandhi 18:6a4db94011d3 1863
sahilmgandhi 18:6a4db94011d3 1864 /* Initialize the Data control register */
sahilmgandhi 18:6a4db94011d3 1865 hsd->Instance->DCTRL = 0U;
sahilmgandhi 18:6a4db94011d3 1866
sahilmgandhi 18:6a4db94011d3 1867 /* Get SCR Register */
sahilmgandhi 18:6a4db94011d3 1868 errorstate = SD_FindSCR(hsd, SD_scr);
sahilmgandhi 18:6a4db94011d3 1869
sahilmgandhi 18:6a4db94011d3 1870 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 1871 {
sahilmgandhi 18:6a4db94011d3 1872 return errorstate;
sahilmgandhi 18:6a4db94011d3 1873 }
sahilmgandhi 18:6a4db94011d3 1874
sahilmgandhi 18:6a4db94011d3 1875 /* Test the Version supported by the card*/
sahilmgandhi 18:6a4db94011d3 1876 SD_SPEC = (SD_scr[1U] & 0x01000000U) | (SD_scr[1U] & 0x02000000U);
sahilmgandhi 18:6a4db94011d3 1877
sahilmgandhi 18:6a4db94011d3 1878 if (SD_SPEC != SD_ALLZERO)
sahilmgandhi 18:6a4db94011d3 1879 {
sahilmgandhi 18:6a4db94011d3 1880 /* Set Block Size for Card */
sahilmgandhi 18:6a4db94011d3 1881 sdio_cmdinitstructure.Argument = (uint32_t)64U;
sahilmgandhi 18:6a4db94011d3 1882 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
sahilmgandhi 18:6a4db94011d3 1883 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 1884 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 1885 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 1886 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 1887
sahilmgandhi 18:6a4db94011d3 1888 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 1889 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
sahilmgandhi 18:6a4db94011d3 1890
sahilmgandhi 18:6a4db94011d3 1891 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 1892 {
sahilmgandhi 18:6a4db94011d3 1893 return errorstate;
sahilmgandhi 18:6a4db94011d3 1894 }
sahilmgandhi 18:6a4db94011d3 1895
sahilmgandhi 18:6a4db94011d3 1896 /* Configure the SD DPSM (Data Path State Machine) */
sahilmgandhi 18:6a4db94011d3 1897 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 1898 sdio_datainitstructure.DataLength = 64U;
sahilmgandhi 18:6a4db94011d3 1899 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B ;
sahilmgandhi 18:6a4db94011d3 1900 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
sahilmgandhi 18:6a4db94011d3 1901 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
sahilmgandhi 18:6a4db94011d3 1902 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 1903 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
sahilmgandhi 18:6a4db94011d3 1904
sahilmgandhi 18:6a4db94011d3 1905 /* Send CMD6 switch mode */
sahilmgandhi 18:6a4db94011d3 1906 sdio_cmdinitstructure.Argument = 0x80FFFF01U;
sahilmgandhi 18:6a4db94011d3 1907 sdio_cmdinitstructure.CmdIndex = SD_CMD_HS_SWITCH;
sahilmgandhi 18:6a4db94011d3 1908 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 1909
sahilmgandhi 18:6a4db94011d3 1910 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 1911 errorstate = SD_CmdResp1Error(hsd, SD_CMD_HS_SWITCH);
sahilmgandhi 18:6a4db94011d3 1912
sahilmgandhi 18:6a4db94011d3 1913 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 1914 {
sahilmgandhi 18:6a4db94011d3 1915 return errorstate;
sahilmgandhi 18:6a4db94011d3 1916 }
sahilmgandhi 18:6a4db94011d3 1917 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 1918 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 1919 #else /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 1920 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
sahilmgandhi 18:6a4db94011d3 1921 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 1922 {
sahilmgandhi 18:6a4db94011d3 1923 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
sahilmgandhi 18:6a4db94011d3 1924 {
sahilmgandhi 18:6a4db94011d3 1925 for (count = 0U; count < 8U; count++)
sahilmgandhi 18:6a4db94011d3 1926 {
sahilmgandhi 18:6a4db94011d3 1927 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 1928 }
sahilmgandhi 18:6a4db94011d3 1929
sahilmgandhi 18:6a4db94011d3 1930 tempbuff += 8U;
sahilmgandhi 18:6a4db94011d3 1931 }
sahilmgandhi 18:6a4db94011d3 1932 }
sahilmgandhi 18:6a4db94011d3 1933
sahilmgandhi 18:6a4db94011d3 1934 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
sahilmgandhi 18:6a4db94011d3 1935 {
sahilmgandhi 18:6a4db94011d3 1936 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
sahilmgandhi 18:6a4db94011d3 1937
sahilmgandhi 18:6a4db94011d3 1938 errorstate = SD_DATA_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 1939
sahilmgandhi 18:6a4db94011d3 1940 return errorstate;
sahilmgandhi 18:6a4db94011d3 1941 }
sahilmgandhi 18:6a4db94011d3 1942 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
sahilmgandhi 18:6a4db94011d3 1943 {
sahilmgandhi 18:6a4db94011d3 1944 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
sahilmgandhi 18:6a4db94011d3 1945
sahilmgandhi 18:6a4db94011d3 1946 errorstate = SD_DATA_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 1947
sahilmgandhi 18:6a4db94011d3 1948 return errorstate;
sahilmgandhi 18:6a4db94011d3 1949 }
sahilmgandhi 18:6a4db94011d3 1950 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
sahilmgandhi 18:6a4db94011d3 1951 {
sahilmgandhi 18:6a4db94011d3 1952 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
sahilmgandhi 18:6a4db94011d3 1953
sahilmgandhi 18:6a4db94011d3 1954 errorstate = SD_RX_OVERRUN;
sahilmgandhi 18:6a4db94011d3 1955
sahilmgandhi 18:6a4db94011d3 1956 return errorstate;
sahilmgandhi 18:6a4db94011d3 1957 }
sahilmgandhi 18:6a4db94011d3 1958 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 1959 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 1960 {
sahilmgandhi 18:6a4db94011d3 1961 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
sahilmgandhi 18:6a4db94011d3 1962
sahilmgandhi 18:6a4db94011d3 1963 errorstate = SD_START_BIT_ERR;
sahilmgandhi 18:6a4db94011d3 1964
sahilmgandhi 18:6a4db94011d3 1965 return errorstate;
sahilmgandhi 18:6a4db94011d3 1966 }
sahilmgandhi 18:6a4db94011d3 1967 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 1968 else
sahilmgandhi 18:6a4db94011d3 1969 {
sahilmgandhi 18:6a4db94011d3 1970 /* No error flag set */
sahilmgandhi 18:6a4db94011d3 1971 }
sahilmgandhi 18:6a4db94011d3 1972
sahilmgandhi 18:6a4db94011d3 1973 count = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 1974
sahilmgandhi 18:6a4db94011d3 1975 while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0U))
sahilmgandhi 18:6a4db94011d3 1976 {
sahilmgandhi 18:6a4db94011d3 1977 *tempbuff = SDIO_ReadFIFO(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 1978 tempbuff++;
sahilmgandhi 18:6a4db94011d3 1979 count--;
sahilmgandhi 18:6a4db94011d3 1980 }
sahilmgandhi 18:6a4db94011d3 1981
sahilmgandhi 18:6a4db94011d3 1982 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 1983 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 1984
sahilmgandhi 18:6a4db94011d3 1985 /* Test if the switch mode HS is ok */
sahilmgandhi 18:6a4db94011d3 1986 if ((SD_hs[13U]& 2U) != 2U)
sahilmgandhi 18:6a4db94011d3 1987 {
sahilmgandhi 18:6a4db94011d3 1988 errorstate = SD_UNSUPPORTED_FEATURE;
sahilmgandhi 18:6a4db94011d3 1989 }
sahilmgandhi 18:6a4db94011d3 1990 }
sahilmgandhi 18:6a4db94011d3 1991
sahilmgandhi 18:6a4db94011d3 1992 return errorstate;
sahilmgandhi 18:6a4db94011d3 1993 }
sahilmgandhi 18:6a4db94011d3 1994
sahilmgandhi 18:6a4db94011d3 1995 /**
sahilmgandhi 18:6a4db94011d3 1996 * @}
sahilmgandhi 18:6a4db94011d3 1997 */
sahilmgandhi 18:6a4db94011d3 1998
sahilmgandhi 18:6a4db94011d3 1999 /** @addtogroup SD_Exported_Functions_Group4
sahilmgandhi 18:6a4db94011d3 2000 * @brief Peripheral State functions
sahilmgandhi 18:6a4db94011d3 2001 *
sahilmgandhi 18:6a4db94011d3 2002 @verbatim
sahilmgandhi 18:6a4db94011d3 2003 ==============================================================================
sahilmgandhi 18:6a4db94011d3 2004 ##### Peripheral State functions #####
sahilmgandhi 18:6a4db94011d3 2005 ==============================================================================
sahilmgandhi 18:6a4db94011d3 2006 [..]
sahilmgandhi 18:6a4db94011d3 2007 This subsection permits to get in runtime the status of the peripheral
sahilmgandhi 18:6a4db94011d3 2008 and the data flow.
sahilmgandhi 18:6a4db94011d3 2009
sahilmgandhi 18:6a4db94011d3 2010 @endverbatim
sahilmgandhi 18:6a4db94011d3 2011 * @{
sahilmgandhi 18:6a4db94011d3 2012 */
sahilmgandhi 18:6a4db94011d3 2013
sahilmgandhi 18:6a4db94011d3 2014 /**
sahilmgandhi 18:6a4db94011d3 2015 * @brief Returns the current SD card's status.
sahilmgandhi 18:6a4db94011d3 2016 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2017 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
sahilmgandhi 18:6a4db94011d3 2018 * SD Status register)
sahilmgandhi 18:6a4db94011d3 2019 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2020 */
sahilmgandhi 18:6a4db94011d3 2021 HAL_SD_ErrorTypedef HAL_SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
sahilmgandhi 18:6a4db94011d3 2022 {
sahilmgandhi 18:6a4db94011d3 2023 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 2024 SDIO_DataInitTypeDef sdio_datainitstructure;
sahilmgandhi 18:6a4db94011d3 2025 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2026 uint32_t count = 0U;
sahilmgandhi 18:6a4db94011d3 2027
sahilmgandhi 18:6a4db94011d3 2028 /* Check SD response */
sahilmgandhi 18:6a4db94011d3 2029 if ((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
sahilmgandhi 18:6a4db94011d3 2030 {
sahilmgandhi 18:6a4db94011d3 2031 errorstate = SD_LOCK_UNLOCK_FAILED;
sahilmgandhi 18:6a4db94011d3 2032
sahilmgandhi 18:6a4db94011d3 2033 return errorstate;
sahilmgandhi 18:6a4db94011d3 2034 }
sahilmgandhi 18:6a4db94011d3 2035
sahilmgandhi 18:6a4db94011d3 2036 /* Set block size for card if it is not equal to current block size for card */
sahilmgandhi 18:6a4db94011d3 2037 sdio_cmdinitstructure.Argument = 64U;
sahilmgandhi 18:6a4db94011d3 2038 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
sahilmgandhi 18:6a4db94011d3 2039 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 2040 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 2041 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 2042 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2043
sahilmgandhi 18:6a4db94011d3 2044 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2045 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
sahilmgandhi 18:6a4db94011d3 2046
sahilmgandhi 18:6a4db94011d3 2047 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2048 {
sahilmgandhi 18:6a4db94011d3 2049 return errorstate;
sahilmgandhi 18:6a4db94011d3 2050 }
sahilmgandhi 18:6a4db94011d3 2051
sahilmgandhi 18:6a4db94011d3 2052 /* Send CMD55 */
sahilmgandhi 18:6a4db94011d3 2053 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16U);
sahilmgandhi 18:6a4db94011d3 2054 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
sahilmgandhi 18:6a4db94011d3 2055 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2056
sahilmgandhi 18:6a4db94011d3 2057 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2058 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
sahilmgandhi 18:6a4db94011d3 2059
sahilmgandhi 18:6a4db94011d3 2060 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2061 {
sahilmgandhi 18:6a4db94011d3 2062 return errorstate;
sahilmgandhi 18:6a4db94011d3 2063 }
sahilmgandhi 18:6a4db94011d3 2064
sahilmgandhi 18:6a4db94011d3 2065 /* Configure the SD DPSM (Data Path State Machine) */
sahilmgandhi 18:6a4db94011d3 2066 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 2067 sdio_datainitstructure.DataLength = 64U;
sahilmgandhi 18:6a4db94011d3 2068 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
sahilmgandhi 18:6a4db94011d3 2069 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
sahilmgandhi 18:6a4db94011d3 2070 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
sahilmgandhi 18:6a4db94011d3 2071 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 2072 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
sahilmgandhi 18:6a4db94011d3 2073
sahilmgandhi 18:6a4db94011d3 2074 /* Send ACMD13 (SD_APP_STATUS) with argument as card's RCA */
sahilmgandhi 18:6a4db94011d3 2075 sdio_cmdinitstructure.Argument = 0U;
sahilmgandhi 18:6a4db94011d3 2076 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_STATUS;
sahilmgandhi 18:6a4db94011d3 2077 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2078
sahilmgandhi 18:6a4db94011d3 2079 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2080 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_STATUS);
sahilmgandhi 18:6a4db94011d3 2081
sahilmgandhi 18:6a4db94011d3 2082 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2083 {
sahilmgandhi 18:6a4db94011d3 2084 return errorstate;
sahilmgandhi 18:6a4db94011d3 2085 }
sahilmgandhi 18:6a4db94011d3 2086
sahilmgandhi 18:6a4db94011d3 2087 /* Get status data */
sahilmgandhi 18:6a4db94011d3 2088 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 2089 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 2090 #else /* SDIO_STA_STBITERR not defined */
sahilmgandhi 18:6a4db94011d3 2091 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
sahilmgandhi 18:6a4db94011d3 2092 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 2093 {
sahilmgandhi 18:6a4db94011d3 2094 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
sahilmgandhi 18:6a4db94011d3 2095 {
sahilmgandhi 18:6a4db94011d3 2096 for (count = 0U; count < 8U; count++)
sahilmgandhi 18:6a4db94011d3 2097 {
sahilmgandhi 18:6a4db94011d3 2098 *(pSDstatus + count) = SDIO_ReadFIFO(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 2099 }
sahilmgandhi 18:6a4db94011d3 2100
sahilmgandhi 18:6a4db94011d3 2101 pSDstatus += 8U;
sahilmgandhi 18:6a4db94011d3 2102 }
sahilmgandhi 18:6a4db94011d3 2103 }
sahilmgandhi 18:6a4db94011d3 2104
sahilmgandhi 18:6a4db94011d3 2105 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
sahilmgandhi 18:6a4db94011d3 2106 {
sahilmgandhi 18:6a4db94011d3 2107 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
sahilmgandhi 18:6a4db94011d3 2108
sahilmgandhi 18:6a4db94011d3 2109 errorstate = SD_DATA_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 2110
sahilmgandhi 18:6a4db94011d3 2111 return errorstate;
sahilmgandhi 18:6a4db94011d3 2112 }
sahilmgandhi 18:6a4db94011d3 2113 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
sahilmgandhi 18:6a4db94011d3 2114 {
sahilmgandhi 18:6a4db94011d3 2115 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
sahilmgandhi 18:6a4db94011d3 2116
sahilmgandhi 18:6a4db94011d3 2117 errorstate = SD_DATA_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 2118
sahilmgandhi 18:6a4db94011d3 2119 return errorstate;
sahilmgandhi 18:6a4db94011d3 2120 }
sahilmgandhi 18:6a4db94011d3 2121 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
sahilmgandhi 18:6a4db94011d3 2122 {
sahilmgandhi 18:6a4db94011d3 2123 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
sahilmgandhi 18:6a4db94011d3 2124
sahilmgandhi 18:6a4db94011d3 2125 errorstate = SD_RX_OVERRUN;
sahilmgandhi 18:6a4db94011d3 2126
sahilmgandhi 18:6a4db94011d3 2127 return errorstate;
sahilmgandhi 18:6a4db94011d3 2128 }
sahilmgandhi 18:6a4db94011d3 2129 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 2130 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 2131 {
sahilmgandhi 18:6a4db94011d3 2132 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
sahilmgandhi 18:6a4db94011d3 2133
sahilmgandhi 18:6a4db94011d3 2134 errorstate = SD_START_BIT_ERR;
sahilmgandhi 18:6a4db94011d3 2135
sahilmgandhi 18:6a4db94011d3 2136 return errorstate;
sahilmgandhi 18:6a4db94011d3 2137 }
sahilmgandhi 18:6a4db94011d3 2138 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 2139 else
sahilmgandhi 18:6a4db94011d3 2140 {
sahilmgandhi 18:6a4db94011d3 2141 /* No error flag set */
sahilmgandhi 18:6a4db94011d3 2142 }
sahilmgandhi 18:6a4db94011d3 2143
sahilmgandhi 18:6a4db94011d3 2144 count = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 2145 while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0U))
sahilmgandhi 18:6a4db94011d3 2146 {
sahilmgandhi 18:6a4db94011d3 2147 *pSDstatus = SDIO_ReadFIFO(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 2148 pSDstatus++;
sahilmgandhi 18:6a4db94011d3 2149 count--;
sahilmgandhi 18:6a4db94011d3 2150 }
sahilmgandhi 18:6a4db94011d3 2151
sahilmgandhi 18:6a4db94011d3 2152 /* Clear all the static status flags*/
sahilmgandhi 18:6a4db94011d3 2153 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 2154
sahilmgandhi 18:6a4db94011d3 2155 return errorstate;
sahilmgandhi 18:6a4db94011d3 2156 }
sahilmgandhi 18:6a4db94011d3 2157
sahilmgandhi 18:6a4db94011d3 2158 /**
sahilmgandhi 18:6a4db94011d3 2159 * @brief Gets the current sd card data status.
sahilmgandhi 18:6a4db94011d3 2160 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2161 * @retval Data Transfer state
sahilmgandhi 18:6a4db94011d3 2162 */
sahilmgandhi 18:6a4db94011d3 2163 HAL_SD_TransferStateTypedef HAL_SD_GetStatus(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 2164 {
sahilmgandhi 18:6a4db94011d3 2165 HAL_SD_CardStateTypedef cardstate = SD_CARD_TRANSFER;
sahilmgandhi 18:6a4db94011d3 2166
sahilmgandhi 18:6a4db94011d3 2167 /* Get SD card state */
sahilmgandhi 18:6a4db94011d3 2168 cardstate = SD_GetState(hsd);
sahilmgandhi 18:6a4db94011d3 2169
sahilmgandhi 18:6a4db94011d3 2170 /* Find SD status according to card state*/
sahilmgandhi 18:6a4db94011d3 2171 if (cardstate == SD_CARD_TRANSFER)
sahilmgandhi 18:6a4db94011d3 2172 {
sahilmgandhi 18:6a4db94011d3 2173 return SD_TRANSFER_OK;
sahilmgandhi 18:6a4db94011d3 2174 }
sahilmgandhi 18:6a4db94011d3 2175 else if(cardstate == SD_CARD_ERROR)
sahilmgandhi 18:6a4db94011d3 2176 {
sahilmgandhi 18:6a4db94011d3 2177 return SD_TRANSFER_ERROR;
sahilmgandhi 18:6a4db94011d3 2178 }
sahilmgandhi 18:6a4db94011d3 2179 else
sahilmgandhi 18:6a4db94011d3 2180 {
sahilmgandhi 18:6a4db94011d3 2181 return SD_TRANSFER_BUSY;
sahilmgandhi 18:6a4db94011d3 2182 }
sahilmgandhi 18:6a4db94011d3 2183 }
sahilmgandhi 18:6a4db94011d3 2184
sahilmgandhi 18:6a4db94011d3 2185 /**
sahilmgandhi 18:6a4db94011d3 2186 * @brief Gets the SD card status.
sahilmgandhi 18:6a4db94011d3 2187 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2188 * @param pCardStatus: Pointer to the HAL_SD_CardStatusTypedef structure that
sahilmgandhi 18:6a4db94011d3 2189 * will contain the SD card status information
sahilmgandhi 18:6a4db94011d3 2190 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2191 */
sahilmgandhi 18:6a4db94011d3 2192 HAL_SD_ErrorTypedef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus)
sahilmgandhi 18:6a4db94011d3 2193 {
sahilmgandhi 18:6a4db94011d3 2194 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2195 uint32_t tmp = 0U;
sahilmgandhi 18:6a4db94011d3 2196 uint32_t sd_status[16U];
sahilmgandhi 18:6a4db94011d3 2197
sahilmgandhi 18:6a4db94011d3 2198 errorstate = HAL_SD_SendSDStatus(hsd, sd_status);
sahilmgandhi 18:6a4db94011d3 2199
sahilmgandhi 18:6a4db94011d3 2200 if (errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2201 {
sahilmgandhi 18:6a4db94011d3 2202 return errorstate;
sahilmgandhi 18:6a4db94011d3 2203 }
sahilmgandhi 18:6a4db94011d3 2204
sahilmgandhi 18:6a4db94011d3 2205 /* Byte 0 */
sahilmgandhi 18:6a4db94011d3 2206 tmp = (sd_status[0U] & 0xC0U) >> 6U;
sahilmgandhi 18:6a4db94011d3 2207 pCardStatus->DAT_BUS_WIDTH = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2208
sahilmgandhi 18:6a4db94011d3 2209 /* Byte 0 */
sahilmgandhi 18:6a4db94011d3 2210 tmp = (sd_status[0U] & 0x20U) >> 5U;
sahilmgandhi 18:6a4db94011d3 2211 pCardStatus->SECURED_MODE = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2212
sahilmgandhi 18:6a4db94011d3 2213 /* Byte 2 */
sahilmgandhi 18:6a4db94011d3 2214 tmp = (sd_status[2U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2215 pCardStatus->SD_CARD_TYPE = (uint8_t)(tmp << 8U);
sahilmgandhi 18:6a4db94011d3 2216
sahilmgandhi 18:6a4db94011d3 2217 /* Byte 3 */
sahilmgandhi 18:6a4db94011d3 2218 tmp = (sd_status[3U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2219 pCardStatus->SD_CARD_TYPE |= (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2220
sahilmgandhi 18:6a4db94011d3 2221 /* Byte 4 */
sahilmgandhi 18:6a4db94011d3 2222 tmp = (sd_status[4U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2223 pCardStatus->SIZE_OF_PROTECTED_AREA = (uint8_t)(tmp << 24U);
sahilmgandhi 18:6a4db94011d3 2224
sahilmgandhi 18:6a4db94011d3 2225 /* Byte 5 */
sahilmgandhi 18:6a4db94011d3 2226 tmp = (sd_status[5U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2227 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 16U);
sahilmgandhi 18:6a4db94011d3 2228
sahilmgandhi 18:6a4db94011d3 2229 /* Byte 6 */
sahilmgandhi 18:6a4db94011d3 2230 tmp = (sd_status[6U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2231 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 8U);
sahilmgandhi 18:6a4db94011d3 2232
sahilmgandhi 18:6a4db94011d3 2233 /* Byte 7 */
sahilmgandhi 18:6a4db94011d3 2234 tmp = (sd_status[7U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2235 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2236
sahilmgandhi 18:6a4db94011d3 2237 /* Byte 8 */
sahilmgandhi 18:6a4db94011d3 2238 tmp = (sd_status[8U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2239 pCardStatus->SPEED_CLASS = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2240
sahilmgandhi 18:6a4db94011d3 2241 /* Byte 9 */
sahilmgandhi 18:6a4db94011d3 2242 tmp = (sd_status[9U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2243 pCardStatus->PERFORMANCE_MOVE = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2244
sahilmgandhi 18:6a4db94011d3 2245 /* Byte 10 */
sahilmgandhi 18:6a4db94011d3 2246 tmp = (sd_status[10U] & 0xF0U) >> 4U;
sahilmgandhi 18:6a4db94011d3 2247 pCardStatus->AU_SIZE = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2248
sahilmgandhi 18:6a4db94011d3 2249 /* Byte 11 */
sahilmgandhi 18:6a4db94011d3 2250 tmp = (sd_status[11U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2251 pCardStatus->ERASE_SIZE = (uint8_t)(tmp << 8U);
sahilmgandhi 18:6a4db94011d3 2252
sahilmgandhi 18:6a4db94011d3 2253 /* Byte 12 */
sahilmgandhi 18:6a4db94011d3 2254 tmp = (sd_status[12U] & 0xFFU);
sahilmgandhi 18:6a4db94011d3 2255 pCardStatus->ERASE_SIZE |= (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2256
sahilmgandhi 18:6a4db94011d3 2257 /* Byte 13 */
sahilmgandhi 18:6a4db94011d3 2258 tmp = (sd_status[13U] & 0xFCU) >> 2U;
sahilmgandhi 18:6a4db94011d3 2259 pCardStatus->ERASE_TIMEOUT = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2260
sahilmgandhi 18:6a4db94011d3 2261 /* Byte 13 */
sahilmgandhi 18:6a4db94011d3 2262 tmp = (sd_status[13U] & 0x3U);
sahilmgandhi 18:6a4db94011d3 2263 pCardStatus->ERASE_OFFSET = (uint8_t)tmp;
sahilmgandhi 18:6a4db94011d3 2264
sahilmgandhi 18:6a4db94011d3 2265 return errorstate;
sahilmgandhi 18:6a4db94011d3 2266 }
sahilmgandhi 18:6a4db94011d3 2267
sahilmgandhi 18:6a4db94011d3 2268 /**
sahilmgandhi 18:6a4db94011d3 2269 * @}
sahilmgandhi 18:6a4db94011d3 2270 */
sahilmgandhi 18:6a4db94011d3 2271
sahilmgandhi 18:6a4db94011d3 2272 /**
sahilmgandhi 18:6a4db94011d3 2273 * @}
sahilmgandhi 18:6a4db94011d3 2274 */
sahilmgandhi 18:6a4db94011d3 2275
sahilmgandhi 18:6a4db94011d3 2276 /* Private function ----------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 2277 /** @addtogroup SD_Private_Functions
sahilmgandhi 18:6a4db94011d3 2278 * @{
sahilmgandhi 18:6a4db94011d3 2279 */
sahilmgandhi 18:6a4db94011d3 2280
sahilmgandhi 18:6a4db94011d3 2281 /**
sahilmgandhi 18:6a4db94011d3 2282 * @brief SD DMA transfer complete Rx callback.
sahilmgandhi 18:6a4db94011d3 2283 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
sahilmgandhi 18:6a4db94011d3 2284 * the configuration information for the specified DMA module.
sahilmgandhi 18:6a4db94011d3 2285 * @retval None
sahilmgandhi 18:6a4db94011d3 2286 */
sahilmgandhi 18:6a4db94011d3 2287 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma)
sahilmgandhi 18:6a4db94011d3 2288 {
sahilmgandhi 18:6a4db94011d3 2289 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
sahilmgandhi 18:6a4db94011d3 2290
sahilmgandhi 18:6a4db94011d3 2291 /* DMA transfer is complete */
sahilmgandhi 18:6a4db94011d3 2292 hsd->DmaTransferCplt = 1U;
sahilmgandhi 18:6a4db94011d3 2293
sahilmgandhi 18:6a4db94011d3 2294 /* Wait until SD transfer is complete */
sahilmgandhi 18:6a4db94011d3 2295 while(hsd->SdTransferCplt == 0U)
sahilmgandhi 18:6a4db94011d3 2296 {
sahilmgandhi 18:6a4db94011d3 2297 }
sahilmgandhi 18:6a4db94011d3 2298
sahilmgandhi 18:6a4db94011d3 2299 /* Disable the DMA channel */
sahilmgandhi 18:6a4db94011d3 2300 HAL_DMA_Abort(hdma);
sahilmgandhi 18:6a4db94011d3 2301
sahilmgandhi 18:6a4db94011d3 2302 /* Transfer complete user callback */
sahilmgandhi 18:6a4db94011d3 2303 HAL_SD_DMA_RxCpltCallback(hsd->hdmarx);
sahilmgandhi 18:6a4db94011d3 2304 }
sahilmgandhi 18:6a4db94011d3 2305
sahilmgandhi 18:6a4db94011d3 2306 /**
sahilmgandhi 18:6a4db94011d3 2307 * @brief SD DMA transfer Error Rx callback.
sahilmgandhi 18:6a4db94011d3 2308 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
sahilmgandhi 18:6a4db94011d3 2309 * the configuration information for the specified DMA module.
sahilmgandhi 18:6a4db94011d3 2310 * @retval None
sahilmgandhi 18:6a4db94011d3 2311 */
sahilmgandhi 18:6a4db94011d3 2312 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma)
sahilmgandhi 18:6a4db94011d3 2313 {
sahilmgandhi 18:6a4db94011d3 2314 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
sahilmgandhi 18:6a4db94011d3 2315
sahilmgandhi 18:6a4db94011d3 2316 /* Transfer complete user callback */
sahilmgandhi 18:6a4db94011d3 2317 HAL_SD_DMA_RxErrorCallback(hsd->hdmarx);
sahilmgandhi 18:6a4db94011d3 2318 }
sahilmgandhi 18:6a4db94011d3 2319
sahilmgandhi 18:6a4db94011d3 2320 /**
sahilmgandhi 18:6a4db94011d3 2321 * @brief SD DMA transfer complete Tx callback.
sahilmgandhi 18:6a4db94011d3 2322 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
sahilmgandhi 18:6a4db94011d3 2323 * the configuration information for the specified DMA module.
sahilmgandhi 18:6a4db94011d3 2324 * @retval None
sahilmgandhi 18:6a4db94011d3 2325 */
sahilmgandhi 18:6a4db94011d3 2326 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma)
sahilmgandhi 18:6a4db94011d3 2327 {
sahilmgandhi 18:6a4db94011d3 2328 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
sahilmgandhi 18:6a4db94011d3 2329
sahilmgandhi 18:6a4db94011d3 2330 /* DMA transfer is complete */
sahilmgandhi 18:6a4db94011d3 2331 hsd->DmaTransferCplt = 1U;
sahilmgandhi 18:6a4db94011d3 2332
sahilmgandhi 18:6a4db94011d3 2333 /* Wait until SD transfer is complete */
sahilmgandhi 18:6a4db94011d3 2334 while(hsd->SdTransferCplt == 0U)
sahilmgandhi 18:6a4db94011d3 2335 {
sahilmgandhi 18:6a4db94011d3 2336 }
sahilmgandhi 18:6a4db94011d3 2337
sahilmgandhi 18:6a4db94011d3 2338 /* Disable the DMA channel */
sahilmgandhi 18:6a4db94011d3 2339 HAL_DMA_Abort(hdma);
sahilmgandhi 18:6a4db94011d3 2340
sahilmgandhi 18:6a4db94011d3 2341 /* Transfer complete user callback */
sahilmgandhi 18:6a4db94011d3 2342 HAL_SD_DMA_TxCpltCallback(hsd->hdmatx);
sahilmgandhi 18:6a4db94011d3 2343 }
sahilmgandhi 18:6a4db94011d3 2344
sahilmgandhi 18:6a4db94011d3 2345 /**
sahilmgandhi 18:6a4db94011d3 2346 * @brief SD DMA transfer Error Tx callback.
sahilmgandhi 18:6a4db94011d3 2347 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
sahilmgandhi 18:6a4db94011d3 2348 * the configuration information for the specified DMA module.
sahilmgandhi 18:6a4db94011d3 2349 * @retval None
sahilmgandhi 18:6a4db94011d3 2350 */
sahilmgandhi 18:6a4db94011d3 2351 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma)
sahilmgandhi 18:6a4db94011d3 2352 {
sahilmgandhi 18:6a4db94011d3 2353 SD_HandleTypeDef *hsd = ( SD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
sahilmgandhi 18:6a4db94011d3 2354
sahilmgandhi 18:6a4db94011d3 2355 /* Transfer complete user callback */
sahilmgandhi 18:6a4db94011d3 2356 HAL_SD_DMA_TxErrorCallback(hsd->hdmatx);
sahilmgandhi 18:6a4db94011d3 2357 }
sahilmgandhi 18:6a4db94011d3 2358
sahilmgandhi 18:6a4db94011d3 2359 /**
sahilmgandhi 18:6a4db94011d3 2360 * @brief Returns the SD current state.
sahilmgandhi 18:6a4db94011d3 2361 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2362 * @retval SD card current state
sahilmgandhi 18:6a4db94011d3 2363 */
sahilmgandhi 18:6a4db94011d3 2364 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 2365 {
sahilmgandhi 18:6a4db94011d3 2366 uint32_t resp1 = 0U;
sahilmgandhi 18:6a4db94011d3 2367
sahilmgandhi 18:6a4db94011d3 2368 if (SD_SendStatus(hsd, &resp1) != SD_OK)
sahilmgandhi 18:6a4db94011d3 2369 {
sahilmgandhi 18:6a4db94011d3 2370 return SD_CARD_ERROR;
sahilmgandhi 18:6a4db94011d3 2371 }
sahilmgandhi 18:6a4db94011d3 2372 else
sahilmgandhi 18:6a4db94011d3 2373 {
sahilmgandhi 18:6a4db94011d3 2374 return (HAL_SD_CardStateTypedef)((resp1 >> 9U) & 0x0FU);
sahilmgandhi 18:6a4db94011d3 2375 }
sahilmgandhi 18:6a4db94011d3 2376 }
sahilmgandhi 18:6a4db94011d3 2377
sahilmgandhi 18:6a4db94011d3 2378 /**
sahilmgandhi 18:6a4db94011d3 2379 * @brief Initializes all cards or single card as the case may be Card(s) come
sahilmgandhi 18:6a4db94011d3 2380 * into standby state.
sahilmgandhi 18:6a4db94011d3 2381 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2382 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2383 */
sahilmgandhi 18:6a4db94011d3 2384 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 2385 {
sahilmgandhi 18:6a4db94011d3 2386 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 2387 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2388 uint16_t sd_rca = 1U;
sahilmgandhi 18:6a4db94011d3 2389
sahilmgandhi 18:6a4db94011d3 2390 if(SDIO_GetPowerState(hsd->Instance) == 0U) /* Power off */
sahilmgandhi 18:6a4db94011d3 2391 {
sahilmgandhi 18:6a4db94011d3 2392 errorstate = SD_REQUEST_NOT_APPLICABLE;
sahilmgandhi 18:6a4db94011d3 2393
sahilmgandhi 18:6a4db94011d3 2394 return errorstate;
sahilmgandhi 18:6a4db94011d3 2395 }
sahilmgandhi 18:6a4db94011d3 2396
sahilmgandhi 18:6a4db94011d3 2397 if(hsd->CardType != SECURE_DIGITAL_IO_CARD)
sahilmgandhi 18:6a4db94011d3 2398 {
sahilmgandhi 18:6a4db94011d3 2399 /* Send CMD2 ALL_SEND_CID */
sahilmgandhi 18:6a4db94011d3 2400 sdio_cmdinitstructure.Argument = 0U;
sahilmgandhi 18:6a4db94011d3 2401 sdio_cmdinitstructure.CmdIndex = SD_CMD_ALL_SEND_CID;
sahilmgandhi 18:6a4db94011d3 2402 sdio_cmdinitstructure.Response = SDIO_RESPONSE_LONG;
sahilmgandhi 18:6a4db94011d3 2403 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 2404 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 2405 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2406
sahilmgandhi 18:6a4db94011d3 2407 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2408 errorstate = SD_CmdResp2Error(hsd);
sahilmgandhi 18:6a4db94011d3 2409
sahilmgandhi 18:6a4db94011d3 2410 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2411 {
sahilmgandhi 18:6a4db94011d3 2412 return errorstate;
sahilmgandhi 18:6a4db94011d3 2413 }
sahilmgandhi 18:6a4db94011d3 2414
sahilmgandhi 18:6a4db94011d3 2415 /* Get Card identification number data */
sahilmgandhi 18:6a4db94011d3 2416 hsd->CID[0U] = SDIO_GetResponse(SDIO_RESP1);
sahilmgandhi 18:6a4db94011d3 2417 hsd->CID[1U] = SDIO_GetResponse(SDIO_RESP2);
sahilmgandhi 18:6a4db94011d3 2418 hsd->CID[2U] = SDIO_GetResponse(SDIO_RESP3);
sahilmgandhi 18:6a4db94011d3 2419 hsd->CID[3U] = SDIO_GetResponse(SDIO_RESP4);
sahilmgandhi 18:6a4db94011d3 2420 }
sahilmgandhi 18:6a4db94011d3 2421
sahilmgandhi 18:6a4db94011d3 2422 if((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
sahilmgandhi 18:6a4db94011d3 2423 (hsd->CardType == SECURE_DIGITAL_IO_COMBO_CARD) || (hsd->CardType == HIGH_CAPACITY_SD_CARD))
sahilmgandhi 18:6a4db94011d3 2424 {
sahilmgandhi 18:6a4db94011d3 2425 /* Send CMD3 SET_REL_ADDR with argument 0 */
sahilmgandhi 18:6a4db94011d3 2426 /* SD Card publishes its RCA. */
sahilmgandhi 18:6a4db94011d3 2427 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_REL_ADDR;
sahilmgandhi 18:6a4db94011d3 2428 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 2429 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2430
sahilmgandhi 18:6a4db94011d3 2431 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2432 errorstate = SD_CmdResp6Error(hsd, SD_CMD_SET_REL_ADDR, &sd_rca);
sahilmgandhi 18:6a4db94011d3 2433
sahilmgandhi 18:6a4db94011d3 2434 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2435 {
sahilmgandhi 18:6a4db94011d3 2436 return errorstate;
sahilmgandhi 18:6a4db94011d3 2437 }
sahilmgandhi 18:6a4db94011d3 2438 }
sahilmgandhi 18:6a4db94011d3 2439
sahilmgandhi 18:6a4db94011d3 2440 if (hsd->CardType != SECURE_DIGITAL_IO_CARD)
sahilmgandhi 18:6a4db94011d3 2441 {
sahilmgandhi 18:6a4db94011d3 2442 /* Get the SD card RCA */
sahilmgandhi 18:6a4db94011d3 2443 hsd->RCA = sd_rca;
sahilmgandhi 18:6a4db94011d3 2444
sahilmgandhi 18:6a4db94011d3 2445 /* Send CMD9 SEND_CSD with argument as card's RCA */
sahilmgandhi 18:6a4db94011d3 2446 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16U);
sahilmgandhi 18:6a4db94011d3 2447 sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_CSD;
sahilmgandhi 18:6a4db94011d3 2448 sdio_cmdinitstructure.Response = SDIO_RESPONSE_LONG;
sahilmgandhi 18:6a4db94011d3 2449 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2450
sahilmgandhi 18:6a4db94011d3 2451 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2452 errorstate = SD_CmdResp2Error(hsd);
sahilmgandhi 18:6a4db94011d3 2453
sahilmgandhi 18:6a4db94011d3 2454 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2455 {
sahilmgandhi 18:6a4db94011d3 2456 return errorstate;
sahilmgandhi 18:6a4db94011d3 2457 }
sahilmgandhi 18:6a4db94011d3 2458
sahilmgandhi 18:6a4db94011d3 2459 /* Get Card Specific Data */
sahilmgandhi 18:6a4db94011d3 2460 hsd->CSD[0U] = SDIO_GetResponse(SDIO_RESP1);
sahilmgandhi 18:6a4db94011d3 2461 hsd->CSD[1U] = SDIO_GetResponse(SDIO_RESP2);
sahilmgandhi 18:6a4db94011d3 2462 hsd->CSD[2U] = SDIO_GetResponse(SDIO_RESP3);
sahilmgandhi 18:6a4db94011d3 2463 hsd->CSD[3U] = SDIO_GetResponse(SDIO_RESP4);
sahilmgandhi 18:6a4db94011d3 2464 }
sahilmgandhi 18:6a4db94011d3 2465
sahilmgandhi 18:6a4db94011d3 2466 /* All cards are initialized */
sahilmgandhi 18:6a4db94011d3 2467 return errorstate;
sahilmgandhi 18:6a4db94011d3 2468 }
sahilmgandhi 18:6a4db94011d3 2469
sahilmgandhi 18:6a4db94011d3 2470 /**
sahilmgandhi 18:6a4db94011d3 2471 * @brief Selects of Deselects the corresponding card.
sahilmgandhi 18:6a4db94011d3 2472 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2473 * @param addr: Address of the card to be selected
sahilmgandhi 18:6a4db94011d3 2474 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2475 */
sahilmgandhi 18:6a4db94011d3 2476 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr)
sahilmgandhi 18:6a4db94011d3 2477 {
sahilmgandhi 18:6a4db94011d3 2478 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 2479 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2480
sahilmgandhi 18:6a4db94011d3 2481 /* Send CMD7 SDIO_SEL_DESEL_CARD */
sahilmgandhi 18:6a4db94011d3 2482 sdio_cmdinitstructure.Argument = (uint32_t)addr;
sahilmgandhi 18:6a4db94011d3 2483 sdio_cmdinitstructure.CmdIndex = SD_CMD_SEL_DESEL_CARD;
sahilmgandhi 18:6a4db94011d3 2484 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 2485 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 2486 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 2487 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2488
sahilmgandhi 18:6a4db94011d3 2489 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2490 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEL_DESEL_CARD);
sahilmgandhi 18:6a4db94011d3 2491
sahilmgandhi 18:6a4db94011d3 2492 return errorstate;
sahilmgandhi 18:6a4db94011d3 2493 }
sahilmgandhi 18:6a4db94011d3 2494
sahilmgandhi 18:6a4db94011d3 2495 /**
sahilmgandhi 18:6a4db94011d3 2496 * @brief Enquires cards about their operating voltage and configures clock
sahilmgandhi 18:6a4db94011d3 2497 * controls and stores SD information that will be needed in future
sahilmgandhi 18:6a4db94011d3 2498 * in the SD handle.
sahilmgandhi 18:6a4db94011d3 2499 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2500 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2501 */
sahilmgandhi 18:6a4db94011d3 2502 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 2503 {
sahilmgandhi 18:6a4db94011d3 2504 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 2505 __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2506 uint32_t response = 0U, count = 0U, validvoltage = 0U;
sahilmgandhi 18:6a4db94011d3 2507 uint32_t sdtype = SD_STD_CAPACITY;
sahilmgandhi 18:6a4db94011d3 2508
sahilmgandhi 18:6a4db94011d3 2509 /* Power ON Sequence -------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 2510 /* Disable SDIO Clock */
sahilmgandhi 18:6a4db94011d3 2511 __HAL_SD_SDIO_DISABLE();
sahilmgandhi 18:6a4db94011d3 2512
sahilmgandhi 18:6a4db94011d3 2513 /* Set Power State to ON */
sahilmgandhi 18:6a4db94011d3 2514 SDIO_PowerState_ON(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 2515
sahilmgandhi 18:6a4db94011d3 2516 /* 1ms: required power up waiting time before starting the SD initialization
sahilmgandhi 18:6a4db94011d3 2517 sequence */
sahilmgandhi 18:6a4db94011d3 2518 HAL_Delay(1);
sahilmgandhi 18:6a4db94011d3 2519
sahilmgandhi 18:6a4db94011d3 2520 /* Enable SDIO Clock */
sahilmgandhi 18:6a4db94011d3 2521 __HAL_SD_SDIO_ENABLE();
sahilmgandhi 18:6a4db94011d3 2522
sahilmgandhi 18:6a4db94011d3 2523 /* CMD0: GO_IDLE_STATE -----------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 2524 /* No CMD response required */
sahilmgandhi 18:6a4db94011d3 2525 sdio_cmdinitstructure.Argument = 0U;
sahilmgandhi 18:6a4db94011d3 2526 sdio_cmdinitstructure.CmdIndex = SD_CMD_GO_IDLE_STATE;
sahilmgandhi 18:6a4db94011d3 2527 sdio_cmdinitstructure.Response = SDIO_RESPONSE_NO;
sahilmgandhi 18:6a4db94011d3 2528 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 2529 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 2530 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2531
sahilmgandhi 18:6a4db94011d3 2532 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2533 errorstate = SD_CmdError(hsd);
sahilmgandhi 18:6a4db94011d3 2534
sahilmgandhi 18:6a4db94011d3 2535 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2536 {
sahilmgandhi 18:6a4db94011d3 2537 /* CMD Response Timeout (wait for CMDSENT flag) */
sahilmgandhi 18:6a4db94011d3 2538 return errorstate;
sahilmgandhi 18:6a4db94011d3 2539 }
sahilmgandhi 18:6a4db94011d3 2540
sahilmgandhi 18:6a4db94011d3 2541 /* CMD8: SEND_IF_COND ------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 2542 /* Send CMD8 to verify SD card interface operating condition */
sahilmgandhi 18:6a4db94011d3 2543 /* Argument: - [31:12]: Reserved (shall be set to '0')
sahilmgandhi 18:6a4db94011d3 2544 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
sahilmgandhi 18:6a4db94011d3 2545 - [7:0]: Check Pattern (recommended 0xAA) */
sahilmgandhi 18:6a4db94011d3 2546 /* CMD Response: R7 */
sahilmgandhi 18:6a4db94011d3 2547 sdio_cmdinitstructure.Argument = SD_CHECK_PATTERN;
sahilmgandhi 18:6a4db94011d3 2548 sdio_cmdinitstructure.CmdIndex = SD_SDIO_SEND_IF_COND;
sahilmgandhi 18:6a4db94011d3 2549 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 2550 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2551
sahilmgandhi 18:6a4db94011d3 2552 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2553 errorstate = SD_CmdResp7Error(hsd);
sahilmgandhi 18:6a4db94011d3 2554
sahilmgandhi 18:6a4db94011d3 2555 if (errorstate == SD_OK)
sahilmgandhi 18:6a4db94011d3 2556 {
sahilmgandhi 18:6a4db94011d3 2557 /* SD Card 2.0 */
sahilmgandhi 18:6a4db94011d3 2558 hsd->CardType = STD_CAPACITY_SD_CARD_V2_0;
sahilmgandhi 18:6a4db94011d3 2559 sdtype = SD_HIGH_CAPACITY;
sahilmgandhi 18:6a4db94011d3 2560 }
sahilmgandhi 18:6a4db94011d3 2561
sahilmgandhi 18:6a4db94011d3 2562 /* Send CMD55 */
sahilmgandhi 18:6a4db94011d3 2563 sdio_cmdinitstructure.Argument = 0U;
sahilmgandhi 18:6a4db94011d3 2564 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
sahilmgandhi 18:6a4db94011d3 2565 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2566
sahilmgandhi 18:6a4db94011d3 2567 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2568 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
sahilmgandhi 18:6a4db94011d3 2569
sahilmgandhi 18:6a4db94011d3 2570 /* If errorstate is Command Timeout, it is a MMC card */
sahilmgandhi 18:6a4db94011d3 2571 /* If errorstate is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
sahilmgandhi 18:6a4db94011d3 2572 or SD card 1.x */
sahilmgandhi 18:6a4db94011d3 2573 if(errorstate == SD_OK)
sahilmgandhi 18:6a4db94011d3 2574 {
sahilmgandhi 18:6a4db94011d3 2575 /* SD CARD */
sahilmgandhi 18:6a4db94011d3 2576 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
sahilmgandhi 18:6a4db94011d3 2577 while((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
sahilmgandhi 18:6a4db94011d3 2578 {
sahilmgandhi 18:6a4db94011d3 2579
sahilmgandhi 18:6a4db94011d3 2580 /* SEND CMD55 APP_CMD with RCA as 0 */
sahilmgandhi 18:6a4db94011d3 2581 sdio_cmdinitstructure.Argument = 0U;
sahilmgandhi 18:6a4db94011d3 2582 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
sahilmgandhi 18:6a4db94011d3 2583 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 2584 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 2585 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 2586 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2587
sahilmgandhi 18:6a4db94011d3 2588 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2589 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
sahilmgandhi 18:6a4db94011d3 2590
sahilmgandhi 18:6a4db94011d3 2591 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2592 {
sahilmgandhi 18:6a4db94011d3 2593 return errorstate;
sahilmgandhi 18:6a4db94011d3 2594 }
sahilmgandhi 18:6a4db94011d3 2595
sahilmgandhi 18:6a4db94011d3 2596 /* Send CMD41 */
sahilmgandhi 18:6a4db94011d3 2597 sdio_cmdinitstructure.Argument = SD_VOLTAGE_WINDOW_SD | sdtype;
sahilmgandhi 18:6a4db94011d3 2598 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_OP_COND;
sahilmgandhi 18:6a4db94011d3 2599 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 2600 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 2601 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 2602 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2603
sahilmgandhi 18:6a4db94011d3 2604 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2605 errorstate = SD_CmdResp3Error(hsd);
sahilmgandhi 18:6a4db94011d3 2606
sahilmgandhi 18:6a4db94011d3 2607 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2608 {
sahilmgandhi 18:6a4db94011d3 2609 return errorstate;
sahilmgandhi 18:6a4db94011d3 2610 }
sahilmgandhi 18:6a4db94011d3 2611
sahilmgandhi 18:6a4db94011d3 2612 /* Get command response */
sahilmgandhi 18:6a4db94011d3 2613 response = SDIO_GetResponse(SDIO_RESP1);
sahilmgandhi 18:6a4db94011d3 2614
sahilmgandhi 18:6a4db94011d3 2615 /* Get operating voltage*/
sahilmgandhi 18:6a4db94011d3 2616 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
sahilmgandhi 18:6a4db94011d3 2617
sahilmgandhi 18:6a4db94011d3 2618 count++;
sahilmgandhi 18:6a4db94011d3 2619 }
sahilmgandhi 18:6a4db94011d3 2620
sahilmgandhi 18:6a4db94011d3 2621 if(count >= SD_MAX_VOLT_TRIAL)
sahilmgandhi 18:6a4db94011d3 2622 {
sahilmgandhi 18:6a4db94011d3 2623 errorstate = SD_INVALID_VOLTRANGE;
sahilmgandhi 18:6a4db94011d3 2624
sahilmgandhi 18:6a4db94011d3 2625 return errorstate;
sahilmgandhi 18:6a4db94011d3 2626 }
sahilmgandhi 18:6a4db94011d3 2627
sahilmgandhi 18:6a4db94011d3 2628 if((response & SD_HIGH_CAPACITY) == SD_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
sahilmgandhi 18:6a4db94011d3 2629 {
sahilmgandhi 18:6a4db94011d3 2630 hsd->CardType = HIGH_CAPACITY_SD_CARD;
sahilmgandhi 18:6a4db94011d3 2631 }
sahilmgandhi 18:6a4db94011d3 2632
sahilmgandhi 18:6a4db94011d3 2633 } /* else MMC Card */
sahilmgandhi 18:6a4db94011d3 2634
sahilmgandhi 18:6a4db94011d3 2635 return errorstate;
sahilmgandhi 18:6a4db94011d3 2636 }
sahilmgandhi 18:6a4db94011d3 2637
sahilmgandhi 18:6a4db94011d3 2638 /**
sahilmgandhi 18:6a4db94011d3 2639 * @brief Turns the SDIO output signals off.
sahilmgandhi 18:6a4db94011d3 2640 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2641 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2642 */
sahilmgandhi 18:6a4db94011d3 2643 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 2644 {
sahilmgandhi 18:6a4db94011d3 2645 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2646
sahilmgandhi 18:6a4db94011d3 2647 /* Set Power State to OFF */
sahilmgandhi 18:6a4db94011d3 2648 SDIO_PowerState_OFF(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 2649
sahilmgandhi 18:6a4db94011d3 2650 return errorstate;
sahilmgandhi 18:6a4db94011d3 2651 }
sahilmgandhi 18:6a4db94011d3 2652
sahilmgandhi 18:6a4db94011d3 2653 /**
sahilmgandhi 18:6a4db94011d3 2654 * @brief Returns the current card's status.
sahilmgandhi 18:6a4db94011d3 2655 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2656 * @param pCardStatus: pointer to the buffer that will contain the SD card
sahilmgandhi 18:6a4db94011d3 2657 * status (Card Status register)
sahilmgandhi 18:6a4db94011d3 2658 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2659 */
sahilmgandhi 18:6a4db94011d3 2660 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
sahilmgandhi 18:6a4db94011d3 2661 {
sahilmgandhi 18:6a4db94011d3 2662 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 2663 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2664
sahilmgandhi 18:6a4db94011d3 2665 if(pCardStatus == NULL)
sahilmgandhi 18:6a4db94011d3 2666 {
sahilmgandhi 18:6a4db94011d3 2667 errorstate = SD_INVALID_PARAMETER;
sahilmgandhi 18:6a4db94011d3 2668
sahilmgandhi 18:6a4db94011d3 2669 return errorstate;
sahilmgandhi 18:6a4db94011d3 2670 }
sahilmgandhi 18:6a4db94011d3 2671
sahilmgandhi 18:6a4db94011d3 2672 /* Send Status command */
sahilmgandhi 18:6a4db94011d3 2673 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16U);
sahilmgandhi 18:6a4db94011d3 2674 sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
sahilmgandhi 18:6a4db94011d3 2675 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 2676 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 2677 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 2678 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 2679
sahilmgandhi 18:6a4db94011d3 2680 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 2681 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEND_STATUS);
sahilmgandhi 18:6a4db94011d3 2682
sahilmgandhi 18:6a4db94011d3 2683 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 2684 {
sahilmgandhi 18:6a4db94011d3 2685 return errorstate;
sahilmgandhi 18:6a4db94011d3 2686 }
sahilmgandhi 18:6a4db94011d3 2687
sahilmgandhi 18:6a4db94011d3 2688 /* Get SD card status */
sahilmgandhi 18:6a4db94011d3 2689 *pCardStatus = SDIO_GetResponse(SDIO_RESP1);
sahilmgandhi 18:6a4db94011d3 2690
sahilmgandhi 18:6a4db94011d3 2691 return errorstate;
sahilmgandhi 18:6a4db94011d3 2692 }
sahilmgandhi 18:6a4db94011d3 2693
sahilmgandhi 18:6a4db94011d3 2694 /**
sahilmgandhi 18:6a4db94011d3 2695 * @brief Checks for error conditions for CMD0.
sahilmgandhi 18:6a4db94011d3 2696 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2697 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2698 */
sahilmgandhi 18:6a4db94011d3 2699 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 2700 {
sahilmgandhi 18:6a4db94011d3 2701 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2702 uint32_t timeout, tmp;
sahilmgandhi 18:6a4db94011d3 2703
sahilmgandhi 18:6a4db94011d3 2704 timeout = SDIO_CMD0TIMEOUT;
sahilmgandhi 18:6a4db94011d3 2705
sahilmgandhi 18:6a4db94011d3 2706 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT);
sahilmgandhi 18:6a4db94011d3 2707
sahilmgandhi 18:6a4db94011d3 2708 while((timeout > 0U) && (!tmp))
sahilmgandhi 18:6a4db94011d3 2709 {
sahilmgandhi 18:6a4db94011d3 2710 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT);
sahilmgandhi 18:6a4db94011d3 2711 timeout--;
sahilmgandhi 18:6a4db94011d3 2712 }
sahilmgandhi 18:6a4db94011d3 2713
sahilmgandhi 18:6a4db94011d3 2714 if(timeout == 0U)
sahilmgandhi 18:6a4db94011d3 2715 {
sahilmgandhi 18:6a4db94011d3 2716 errorstate = SD_CMD_RSP_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 2717 return errorstate;
sahilmgandhi 18:6a4db94011d3 2718 }
sahilmgandhi 18:6a4db94011d3 2719
sahilmgandhi 18:6a4db94011d3 2720 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 2721 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 2722
sahilmgandhi 18:6a4db94011d3 2723 return errorstate;
sahilmgandhi 18:6a4db94011d3 2724 }
sahilmgandhi 18:6a4db94011d3 2725
sahilmgandhi 18:6a4db94011d3 2726 /**
sahilmgandhi 18:6a4db94011d3 2727 * @brief Checks for error conditions for R7 response.
sahilmgandhi 18:6a4db94011d3 2728 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2729 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2730 */
sahilmgandhi 18:6a4db94011d3 2731 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 2732 {
sahilmgandhi 18:6a4db94011d3 2733 HAL_SD_ErrorTypedef errorstate = SD_ERROR;
sahilmgandhi 18:6a4db94011d3 2734 uint32_t timeout = SDIO_CMD0TIMEOUT, tmp;
sahilmgandhi 18:6a4db94011d3 2735
sahilmgandhi 18:6a4db94011d3 2736 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT);
sahilmgandhi 18:6a4db94011d3 2737
sahilmgandhi 18:6a4db94011d3 2738 while((!tmp) && (timeout > 0U))
sahilmgandhi 18:6a4db94011d3 2739 {
sahilmgandhi 18:6a4db94011d3 2740 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT);
sahilmgandhi 18:6a4db94011d3 2741 timeout--;
sahilmgandhi 18:6a4db94011d3 2742 }
sahilmgandhi 18:6a4db94011d3 2743
sahilmgandhi 18:6a4db94011d3 2744 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
sahilmgandhi 18:6a4db94011d3 2745
sahilmgandhi 18:6a4db94011d3 2746 if((timeout == 0U) || tmp)
sahilmgandhi 18:6a4db94011d3 2747 {
sahilmgandhi 18:6a4db94011d3 2748 /* Card is not V2.0 compliant or card does not support the set voltage range */
sahilmgandhi 18:6a4db94011d3 2749 errorstate = SD_CMD_RSP_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 2750
sahilmgandhi 18:6a4db94011d3 2751 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
sahilmgandhi 18:6a4db94011d3 2752
sahilmgandhi 18:6a4db94011d3 2753 return errorstate;
sahilmgandhi 18:6a4db94011d3 2754 }
sahilmgandhi 18:6a4db94011d3 2755
sahilmgandhi 18:6a4db94011d3 2756 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDREND))
sahilmgandhi 18:6a4db94011d3 2757 {
sahilmgandhi 18:6a4db94011d3 2758 /* Card is SD V2.0 compliant */
sahilmgandhi 18:6a4db94011d3 2759 errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2760
sahilmgandhi 18:6a4db94011d3 2761 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CMDREND);
sahilmgandhi 18:6a4db94011d3 2762
sahilmgandhi 18:6a4db94011d3 2763 return errorstate;
sahilmgandhi 18:6a4db94011d3 2764 }
sahilmgandhi 18:6a4db94011d3 2765
sahilmgandhi 18:6a4db94011d3 2766 return errorstate;
sahilmgandhi 18:6a4db94011d3 2767 }
sahilmgandhi 18:6a4db94011d3 2768
sahilmgandhi 18:6a4db94011d3 2769 /**
sahilmgandhi 18:6a4db94011d3 2770 * @brief Checks for error conditions for R1 response.
sahilmgandhi 18:6a4db94011d3 2771 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2772 * @param SD_CMD: The sent command index
sahilmgandhi 18:6a4db94011d3 2773 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2774 */
sahilmgandhi 18:6a4db94011d3 2775 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD)
sahilmgandhi 18:6a4db94011d3 2776 {
sahilmgandhi 18:6a4db94011d3 2777 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2778 uint32_t response_r1;
sahilmgandhi 18:6a4db94011d3 2779
sahilmgandhi 18:6a4db94011d3 2780 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 2781 {
sahilmgandhi 18:6a4db94011d3 2782 }
sahilmgandhi 18:6a4db94011d3 2783
sahilmgandhi 18:6a4db94011d3 2784 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 2785 {
sahilmgandhi 18:6a4db94011d3 2786 errorstate = SD_CMD_RSP_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 2787
sahilmgandhi 18:6a4db94011d3 2788 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
sahilmgandhi 18:6a4db94011d3 2789
sahilmgandhi 18:6a4db94011d3 2790 return errorstate;
sahilmgandhi 18:6a4db94011d3 2791 }
sahilmgandhi 18:6a4db94011d3 2792 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
sahilmgandhi 18:6a4db94011d3 2793 {
sahilmgandhi 18:6a4db94011d3 2794 errorstate = SD_CMD_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 2795
sahilmgandhi 18:6a4db94011d3 2796 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
sahilmgandhi 18:6a4db94011d3 2797
sahilmgandhi 18:6a4db94011d3 2798 return errorstate;
sahilmgandhi 18:6a4db94011d3 2799 }
sahilmgandhi 18:6a4db94011d3 2800
sahilmgandhi 18:6a4db94011d3 2801 /* Check response received is of desired command */
sahilmgandhi 18:6a4db94011d3 2802 if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD)
sahilmgandhi 18:6a4db94011d3 2803 {
sahilmgandhi 18:6a4db94011d3 2804 errorstate = SD_ILLEGAL_CMD;
sahilmgandhi 18:6a4db94011d3 2805
sahilmgandhi 18:6a4db94011d3 2806 return errorstate;
sahilmgandhi 18:6a4db94011d3 2807 }
sahilmgandhi 18:6a4db94011d3 2808
sahilmgandhi 18:6a4db94011d3 2809 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 2810 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 2811
sahilmgandhi 18:6a4db94011d3 2812 /* We have received response, retrieve it for analysis */
sahilmgandhi 18:6a4db94011d3 2813 response_r1 = SDIO_GetResponse(SDIO_RESP1);
sahilmgandhi 18:6a4db94011d3 2814
sahilmgandhi 18:6a4db94011d3 2815 if((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
sahilmgandhi 18:6a4db94011d3 2816 {
sahilmgandhi 18:6a4db94011d3 2817 return errorstate;
sahilmgandhi 18:6a4db94011d3 2818 }
sahilmgandhi 18:6a4db94011d3 2819
sahilmgandhi 18:6a4db94011d3 2820 if((response_r1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
sahilmgandhi 18:6a4db94011d3 2821 {
sahilmgandhi 18:6a4db94011d3 2822 return(SD_ADDR_OUT_OF_RANGE);
sahilmgandhi 18:6a4db94011d3 2823 }
sahilmgandhi 18:6a4db94011d3 2824
sahilmgandhi 18:6a4db94011d3 2825 if((response_r1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
sahilmgandhi 18:6a4db94011d3 2826 {
sahilmgandhi 18:6a4db94011d3 2827 return(SD_ADDR_MISALIGNED);
sahilmgandhi 18:6a4db94011d3 2828 }
sahilmgandhi 18:6a4db94011d3 2829
sahilmgandhi 18:6a4db94011d3 2830 if((response_r1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
sahilmgandhi 18:6a4db94011d3 2831 {
sahilmgandhi 18:6a4db94011d3 2832 return(SD_BLOCK_LEN_ERR);
sahilmgandhi 18:6a4db94011d3 2833 }
sahilmgandhi 18:6a4db94011d3 2834
sahilmgandhi 18:6a4db94011d3 2835 if((response_r1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
sahilmgandhi 18:6a4db94011d3 2836 {
sahilmgandhi 18:6a4db94011d3 2837 return(SD_ERASE_SEQ_ERR);
sahilmgandhi 18:6a4db94011d3 2838 }
sahilmgandhi 18:6a4db94011d3 2839
sahilmgandhi 18:6a4db94011d3 2840 if((response_r1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
sahilmgandhi 18:6a4db94011d3 2841 {
sahilmgandhi 18:6a4db94011d3 2842 return(SD_BAD_ERASE_PARAM);
sahilmgandhi 18:6a4db94011d3 2843 }
sahilmgandhi 18:6a4db94011d3 2844
sahilmgandhi 18:6a4db94011d3 2845 if((response_r1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
sahilmgandhi 18:6a4db94011d3 2846 {
sahilmgandhi 18:6a4db94011d3 2847 return(SD_WRITE_PROT_VIOLATION);
sahilmgandhi 18:6a4db94011d3 2848 }
sahilmgandhi 18:6a4db94011d3 2849
sahilmgandhi 18:6a4db94011d3 2850 if((response_r1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
sahilmgandhi 18:6a4db94011d3 2851 {
sahilmgandhi 18:6a4db94011d3 2852 return(SD_LOCK_UNLOCK_FAILED);
sahilmgandhi 18:6a4db94011d3 2853 }
sahilmgandhi 18:6a4db94011d3 2854
sahilmgandhi 18:6a4db94011d3 2855 if((response_r1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
sahilmgandhi 18:6a4db94011d3 2856 {
sahilmgandhi 18:6a4db94011d3 2857 return(SD_COM_CRC_FAILED);
sahilmgandhi 18:6a4db94011d3 2858 }
sahilmgandhi 18:6a4db94011d3 2859
sahilmgandhi 18:6a4db94011d3 2860 if((response_r1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
sahilmgandhi 18:6a4db94011d3 2861 {
sahilmgandhi 18:6a4db94011d3 2862 return(SD_ILLEGAL_CMD);
sahilmgandhi 18:6a4db94011d3 2863 }
sahilmgandhi 18:6a4db94011d3 2864
sahilmgandhi 18:6a4db94011d3 2865 if((response_r1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
sahilmgandhi 18:6a4db94011d3 2866 {
sahilmgandhi 18:6a4db94011d3 2867 return(SD_CARD_ECC_FAILED);
sahilmgandhi 18:6a4db94011d3 2868 }
sahilmgandhi 18:6a4db94011d3 2869
sahilmgandhi 18:6a4db94011d3 2870 if((response_r1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
sahilmgandhi 18:6a4db94011d3 2871 {
sahilmgandhi 18:6a4db94011d3 2872 return(SD_CC_ERROR);
sahilmgandhi 18:6a4db94011d3 2873 }
sahilmgandhi 18:6a4db94011d3 2874
sahilmgandhi 18:6a4db94011d3 2875 if((response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
sahilmgandhi 18:6a4db94011d3 2876 {
sahilmgandhi 18:6a4db94011d3 2877 return(SD_GENERAL_UNKNOWN_ERROR);
sahilmgandhi 18:6a4db94011d3 2878 }
sahilmgandhi 18:6a4db94011d3 2879
sahilmgandhi 18:6a4db94011d3 2880 if((response_r1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
sahilmgandhi 18:6a4db94011d3 2881 {
sahilmgandhi 18:6a4db94011d3 2882 return(SD_STREAM_READ_UNDERRUN);
sahilmgandhi 18:6a4db94011d3 2883 }
sahilmgandhi 18:6a4db94011d3 2884
sahilmgandhi 18:6a4db94011d3 2885 if((response_r1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
sahilmgandhi 18:6a4db94011d3 2886 {
sahilmgandhi 18:6a4db94011d3 2887 return(SD_STREAM_WRITE_OVERRUN);
sahilmgandhi 18:6a4db94011d3 2888 }
sahilmgandhi 18:6a4db94011d3 2889
sahilmgandhi 18:6a4db94011d3 2890 if((response_r1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE)
sahilmgandhi 18:6a4db94011d3 2891 {
sahilmgandhi 18:6a4db94011d3 2892 return(SD_CID_CSD_OVERWRITE);
sahilmgandhi 18:6a4db94011d3 2893 }
sahilmgandhi 18:6a4db94011d3 2894
sahilmgandhi 18:6a4db94011d3 2895 if((response_r1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
sahilmgandhi 18:6a4db94011d3 2896 {
sahilmgandhi 18:6a4db94011d3 2897 return(SD_WP_ERASE_SKIP);
sahilmgandhi 18:6a4db94011d3 2898 }
sahilmgandhi 18:6a4db94011d3 2899
sahilmgandhi 18:6a4db94011d3 2900 if((response_r1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
sahilmgandhi 18:6a4db94011d3 2901 {
sahilmgandhi 18:6a4db94011d3 2902 return(SD_CARD_ECC_DISABLED);
sahilmgandhi 18:6a4db94011d3 2903 }
sahilmgandhi 18:6a4db94011d3 2904
sahilmgandhi 18:6a4db94011d3 2905 if((response_r1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
sahilmgandhi 18:6a4db94011d3 2906 {
sahilmgandhi 18:6a4db94011d3 2907 return(SD_ERASE_RESET);
sahilmgandhi 18:6a4db94011d3 2908 }
sahilmgandhi 18:6a4db94011d3 2909
sahilmgandhi 18:6a4db94011d3 2910 if((response_r1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
sahilmgandhi 18:6a4db94011d3 2911 {
sahilmgandhi 18:6a4db94011d3 2912 return(SD_AKE_SEQ_ERROR);
sahilmgandhi 18:6a4db94011d3 2913 }
sahilmgandhi 18:6a4db94011d3 2914
sahilmgandhi 18:6a4db94011d3 2915 return errorstate;
sahilmgandhi 18:6a4db94011d3 2916 }
sahilmgandhi 18:6a4db94011d3 2917
sahilmgandhi 18:6a4db94011d3 2918 /**
sahilmgandhi 18:6a4db94011d3 2919 * @brief Checks for error conditions for R3 (OCR) response.
sahilmgandhi 18:6a4db94011d3 2920 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2921 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2922 */
sahilmgandhi 18:6a4db94011d3 2923 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 2924 {
sahilmgandhi 18:6a4db94011d3 2925 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2926
sahilmgandhi 18:6a4db94011d3 2927 while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 2928 {
sahilmgandhi 18:6a4db94011d3 2929 }
sahilmgandhi 18:6a4db94011d3 2930
sahilmgandhi 18:6a4db94011d3 2931 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 2932 {
sahilmgandhi 18:6a4db94011d3 2933 errorstate = SD_CMD_RSP_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 2934
sahilmgandhi 18:6a4db94011d3 2935 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
sahilmgandhi 18:6a4db94011d3 2936
sahilmgandhi 18:6a4db94011d3 2937 return errorstate;
sahilmgandhi 18:6a4db94011d3 2938 }
sahilmgandhi 18:6a4db94011d3 2939
sahilmgandhi 18:6a4db94011d3 2940 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 2941 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 2942
sahilmgandhi 18:6a4db94011d3 2943 return errorstate;
sahilmgandhi 18:6a4db94011d3 2944 }
sahilmgandhi 18:6a4db94011d3 2945
sahilmgandhi 18:6a4db94011d3 2946 /**
sahilmgandhi 18:6a4db94011d3 2947 * @brief Checks for error conditions for R2 (CID or CSD) response.
sahilmgandhi 18:6a4db94011d3 2948 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2949 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2950 */
sahilmgandhi 18:6a4db94011d3 2951 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 2952 {
sahilmgandhi 18:6a4db94011d3 2953 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2954
sahilmgandhi 18:6a4db94011d3 2955 while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 2956 {
sahilmgandhi 18:6a4db94011d3 2957 }
sahilmgandhi 18:6a4db94011d3 2958
sahilmgandhi 18:6a4db94011d3 2959 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 2960 {
sahilmgandhi 18:6a4db94011d3 2961 errorstate = SD_CMD_RSP_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 2962
sahilmgandhi 18:6a4db94011d3 2963 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
sahilmgandhi 18:6a4db94011d3 2964
sahilmgandhi 18:6a4db94011d3 2965 return errorstate;
sahilmgandhi 18:6a4db94011d3 2966 }
sahilmgandhi 18:6a4db94011d3 2967 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
sahilmgandhi 18:6a4db94011d3 2968 {
sahilmgandhi 18:6a4db94011d3 2969 errorstate = SD_CMD_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 2970
sahilmgandhi 18:6a4db94011d3 2971 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
sahilmgandhi 18:6a4db94011d3 2972
sahilmgandhi 18:6a4db94011d3 2973 return errorstate;
sahilmgandhi 18:6a4db94011d3 2974 }
sahilmgandhi 18:6a4db94011d3 2975 else
sahilmgandhi 18:6a4db94011d3 2976 {
sahilmgandhi 18:6a4db94011d3 2977 /* No error flag set */
sahilmgandhi 18:6a4db94011d3 2978 }
sahilmgandhi 18:6a4db94011d3 2979
sahilmgandhi 18:6a4db94011d3 2980 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 2981 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 2982
sahilmgandhi 18:6a4db94011d3 2983 return errorstate;
sahilmgandhi 18:6a4db94011d3 2984 }
sahilmgandhi 18:6a4db94011d3 2985
sahilmgandhi 18:6a4db94011d3 2986 /**
sahilmgandhi 18:6a4db94011d3 2987 * @brief Checks for error conditions for R6 (RCA) response.
sahilmgandhi 18:6a4db94011d3 2988 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 2989 * @param SD_CMD: The sent command index
sahilmgandhi 18:6a4db94011d3 2990 * @param pRCA: Pointer to the variable that will contain the SD card relative
sahilmgandhi 18:6a4db94011d3 2991 * address RCA
sahilmgandhi 18:6a4db94011d3 2992 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 2993 */
sahilmgandhi 18:6a4db94011d3 2994 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA)
sahilmgandhi 18:6a4db94011d3 2995 {
sahilmgandhi 18:6a4db94011d3 2996 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 2997 uint32_t response_r1;
sahilmgandhi 18:6a4db94011d3 2998
sahilmgandhi 18:6a4db94011d3 2999 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 3000 {
sahilmgandhi 18:6a4db94011d3 3001 }
sahilmgandhi 18:6a4db94011d3 3002
sahilmgandhi 18:6a4db94011d3 3003 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 3004 {
sahilmgandhi 18:6a4db94011d3 3005 errorstate = SD_CMD_RSP_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 3006
sahilmgandhi 18:6a4db94011d3 3007 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
sahilmgandhi 18:6a4db94011d3 3008
sahilmgandhi 18:6a4db94011d3 3009 return errorstate;
sahilmgandhi 18:6a4db94011d3 3010 }
sahilmgandhi 18:6a4db94011d3 3011 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
sahilmgandhi 18:6a4db94011d3 3012 {
sahilmgandhi 18:6a4db94011d3 3013 errorstate = SD_CMD_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 3014
sahilmgandhi 18:6a4db94011d3 3015 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
sahilmgandhi 18:6a4db94011d3 3016
sahilmgandhi 18:6a4db94011d3 3017 return errorstate;
sahilmgandhi 18:6a4db94011d3 3018 }
sahilmgandhi 18:6a4db94011d3 3019 else
sahilmgandhi 18:6a4db94011d3 3020 {
sahilmgandhi 18:6a4db94011d3 3021 /* No error flag set */
sahilmgandhi 18:6a4db94011d3 3022 }
sahilmgandhi 18:6a4db94011d3 3023
sahilmgandhi 18:6a4db94011d3 3024 /* Check response received is of desired command */
sahilmgandhi 18:6a4db94011d3 3025 if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD)
sahilmgandhi 18:6a4db94011d3 3026 {
sahilmgandhi 18:6a4db94011d3 3027 errorstate = SD_ILLEGAL_CMD;
sahilmgandhi 18:6a4db94011d3 3028
sahilmgandhi 18:6a4db94011d3 3029 return errorstate;
sahilmgandhi 18:6a4db94011d3 3030 }
sahilmgandhi 18:6a4db94011d3 3031
sahilmgandhi 18:6a4db94011d3 3032 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 3033 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 3034
sahilmgandhi 18:6a4db94011d3 3035 /* We have received response, retrieve it. */
sahilmgandhi 18:6a4db94011d3 3036 response_r1 = SDIO_GetResponse(SDIO_RESP1);
sahilmgandhi 18:6a4db94011d3 3037
sahilmgandhi 18:6a4db94011d3 3038 if((response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)) == SD_ALLZERO)
sahilmgandhi 18:6a4db94011d3 3039 {
sahilmgandhi 18:6a4db94011d3 3040 *pRCA = (uint16_t) (response_r1 >> 16U);
sahilmgandhi 18:6a4db94011d3 3041
sahilmgandhi 18:6a4db94011d3 3042 return errorstate;
sahilmgandhi 18:6a4db94011d3 3043 }
sahilmgandhi 18:6a4db94011d3 3044
sahilmgandhi 18:6a4db94011d3 3045 if((response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR) == SD_R6_GENERAL_UNKNOWN_ERROR)
sahilmgandhi 18:6a4db94011d3 3046 {
sahilmgandhi 18:6a4db94011d3 3047 return(SD_GENERAL_UNKNOWN_ERROR);
sahilmgandhi 18:6a4db94011d3 3048 }
sahilmgandhi 18:6a4db94011d3 3049
sahilmgandhi 18:6a4db94011d3 3050 if((response_r1 & SD_R6_ILLEGAL_CMD) == SD_R6_ILLEGAL_CMD)
sahilmgandhi 18:6a4db94011d3 3051 {
sahilmgandhi 18:6a4db94011d3 3052 return(SD_ILLEGAL_CMD);
sahilmgandhi 18:6a4db94011d3 3053 }
sahilmgandhi 18:6a4db94011d3 3054
sahilmgandhi 18:6a4db94011d3 3055 if((response_r1 & SD_R6_COM_CRC_FAILED) == SD_R6_COM_CRC_FAILED)
sahilmgandhi 18:6a4db94011d3 3056 {
sahilmgandhi 18:6a4db94011d3 3057 return(SD_COM_CRC_FAILED);
sahilmgandhi 18:6a4db94011d3 3058 }
sahilmgandhi 18:6a4db94011d3 3059
sahilmgandhi 18:6a4db94011d3 3060 return errorstate;
sahilmgandhi 18:6a4db94011d3 3061 }
sahilmgandhi 18:6a4db94011d3 3062
sahilmgandhi 18:6a4db94011d3 3063 /**
sahilmgandhi 18:6a4db94011d3 3064 * @brief Enables the SDIO wide bus mode.
sahilmgandhi 18:6a4db94011d3 3065 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 3066 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 3067 */
sahilmgandhi 18:6a4db94011d3 3068 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 3069 {
sahilmgandhi 18:6a4db94011d3 3070 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 3071 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 3072
sahilmgandhi 18:6a4db94011d3 3073 uint32_t scr[2U] = {0U, 0U};
sahilmgandhi 18:6a4db94011d3 3074
sahilmgandhi 18:6a4db94011d3 3075 if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
sahilmgandhi 18:6a4db94011d3 3076 {
sahilmgandhi 18:6a4db94011d3 3077 errorstate = SD_LOCK_UNLOCK_FAILED;
sahilmgandhi 18:6a4db94011d3 3078
sahilmgandhi 18:6a4db94011d3 3079 return errorstate;
sahilmgandhi 18:6a4db94011d3 3080 }
sahilmgandhi 18:6a4db94011d3 3081
sahilmgandhi 18:6a4db94011d3 3082 /* Get SCR Register */
sahilmgandhi 18:6a4db94011d3 3083 errorstate = SD_FindSCR(hsd, scr);
sahilmgandhi 18:6a4db94011d3 3084
sahilmgandhi 18:6a4db94011d3 3085 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 3086 {
sahilmgandhi 18:6a4db94011d3 3087 return errorstate;
sahilmgandhi 18:6a4db94011d3 3088 }
sahilmgandhi 18:6a4db94011d3 3089
sahilmgandhi 18:6a4db94011d3 3090 /* If requested card supports wide bus operation */
sahilmgandhi 18:6a4db94011d3 3091 if((scr[1U] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
sahilmgandhi 18:6a4db94011d3 3092 {
sahilmgandhi 18:6a4db94011d3 3093 /* Send CMD55 APP_CMD with argument as card's RCA.*/
sahilmgandhi 18:6a4db94011d3 3094 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16U);
sahilmgandhi 18:6a4db94011d3 3095 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
sahilmgandhi 18:6a4db94011d3 3096 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 3097 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 3098 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 3099 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 3100
sahilmgandhi 18:6a4db94011d3 3101 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 3102 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
sahilmgandhi 18:6a4db94011d3 3103
sahilmgandhi 18:6a4db94011d3 3104 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 3105 {
sahilmgandhi 18:6a4db94011d3 3106 return errorstate;
sahilmgandhi 18:6a4db94011d3 3107 }
sahilmgandhi 18:6a4db94011d3 3108
sahilmgandhi 18:6a4db94011d3 3109 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
sahilmgandhi 18:6a4db94011d3 3110 sdio_cmdinitstructure.Argument = 2U;
sahilmgandhi 18:6a4db94011d3 3111 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
sahilmgandhi 18:6a4db94011d3 3112 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 3113
sahilmgandhi 18:6a4db94011d3 3114 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 3115 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
sahilmgandhi 18:6a4db94011d3 3116
sahilmgandhi 18:6a4db94011d3 3117 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 3118 {
sahilmgandhi 18:6a4db94011d3 3119 return errorstate;
sahilmgandhi 18:6a4db94011d3 3120 }
sahilmgandhi 18:6a4db94011d3 3121
sahilmgandhi 18:6a4db94011d3 3122 return errorstate;
sahilmgandhi 18:6a4db94011d3 3123 }
sahilmgandhi 18:6a4db94011d3 3124 else
sahilmgandhi 18:6a4db94011d3 3125 {
sahilmgandhi 18:6a4db94011d3 3126 errorstate = SD_REQUEST_NOT_APPLICABLE;
sahilmgandhi 18:6a4db94011d3 3127
sahilmgandhi 18:6a4db94011d3 3128 return errorstate;
sahilmgandhi 18:6a4db94011d3 3129 }
sahilmgandhi 18:6a4db94011d3 3130 }
sahilmgandhi 18:6a4db94011d3 3131
sahilmgandhi 18:6a4db94011d3 3132 /**
sahilmgandhi 18:6a4db94011d3 3133 * @brief Disables the SDIO wide bus mode.
sahilmgandhi 18:6a4db94011d3 3134 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 3135 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 3136 */
sahilmgandhi 18:6a4db94011d3 3137 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd)
sahilmgandhi 18:6a4db94011d3 3138 {
sahilmgandhi 18:6a4db94011d3 3139 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 3140 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 3141
sahilmgandhi 18:6a4db94011d3 3142 uint32_t scr[2U] = {0U, 0U};
sahilmgandhi 18:6a4db94011d3 3143
sahilmgandhi 18:6a4db94011d3 3144 if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
sahilmgandhi 18:6a4db94011d3 3145 {
sahilmgandhi 18:6a4db94011d3 3146 errorstate = SD_LOCK_UNLOCK_FAILED;
sahilmgandhi 18:6a4db94011d3 3147
sahilmgandhi 18:6a4db94011d3 3148 return errorstate;
sahilmgandhi 18:6a4db94011d3 3149 }
sahilmgandhi 18:6a4db94011d3 3150
sahilmgandhi 18:6a4db94011d3 3151 /* Get SCR Register */
sahilmgandhi 18:6a4db94011d3 3152 errorstate = SD_FindSCR(hsd, scr);
sahilmgandhi 18:6a4db94011d3 3153
sahilmgandhi 18:6a4db94011d3 3154 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 3155 {
sahilmgandhi 18:6a4db94011d3 3156 return errorstate;
sahilmgandhi 18:6a4db94011d3 3157 }
sahilmgandhi 18:6a4db94011d3 3158
sahilmgandhi 18:6a4db94011d3 3159 /* If requested card supports 1 bit mode operation */
sahilmgandhi 18:6a4db94011d3 3160 if((scr[1U] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
sahilmgandhi 18:6a4db94011d3 3161 {
sahilmgandhi 18:6a4db94011d3 3162 /* Send CMD55 APP_CMD with argument as card's RCA */
sahilmgandhi 18:6a4db94011d3 3163 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16U);
sahilmgandhi 18:6a4db94011d3 3164 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
sahilmgandhi 18:6a4db94011d3 3165 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 3166 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 3167 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 3168 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 3169
sahilmgandhi 18:6a4db94011d3 3170 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 3171 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
sahilmgandhi 18:6a4db94011d3 3172
sahilmgandhi 18:6a4db94011d3 3173 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 3174 {
sahilmgandhi 18:6a4db94011d3 3175 return errorstate;
sahilmgandhi 18:6a4db94011d3 3176 }
sahilmgandhi 18:6a4db94011d3 3177
sahilmgandhi 18:6a4db94011d3 3178 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
sahilmgandhi 18:6a4db94011d3 3179 sdio_cmdinitstructure.Argument = 0U;
sahilmgandhi 18:6a4db94011d3 3180 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
sahilmgandhi 18:6a4db94011d3 3181 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 3182
sahilmgandhi 18:6a4db94011d3 3183 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 3184 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
sahilmgandhi 18:6a4db94011d3 3185
sahilmgandhi 18:6a4db94011d3 3186 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 3187 {
sahilmgandhi 18:6a4db94011d3 3188 return errorstate;
sahilmgandhi 18:6a4db94011d3 3189 }
sahilmgandhi 18:6a4db94011d3 3190
sahilmgandhi 18:6a4db94011d3 3191 return errorstate;
sahilmgandhi 18:6a4db94011d3 3192 }
sahilmgandhi 18:6a4db94011d3 3193 else
sahilmgandhi 18:6a4db94011d3 3194 {
sahilmgandhi 18:6a4db94011d3 3195 errorstate = SD_REQUEST_NOT_APPLICABLE;
sahilmgandhi 18:6a4db94011d3 3196
sahilmgandhi 18:6a4db94011d3 3197 return errorstate;
sahilmgandhi 18:6a4db94011d3 3198 }
sahilmgandhi 18:6a4db94011d3 3199 }
sahilmgandhi 18:6a4db94011d3 3200
sahilmgandhi 18:6a4db94011d3 3201
sahilmgandhi 18:6a4db94011d3 3202 /**
sahilmgandhi 18:6a4db94011d3 3203 * @brief Finds the SD card SCR register value.
sahilmgandhi 18:6a4db94011d3 3204 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 3205 * @param pSCR: pointer to the buffer that will contain the SCR value
sahilmgandhi 18:6a4db94011d3 3206 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 3207 */
sahilmgandhi 18:6a4db94011d3 3208 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
sahilmgandhi 18:6a4db94011d3 3209 {
sahilmgandhi 18:6a4db94011d3 3210 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 3211 SDIO_DataInitTypeDef sdio_datainitstructure;
sahilmgandhi 18:6a4db94011d3 3212 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 3213 uint32_t index = 0U;
sahilmgandhi 18:6a4db94011d3 3214 uint32_t tempscr[2U] = {0U, 0U};
sahilmgandhi 18:6a4db94011d3 3215
sahilmgandhi 18:6a4db94011d3 3216 /* Set Block Size To 8 Bytes */
sahilmgandhi 18:6a4db94011d3 3217 /* Send CMD55 APP_CMD with argument as card's RCA */
sahilmgandhi 18:6a4db94011d3 3218 sdio_cmdinitstructure.Argument = (uint32_t)8U;
sahilmgandhi 18:6a4db94011d3 3219 sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
sahilmgandhi 18:6a4db94011d3 3220 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 3221 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 3222 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 3223 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 3224
sahilmgandhi 18:6a4db94011d3 3225 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 3226 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
sahilmgandhi 18:6a4db94011d3 3227
sahilmgandhi 18:6a4db94011d3 3228 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 3229 {
sahilmgandhi 18:6a4db94011d3 3230 return errorstate;
sahilmgandhi 18:6a4db94011d3 3231 }
sahilmgandhi 18:6a4db94011d3 3232
sahilmgandhi 18:6a4db94011d3 3233 /* Send CMD55 APP_CMD with argument as card's RCA */
sahilmgandhi 18:6a4db94011d3 3234 sdio_cmdinitstructure.Argument = (uint32_t)((hsd->RCA) << 16U);
sahilmgandhi 18:6a4db94011d3 3235 sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
sahilmgandhi 18:6a4db94011d3 3236 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 3237
sahilmgandhi 18:6a4db94011d3 3238 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 3239 errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
sahilmgandhi 18:6a4db94011d3 3240
sahilmgandhi 18:6a4db94011d3 3241 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 3242 {
sahilmgandhi 18:6a4db94011d3 3243 return errorstate;
sahilmgandhi 18:6a4db94011d3 3244 }
sahilmgandhi 18:6a4db94011d3 3245 sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
sahilmgandhi 18:6a4db94011d3 3246 sdio_datainitstructure.DataLength = 8U;
sahilmgandhi 18:6a4db94011d3 3247 sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
sahilmgandhi 18:6a4db94011d3 3248 sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
sahilmgandhi 18:6a4db94011d3 3249 sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
sahilmgandhi 18:6a4db94011d3 3250 sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 3251 SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
sahilmgandhi 18:6a4db94011d3 3252
sahilmgandhi 18:6a4db94011d3 3253 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
sahilmgandhi 18:6a4db94011d3 3254 sdio_cmdinitstructure.Argument = 0U;
sahilmgandhi 18:6a4db94011d3 3255 sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_SEND_SCR;
sahilmgandhi 18:6a4db94011d3 3256 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 3257
sahilmgandhi 18:6a4db94011d3 3258 /* Check for error conditions */
sahilmgandhi 18:6a4db94011d3 3259 errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_SEND_SCR);
sahilmgandhi 18:6a4db94011d3 3260
sahilmgandhi 18:6a4db94011d3 3261 if(errorstate != SD_OK)
sahilmgandhi 18:6a4db94011d3 3262 {
sahilmgandhi 18:6a4db94011d3 3263 return errorstate;
sahilmgandhi 18:6a4db94011d3 3264 }
sahilmgandhi 18:6a4db94011d3 3265 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 3266 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 3267 #else /* SDIO_STA_STBITERR not defined */
sahilmgandhi 18:6a4db94011d3 3268 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
sahilmgandhi 18:6a4db94011d3 3269 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 3270 {
sahilmgandhi 18:6a4db94011d3 3271 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
sahilmgandhi 18:6a4db94011d3 3272 {
sahilmgandhi 18:6a4db94011d3 3273 *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
sahilmgandhi 18:6a4db94011d3 3274 index++;
sahilmgandhi 18:6a4db94011d3 3275 }
sahilmgandhi 18:6a4db94011d3 3276 }
sahilmgandhi 18:6a4db94011d3 3277
sahilmgandhi 18:6a4db94011d3 3278 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
sahilmgandhi 18:6a4db94011d3 3279 {
sahilmgandhi 18:6a4db94011d3 3280 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
sahilmgandhi 18:6a4db94011d3 3281
sahilmgandhi 18:6a4db94011d3 3282 errorstate = SD_DATA_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 3283
sahilmgandhi 18:6a4db94011d3 3284 return errorstate;
sahilmgandhi 18:6a4db94011d3 3285 }
sahilmgandhi 18:6a4db94011d3 3286 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
sahilmgandhi 18:6a4db94011d3 3287 {
sahilmgandhi 18:6a4db94011d3 3288 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
sahilmgandhi 18:6a4db94011d3 3289
sahilmgandhi 18:6a4db94011d3 3290 errorstate = SD_DATA_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 3291
sahilmgandhi 18:6a4db94011d3 3292 return errorstate;
sahilmgandhi 18:6a4db94011d3 3293 }
sahilmgandhi 18:6a4db94011d3 3294 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
sahilmgandhi 18:6a4db94011d3 3295 {
sahilmgandhi 18:6a4db94011d3 3296 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
sahilmgandhi 18:6a4db94011d3 3297
sahilmgandhi 18:6a4db94011d3 3298 errorstate = SD_RX_OVERRUN;
sahilmgandhi 18:6a4db94011d3 3299
sahilmgandhi 18:6a4db94011d3 3300 return errorstate;
sahilmgandhi 18:6a4db94011d3 3301 }
sahilmgandhi 18:6a4db94011d3 3302 #ifdef SDIO_STA_STBITERR
sahilmgandhi 18:6a4db94011d3 3303 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
sahilmgandhi 18:6a4db94011d3 3304 {
sahilmgandhi 18:6a4db94011d3 3305 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
sahilmgandhi 18:6a4db94011d3 3306
sahilmgandhi 18:6a4db94011d3 3307 errorstate = SD_START_BIT_ERR;
sahilmgandhi 18:6a4db94011d3 3308
sahilmgandhi 18:6a4db94011d3 3309 return errorstate;
sahilmgandhi 18:6a4db94011d3 3310 }
sahilmgandhi 18:6a4db94011d3 3311 #endif /* SDIO_STA_STBITERR */
sahilmgandhi 18:6a4db94011d3 3312 else
sahilmgandhi 18:6a4db94011d3 3313 {
sahilmgandhi 18:6a4db94011d3 3314 /* No error flag set */
sahilmgandhi 18:6a4db94011d3 3315 }
sahilmgandhi 18:6a4db94011d3 3316
sahilmgandhi 18:6a4db94011d3 3317 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 3318 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 3319
sahilmgandhi 18:6a4db94011d3 3320 *(pSCR + 1U) = ((tempscr[0U] & SD_0TO7BITS) << 24U) | ((tempscr[0U] & SD_8TO15BITS) << 8U) |\
sahilmgandhi 18:6a4db94011d3 3321 ((tempscr[0U] & SD_16TO23BITS) >> 8U) | ((tempscr[0U] & SD_24TO31BITS) >> 24U);
sahilmgandhi 18:6a4db94011d3 3322
sahilmgandhi 18:6a4db94011d3 3323 *(pSCR) = ((tempscr[1U] & SD_0TO7BITS) << 24U) | ((tempscr[1U] & SD_8TO15BITS) << 8U) |\
sahilmgandhi 18:6a4db94011d3 3324 ((tempscr[1U] & SD_16TO23BITS) >> 8U) | ((tempscr[1U] & SD_24TO31BITS) >> 24U);
sahilmgandhi 18:6a4db94011d3 3325
sahilmgandhi 18:6a4db94011d3 3326 return errorstate;
sahilmgandhi 18:6a4db94011d3 3327 }
sahilmgandhi 18:6a4db94011d3 3328
sahilmgandhi 18:6a4db94011d3 3329 /**
sahilmgandhi 18:6a4db94011d3 3330 * @brief Checks if the SD card is in programming state.
sahilmgandhi 18:6a4db94011d3 3331 * @param hsd: SD handle
sahilmgandhi 18:6a4db94011d3 3332 * @param pStatus: pointer to the variable that will contain the SD card state
sahilmgandhi 18:6a4db94011d3 3333 * @retval SD Card error state
sahilmgandhi 18:6a4db94011d3 3334 */
sahilmgandhi 18:6a4db94011d3 3335 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus)
sahilmgandhi 18:6a4db94011d3 3336 {
sahilmgandhi 18:6a4db94011d3 3337 SDIO_CmdInitTypeDef sdio_cmdinitstructure;
sahilmgandhi 18:6a4db94011d3 3338 HAL_SD_ErrorTypedef errorstate = SD_OK;
sahilmgandhi 18:6a4db94011d3 3339 __IO uint32_t responseR1 = 0U;
sahilmgandhi 18:6a4db94011d3 3340
sahilmgandhi 18:6a4db94011d3 3341 sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16U);
sahilmgandhi 18:6a4db94011d3 3342 sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
sahilmgandhi 18:6a4db94011d3 3343 sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
sahilmgandhi 18:6a4db94011d3 3344 sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
sahilmgandhi 18:6a4db94011d3 3345 sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
sahilmgandhi 18:6a4db94011d3 3346 SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
sahilmgandhi 18:6a4db94011d3 3347
sahilmgandhi 18:6a4db94011d3 3348 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 3349 {
sahilmgandhi 18:6a4db94011d3 3350 }
sahilmgandhi 18:6a4db94011d3 3351
sahilmgandhi 18:6a4db94011d3 3352 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
sahilmgandhi 18:6a4db94011d3 3353 {
sahilmgandhi 18:6a4db94011d3 3354 errorstate = SD_CMD_RSP_TIMEOUT;
sahilmgandhi 18:6a4db94011d3 3355
sahilmgandhi 18:6a4db94011d3 3356 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
sahilmgandhi 18:6a4db94011d3 3357
sahilmgandhi 18:6a4db94011d3 3358 return errorstate;
sahilmgandhi 18:6a4db94011d3 3359 }
sahilmgandhi 18:6a4db94011d3 3360 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
sahilmgandhi 18:6a4db94011d3 3361 {
sahilmgandhi 18:6a4db94011d3 3362 errorstate = SD_CMD_CRC_FAIL;
sahilmgandhi 18:6a4db94011d3 3363
sahilmgandhi 18:6a4db94011d3 3364 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
sahilmgandhi 18:6a4db94011d3 3365
sahilmgandhi 18:6a4db94011d3 3366 return errorstate;
sahilmgandhi 18:6a4db94011d3 3367 }
sahilmgandhi 18:6a4db94011d3 3368 else
sahilmgandhi 18:6a4db94011d3 3369 {
sahilmgandhi 18:6a4db94011d3 3370 /* No error flag set */
sahilmgandhi 18:6a4db94011d3 3371 }
sahilmgandhi 18:6a4db94011d3 3372
sahilmgandhi 18:6a4db94011d3 3373 /* Check response received is of desired command */
sahilmgandhi 18:6a4db94011d3 3374 if((uint32_t)SDIO_GetCommandResponse(hsd->Instance) != SD_CMD_SEND_STATUS)
sahilmgandhi 18:6a4db94011d3 3375 {
sahilmgandhi 18:6a4db94011d3 3376 errorstate = SD_ILLEGAL_CMD;
sahilmgandhi 18:6a4db94011d3 3377
sahilmgandhi 18:6a4db94011d3 3378 return errorstate;
sahilmgandhi 18:6a4db94011d3 3379 }
sahilmgandhi 18:6a4db94011d3 3380
sahilmgandhi 18:6a4db94011d3 3381 /* Clear all the static flags */
sahilmgandhi 18:6a4db94011d3 3382 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
sahilmgandhi 18:6a4db94011d3 3383
sahilmgandhi 18:6a4db94011d3 3384
sahilmgandhi 18:6a4db94011d3 3385 /* We have received response, retrieve it for analysis */
sahilmgandhi 18:6a4db94011d3 3386 responseR1 = SDIO_GetResponse(SDIO_RESP1);
sahilmgandhi 18:6a4db94011d3 3387
sahilmgandhi 18:6a4db94011d3 3388 /* Find out card status */
sahilmgandhi 18:6a4db94011d3 3389 *pStatus = (uint8_t)((responseR1 >> 9U) & 0x0000000FU);
sahilmgandhi 18:6a4db94011d3 3390
sahilmgandhi 18:6a4db94011d3 3391 if((responseR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
sahilmgandhi 18:6a4db94011d3 3392 {
sahilmgandhi 18:6a4db94011d3 3393 return errorstate;
sahilmgandhi 18:6a4db94011d3 3394 }
sahilmgandhi 18:6a4db94011d3 3395
sahilmgandhi 18:6a4db94011d3 3396 if((responseR1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
sahilmgandhi 18:6a4db94011d3 3397 {
sahilmgandhi 18:6a4db94011d3 3398 return(SD_ADDR_OUT_OF_RANGE);
sahilmgandhi 18:6a4db94011d3 3399 }
sahilmgandhi 18:6a4db94011d3 3400
sahilmgandhi 18:6a4db94011d3 3401 if((responseR1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
sahilmgandhi 18:6a4db94011d3 3402 {
sahilmgandhi 18:6a4db94011d3 3403 return(SD_ADDR_MISALIGNED);
sahilmgandhi 18:6a4db94011d3 3404 }
sahilmgandhi 18:6a4db94011d3 3405
sahilmgandhi 18:6a4db94011d3 3406 if((responseR1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
sahilmgandhi 18:6a4db94011d3 3407 {
sahilmgandhi 18:6a4db94011d3 3408 return(SD_BLOCK_LEN_ERR);
sahilmgandhi 18:6a4db94011d3 3409 }
sahilmgandhi 18:6a4db94011d3 3410
sahilmgandhi 18:6a4db94011d3 3411 if((responseR1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
sahilmgandhi 18:6a4db94011d3 3412 {
sahilmgandhi 18:6a4db94011d3 3413 return(SD_ERASE_SEQ_ERR);
sahilmgandhi 18:6a4db94011d3 3414 }
sahilmgandhi 18:6a4db94011d3 3415
sahilmgandhi 18:6a4db94011d3 3416 if((responseR1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
sahilmgandhi 18:6a4db94011d3 3417 {
sahilmgandhi 18:6a4db94011d3 3418 return(SD_BAD_ERASE_PARAM);
sahilmgandhi 18:6a4db94011d3 3419 }
sahilmgandhi 18:6a4db94011d3 3420
sahilmgandhi 18:6a4db94011d3 3421 if((responseR1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
sahilmgandhi 18:6a4db94011d3 3422 {
sahilmgandhi 18:6a4db94011d3 3423 return(SD_WRITE_PROT_VIOLATION);
sahilmgandhi 18:6a4db94011d3 3424 }
sahilmgandhi 18:6a4db94011d3 3425
sahilmgandhi 18:6a4db94011d3 3426 if((responseR1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
sahilmgandhi 18:6a4db94011d3 3427 {
sahilmgandhi 18:6a4db94011d3 3428 return(SD_LOCK_UNLOCK_FAILED);
sahilmgandhi 18:6a4db94011d3 3429 }
sahilmgandhi 18:6a4db94011d3 3430
sahilmgandhi 18:6a4db94011d3 3431 if((responseR1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
sahilmgandhi 18:6a4db94011d3 3432 {
sahilmgandhi 18:6a4db94011d3 3433 return(SD_COM_CRC_FAILED);
sahilmgandhi 18:6a4db94011d3 3434 }
sahilmgandhi 18:6a4db94011d3 3435
sahilmgandhi 18:6a4db94011d3 3436 if((responseR1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
sahilmgandhi 18:6a4db94011d3 3437 {
sahilmgandhi 18:6a4db94011d3 3438 return(SD_ILLEGAL_CMD);
sahilmgandhi 18:6a4db94011d3 3439 }
sahilmgandhi 18:6a4db94011d3 3440
sahilmgandhi 18:6a4db94011d3 3441 if((responseR1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
sahilmgandhi 18:6a4db94011d3 3442 {
sahilmgandhi 18:6a4db94011d3 3443 return(SD_CARD_ECC_FAILED);
sahilmgandhi 18:6a4db94011d3 3444 }
sahilmgandhi 18:6a4db94011d3 3445
sahilmgandhi 18:6a4db94011d3 3446 if((responseR1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
sahilmgandhi 18:6a4db94011d3 3447 {
sahilmgandhi 18:6a4db94011d3 3448 return(SD_CC_ERROR);
sahilmgandhi 18:6a4db94011d3 3449 }
sahilmgandhi 18:6a4db94011d3 3450
sahilmgandhi 18:6a4db94011d3 3451 if((responseR1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
sahilmgandhi 18:6a4db94011d3 3452 {
sahilmgandhi 18:6a4db94011d3 3453 return(SD_GENERAL_UNKNOWN_ERROR);
sahilmgandhi 18:6a4db94011d3 3454 }
sahilmgandhi 18:6a4db94011d3 3455
sahilmgandhi 18:6a4db94011d3 3456 if((responseR1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
sahilmgandhi 18:6a4db94011d3 3457 {
sahilmgandhi 18:6a4db94011d3 3458 return(SD_STREAM_READ_UNDERRUN);
sahilmgandhi 18:6a4db94011d3 3459 }
sahilmgandhi 18:6a4db94011d3 3460
sahilmgandhi 18:6a4db94011d3 3461 if((responseR1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
sahilmgandhi 18:6a4db94011d3 3462 {
sahilmgandhi 18:6a4db94011d3 3463 return(SD_STREAM_WRITE_OVERRUN);
sahilmgandhi 18:6a4db94011d3 3464 }
sahilmgandhi 18:6a4db94011d3 3465
sahilmgandhi 18:6a4db94011d3 3466 if((responseR1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE)
sahilmgandhi 18:6a4db94011d3 3467 {
sahilmgandhi 18:6a4db94011d3 3468 return(SD_CID_CSD_OVERWRITE);
sahilmgandhi 18:6a4db94011d3 3469 }
sahilmgandhi 18:6a4db94011d3 3470
sahilmgandhi 18:6a4db94011d3 3471 if((responseR1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
sahilmgandhi 18:6a4db94011d3 3472 {
sahilmgandhi 18:6a4db94011d3 3473 return(SD_WP_ERASE_SKIP);
sahilmgandhi 18:6a4db94011d3 3474 }
sahilmgandhi 18:6a4db94011d3 3475
sahilmgandhi 18:6a4db94011d3 3476 if((responseR1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
sahilmgandhi 18:6a4db94011d3 3477 {
sahilmgandhi 18:6a4db94011d3 3478 return(SD_CARD_ECC_DISABLED);
sahilmgandhi 18:6a4db94011d3 3479 }
sahilmgandhi 18:6a4db94011d3 3480
sahilmgandhi 18:6a4db94011d3 3481 if((responseR1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
sahilmgandhi 18:6a4db94011d3 3482 {
sahilmgandhi 18:6a4db94011d3 3483 return(SD_ERASE_RESET);
sahilmgandhi 18:6a4db94011d3 3484 }
sahilmgandhi 18:6a4db94011d3 3485
sahilmgandhi 18:6a4db94011d3 3486 if((responseR1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
sahilmgandhi 18:6a4db94011d3 3487 {
sahilmgandhi 18:6a4db94011d3 3488 return(SD_AKE_SEQ_ERROR);
sahilmgandhi 18:6a4db94011d3 3489 }
sahilmgandhi 18:6a4db94011d3 3490
sahilmgandhi 18:6a4db94011d3 3491 return errorstate;
sahilmgandhi 18:6a4db94011d3 3492 }
sahilmgandhi 18:6a4db94011d3 3493
sahilmgandhi 18:6a4db94011d3 3494 /**
sahilmgandhi 18:6a4db94011d3 3495 * @}
sahilmgandhi 18:6a4db94011d3 3496 */
sahilmgandhi 18:6a4db94011d3 3497 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
sahilmgandhi 18:6a4db94011d3 3498 STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx ||
sahilmgandhi 18:6a4db94011d3 3499 STM32F412Rx || STM32F412Cx */
sahilmgandhi 18:6a4db94011d3 3500 #endif /* HAL_SD_MODULE_ENABLED */
sahilmgandhi 18:6a4db94011d3 3501
sahilmgandhi 18:6a4db94011d3 3502 /**
sahilmgandhi 18:6a4db94011d3 3503 * @}
sahilmgandhi 18:6a4db94011d3 3504 */
sahilmgandhi 18:6a4db94011d3 3505
sahilmgandhi 18:6a4db94011d3 3506 /**
sahilmgandhi 18:6a4db94011d3 3507 * @}
sahilmgandhi 18:6a4db94011d3 3508 */
sahilmgandhi 18:6a4db94011d3 3509
sahilmgandhi 18:6a4db94011d3 3510 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/