mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Jul 31 14:15:09 2015 +0100
Revision:
600:7d17ca308cd1
Parent:
489:119543c9f674
Synchronized with git revision e4cd8bbd3e05b68e5a7f466c74035a85743d45e0

Full URL: https://github.com/mbedmicro/mbed/commit/e4cd8bbd3e05b68e5a7f466c74035a85743d45e0/

Enable LPC8xx usart when configuring it

Who changed what in which revision?

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